Path: blob/master/test/jdk/java/util/AbstractCollection/ToString.java
41149 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.AbstractCollection;32import java.util.ArrayList;33import java.util.Collection;34import java.util.LinkedHashSet;35import java.util.Vector;36import java.util.concurrent.CopyOnWriteArrayList;37import java.util.concurrent.CopyOnWriteArraySet;3839public class ToString {40private static void realMain(String[] args) {41testCollection(new LinkedHashSet<Object>() {42public int size() {43return super.size() + 1; // Lies, lies, all lies!44}});45testCollection(new ArrayList<Object>());46testCollection(new Vector<Object>());47testCollection(new CopyOnWriteArrayList<Object>());48testCollection(new CopyOnWriteArraySet<Object>());49}5051private static void testCollection(Collection<Object> c) {52System.out.println(c.getClass());53equal(c.toString(), "[]");54check(c.add("x"));55equal(c.toString(), "[x]");56check(c.add("y"));57equal(c.toString(), "[x, y]");58check(c.add(null));59equal(c.toString(), "[x, y, null]");60if (c instanceof AbstractCollection) {61check(c.add(c));62equal(c.toString(), "[x, y, null, (this Collection)]");63}64}6566//--------------------- Infrastructure ---------------------------67static volatile int passed = 0, failed = 0;68static void pass() { passed++; }69static void fail() { failed++; Thread.dumpStack(); }70static void fail(String msg) { System.out.println(msg); fail(); }71static void unexpected(Throwable t) { failed++; t.printStackTrace(); }72static void check(boolean cond) { if (cond) pass(); else fail(); }73static void equal(Object x, Object y) {74if (x == null ? y == null : x.equals(y)) pass();75else {System.out.println(x + " not equal to " + y); fail(); }}7677public static void main(String[] args) throws Throwable {78try { realMain(args); } catch (Throwable t) { unexpected(t); }7980System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);81if (failed > 0) throw new Exception("Some tests failed");82}83}848586