Path: blob/master/test/jdk/java/util/Collections/T6433170.java
41152 views
/*1* Copyright (c) 2007, 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*/2223/*24* @test25* @bug 643317026* @summary CheckedCollection.addAll should be all-or-nothing27*/2829import java.util.ArrayList;30import java.util.Arrays;31import java.util.Collection;32import java.util.HashSet;33import java.util.List;34import java.util.Vector;3536import static java.util.Collections.checkedCollection;37import static java.util.Collections.checkedList;38import static java.util.Collections.checkedSet;3940@SuppressWarnings("unchecked")41public class T6433170 {42private void checkEmpty(Collection x) {43check(x.isEmpty());44check(x.size() == 0);45check(x.toArray().length == 0);46}4748void test(String[] args) throws Throwable {49test(checkedList(50checkedList(new ArrayList(), String.class),51Object.class));52test(checkedSet(53checkedSet(new HashSet(), String.class),54Object.class));55test(checkedCollection(56checkedCollection(new Vector(), String.class),57Object.class));58}5960void test(final Collection checked) {61checkEmpty(checked);62final List mixedList = Arrays.asList("1", 2, "3");63THROWS(ClassCastException.class,64new F(){void f(){ checked.addAll(mixedList); }});65checkEmpty(checked);66}6768//--------------------- Infrastructure ---------------------------69volatile int passed = 0, failed = 0;70void pass() {passed++;}71void fail() {failed++; Thread.dumpStack();}72void fail(String msg) {System.err.println(msg); fail();}73void unexpected(Throwable t) {failed++; t.printStackTrace();}74void check(boolean cond) {if (cond) pass(); else fail();}75void equal(Object x, Object y) {76if (x == null ? y == null : x.equals(y)) pass();77else fail(x + " not equal to " + y);}78public static void main(String[] args) throws Throwable {79new T6433170().instanceMain(args);}80void instanceMain(String[] args) throws Throwable {81try {test(args);} catch (Throwable t) {unexpected(t);}82System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);83if (failed > 0) throw new AssertionError("Some tests failed");}84abstract class F {abstract void f() throws Throwable;}85void THROWS(Class<? extends Throwable> k, F... fs) {86for (F f : fs)87try {f.f(); fail("Expected " + k.getName() + " not thrown");}88catch (Throwable t) {89if (k.isAssignableFrom(t.getClass())) pass();90else unexpected(t);}}91}929394