2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
14 |
|
|
15 |
|
public class ConcurrentHashMapTest extends JSR166TestCase{ |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(ConcurrentHashMapTest.class); |
23 |
|
/** |
24 |
|
* Create a map from Integers 1-5 to Strings "A"-"E". |
25 |
|
*/ |
26 |
< |
private static ConcurrentHashMap map5() { |
26 |
> |
private static ConcurrentHashMap map5() { |
27 |
|
ConcurrentHashMap map = new ConcurrentHashMap(5); |
28 |
|
assertTrue(map.isEmpty()); |
29 |
|
map.put(one, "A"); |
66 |
|
assertTrue(map.contains("A")); |
67 |
|
assertFalse(map.contains("Z")); |
68 |
|
} |
69 |
< |
|
69 |
> |
|
70 |
|
/** |
71 |
|
* containsKey returns true for contained key |
72 |
|
*/ |
150 |
|
} |
151 |
|
|
152 |
|
/** |
153 |
+ |
* keySet.toArray returns contains all keys |
154 |
+ |
*/ |
155 |
+ |
public void testKeySetToArray() { |
156 |
+ |
ConcurrentHashMap map = map5(); |
157 |
+ |
Set s = map.keySet(); |
158 |
+ |
Object[] ar = s.toArray(); |
159 |
+ |
assertTrue(s.containsAll(Arrays.asList(ar))); |
160 |
+ |
assertEquals(5, ar.length); |
161 |
+ |
ar[0] = m10; |
162 |
+ |
assertFalse(s.containsAll(Arrays.asList(ar))); |
163 |
+ |
} |
164 |
+ |
|
165 |
+ |
/** |
166 |
+ |
* Values.toArray contains all values |
167 |
+ |
*/ |
168 |
+ |
public void testValuesToArray() { |
169 |
+ |
ConcurrentHashMap map = map5(); |
170 |
+ |
Collection v = map.values(); |
171 |
+ |
Object[] ar = v.toArray(); |
172 |
+ |
ArrayList s = new ArrayList(Arrays.asList(ar)); |
173 |
+ |
assertEquals(5, ar.length); |
174 |
+ |
assertTrue(s.contains("A")); |
175 |
+ |
assertTrue(s.contains("B")); |
176 |
+ |
assertTrue(s.contains("C")); |
177 |
+ |
assertTrue(s.contains("D")); |
178 |
+ |
assertTrue(s.contains("E")); |
179 |
+ |
} |
180 |
+ |
|
181 |
+ |
/** |
182 |
+ |
* entrySet.toArray contains all entries |
183 |
+ |
*/ |
184 |
+ |
public void testEntrySetToArray() { |
185 |
+ |
ConcurrentHashMap map = map5(); |
186 |
+ |
Set s = map.entrySet(); |
187 |
+ |
Object[] ar = s.toArray(); |
188 |
+ |
assertEquals(5, ar.length); |
189 |
+ |
for (int i = 0; i < 5; ++i) { |
190 |
+ |
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); |
191 |
+ |
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); |
192 |
+ |
} |
193 |
+ |
} |
194 |
+ |
|
195 |
+ |
/** |
196 |
|
* values collection contains all values |
197 |
|
*/ |
198 |
|
public void testValues() { |
216 |
|
Iterator it = s.iterator(); |
217 |
|
while (it.hasNext()) { |
218 |
|
Map.Entry e = (Map.Entry) it.next(); |
219 |
< |
assertTrue( |
219 |
> |
assertTrue( |
220 |
|
(e.getKey().equals(one) && e.getValue().equals("A")) || |
221 |
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
222 |
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
340 |
|
for (int i = 1; i <= 5; ++i) { |
341 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
342 |
|
} |
343 |
< |
} |
343 |
> |
} |
344 |
|
|
345 |
|
// Exception tests |
346 |
< |
|
346 |
> |
|
347 |
|
/** |
348 |
< |
* Cannot create with negative capacity |
348 |
> |
* Cannot create with negative capacity |
349 |
|
*/ |
350 |
|
public void testConstructor1() { |
351 |
|
try { |
585 |
|
* SetValue of an EntrySet entry sets value in the map. |
586 |
|
*/ |
587 |
|
public void testSetValueWriteThrough() { |
588 |
< |
// Adapted from a bug report by Eric Zoerner |
588 |
> |
// Adapted from a bug report by Eric Zoerner |
589 |
|
ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1); |
590 |
|
assertTrue(map.isEmpty()); |
591 |
|
for (int i = 0; i < 20; i++) |
592 |
|
map.put(new Integer(i), new Integer(i)); |
593 |
|
assertFalse(map.isEmpty()); |
594 |
|
Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); |
595 |
< |
|
595 |
> |
|
596 |
|
// assert that entry1 is not 16 |
597 |
|
assertTrue("entry is 16, test not valid", |
598 |
|
!entry1.getKey().equals(new Integer(16))); |
599 |
< |
|
600 |
< |
// remove 16 (a different key) from map |
599 |
> |
|
600 |
> |
// remove 16 (a different key) from map |
601 |
|
// which just happens to cause entry1 to be cloned in map |
602 |
|
map.remove(new Integer(16)); |
603 |
|
entry1.setValue("XYZ"); |
604 |
|
assertTrue(map.containsValue("XYZ")); // fails |
605 |
|
} |
606 |
< |
|
606 |
> |
|
607 |
|
} |