ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/CheckedNull.java
Revision: 1.4
Committed: Mon Jan 8 03:12:03 2018 UTC (6 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +12 -2 lines
Log Message:
organize imports

File Contents

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