Path: blob/master/test/jdk/java/util/Collection/SetFactories.java
41149 views
/*1* Copyright (c) 2015, 2017, 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.util.ArrayList;29import java.util.Arrays;30import java.util.Collections;31import java.util.Iterator;32import java.util.HashSet;33import java.util.List;34import java.util.Set;35import java.util.stream.Collectors;36import static org.testng.Assert.assertEquals;3738import org.testng.annotations.DataProvider;39import org.testng.annotations.Test;4041import static org.testng.Assert.assertFalse;42import static org.testng.Assert.assertNotEquals;43import static org.testng.Assert.assertNotSame;44import static org.testng.Assert.assertSame;45import static org.testng.Assert.assertTrue;46import static org.testng.Assert.fail;4748/*49* @test50* @bug 804833051* @summary Test convenience static factory methods on Set.52* @run testng SetFactories53*/545556public class SetFactories {5758static final int NUM_STRINGS = 20; // should be larger than the largest fixed-arg overload59static final String[] stringArray;60static {61String[] sa = new String[NUM_STRINGS];62for (int i = 0; i < NUM_STRINGS; i++) {63sa[i] = String.valueOf((char)('a' + i));64}65stringArray = sa;66}6768static Object[] a(Set<String> act, Set<String> exp) {69return new Object[] { act, exp };70}7172static Set<String> hashSetOf(String... args) {73return new HashSet<>(Arrays.asList(args));74}7576@DataProvider(name="empty")77public Iterator<Object[]> empty() {78return Collections.singletonList(79// actual, expected80a(Set.of(), Collections.emptySet())81).iterator();82}8384@DataProvider(name="nonempty")85public Iterator<Object[]> nonempty() {86return Arrays.asList(87// actual, expected88a( Set.of("a"),89hashSetOf("a")),90a( Set.of("a", "b"),91hashSetOf("a", "b")),92a( Set.of("a", "b", "c"),93hashSetOf("a", "b", "c")),94a( Set.of("a", "b", "c", "d"),95hashSetOf("a", "b", "c", "d")),96a( Set.of("a", "b", "c", "d", "e"),97hashSetOf("a", "b", "c", "d", "e")),98a( Set.of("a", "b", "c", "d", "e", "f"),99hashSetOf("a", "b", "c", "d", "e", "f")),100a( Set.of("a", "b", "c", "d", "e", "f", "g"),101hashSetOf("a", "b", "c", "d", "e", "f", "g")),102a( Set.of("a", "b", "c", "d", "e", "f", "g", "h"),103hashSetOf("a", "b", "c", "d", "e", "f", "g", "h")),104a( Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i"),105hashSetOf("a", "b", "c", "d", "e", "f", "g", "h", "i")),106a( Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),107hashSetOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")),108a( Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),109Set.of("j", "i", "h", "g", "f", "e", "d", "c", "b", "a")),110a( Set.of(stringArray),111hashSetOf(stringArray))112).iterator();113}114115@DataProvider(name="all")116public Iterator<Object[]> all() {117List<Object[]> all = new ArrayList<>();118empty().forEachRemaining(all::add);119nonempty().forEachRemaining(all::add);120return all.iterator();121}122123@Test(dataProvider="all", expectedExceptions=UnsupportedOperationException.class)124public void cannotAdd(Set<String> act, Set<String> exp) {125act.add("x");126}127128@Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)129public void cannotRemove(Set<String> act, Set<String> exp) {130act.remove(act.iterator().next());131}132133@Test(dataProvider="all")134public void contentsMatch(Set<String> act, Set<String> exp) {135assertEquals(act, exp);136}137138@Test(expectedExceptions=IllegalArgumentException.class)139public void dupsDisallowed2() {140Set<String> set = Set.of("a", "a");141}142143@Test(expectedExceptions=IllegalArgumentException.class)144public void dupsDisallowed3() {145Set<String> set = Set.of("a", "b", "a");146}147148@Test(expectedExceptions=IllegalArgumentException.class)149public void dupsDisallowed4() {150Set<String> set = Set.of("a", "b", "c", "a");151}152153@Test(expectedExceptions=IllegalArgumentException.class)154public void dupsDisallowed5() {155Set<String> set = Set.of("a", "b", "c", "d", "a");156}157158@Test(expectedExceptions=IllegalArgumentException.class)159public void dupsDisallowed6() {160Set<String> set = Set.of("a", "b", "c", "d", "e", "a");161}162163@Test(expectedExceptions=IllegalArgumentException.class)164public void dupsDisallowed7() {165Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "a");166}167168@Test(expectedExceptions=IllegalArgumentException.class)169public void dupsDisallowed8() {170Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "g", "a");171}172173@Test(expectedExceptions=IllegalArgumentException.class)174public void dupsDisallowed9() {175Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "a");176}177178@Test(expectedExceptions=IllegalArgumentException.class)179public void dupsDisallowed10() {180Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "a");181}182183@Test(expectedExceptions=IllegalArgumentException.class)184public void dupsDisallowedN() {185String[] array = stringArray.clone();186array[0] = array[1];187Set<String> set = Set.of(array);188}189190@Test(dataProvider="all")191public void hashCodeEqual(Set<String> act, Set<String> exp) {192assertEquals(act.hashCode(), exp.hashCode());193}194195@Test(dataProvider="all")196public void containsAll(Set<String> act, Set<String> exp) {197assertTrue(act.containsAll(exp));198assertTrue(exp.containsAll(act));199}200201@Test(expectedExceptions=NullPointerException.class)202public void nullDisallowed1() {203Set.of((String)null); // force one-arg overload204}205206@Test(expectedExceptions=NullPointerException.class)207public void nullDisallowed2a() {208Set.of("a", null);209}210211@Test(expectedExceptions=NullPointerException.class)212public void nullDisallowed2b() {213Set.of(null, "b");214}215216@Test(expectedExceptions=NullPointerException.class)217public void nullDisallowed3() {218Set.of("a", "b", null);219}220221@Test(expectedExceptions=NullPointerException.class)222public void nullDisallowed4() {223Set.of("a", "b", "c", null);224}225226@Test(expectedExceptions=NullPointerException.class)227public void nullDisallowed5() {228Set.of("a", "b", "c", "d", null);229}230231@Test(expectedExceptions=NullPointerException.class)232public void nullDisallowed6() {233Set.of("a", "b", "c", "d", "e", null);234}235236@Test(expectedExceptions=NullPointerException.class)237public void nullDisallowed7() {238Set.of("a", "b", "c", "d", "e", "f", null);239}240241@Test(expectedExceptions=NullPointerException.class)242public void nullDisallowed8() {243Set.of("a", "b", "c", "d", "e", "f", "g", null);244}245246@Test(expectedExceptions=NullPointerException.class)247public void nullDisallowed9() {248Set.of("a", "b", "c", "d", "e", "f", "g", "h", null);249}250251@Test(expectedExceptions=NullPointerException.class)252public void nullDisallowed10() {253Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", null);254}255256@Test(expectedExceptions=NullPointerException.class)257public void nullDisallowedN() {258String[] array = stringArray.clone();259array[0] = null;260Set.of(array);261}262263@Test(expectedExceptions=NullPointerException.class)264public void nullArrayDisallowed() {265Set.of((Object[])null);266}267268@Test(dataProvider="all", expectedExceptions=NullPointerException.class)269public void containsNullShouldThrowNPE(Set<String> act, Set<String> exp) {270act.contains(null);271}272273@Test(dataProvider="all")274public void serialEquality(Set<String> act, Set<String> exp) {275// assume that act.equals(exp) tested elsewhere276Set<String> copy = serialClone(act);277assertEquals(act, copy);278assertEquals(copy, exp);279}280281@SuppressWarnings("unchecked")282static <T> T serialClone(T obj) {283try {284ByteArrayOutputStream baos = new ByteArrayOutputStream();285try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {286oos.writeObject(obj);287}288ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());289ObjectInputStream ois = new ObjectInputStream(bais);290return (T) ois.readObject();291} catch (IOException | ClassNotFoundException e) {292throw new AssertionError(e);293}294}295296Set<Integer> genSet() {297return new HashSet<>(Arrays.asList(1, 2, 3));298}299300@Test301public void copyOfResultsEqual() {302Set<Integer> orig = genSet();303Set<Integer> copy = Set.copyOf(orig);304305assertEquals(orig, copy);306assertEquals(copy, orig);307}308309@Test310public void copyOfModifiedUnequal() {311Set<Integer> orig = genSet();312Set<Integer> copy = Set.copyOf(orig);313orig.add(4);314315assertNotEquals(orig, copy);316assertNotEquals(copy, orig);317}318319@Test320public void copyOfIdentity() {321Set<Integer> orig = genSet();322Set<Integer> copy1 = Set.copyOf(orig);323Set<Integer> copy2 = Set.copyOf(copy1);324325assertNotSame(orig, copy1);326assertSame(copy1, copy2);327}328329@Test(expectedExceptions=NullPointerException.class)330public void copyOfRejectsNullCollection() {331Set<Integer> set = Set.copyOf(null);332}333334@Test(expectedExceptions=NullPointerException.class)335public void copyOfRejectsNullElements() {336Set<Integer> set = Set.copyOf(Arrays.asList(1, null, 3));337}338339@Test340public void copyOfAcceptsDuplicates() {341Set<Integer> set = Set.copyOf(Arrays.asList(1, 1, 2, 3, 3, 3));342assertEquals(set, Set.of(1, 2, 3));343}344}345346347