1 |
/* |
2 |
* Copyright (c) 2001, 2005, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
21 |
* 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.AbstractCollection; |
33 |
import java.util.ArrayList; |
34 |
import java.util.Collection; |
35 |
import java.util.LinkedHashSet; |
36 |
import java.util.Vector; |
37 |
import java.util.concurrent.CopyOnWriteArrayList; |
38 |
import java.util.concurrent.CopyOnWriteArraySet; |
39 |
|
40 |
public class ToString { |
41 |
private static void realMain(String[] args) { |
42 |
testCollection(new LinkedHashSet<Object>() { |
43 |
public int size() { |
44 |
return super.size() + 1; // Lies, lies, all lies! |
45 |
}}); |
46 |
testCollection(new ArrayList<Object>()); |
47 |
testCollection(new Vector<Object>()); |
48 |
testCollection(new CopyOnWriteArrayList<Object>()); |
49 |
testCollection(new CopyOnWriteArraySet<Object>()); |
50 |
} |
51 |
|
52 |
private static void testCollection(Collection<Object> c) { |
53 |
System.out.println(c.getClass()); |
54 |
equal(c.toString(), "[]"); |
55 |
check(c.add("x")); |
56 |
equal(c.toString(), "[x]"); |
57 |
check(c.add("y")); |
58 |
equal(c.toString(), "[x, y]"); |
59 |
check(c.add(null)); |
60 |
equal(c.toString(), "[x, y, null]"); |
61 |
if (c instanceof AbstractCollection) { |
62 |
check(c.add(c)); |
63 |
equal(c.toString(), "[x, y, null, (this Collection)]"); |
64 |
} |
65 |
} |
66 |
|
67 |
//--------------------- Infrastructure --------------------------- |
68 |
static volatile int passed = 0, failed = 0; |
69 |
static void pass() { passed++; } |
70 |
static void fail() { failed++; Thread.dumpStack(); } |
71 |
static void fail(String msg) { System.out.println(msg); fail(); } |
72 |
static void unexpected(Throwable t) { failed++; t.printStackTrace(); } |
73 |
static void check(boolean cond) { if (cond) pass(); else fail(); } |
74 |
static void equal(Object x, Object y) { |
75 |
if (x == null ? y == null : x.equals(y)) pass(); |
76 |
else {System.out.println(x + " not equal to " + y); fail(); }} |
77 |
|
78 |
public static void main(String[] args) throws Throwable { |
79 |
try { realMain(args); } catch (Throwable t) { unexpected(t); } |
80 |
|
81 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
82 |
if (failed > 0) throw new Exception("Some tests failed"); |
83 |
} |
84 |
} |