duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2001-2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4486049 6282555 6318622 |
| 27 | * @summary toString method fails if size changes in between a call to size |
| 28 | * and an attempt to iterate. |
| 29 | * @author Josh Bloch, Martin Buchholz |
| 30 | */ |
| 31 | |
| 32 | import java.util.*; |
| 33 | import java.util.concurrent.*; |
| 34 | |
| 35 | public class ToString { |
| 36 | private static void realMain(String[] args) { |
| 37 | testCollection(new LinkedHashSet<Object>() { |
| 38 | public int size() { |
| 39 | return super.size() + 1; // Lies, lies, all lies! |
| 40 | }}); |
| 41 | testCollection(new ArrayList<Object>()); |
| 42 | testCollection(new Vector<Object>()); |
| 43 | testCollection(new CopyOnWriteArrayList<Object>()); |
| 44 | testCollection(new CopyOnWriteArraySet<Object>()); |
| 45 | } |
| 46 | |
| 47 | private static void testCollection(Collection<Object> c) { |
| 48 | System.out.println(c.getClass()); |
| 49 | equal(c.toString(), "[]"); |
| 50 | check(c.add("x")); |
| 51 | equal(c.toString(), "[x]"); |
| 52 | check(c.add("y")); |
| 53 | equal(c.toString(), "[x, y]"); |
| 54 | check(c.add(null)); |
| 55 | equal(c.toString(), "[x, y, null]"); |
| 56 | if (c instanceof AbstractCollection) { |
| 57 | check(c.add(c)); |
| 58 | equal(c.toString(), "[x, y, null, (this Collection)]"); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | //--------------------- Infrastructure --------------------------- |
| 63 | static volatile int passed = 0, failed = 0; |
| 64 | static void pass() { passed++; } |
| 65 | static void fail() { failed++; Thread.dumpStack(); } |
| 66 | static void fail(String msg) { System.out.println(msg); fail(); } |
| 67 | static void unexpected(Throwable t) { failed++; t.printStackTrace(); } |
| 68 | static void check(boolean cond) { if (cond) pass(); else fail(); } |
| 69 | static void equal(Object x, Object y) { |
| 70 | if (x == null ? y == null : x.equals(y)) pass(); |
| 71 | else {System.out.println(x + " not equal to " + y); fail(); }} |
| 72 | |
| 73 | public static void main(String[] args) throws Throwable { |
| 74 | try { realMain(args); } catch (Throwable t) { unexpected(t); } |
| 75 | |
| 76 | System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
| 77 | if (failed > 0) throw new Exception("Some tests failed"); |
| 78 | } |
| 79 | } |