Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/T6433170.java
38812 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.*;30import static java.util.Collections.*;3132@SuppressWarnings("unchecked")33public class T6433170 {34private void checkEmpty(Collection x) {35check(x.isEmpty());36check(x.size() == 0);37check(x.toArray().length == 0);38}3940void test(String[] args) throws Throwable {41test(checkedList(42checkedList(new ArrayList(), String.class),43Object.class));44test(checkedSet(45checkedSet(new HashSet(), String.class),46Object.class));47test(checkedCollection(48checkedCollection(new Vector(), String.class),49Object.class));50}5152void test(final Collection checked) {53checkEmpty(checked);54final List mixedList = Arrays.asList("1", 2, "3");55THROWS(ClassCastException.class,56new F(){void f(){ checked.addAll(mixedList); }});57checkEmpty(checked);58}596061//--------------------- Infrastructure ---------------------------62volatile int passed = 0, failed = 0;63void pass() {passed++;}64void fail() {failed++; Thread.dumpStack();}65void fail(String msg) {System.err.println(msg); fail();}66void unexpected(Throwable t) {failed++; t.printStackTrace();}67void check(boolean cond) {if (cond) pass(); else fail();}68void equal(Object x, Object y) {69if (x == null ? y == null : x.equals(y)) pass();70else fail(x + " not equal to " + y);}71public static void main(String[] args) throws Throwable {72new T6433170().instanceMain(args);}73void instanceMain(String[] args) throws Throwable {74try {test(args);} catch (Throwable t) {unexpected(t);}75System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);76if (failed > 0) throw new AssertionError("Some tests failed");}77abstract class F {abstract void f() throws Throwable;}78void THROWS(Class<? extends Throwable> k, F... fs) {79for (F f : fs)80try {f.f(); fail("Expected " + k.getName() + " not thrown");}81catch (Throwable t) {82if (k.isAssignableFrom(t.getClass())) pass();83else unexpected(t);}}84}858687