Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/AbstractCollection/ToString.java
38811 views
/*1* Copyright (c) 2001, 2005, 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 4486049 6282555 631862226* @summary toString method fails if size changes in between a call to size27* and an attempt to iterate.28* @author Josh Bloch, Martin Buchholz29*/3031import java.util.*;32import java.util.concurrent.*;3334public class ToString {35private static void realMain(String[] args) {36testCollection(new LinkedHashSet<Object>() {37public int size() {38return super.size() + 1; // Lies, lies, all lies!39}});40testCollection(new ArrayList<Object>());41testCollection(new Vector<Object>());42testCollection(new CopyOnWriteArrayList<Object>());43testCollection(new CopyOnWriteArraySet<Object>());44}4546private static void testCollection(Collection<Object> c) {47System.out.println(c.getClass());48equal(c.toString(), "[]");49check(c.add("x"));50equal(c.toString(), "[x]");51check(c.add("y"));52equal(c.toString(), "[x, y]");53check(c.add(null));54equal(c.toString(), "[x, y, null]");55if (c instanceof AbstractCollection) {56check(c.add(c));57equal(c.toString(), "[x, y, null, (this Collection)]");58}59}6061//--------------------- Infrastructure ---------------------------62static volatile int passed = 0, failed = 0;63static void pass() { passed++; }64static void fail() { failed++; Thread.dumpStack(); }65static void fail(String msg) { System.out.println(msg); fail(); }66static void unexpected(Throwable t) { failed++; t.printStackTrace(); }67static void check(boolean cond) { if (cond) pass(); else fail(); }68static void equal(Object x, Object y) {69if (x == null ? y == null : x.equals(y)) pass();70else {System.out.println(x + " not equal to " + y); fail(); }}7172public static void main(String[] args) throws Throwable {73try { realMain(args); } catch (Throwable t) { unexpected(t); }7475System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);76if (failed > 0) throw new Exception("Some tests failed");77}78}798081