23
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.io.ObjectInputStream;
28 import java.io.ObjectOutputStream;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.Iterator;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Set;
36 import java.util.stream.Collectors;
37 import static org.testng.Assert.assertEquals;
38
39 import org.testng.annotations.DataProvider;
40 import org.testng.annotations.Test;
41
42 import static org.testng.Assert.assertFalse;
43 import static org.testng.Assert.assertTrue;
44 import static org.testng.Assert.fail;
45
46 /*
47 * @test
48 * @bug 8048330
49 * @summary Test convenience static factory methods on Set.
50 * @run testng SetFactories
51 */
52
53
54 public class SetFactories {
55
56 static final int NUM_STRINGS = 20; // should be larger than the largest fixed-arg overload
57 static final String[] stringArray;
58 static {
59 String[] sa = new String[NUM_STRINGS];
60 for (int i = 0; i < NUM_STRINGS; i++) {
61 sa[i] = String.valueOf((char)('a' + i));
62 }
258 Set.of(array);
259 }
260
261 @Test(expectedExceptions=NullPointerException.class)
262 public void nullArrayDisallowed() {
263 Set.of((Object[])null);
264 }
265
266 @Test(dataProvider="all")
267 public void serialEquality(Set<String> act, Set<String> exp) {
268 // assume that act.equals(exp) tested elsewhere
269 Set<String> copy = serialClone(act);
270 assertEquals(act, copy);
271 assertEquals(copy, exp);
272 }
273
274 @SuppressWarnings("unchecked")
275 static <T> T serialClone(T obj) {
276 try {
277 ByteArrayOutputStream baos = new ByteArrayOutputStream();
278 ObjectOutputStream oos = new ObjectOutputStream(baos);
279 oos.writeObject(obj);
280 oos.close();
281 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
282 ObjectInputStream ois = new ObjectInputStream(bais);
283 return (T) ois.readObject();
284 } catch (IOException | ClassNotFoundException e) {
285 throw new AssertionError(e);
286 }
287 }
288 }
|
23
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.io.ObjectInputStream;
28 import java.io.ObjectOutputStream;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.Iterator;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Set;
36 import java.util.stream.Collectors;
37 import static org.testng.Assert.assertEquals;
38
39 import org.testng.annotations.DataProvider;
40 import org.testng.annotations.Test;
41
42 import static org.testng.Assert.assertFalse;
43 import static org.testng.Assert.assertNotEquals;
44 import static org.testng.Assert.assertNotSame;
45 import static org.testng.Assert.assertSame;
46 import static org.testng.Assert.assertTrue;
47 import static org.testng.Assert.fail;
48
49 /*
50 * @test
51 * @bug 8048330
52 * @summary Test convenience static factory methods on Set.
53 * @run testng SetFactories
54 */
55
56
57 public class SetFactories {
58
59 static final int NUM_STRINGS = 20; // should be larger than the largest fixed-arg overload
60 static final String[] stringArray;
61 static {
62 String[] sa = new String[NUM_STRINGS];
63 for (int i = 0; i < NUM_STRINGS; i++) {
64 sa[i] = String.valueOf((char)('a' + i));
65 }
261 Set.of(array);
262 }
263
264 @Test(expectedExceptions=NullPointerException.class)
265 public void nullArrayDisallowed() {
266 Set.of((Object[])null);
267 }
268
269 @Test(dataProvider="all")
270 public void serialEquality(Set<String> act, Set<String> exp) {
271 // assume that act.equals(exp) tested elsewhere
272 Set<String> copy = serialClone(act);
273 assertEquals(act, copy);
274 assertEquals(copy, exp);
275 }
276
277 @SuppressWarnings("unchecked")
278 static <T> T serialClone(T obj) {
279 try {
280 ByteArrayOutputStream baos = new ByteArrayOutputStream();
281 try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
282 oos.writeObject(obj);
283 }
284 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
285 ObjectInputStream ois = new ObjectInputStream(bais);
286 return (T) ois.readObject();
287 } catch (IOException | ClassNotFoundException e) {
288 throw new AssertionError(e);
289 }
290 }
291
292 Set<Integer> genSet() {
293 return new HashSet<>(Arrays.asList(1, 2, 3));
294 }
295
296 @Test
297 public void copyOfResultsEqual() {
298 Set<Integer> orig = genSet();
299 Set<Integer> copy = Set.copyOf(orig);
300
301 assertEquals(orig, copy);
302 assertEquals(copy, orig);
303 }
304
305 @Test
306 public void copyOfModifiedUnequal() {
307 Set<Integer> orig = genSet();
308 Set<Integer> copy = Set.copyOf(orig);
309 orig.add(4);
310
311 assertNotEquals(orig, copy);
312 assertNotEquals(copy, orig);
313 }
314
315 @Test
316 public void copyOfIdentity() {
317 Set<Integer> orig = genSet();
318 Set<Integer> copy1 = Set.copyOf(orig);
319 Set<Integer> copy2 = Set.copyOf(copy1);
320
321 assertNotSame(orig, copy1);
322 assertSame(copy1, copy2);
323 }
324
325 @Test(expectedExceptions=NullPointerException.class)
326 public void copyOfRejectsNullCollection() {
327 Set<Integer> set = Set.copyOf(null);
328 }
329
330 @Test(expectedExceptions=NullPointerException.class)
331 public void copyOfRejectsNullElements() {
332 Set<Integer> set = Set.copyOf(Arrays.asList(1, null, 3));
333 }
334
335 @Test
336 public void copyOfAcceptsDuplicates() {
337 Set<Integer> set = Set.copyOf(Arrays.asList(1, 1, 2, 3, 3, 3));
338 assertEquals(set, Set.of(1, 2, 3));
339 }
340 }
|