ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/CheckedNull.java
Revision: 1.1
Committed: Tue Sep 1 01:24:16 2009 UTC (14 years, 8 months ago) by jsr166
Branch: MAIN
Log Message:
import tests from openjdk

File Contents

# Content
1 /*
2 * Copyright 2007 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 6409434
27 * @summary Test behavior of nulls in checked collections
28 */
29
30 import java.util.*;
31 import static java.util.Collections.*;
32
33 @SuppressWarnings({"unchecked","serial"})
34 public class CheckedNull {
35
36 void test(String[] args) throws Throwable {
37 testCollection(Collections.checkedCollection(
38 new ArrayList<String>(), String.class));
39 testCollection(Collections.checkedList(
40 new ArrayList<String>(), String.class));
41 testCollection(Collections.checkedSet(
42 new HashSet<String>(), String.class));
43
44 final Comparator nullLow = new Comparator() {
45 public int compare(Object x, Object y) {
46 return x == y ? 0 :
47 x == null ? -1 :
48 y == null ? 1 :
49 ((Comparable)x).compareTo(y); }};
50 testCollection(Collections.checkedSortedSet(
51 new TreeSet<String>(nullLow), String.class));
52
53 testMap(Collections.checkedMap(
54 new HashMap<String, String>(),
55 String.class, String.class));;
56 }
57
58 ClassCastException cce(F f) {
59 try { f.f(); fail(); return null; }
60 catch (ClassCastException cce) { pass(); return cce; }
61 catch (Throwable t) { unexpected(t); return null; }
62 }
63
64 void equalCCE(F ... fs) {
65 String detailMessage = null;
66 for (F f : fs)
67 if (detailMessage == null)
68 detailMessage = cce(f).getMessage();
69 else
70 equal(detailMessage, cce(f).getMessage());
71 }
72
73 void add(Collection c, Object o) {
74 int s = c.size();
75 check(! c.contains(o));
76 check(c.add(o));
77 check(c.contains(o));
78 equal(c.size(), s+1);
79 check(c.remove(o));
80 check(! c.contains(o));
81 check(c.addAll(singleton(o)));
82 check(c.contains(o));
83 equal(c.size(), s+1);
84 check(c.remove(o));
85 equal(c.size(), s);
86 }
87
88 void testCollection(final Collection c) {
89 try {
90 check(c.isEmpty());
91 add(c, null);
92 add(c, "foo");
93
94 check(c.add("bar"));
95 add(c, null);
96 add(c, "foo");
97
98 equalCCE(
99 new F(){void f(){ c.add(1); }},
100 new F(){void f(){ c.addAll(singleton(1)); }});
101
102 } catch (Throwable t) { unexpected(t); }
103 }
104
105 void put(Map m, Object k, Object v) {
106 int s = m.size();
107 check(! m.containsKey(k));
108 check(! m.containsValue(v));
109 equal(null, m.put(k, v));
110 check(m.containsKey(k));
111 check(m.containsValue(v));
112 equal(m.size(), s+1);
113 equal(v, m.remove(k));
114 check(! m.containsKey(k));
115 check(! m.containsValue(v));
116 m.putAll(singletonMap(k,v));
117 check(m.containsKey(k));
118 check(m.containsValue(v));
119 equal(m.size(), s+1);
120 equal(v,m.remove(k));
121 equal(m.size(), s);
122 }
123
124 void testMap(final Map m) {
125 try {
126 check(m.isEmpty());
127
128 put(m, "foo", null);
129 put(m, null, "foo");
130 put(m, null, null);
131 put(m, "foo", "bar");
132
133 m.put("a", "b");
134
135 put(m, "foo", null);
136 put(m, null, "foo");
137 put(m, null, null);
138 put(m, "foo", "bar");
139
140 equalCCE(
141 new F(){void f(){ m.put(1, "foo"); }},
142 new F(){void f(){ m.putAll(singletonMap(1, "foo")); }});
143
144 final Collection cheater = new ArrayList() {
145 public boolean contains(Object o) {
146 if (o instanceof Map.Entry)
147 ((Map.Entry)o).setValue(1);
148 return false; }};
149
150 equalCCE(
151 new F(){void f(){ m.put("foo", 1); }},
152 new F(){void f(){ m.putAll(singletonMap("foo", 1)); }},
153 new F(){void f(){
154 ((Map.Entry)m.entrySet().iterator().next()).setValue(1); }},
155 new F(){void f(){
156 m.entrySet().removeAll(cheater);}},
157 new F(){void f(){
158 m.entrySet().retainAll(cheater);}});
159
160 equalCCE(
161 new F(){void f(){ m.put(3, 1); }},
162 new F(){void f(){ m.putAll(singletonMap(3, 1)); }});
163
164 equal(m.size(), 1);
165 equal(m.keySet(), singleton("a"));
166 equal(m.entrySet(),
167 singleton(new AbstractMap.SimpleImmutableEntry("a","b")));
168
169 } catch (Throwable t) { unexpected(t); }
170 }
171
172 //--------------------- Infrastructure ---------------------------
173 volatile int passed = 0, failed = 0;
174 void pass() {passed++;}
175 void fail() {failed++; Thread.dumpStack();}
176 void fail(String msg) {System.err.println(msg); fail();}
177 void unexpected(Throwable t) {failed++; t.printStackTrace();}
178 void check(boolean cond) {if (cond) pass(); else fail();}
179 void equal(Object x, Object y) {
180 if (x == null ? y == null : x.equals(y)) pass();
181 else fail(x + " not equal to " + y);}
182 public static void main(String[] args) throws Throwable {
183 new CheckedNull().instanceMain(args);}
184 void instanceMain(String[] args) throws Throwable {
185 try {test(args);} catch (Throwable t) {unexpected(t);}
186 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
187 if (failed > 0) throw new AssertionError("Some tests failed");}
188 abstract class F {abstract void f() throws Throwable;}
189 }