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.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(stringArray), 107 hashSetOf(stringArray)) 108 ).iterator(); 109 } 110 111 @DataProvider(name="all") 112 public Iterator<Object[]> all() { 113 List<Object[]> all = new ArrayList<>(); 114 empty().forEachRemaining(all::add); 115 nonempty().forEachRemaining(all::add); 116 return all.iterator(); 117 } 118 119 @Test(dataProvider="all", expectedExceptions=UnsupportedOperationException.class) 120 public void cannotAdd(Set<String> act, Set<String> exp) { 121 act.add("x"); 122 } 123 124 @Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class) 125 public void cannotRemove(Set<String> act, Set<String> exp) { 126 act.remove(act.iterator().next()); 127 } 128 129 @Test(dataProvider="all") 130 public void contentsMatch(Set<String> act, Set<String> exp) { 131 assertEquals(act, exp); 132 } 133 134 @Test(expectedExceptions=IllegalArgumentException.class) 135 public void dupsDisallowed2() { 136 Set<String> set = Set.of("a", "a"); 137 } 138 139 @Test(expectedExceptions=IllegalArgumentException.class) 140 public void dupsDisallowed3() { 141 Set<String> set = Set.of("a", "b", "a"); 142 } 143 144 @Test(expectedExceptions=IllegalArgumentException.class) 145 public void dupsDisallowed4() { 146 Set<String> set = Set.of("a", "b", "c", "a"); 147 } 148 149 @Test(expectedExceptions=IllegalArgumentException.class) 150 public void dupsDisallowed5() { 151 Set<String> set = Set.of("a", "b", "c", "d", "a"); 152 } 153 154 @Test(expectedExceptions=IllegalArgumentException.class) 155 public void dupsDisallowed6() { 156 Set<String> set = Set.of("a", "b", "c", "d", "e", "a"); 157 } 158 159 @Test(expectedExceptions=IllegalArgumentException.class) 160 public void dupsDisallowed7() { 161 Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "a"); 162 } 163 164 @Test(expectedExceptions=IllegalArgumentException.class) 165 public void dupsDisallowed8() { 166 Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "g", "a"); 167 } 168 169 @Test(expectedExceptions=IllegalArgumentException.class) 170 public void dupsDisallowed9() { 171 Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "a"); 172 } 173 174 @Test(expectedExceptions=IllegalArgumentException.class) 175 public void dupsDisallowed10() { 176 Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "a"); 177 } 178 179 @Test(expectedExceptions=IllegalArgumentException.class) 180 public void dupsDisallowedN() { 181 String[] array = stringArray.clone(); 182 array[0] = array[1]; 183 Set<String> set = Set.of(array); 184 } 185 186 @Test(expectedExceptions=NullPointerException.class) 187 public void nullDisallowed1() { 188 Set.of((String)null); // force one-arg overload 189 } 190 191 @Test(expectedExceptions=NullPointerException.class) 192 public void nullDisallowed2a() { 193 Set.of("a", null); 194 } 195 196 @Test(expectedExceptions=NullPointerException.class) 197 public void nullDisallowed2b() { 198 Set.of(null, "b"); 199 } 200 201 @Test(expectedExceptions=NullPointerException.class) 202 public void nullDisallowed3() { 203 Set.of("a", "b", null); 204 } 205 206 @Test(expectedExceptions=NullPointerException.class) 207 public void nullDisallowed4() { 208 Set.of("a", "b", "c", null); 209 } 210 211 @Test(expectedExceptions=NullPointerException.class) 212 public void nullDisallowed5() { 213 Set.of("a", "b", "c", "d", null); 214 } 215 216 @Test(expectedExceptions=NullPointerException.class) 217 public void nullDisallowed6() { 218 Set.of("a", "b", "c", "d", "e", null); 219 } 220 221 @Test(expectedExceptions=NullPointerException.class) 222 public void nullDisallowed7() { 223 Set.of("a", "b", "c", "d", "e", "f", null); 224 } 225 226 @Test(expectedExceptions=NullPointerException.class) 227 public void nullDisallowed8() { 228 Set.of("a", "b", "c", "d", "e", "f", "g", null); 229 } 230 231 @Test(expectedExceptions=NullPointerException.class) 232 public void nullDisallowed9() { 233 Set.of("a", "b", "c", "d", "e", "f", "g", "h", null); 234 } 235 236 @Test(expectedExceptions=NullPointerException.class) 237 public void nullDisallowed10() { 238 Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", null); 239 } 240 241 @Test(expectedExceptions=NullPointerException.class) 242 public void nullDisallowedN() { 243 String[] array = stringArray.clone(); 244 array[0] = null; 245 Set.of(array); 246 } 247 248 @Test(expectedExceptions=NullPointerException.class) 249 public void nullArrayDisallowed() { 250 Set.of((Object[])null); 251 } 252 253 @Test(dataProvider="all") 254 public void serialEquality(Set<String> act, Set<String> exp) { 255 // assume that act.equals(exp) tested elsewhere 256 Set<String> copy = serialClone(act); 257 assertEquals(act, copy); 258 assertEquals(copy, exp); 259 } 260 261 @SuppressWarnings("unchecked") 262 static <T> T serialClone(T obj) { 263 try { 264 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 265 ObjectOutputStream oos = new ObjectOutputStream(baos); 266 oos.writeObject(obj); 267 oos.close(); 268 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 269 ObjectInputStream ois = new ObjectInputStream(bais); 270 return (T) ois.readObject(); 271 } catch (IOException | ClassNotFoundException e) { 272 throw new AssertionError(e); 273 } 274 } 275 }