test/java/util/Collections/EmptyCollectionSerialization.java
Print this page
rev 7291 : 7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
Reviewed-by: dmocek
@@ -21,17 +21,24 @@
* questions.
*/
/*
* @test
- * @bug 4684279
+ * @bug 4684279 7129185
* @summary Empty utility collections should be singletons
* @author Josh Bloch
+ * @run testng EmptyCollectionSerialization
*/
import java.util.*;
+import java.util.function.Supplier;
import java.io.*;
+import org.testng.annotations.Test;
+import org.testng.annotations.DataProvider;
+
+import static org.testng.Assert.fail;
+import static org.testng.Assert.assertSame;
public class EmptyCollectionSerialization {
private static Object patheticDeepCopy(Object o) throws Exception {
// Serialize
ByteArrayOutputStream bos = new ByteArrayOutputStream();
@@ -43,18 +50,44 @@
InputStream is = new ByteArrayInputStream(serializedForm);
ObjectInputStream ois = new ObjectInputStream(is);
return ois.readObject();
}
- private static boolean isSingleton(Object o) throws Exception {
- return patheticDeepCopy(o) == o;
+ @Test(dataProvider="SerializableSingletons")
+ public static void serializableSingletons(String description, Supplier<Object> o) {
+ try {
+ assertSame(patheticDeepCopy(o.get()), o.get(), description + ": not a singleton");
+ } catch(Exception all) {
+ fail(description + ": Unexpected Exception", all);
+ }
+ }
+
+ @DataProvider(name = "SerializableSingletons", parallel = true)
+ public static Iterator<Object[]> navigableMapProvider() {
+ return makeSingletons().iterator();
}
- public static void main(String[] args) throws Exception {
- if (!isSingleton(Collections.EMPTY_SET))
- throw new Exception("EMPTY_SET");
- if (!isSingleton(Collections.EMPTY_LIST))
- throw new Exception("EMPTY_LIST");
- if (!isSingleton(Collections.EMPTY_MAP))
- throw new Exception("EMPTY_MAP");
+ public static Collection<Object[]> makeSingletons() {
+ return Arrays.asList(
+ new Object[]{"Collections.EMPTY_LIST",
+ (Supplier) () -> {return Collections.EMPTY_LIST;}},
+ new Object[]{"Collections.EMPTY_MAP",
+ (Supplier) () -> {return Collections.EMPTY_MAP;}},
+ new Object[]{"Collections.EMPTY_SET",
+ (Supplier) () -> {return Collections.EMPTY_SET;}},
+ new Object[]{"Collections.singletonMap()",
+ (Supplier) () -> {return Collections.emptyList();}},
+ new Object[]{"Collections.emptyMap()",
+ (Supplier) () -> {return Collections.emptyMap();}},
+ new Object[]{"Collections.emptySet()",
+ (Supplier) () -> {return Collections.emptySet();}},
+ new Object[]{"Collections.emptySortedSet()",
+ (Supplier) () -> {return Collections.emptySortedSet();}},
+ new Object[]{"Collections.emptySortedMap()",
+ (Supplier) () -> {return Collections.emptySortedMap();}},
+ new Object[]{"Collections.emptyNavigableSet()",
+ (Supplier) () -> {return Collections.emptyNavigableSet();}},
+ new Object[]{"Collections.emptyNavigableMap()",
+ (Supplier) () -> {return Collections.emptyNavigableMap();}}
+ );
}
}