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.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         }
  66         stringArray = sa;
  67     }
  68 
  69     static Object[] a(Set<String> act, Set<String> exp) {
  70         return new Object[] { act, exp };
  71     }
  72 
  73     static Set<String> hashSetOf(String... args) {
  74         return new HashSet<>(Arrays.asList(args));
  75     }
  76 
  77     @DataProvider(name="empty")
  78     public Iterator<Object[]> empty() {
  79         return Collections.singletonList(
  80             // actual, expected
  81             a(Set.of(), Collections.emptySet())
  82         ).iterator();
  83     }
  84 
  85     @DataProvider(name="nonempty")
  86     public Iterator<Object[]> nonempty() {
  87         return Arrays.asList(
  88             // actual, expected
  89             a(   Set.of("a"),
  90               hashSetOf("a")),
  91             a(   Set.of("a", "b"),
  92               hashSetOf("a", "b")),
  93             a(   Set.of("a", "b", "c"),
  94               hashSetOf("a", "b", "c")),
  95             a(   Set.of("a", "b", "c", "d"),
  96               hashSetOf("a", "b", "c", "d")),
  97             a(   Set.of("a", "b", "c", "d", "e"),
  98               hashSetOf("a", "b", "c", "d", "e")),
  99             a(   Set.of("a", "b", "c", "d", "e", "f"),
 100               hashSetOf("a", "b", "c", "d", "e", "f")),
 101             a(   Set.of("a", "b", "c", "d", "e", "f", "g"),
 102               hashSetOf("a", "b", "c", "d", "e", "f", "g")),
 103             a(   Set.of("a", "b", "c", "d", "e", "f", "g", "h"),
 104               hashSetOf("a", "b", "c", "d", "e", "f", "g", "h")),
 105             a(   Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i"),
 106               hashSetOf("a", "b", "c", "d", "e", "f", "g", "h", "i")),
 107             a(   Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),
 108               hashSetOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")),
 109             a(   Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),
 110                  Set.of("j", "i", "h", "g", "f", "e", "d", "c", "b", "a")),
 111             a(   Set.of(stringArray),
 112               hashSetOf(stringArray))
 113         ).iterator();
 114     }
 115 
 116     @DataProvider(name="all")
 117     public Iterator<Object[]> all() {
 118         List<Object[]> all = new ArrayList<>();
 119         empty().forEachRemaining(all::add);
 120         nonempty().forEachRemaining(all::add);
 121         return all.iterator();
 122     }
 123 
 124     @Test(dataProvider="all", expectedExceptions=UnsupportedOperationException.class)
 125     public void cannotAdd(Set<String> act, Set<String> exp) {
 126         act.add("x");
 127     }
 128 
 129     @Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)
 130     public void cannotRemove(Set<String> act, Set<String> exp) {
 131         act.remove(act.iterator().next());
 132     }
 133 
 134     @Test(dataProvider="all")
 135     public void contentsMatch(Set<String> act, Set<String> exp) {
 136         assertEquals(act, exp);
 137     }
 138 
 139     @Test(expectedExceptions=IllegalArgumentException.class)
 140     public void dupsDisallowed2() {
 141         Set<String> set = Set.of("a", "a");
 142     }
 143 
 144     @Test(expectedExceptions=IllegalArgumentException.class)
 145     public void dupsDisallowed3() {
 146         Set<String> set = Set.of("a", "b", "a");
 147     }
 148 
 149     @Test(expectedExceptions=IllegalArgumentException.class)
 150     public void dupsDisallowed4() {
 151         Set<String> set = Set.of("a", "b", "c", "a");
 152     }
 153 
 154     @Test(expectedExceptions=IllegalArgumentException.class)
 155     public void dupsDisallowed5() {
 156         Set<String> set = Set.of("a", "b", "c", "d", "a");
 157     }
 158 
 159     @Test(expectedExceptions=IllegalArgumentException.class)
 160     public void dupsDisallowed6() {
 161         Set<String> set = Set.of("a", "b", "c", "d", "e", "a");
 162     }
 163 
 164     @Test(expectedExceptions=IllegalArgumentException.class)
 165     public void dupsDisallowed7() {
 166         Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "a");
 167     }
 168 
 169     @Test(expectedExceptions=IllegalArgumentException.class)
 170     public void dupsDisallowed8() {
 171         Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "g", "a");
 172     }
 173 
 174     @Test(expectedExceptions=IllegalArgumentException.class)
 175     public void dupsDisallowed9() {
 176         Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "a");
 177     }
 178 
 179     @Test(expectedExceptions=IllegalArgumentException.class)
 180     public void dupsDisallowed10() {
 181         Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "a");
 182     }
 183 
 184     @Test(expectedExceptions=IllegalArgumentException.class)
 185     public void dupsDisallowedN() {
 186         String[] array = stringArray.clone();
 187         array[0] = array[1];
 188         Set<String> set = Set.of(array);
 189     }
 190 
 191     @Test(dataProvider="all")
 192     public void hashCodeEqual(Set<String> act, Set<String> exp) {
 193         assertEquals(act.hashCode(), exp.hashCode());
 194     }
 195 
 196     @Test(dataProvider="all")
 197     public void containsAll(Set<String> act, Set<String> exp) {
 198         assertTrue(act.containsAll(exp));
 199         assertTrue(exp.containsAll(act));
 200     }
 201 
 202     @Test(expectedExceptions=NullPointerException.class)
 203     public void nullDisallowed1() {
 204         Set.of((String)null); // force one-arg overload
 205     }
 206 
 207     @Test(expectedExceptions=NullPointerException.class)
 208     public void nullDisallowed2a() {
 209         Set.of("a", null);
 210     }
 211 
 212     @Test(expectedExceptions=NullPointerException.class)
 213     public void nullDisallowed2b() {
 214         Set.of(null, "b");
 215     }
 216 
 217     @Test(expectedExceptions=NullPointerException.class)
 218     public void nullDisallowed3() {
 219         Set.of("a", "b", null);
 220     }
 221 
 222     @Test(expectedExceptions=NullPointerException.class)
 223     public void nullDisallowed4() {
 224         Set.of("a", "b", "c", null);
 225     }
 226 
 227     @Test(expectedExceptions=NullPointerException.class)
 228     public void nullDisallowed5() {
 229         Set.of("a", "b", "c", "d", null);
 230     }
 231 
 232     @Test(expectedExceptions=NullPointerException.class)
 233     public void nullDisallowed6() {
 234         Set.of("a", "b", "c", "d", "e", null);
 235     }
 236 
 237     @Test(expectedExceptions=NullPointerException.class)
 238     public void nullDisallowed7() {
 239         Set.of("a", "b", "c", "d", "e", "f", null);
 240     }
 241 
 242     @Test(expectedExceptions=NullPointerException.class)
 243     public void nullDisallowed8() {
 244         Set.of("a", "b", "c", "d", "e", "f", "g", null);
 245     }
 246 
 247     @Test(expectedExceptions=NullPointerException.class)
 248     public void nullDisallowed9() {
 249         Set.of("a", "b", "c", "d", "e", "f", "g", "h", null);
 250     }
 251 
 252     @Test(expectedExceptions=NullPointerException.class)
 253     public void nullDisallowed10() {
 254         Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", null);
 255     }
 256 
 257     @Test(expectedExceptions=NullPointerException.class)
 258     public void nullDisallowedN() {
 259         String[] array = stringArray.clone();
 260         array[0] = null;
 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 }