1 /*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
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.List;
34
35 import org.testng.annotations.DataProvider;
36 import org.testng.annotations.Test;
37
38 import static java.util.Arrays.asList;
39
40 import static org.testng.Assert.assertEquals;
41 import static org.testng.Assert.assertFalse;
42 import static org.testng.Assert.assertTrue;
43 import static org.testng.Assert.fail;
44
45 /*
46 * @test
47 * @bug 8048330
48 * @summary Test convenience static factory methods on List.
49 * @run testng ListFactories
50 */
51
52 public class ListFactories {
53
54 static final int NUM_STRINGS = 20; // should be larger than the largest fixed-arg overload
55 static final String[] stringArray;
56 static {
57 String[] sa = new String[NUM_STRINGS];
58 for (int i = 0; i < NUM_STRINGS; i++) {
59 sa[i] = String.valueOf((char)('a' + i));
60 }
61 stringArray = sa;
204 @Test
205 public void ensureArrayCannotModifyList() {
206 String[] array = stringArray.clone();
207 List<String> list = List.of(array);
208 array[0] = "xyzzy";
209 assertEquals(list, Arrays.asList(stringArray));
210 }
211
212 @Test(dataProvider="all")
213 public void serialEquality(List<String> act, List<String> exp) {
214 // assume that act.equals(exp) tested elsewhere
215 List<String> copy = serialClone(act);
216 assertEquals(act, copy);
217 assertEquals(copy, exp);
218 }
219
220 @SuppressWarnings("unchecked")
221 static <T> T serialClone(T obj) {
222 try {
223 ByteArrayOutputStream baos = new ByteArrayOutputStream();
224 ObjectOutputStream oos = new ObjectOutputStream(baos);
225 oos.writeObject(obj);
226 oos.close();
227 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
228 ObjectInputStream ois = new ObjectInputStream(bais);
229 return (T) ois.readObject();
230 } catch (IOException | ClassNotFoundException e) {
231 throw new AssertionError(e);
232 }
233 }
234 }
|
1 /*
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
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.List;
34
35 import org.testng.annotations.DataProvider;
36 import org.testng.annotations.Test;
37
38 import static java.util.Arrays.asList;
39
40 import static org.testng.Assert.assertEquals;
41 import static org.testng.Assert.assertFalse;
42 import static org.testng.Assert.assertNotEquals;
43 import static org.testng.Assert.assertNotSame;
44 import static org.testng.Assert.assertSame;
45 import static org.testng.Assert.assertTrue;
46 import static org.testng.Assert.fail;
47
48 /*
49 * @test
50 * @bug 8048330
51 * @summary Test convenience static factory methods on List.
52 * @run testng ListFactories
53 */
54
55 public class ListFactories {
56
57 static final int NUM_STRINGS = 20; // should be larger than the largest fixed-arg overload
58 static final String[] stringArray;
59 static {
60 String[] sa = new String[NUM_STRINGS];
61 for (int i = 0; i < NUM_STRINGS; i++) {
62 sa[i] = String.valueOf((char)('a' + i));
63 }
64 stringArray = sa;
207 @Test
208 public void ensureArrayCannotModifyList() {
209 String[] array = stringArray.clone();
210 List<String> list = List.of(array);
211 array[0] = "xyzzy";
212 assertEquals(list, Arrays.asList(stringArray));
213 }
214
215 @Test(dataProvider="all")
216 public void serialEquality(List<String> act, List<String> exp) {
217 // assume that act.equals(exp) tested elsewhere
218 List<String> copy = serialClone(act);
219 assertEquals(act, copy);
220 assertEquals(copy, exp);
221 }
222
223 @SuppressWarnings("unchecked")
224 static <T> T serialClone(T obj) {
225 try {
226 ByteArrayOutputStream baos = new ByteArrayOutputStream();
227 try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
228 oos.writeObject(obj);
229 }
230 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
231 ObjectInputStream ois = new ObjectInputStream(bais);
232 return (T) ois.readObject();
233 } catch (IOException | ClassNotFoundException e) {
234 throw new AssertionError(e);
235 }
236 }
237
238 List<Integer> genList() {
239 return new ArrayList<>(Arrays.asList(1, 2, 3));
240 }
241
242 @Test
243 public void copyOfResultsEqual() {
244 List<Integer> orig = genList();
245 List<Integer> copy = List.copyOf(orig);
246
247 assertEquals(orig, copy);
248 assertEquals(copy, orig);
249 }
250
251 @Test
252 public void copyOfModifiedUnequal() {
253 List<Integer> orig = genList();
254 List<Integer> copy = List.copyOf(orig);
255 orig.add(4);
256
257 assertNotEquals(orig, copy);
258 assertNotEquals(copy, orig);
259 }
260
261 @Test
262 public void copyOfIdentity() {
263 List<Integer> orig = genList();
264 List<Integer> copy1 = List.copyOf(orig);
265 List<Integer> copy2 = List.copyOf(copy1);
266
267 assertNotSame(orig, copy1);
268 assertSame(copy1, copy2);
269 }
270
271 @Test(expectedExceptions=NullPointerException.class)
272 public void copyOfRejectsNullCollection() {
273 List<Integer> list = List.copyOf(null);
274 }
275
276 @Test(expectedExceptions=NullPointerException.class)
277 public void copyOfRejectsNullElements() {
278 List<Integer> list = List.copyOf(Arrays.asList(1, null, 3));
279 }
280 }
|