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