1 5 package com.tc.util.concurrent; 6 7 import java.util.ArrayList ; 8 import java.util.LinkedHashMap ; 9 import java.util.Map ; 10 11 import junit.framework.TestCase; 12 13 public class CopyOnWriteArrayMapTest extends TestCase { 14 15 public void testBasic() throws Exception { 16 CopyOnWriteArrayMap cam = new CopyOnWriteArrayMap(); 17 ArrayList al = new ArrayList (); 18 assertArrayEquals(al.toArray(), cam.valuesArray()); 19 20 String s1 = "Hello there"; 22 al.add(s1); 23 cam.put(s1, s1); 24 assertArrayEquals(al.toArray(), cam.valuesArray()); 25 26 String s2 = "Hello back"; 28 al.add(s2); 29 cam.put(new Integer (10), s2); 30 assertArrayEquals(al.toArray(), cam.valuesArray()); 31 32 String s3 = "Hello Saro"; 34 al.remove(1); 35 al.add(s3); 36 cam.put(new Integer (10), s3); 37 assertArrayEquals(al.toArray(), cam.valuesArray()); 38 39 cam.put(new Integer (10), s3); 41 assertArrayEquals(al.toArray(), cam.valuesArray()); 42 43 Map m = new LinkedHashMap (); 45 m.put(new Long (9), new Float (9.1)); 46 m.put(new Long (19), new Float (91.1)); 47 m.put(new Long (191), new Float (191.1)); 48 al.addAll(m.values()); 49 cam.putAll(m); 50 assertArrayEquals(al.toArray(), cam.valuesArray()); 51 52 cam.remove("uv rays"); 54 assertArrayEquals(al.toArray(), cam.valuesArray()); 55 56 al.remove(1); 58 cam.remove(new Integer (10)); 59 assertArrayEquals(al.toArray(), cam.valuesArray()); 60 al.remove(al.size() - 1); 61 cam.remove(new Long (191)); 62 assertArrayEquals(al.toArray(), cam.valuesArray()); 63 64 al.clear(); 66 cam.clear(); 67 assertArrayEquals(al.toArray(), cam.valuesArray()); 68 69 } 70 71 private void assertArrayEquals(Object [] a1, Object [] 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 name, Object [] 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 |