KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > concurrent > CopyOnWriteArrayMapTest


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.util.concurrent;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.LinkedHashMap JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import junit.framework.TestCase;
12
13 public class CopyOnWriteArrayMapTest extends TestCase {
14
15   public void testBasic() throws Exception JavaDoc {
16     CopyOnWriteArrayMap cam = new CopyOnWriteArrayMap();
17     ArrayList JavaDoc al = new ArrayList JavaDoc();
18     assertArrayEquals(al.toArray(), cam.valuesArray());
19
20     // test put new key
21
String JavaDoc s1 = "Hello there";
22     al.add(s1);
23     cam.put(s1, s1);
24     assertArrayEquals(al.toArray(), cam.valuesArray());
25
26     // test put new key
27
String JavaDoc s2 = "Hello back";
28     al.add(s2);
29     cam.put(new Integer JavaDoc(10), s2);
30     assertArrayEquals(al.toArray(), cam.valuesArray());
31
32     // test put old key
33
String JavaDoc s3 = "Hello Saro";
34     al.remove(1);
35     al.add(s3);
36     cam.put(new Integer JavaDoc(10), s3);
37     assertArrayEquals(al.toArray(), cam.valuesArray());
38
39     // test remap
40
cam.put(new Integer JavaDoc(10), s3);
41     assertArrayEquals(al.toArray(), cam.valuesArray());
42
43     // test putall
44
Map JavaDoc m = new LinkedHashMap JavaDoc();
45     m.put(new Long JavaDoc(9), new Float JavaDoc(9.1));
46     m.put(new Long JavaDoc(19), new Float JavaDoc(91.1));
47     m.put(new Long JavaDoc(191), new Float JavaDoc(191.1));
48     al.addAll(m.values());
49     cam.putAll(m);
50     assertArrayEquals(al.toArray(), cam.valuesArray());
51
52     // test non-existent key removal
53
cam.remove("uv rays");
54     assertArrayEquals(al.toArray(), cam.valuesArray());
55
56     // test existent key removal
57
al.remove(1);
58     cam.remove(new Integer JavaDoc(10));
59     assertArrayEquals(al.toArray(), cam.valuesArray());
60     al.remove(al.size() - 1);
61     cam.remove(new Long JavaDoc(191));
62     assertArrayEquals(al.toArray(), cam.valuesArray());
63
64     // test clear
65
al.clear();
66     cam.clear();
67     assertArrayEquals(al.toArray(), cam.valuesArray());
68
69   }
70
71   private void assertArrayEquals(Object JavaDoc[] a1, Object JavaDoc[] a2) {
72     print("a1", a1);
73     print("a2", a2);
74     if (a1 == null) {
75       assertNull(a2);
76       return;
77     } else {
78       assertNotNull(a2);
79     }
80     assertEquals(a1.length, a2.length);
81     for (int i = 0; i < a1.length; i++) {
82       assertEquals(a1[i], a2[i]);
83     }
84     System.err.println("EQUAL");
85   }
86
87   private void print(String JavaDoc name, Object JavaDoc[] a) {
88     System.err.print(name + " : ");
89     if (a == null) {
90       System.err.println("null");
91       return;
92     }
93     for (int i = 0; i < a.length; i++) {
94       System.err.print(a[i]);
95       if (i < a.length - 1) {
96         System.err.print(", ");
97       }
98     }
99     System.err.println("");
100   }
101
102 }
103
Popular Tags