1 |
jsr166 |
1.1 |
/* |
2 |
jsr166 |
1.3 |
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
3 |
jsr166 |
1.1 |
* 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 |
jsr166 |
1.2 |
* 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 |
jsr166 |
1.1 |
*/ |
23 |
|
|
|
24 |
|
|
/* |
25 |
|
|
* @test |
26 |
|
|
* @bug 6306829 |
27 |
|
|
* @summary Verify assertions in get() javadocs |
28 |
|
|
* @author Martin Buchholz |
29 |
|
|
*/ |
30 |
|
|
|
31 |
jsr166 |
1.4 |
import java.util.HashMap; |
32 |
|
|
import java.util.Hashtable; |
33 |
|
|
import java.util.IdentityHashMap; |
34 |
|
|
import java.util.LinkedHashMap; |
35 |
|
|
import java.util.Map; |
36 |
|
|
import java.util.Objects; |
37 |
|
|
import java.util.SortedMap; |
38 |
|
|
import java.util.TreeMap; |
39 |
|
|
import java.util.WeakHashMap; |
40 |
|
|
import java.util.concurrent.ConcurrentHashMap; |
41 |
|
|
import java.util.concurrent.ConcurrentMap; |
42 |
|
|
import java.util.concurrent.ConcurrentSkipListMap; |
43 |
jsr166 |
1.1 |
|
44 |
|
|
public class Get { |
45 |
|
|
|
46 |
|
|
private static void realMain(String[] args) throws Throwable { |
47 |
|
|
testMap(new Hashtable<Character,Boolean>()); |
48 |
|
|
testMap(new HashMap<Character,Boolean>()); |
49 |
|
|
testMap(new IdentityHashMap<Character,Boolean>()); |
50 |
|
|
testMap(new LinkedHashMap<Character,Boolean>()); |
51 |
|
|
testMap(new ConcurrentHashMap<Character,Boolean>()); |
52 |
|
|
testMap(new WeakHashMap<Character,Boolean>()); |
53 |
|
|
testMap(new TreeMap<Character,Boolean>()); |
54 |
|
|
testMap(new ConcurrentSkipListMap<Character,Boolean>()); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
private static void put(Map<Character,Boolean> m, |
58 |
|
|
Character key, Boolean value, |
59 |
|
|
Boolean oldValue) { |
60 |
|
|
if (oldValue != null) { |
61 |
jsr166 |
1.3 |
check("containsValue(oldValue)", m.containsValue(oldValue)); |
62 |
|
|
check("values.contains(oldValue)", m.values().contains(oldValue)); |
63 |
jsr166 |
1.1 |
} |
64 |
|
|
equal(m.put(key, value), oldValue); |
65 |
|
|
equal(m.get(key), value); |
66 |
jsr166 |
1.3 |
check("containsKey", m.containsKey(key)); |
67 |
|
|
check("keySet.contains", m.keySet().contains(key)); |
68 |
|
|
check("containsValue", m.containsValue(value)); |
69 |
|
|
check("values.contains", m.values().contains(value)); |
70 |
|
|
check("!isEmpty", ! m.isEmpty()); |
71 |
jsr166 |
1.1 |
} |
72 |
|
|
|
73 |
|
|
private static void testMap(Map<Character,Boolean> m) { |
74 |
|
|
// We verify following assertions in get(Object) method javadocs |
75 |
|
|
boolean permitsNullKeys = (! (m instanceof ConcurrentMap || |
76 |
|
|
m instanceof Hashtable || |
77 |
|
|
m instanceof SortedMap)); |
78 |
|
|
boolean permitsNullValues = (! (m instanceof ConcurrentMap || |
79 |
|
|
m instanceof Hashtable)); |
80 |
|
|
boolean usesIdentity = m instanceof IdentityHashMap; |
81 |
|
|
|
82 |
jsr166 |
1.3 |
System.err.println(m.getClass()); |
83 |
jsr166 |
1.1 |
put(m, 'A', true, null); |
84 |
|
|
put(m, 'A', false, true); // Guaranteed identical by JLS |
85 |
|
|
put(m, 'B', true, null); |
86 |
|
|
put(m, new Character('A'), false, usesIdentity ? null : false); |
87 |
|
|
if (permitsNullKeys) { |
88 |
|
|
try { |
89 |
|
|
put(m, null, true, null); |
90 |
|
|
put(m, null, false, true); |
91 |
|
|
} |
92 |
jsr166 |
1.3 |
catch (Throwable t) { unexpected(m.getClass().getName(), t); } |
93 |
jsr166 |
1.1 |
} else { |
94 |
jsr166 |
1.3 |
try { m.get(null); fail(m.getClass().getName() + " did not reject null key"); } |
95 |
jsr166 |
1.1 |
catch (NullPointerException e) {} |
96 |
jsr166 |
1.3 |
catch (Throwable t) { unexpected(m.getClass().getName(), t); } |
97 |
jsr166 |
1.1 |
|
98 |
jsr166 |
1.3 |
try { m.put(null, true); fail(m.getClass().getName() + " did not reject null key"); } |
99 |
jsr166 |
1.1 |
catch (NullPointerException e) {} |
100 |
jsr166 |
1.3 |
catch (Throwable t) { unexpected(m.getClass().getName(), t); } |
101 |
jsr166 |
1.1 |
} |
102 |
|
|
if (permitsNullValues) { |
103 |
|
|
try { |
104 |
|
|
put(m, 'C', null, null); |
105 |
|
|
put(m, 'C', true, null); |
106 |
|
|
put(m, 'C', null, true); |
107 |
|
|
} |
108 |
jsr166 |
1.3 |
catch (Throwable t) { unexpected(m.getClass().getName(), t); } |
109 |
jsr166 |
1.1 |
} else { |
110 |
jsr166 |
1.3 |
try { m.put('A', null); fail(m.getClass().getName() + " did not reject null key"); } |
111 |
jsr166 |
1.1 |
catch (NullPointerException e) {} |
112 |
jsr166 |
1.3 |
catch (Throwable t) { unexpected(m.getClass().getName(), t); } |
113 |
jsr166 |
1.1 |
|
114 |
jsr166 |
1.3 |
try { m.put('C', null); fail(m.getClass().getName() + " did not reject null key"); } |
115 |
jsr166 |
1.1 |
catch (NullPointerException e) {} |
116 |
jsr166 |
1.3 |
catch (Throwable t) { unexpected(m.getClass().getName(), t); } |
117 |
jsr166 |
1.1 |
} |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
//--------------------- Infrastructure --------------------------- |
121 |
|
|
static volatile int passed = 0, failed = 0; |
122 |
|
|
static void pass() { passed++; } |
123 |
jsr166 |
1.5 |
static void fail() { failed++; new Error("Failure").printStackTrace(System.err); } |
124 |
|
|
static void fail(String msg) { failed++; new Error("Failure: " + msg).printStackTrace(System.err); } |
125 |
jsr166 |
1.3 |
static void unexpected(String msg, Throwable t) { System.err.println("Unexpected: " + msg); unexpected(t); } |
126 |
|
|
static void unexpected(Throwable t) { failed++; t.printStackTrace(System.err); } |
127 |
jsr166 |
1.1 |
static void check(boolean cond) { if (cond) pass(); else fail(); } |
128 |
jsr166 |
1.3 |
static void check(String desc, boolean cond) { if (cond) pass(); else fail(desc); } |
129 |
jsr166 |
1.1 |
static void equal(Object x, Object y) { |
130 |
jsr166 |
1.3 |
if (Objects.equals(x,y)) pass(); else fail(x + " not equal to " + y); |
131 |
|
|
} |
132 |
jsr166 |
1.1 |
|
133 |
|
|
public static void main(String[] args) throws Throwable { |
134 |
|
|
try { realMain(args); } catch (Throwable t) { unexpected(t); } |
135 |
|
|
|
136 |
|
|
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
137 |
jsr166 |
1.3 |
if (failed > 0) throw new Error("Some tests failed"); |
138 |
jsr166 |
1.1 |
} |
139 |
|
|
} |