001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.util.concurrent;
006:
007: import java.util.ArrayList;
008: import java.util.Arrays;
009: import java.util.HashMap;
010: import java.util.HashSet;
011: import java.util.Iterator;
012: import java.util.LinkedHashMap;
013: import java.util.Map;
014: import java.util.Set;
015: import java.util.Map.Entry;
016:
017: import junit.framework.TestCase;
018:
019: public class CopyOnWriteArrayMapTest extends TestCase {
020:
021: public void testBasic() throws Exception {
022: CopyOnWriteArrayMap cam = new CopyOnWriteArrayMap();
023: ArrayList al = new ArrayList();
024: assertArrayEquals(al.toArray(), cam.values().toArray());
025:
026: // test put new key
027: String s1 = "Hello there";
028: al.add(s1);
029: cam.put(s1, s1);
030: assertArrayEquals(al.toArray(), cam.values().toArray());
031:
032: // test put new key
033: String s2 = "Hello back";
034: al.add(s2);
035: cam.put(new Integer(10), s2);
036: assertArrayEquals(al.toArray(), cam.values().toArray());
037:
038: // test put old key
039: String s3 = "Hello Saro";
040: al.remove(1);
041: al.add(s3);
042: cam.put(new Integer(10), s3);
043: assertArrayEquals(al.toArray(), cam.values().toArray());
044:
045: // test remap
046: cam.put(new Integer(10), s3);
047: assertArrayEquals(al.toArray(), cam.values().toArray());
048:
049: // test putall
050: Map m = new LinkedHashMap();
051: m.put(new Long(9), new Float(9.1));
052: m.put(new Long(19), new Float(91.1));
053: m.put(new Long(191), new Float(191.1));
054: al.addAll(m.values());
055: cam.putAll(m);
056: assertArrayEquals(al.toArray(), cam.values().toArray());
057:
058: // test non-existent key removal
059: cam.remove("uv rays");
060: assertArrayEquals(al.toArray(), cam.values().toArray());
061:
062: // test existent key removal
063: al.remove(1);
064: cam.remove(new Integer(10));
065: assertArrayEquals(al.toArray(), cam.values().toArray());
066: al.remove(al.size() - 1);
067: cam.remove(new Long(191));
068: assertArrayEquals(al.toArray(), cam.values().toArray());
069:
070: // test clear
071: al.clear();
072: cam.clear();
073: assertArrayEquals(al.toArray(), cam.values().toArray());
074:
075: }
076:
077: public void testSameValueMappedTo2Keys() throws Exception {
078: CopyOnWriteArrayMap cam = new CopyOnWriteArrayMap();
079: ArrayList al = new ArrayList();
080: assertArrayEquals(al.toArray(), cam.values().toArray());
081:
082: Integer val = new Integer(0);
083: for (int i = 0; i < 10;) {
084: cam.put(Integer.toString(i), val);
085: al.add(val);
086: assertArrayEquals(al.toArray(), cam.values().toArray());
087: if (++i % 2 == 0) {
088: val = new Integer(i / 2);
089: }
090: }
091:
092: // remove
093: for (int i = 0; i < 10; i++) {
094: Integer val1 = new Integer(i / 2);
095: Integer val2 = (Integer) cam.remove(Integer.toString(i));
096: assertEquals(val1, val2);
097: al.remove(val2);
098: assertArrayEquals(al.toArray(), cam.values().toArray());
099: }
100:
101: }
102:
103: public void testBasicEntrySet() throws Exception {
104: CopyOnWriteArrayMap cam = new CopyOnWriteArrayMap();
105: cam.put(new Integer(10), "String value 10");
106: cam.put(new Integer(20), "String value 20");
107: cam.put(new Integer(30), "String value 30");
108: cam.put(new Integer(40), "String value 40");
109:
110: System.err.println(" Printing cam : " + cam);
111:
112: Map newMap = new HashMap(cam);
113: assertEquals(cam, newMap);
114: for (Iterator i = cam.entrySet().iterator(); i.hasNext();) {
115: Entry e = (Entry) i.next();
116: System.err
117: .println("Trying to remap : should throw an exception : "
118: + e);
119: try {
120: e.setValue("Hey Jude");
121: } catch (UnsupportedOperationException ue) {
122: System.err.println("Caught exception as expected ;)");
123: }
124: System.err
125: .println("Trying to trying to remove using the iterator : should throw an exception ");
126: try {
127: i.remove();
128: } catch (UnsupportedOperationException ue) {
129: System.err.println("Caught exception as expected ;)");
130: }
131: }
132: }
133:
134: public void testBasicKeySet() throws Exception {
135: CopyOnWriteArrayMap cam = new CopyOnWriteArrayMap();
136: cam.put(new Integer(10), "String value 10");
137: cam.put(new Integer(20), "String value 20");
138: cam.put(new Integer(30), "String value 30");
139: cam.put(new Integer(40), "String value 40");
140:
141: Set expectedKeys = new HashSet();
142: expectedKeys.add(new Integer(10));
143: expectedKeys.add(new Integer(20));
144: expectedKeys.add(new Integer(30));
145: expectedKeys.add(new Integer(40));
146:
147: Set keys = cam.keySet();
148: assertEquals(expectedKeys, keys);
149:
150: int count = expectedKeys.size();
151: for (Iterator i = keys.iterator(); i.hasNext();) {
152: Object key = i.next();
153: assertTrue(expectedKeys.contains(key));
154: count--;
155: }
156: assertEquals(0, count);
157: }
158:
159: public void testTypedArrayFactory() throws Exception {
160: CopyOnWriteArrayMap cam = new CopyOnWriteArrayMap(
161: new CopyOnWriteArrayMap.TypedArrayFactory() {
162: public Object[] createTypedArray(int size) {
163: return new String[size];
164: }
165: });
166: cam.put(new Long(99), "hey");
167: cam.put(new Long(999), "you");
168: cam.put(new Long(9999), "jude");
169:
170: String values[] = (String[]) cam.valuesToArray();
171: assertEquals(3, values.length);
172: Set s = new HashSet(Arrays.asList(values));
173: assertTrue(s.remove("hey"));
174: assertTrue(s.remove("you"));
175: assertTrue(s.remove("jude"));
176: assertTrue(s.isEmpty());
177: }
178:
179: private void assertArrayEquals(Object[] a1, Object[] a2) {
180: print("a1", a1);
181: print("a2", a2);
182: if (a1 == null) {
183: assertNull(a2);
184: return;
185: } else {
186: assertNotNull(a2);
187: }
188: assertEquals(a1.length, a2.length);
189: for (int i = 0; i < a1.length; i++) {
190: assertEquals(a1[i], a2[i]);
191: }
192: System.err.println("EQUAL");
193: }
194:
195: private void print(String name, Object[] a) {
196: System.err.print(name + " : ");
197: if (a == null) {
198: System.err.println("null");
199: return;
200: }
201: for (int i = 0; i < a.length; i++) {
202: System.err.print(a[i]);
203: if (i < a.length - 1) {
204: System.err.print(", ");
205: }
206: }
207: System.err.println("");
208: }
209:
210: }
|