Path: blob/master/test/jdk/java/util/List/ListFactories.java
41149 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.IOException;26import java.io.ObjectInputStream;27import java.io.ObjectOutputStream;28import java.io.Serializable;29import java.util.ArrayList;30import java.util.Arrays;31import java.util.Collections;32import java.util.Iterator;33import java.util.List;34import java.util.ListIterator;35import java.util.stream.Stream;3637import org.testng.annotations.DataProvider;38import org.testng.annotations.Test;3940import static java.util.Arrays.asList;4142import static org.testng.Assert.assertEquals;43import static org.testng.Assert.assertFalse;44import static org.testng.Assert.assertNotEquals;45import static org.testng.Assert.assertNotSame;46import static org.testng.Assert.assertSame;47import static org.testng.Assert.assertTrue;48import static org.testng.Assert.fail;4950/*51* @test52* @bug 8048330 820318453* @summary Test convenience static factory methods on List.54* @run testng ListFactories55*/5657public class ListFactories {5859static final int NUM_STRINGS = 20; // should be larger than the largest fixed-arg overload60static final String[] stringArray;61static {62String[] sa = new String[NUM_STRINGS];63for (int i = 0; i < NUM_STRINGS; i++) {64sa[i] = String.valueOf((char)('a' + i));65}66stringArray = sa;67}6869// returns array of [actual, expected]70static Object[] a(List<String> act, List<String> exp) {71return new Object[] { act, exp };72}7374@DataProvider(name="empty")75public Iterator<Object[]> empty() {76return Collections.singletonList(77a(List.of(), asList())78).iterator();79}8081@DataProvider(name="nonempty")82public Iterator<Object[]> nonempty() {83return asList(84a(List.of("a"),85asList("a")),86a(List.of("a", "b"),87asList("a", "b")),88a(List.of("a", "b", "c"),89asList("a", "b", "c")),90a(List.of("a", "b", "c", "d"),91asList("a", "b", "c", "d")),92a(List.of("a", "b", "c", "d", "e"),93asList("a", "b", "c", "d", "e")),94a(List.of("a", "b", "c", "d", "e", "f"),95asList("a", "b", "c", "d", "e", "f")),96a(List.of("a", "b", "c", "d", "e", "f", "g"),97asList("a", "b", "c", "d", "e", "f", "g")),98a(List.of("a", "b", "c", "d", "e", "f", "g", "h"),99asList("a", "b", "c", "d", "e", "f", "g", "h")),100a(List.of("a", "b", "c", "d", "e", "f", "g", "h", "i"),101asList("a", "b", "c", "d", "e", "f", "g", "h", "i")),102a(List.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),103asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")),104a(List.of(stringArray),105asList(stringArray))106).iterator();107}108109@DataProvider(name="sublists")110public Iterator<Object[]> sublists() {111return asList(112a(List.<String>of().subList(0,0),113asList()),114a(List.of("a").subList(0,0),115asList("a").subList(0,0)),116a(List.of("a", "b").subList(0,1),117asList("a", "b").subList(0,1)),118a(List.of("a", "b", "c").subList(1,3),119asList("a", "b", "c").subList(1,3)),120a(List.of("a", "b", "c", "d").subList(0,4),121asList("a", "b", "c", "d").subList(0,4)),122a(List.of("a", "b", "c", "d", "e").subList(0,3),123asList("a", "b", "c", "d", "e").subList(0,3)),124a(List.of("a", "b", "c", "d", "e", "f").subList(3, 5),125asList("a", "b", "c", "d", "e", "f").subList(3, 5)),126a(List.of("a", "b", "c", "d", "e", "f", "g").subList(0, 7),127asList("a", "b", "c", "d", "e", "f", "g").subList(0, 7)),128a(List.of("a", "b", "c", "d", "e", "f", "g", "h").subList(0, 0),129asList("a", "b", "c", "d", "e", "f", "g", "h").subList(0, 0)),130a(List.of("a", "b", "c", "d", "e", "f", "g", "h", "i").subList(4, 5),131asList("a", "b", "c", "d", "e", "f", "g", "h", "i").subList(4, 5)),132a(List.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j").subList(1,10),133asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j").subList(1,10)),134a(List.of(stringArray).subList(5, NUM_STRINGS),135asList(Arrays.copyOfRange(stringArray, 5, NUM_STRINGS)))136).iterator();137}138139@DataProvider(name="all")140public Iterator<Object[]> all() {141List<Object[]> all = new ArrayList<>();142empty().forEachRemaining(all::add);143nonempty().forEachRemaining(all::add);144sublists().forEachRemaining(all::add);145return all.iterator();146}147148@DataProvider(name="nonsublists")149public Iterator<Object[]> nonsublists() {150List<Object[]> all = new ArrayList<>();151empty().forEachRemaining(all::add);152nonempty().forEachRemaining(all::add);153return all.iterator();154}155156@Test(dataProvider="all", expectedExceptions=UnsupportedOperationException.class)157public void cannotAddLast(List<String> act, List<String> exp) {158act.add("x");159}160161@Test(dataProvider="all", expectedExceptions=UnsupportedOperationException.class)162public void cannotAddFirst(List<String> act, List<String> exp) {163act.add(0, "x");164}165166@Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)167public void cannotRemove(List<String> act, List<String> exp) {168act.remove(0);169}170171@Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)172public void cannotSet(List<String> act, List<String> exp) {173act.set(0, "x");174}175176@Test(dataProvider="all")177public void contentsMatch(List<String> act, List<String> exp) {178assertEquals(act, exp);179}180181@Test(expectedExceptions=NullPointerException.class)182public void nullDisallowed1() {183List.of((Object)null); // force one-arg overload184}185186@Test(expectedExceptions=NullPointerException.class)187public void nullDisallowed2a() {188List.of("a", null);189}190191@Test(expectedExceptions=NullPointerException.class)192public void nullDisallowed2b() {193List.of(null, "b");194}195196@Test(expectedExceptions=NullPointerException.class)197public void nullDisallowed3() {198List.of("a", "b", null);199}200201@Test(expectedExceptions=NullPointerException.class)202public void nullDisallowed4() {203List.of("a", "b", "c", null);204}205206@Test(expectedExceptions=NullPointerException.class)207public void nullDisallowed5() {208List.of("a", "b", "c", "d", null);209}210211@Test(expectedExceptions=NullPointerException.class)212public void nullDisallowed6() {213List.of("a", "b", "c", "d", "e", null);214}215216@Test(expectedExceptions=NullPointerException.class)217public void nullDisallowed7() {218List.of("a", "b", "c", "d", "e", "f", null);219}220221@Test(expectedExceptions=NullPointerException.class)222public void nullDisallowed8() {223List.of("a", "b", "c", "d", "e", "f", "g", null);224}225226@Test(expectedExceptions=NullPointerException.class)227public void nullDisallowed9() {228List.of("a", "b", "c", "d", "e", "f", "g", "h", null);229}230231@Test(expectedExceptions=NullPointerException.class)232public void nullDisallowed10() {233List.of("a", "b", "c", "d", "e", "f", "g", "h", "i", null);234}235236@Test(expectedExceptions=NullPointerException.class)237public void nullDisallowedN() {238String[] array = stringArray.clone();239array[0] = null;240List.of(array);241}242243@Test(expectedExceptions=NullPointerException.class)244public void nullArrayDisallowed() {245List.of((Object[])null);246}247248@Test249public void ensureArrayCannotModifyList() {250String[] array = stringArray.clone();251List<String> list = List.of(array);252array[0] = "xyzzy";253assertEquals(list, Arrays.asList(stringArray));254}255256@Test(dataProvider="all", expectedExceptions=NullPointerException.class)257public void containsNullShouldThrowNPE(List<String> act, List<String> exp) {258act.contains(null);259}260261@Test(dataProvider="all", expectedExceptions=NullPointerException.class)262public void indexOfNullShouldThrowNPE(List<String> act, List<String> exp) {263act.indexOf(null);264}265266@Test(dataProvider="all", expectedExceptions=NullPointerException.class)267public void lastIndexOfNullShouldThrowNPE(List<String> act, List<String> exp) {268act.lastIndexOf(null);269}270271// List.of().subList views should not be Serializable272@Test(dataProvider="sublists")273public void isNotSerializable(List<String> act, List<String> exp) {274assertFalse(act instanceof Serializable);275}276277// ... but List.of() should be278@Test(dataProvider="nonsublists")279public void serialEquality(List<String> act, List<String> exp) {280// assume that act.equals(exp) tested elsewhere281List<String> copy = serialClone(act);282assertEquals(act, copy);283assertEquals(copy, exp);284}285286@SuppressWarnings("unchecked")287static <T> T serialClone(T obj) {288try {289ByteArrayOutputStream baos = new ByteArrayOutputStream();290try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {291oos.writeObject(obj);292}293ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());294ObjectInputStream ois = new ObjectInputStream(bais);295return (T) ois.readObject();296} catch (IOException | ClassNotFoundException e) {297throw new AssertionError(e);298}299}300301List<Integer> genList() {302return new ArrayList<>(Arrays.asList(1, 2, 3));303}304305@Test306public void copyOfResultsEqual() {307List<Integer> orig = genList();308List<Integer> copy = List.copyOf(orig);309310assertEquals(orig, copy);311assertEquals(copy, orig);312}313314@Test315public void copyOfModifiedUnequal() {316List<Integer> orig = genList();317List<Integer> copy = List.copyOf(orig);318orig.add(4);319320assertNotEquals(orig, copy);321assertNotEquals(copy, orig);322}323324@Test325public void copyOfIdentity() {326List<Integer> orig = genList();327List<Integer> copy1 = List.copyOf(orig);328List<Integer> copy2 = List.copyOf(copy1);329330assertNotSame(orig, copy1);331assertSame(copy1, copy2);332}333334@Test335public void copyOfSubList() {336List<Integer> orig = List.of(0, 1, 2, 3);337List<Integer> sub = orig.subList(0, 3);338List<Integer> copy = List.copyOf(sub);339340assertNotSame(sub, copy);341}342343@Test344public void copyOfSubSubList() {345List<Integer> orig = List.of(0, 1, 2, 3);346List<Integer> sub = orig.subList(0, 3).subList(0, 2);347List<Integer> copy = List.copyOf(sub);348349assertNotSame(sub, copy);350}351352@Test(expectedExceptions=NullPointerException.class)353public void copyOfRejectsNullCollection() {354List<Integer> list = List.copyOf(null);355}356357@Test(expectedExceptions=NullPointerException.class)358public void copyOfRejectsNullElements() {359List<Integer> list = List.copyOf(Arrays.asList(1, null, 3));360}361362@Test(expectedExceptions=NullPointerException.class)363public void copyOfRejectsNullElements2() {364List<String> list = List.copyOf(Stream.of("a", null, "c").toList());365}366367@Test368public void copyOfCopiesNullAllowingList() {369List<String> orig = Stream.of("a", "b", "c").toList();370List<String> copy = List.copyOf(orig);371372assertNotSame(orig, copy);373}374375@Test376public void iteratorShouldNotBeListIterator() {377List<Integer> list = List.of(1, 2, 3, 4, 5);378Iterator<Integer> it = list.iterator();379it.next();380try {381((ListIterator<Integer>) it).previous();382fail("ListIterator operation succeeded on Iterator");383} catch (ClassCastException|UnsupportedOperationException ignore) { }384}385}386387388