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;
  62     }
  63 
  64     // returns array of [actual, expected]
  65     static Object[] a(List<String> act, List<String> exp) {
  66         return new Object[] { act, exp };
  67     }
  68 
  69     @DataProvider(name="empty")
  70     public Iterator<Object[]> empty() {
  71         return Collections.singletonList(
  72             a(List.of(), Collections.emptyList())
  73         ).iterator();
  74     }
  75 
  76     @DataProvider(name="nonempty")
  77     public Iterator<Object[]> nonempty() {
  78         return asList(
  79             a(List.of("a"),
  80                asList("a")),
  81             a(List.of("a", "b"),
  82                asList("a", "b")),
  83             a(List.of("a", "b", "c"),
  84                asList("a", "b", "c")),
  85             a(List.of("a", "b", "c", "d"),
  86                asList("a", "b", "c", "d")),
  87             a(List.of("a", "b", "c", "d", "e"),
  88                asList("a", "b", "c", "d", "e")),
  89             a(List.of("a", "b", "c", "d", "e", "f"),
  90                asList("a", "b", "c", "d", "e", "f")),
  91             a(List.of("a", "b", "c", "d", "e", "f", "g"),
  92                asList("a", "b", "c", "d", "e", "f", "g")),
  93             a(List.of("a", "b", "c", "d", "e", "f", "g", "h"),
  94                asList("a", "b", "c", "d", "e", "f", "g", "h")),
  95             a(List.of("a", "b", "c", "d", "e", "f", "g", "h", "i"),
  96                asList("a", "b", "c", "d", "e", "f", "g", "h", "i")),
  97             a(List.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),
  98                asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")),
  99             a(List.of(stringArray),
 100                asList(stringArray))
 101         ).iterator();
 102     }
 103 
 104     @DataProvider(name="all")
 105     public Iterator<Object[]> all() {
 106         List<Object[]> all = new ArrayList<>();
 107         empty().forEachRemaining(all::add);
 108         nonempty().forEachRemaining(all::add);
 109         return all.iterator();
 110     }
 111 
 112     @Test(dataProvider="all", expectedExceptions=UnsupportedOperationException.class)
 113     public void cannotAddLast(List<String> act, List<String> exp) {
 114         act.add("x");
 115     }
 116 
 117     @Test(dataProvider="all", expectedExceptions=UnsupportedOperationException.class)
 118     public void cannotAddFirst(List<String> act, List<String> exp) {
 119         act.add(0, "x");
 120     }
 121 
 122     @Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)
 123     public void cannotRemove(List<String> act, List<String> exp) {
 124         act.remove(0);
 125     }
 126 
 127     @Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)
 128     public void cannotSet(List<String> act, List<String> exp) {
 129         act.set(0, "x");
 130     }
 131 
 132     @Test(dataProvider="all")
 133     public void contentsMatch(List<String> act, List<String> exp) {
 134         assertEquals(act, exp);
 135     }
 136 
 137     @Test(expectedExceptions=NullPointerException.class)
 138     public void nullDisallowed1() {
 139         List.of((Object)null); // force one-arg overload
 140     }
 141 
 142     @Test(expectedExceptions=NullPointerException.class)
 143     public void nullDisallowed2a() {
 144         List.of("a", null);
 145     }
 146 
 147     @Test(expectedExceptions=NullPointerException.class)
 148     public void nullDisallowed2b() {
 149         List.of(null, "b");
 150     }
 151 
 152     @Test(expectedExceptions=NullPointerException.class)
 153     public void nullDisallowed3() {
 154         List.of("a", "b", null);
 155     }
 156 
 157     @Test(expectedExceptions=NullPointerException.class)
 158     public void nullDisallowed4() {
 159         List.of("a", "b", "c", null);
 160     }
 161 
 162     @Test(expectedExceptions=NullPointerException.class)
 163     public void nullDisallowed5() {
 164         List.of("a", "b", "c", "d", null);
 165     }
 166 
 167     @Test(expectedExceptions=NullPointerException.class)
 168     public void nullDisallowed6() {
 169         List.of("a", "b", "c", "d", "e", null);
 170     }
 171 
 172     @Test(expectedExceptions=NullPointerException.class)
 173     public void nullDisallowed7() {
 174         List.of("a", "b", "c", "d", "e", "f", null);
 175     }
 176 
 177     @Test(expectedExceptions=NullPointerException.class)
 178     public void nullDisallowed8() {
 179         List.of("a", "b", "c", "d", "e", "f", "g", null);
 180     }
 181 
 182     @Test(expectedExceptions=NullPointerException.class)
 183     public void nullDisallowed9() {
 184         List.of("a", "b", "c", "d", "e", "f", "g", "h", null);
 185     }
 186 
 187     @Test(expectedExceptions=NullPointerException.class)
 188     public void nullDisallowed10() {
 189         List.of("a", "b", "c", "d", "e", "f", "g", "h", "i", null);
 190     }
 191 
 192     @Test(expectedExceptions=NullPointerException.class)
 193     public void nullDisallowedN() {
 194         String[] array = stringArray.clone();
 195         array[0] = null;
 196         List.of(array);
 197     }
 198 
 199     @Test(expectedExceptions=NullPointerException.class)
 200     public void nullArrayDisallowed() {
 201         List.of((Object[])null);
 202     }
 203 
 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 }