001: package org.jgroups.tests;
002:
003: import org.jgroups.Channel;
004: import org.jgroups.blocks.DistributedHashtable;
005:
006: import java.util.HashMap;
007: import java.util.Map;
008:
009: /**
010: * Test methods for DistributedHashtable
011: * @author denis.pasek@senacor.com
012: * @version $Revision: 1.2.2.3 $
013: **/
014: public class DistributedHashtableUnitTest extends ChannelTestBase {
015:
016: private static int testCount = 1;
017:
018: private DistributedHashtable map1;
019: private DistributedHashtable map2;
020:
021: public DistributedHashtableUnitTest(String testName) {
022: super (testName);
023: }
024:
025: public void setUp() throws Exception {
026: super .setUp();
027: System.out.println("#### Setup Test " + testCount);
028:
029: Channel c1 = createChannel("A");
030: this .map1 = new DistributedHashtable(c1, false, 5000);
031: c1.connect("demo");
032: this .map1.start(5000);
033:
034: Channel c2 = createChannel("A");
035: this .map2 = new DistributedHashtable(c2, false, 5000);
036: c2.connect("demo");
037: this .map2.start(5000);
038: }
039:
040: public void tearDown() throws Exception {
041: this .map1.stop();
042: this .map2.stop();
043: System.out.println("#### TearDown Test " + testCount + "\n\n");
044: testCount++;
045: super .tearDown();
046: }
047:
048: public void testSize() {
049: assertEquals(0, this .map1.size());
050: assertEquals(this .map2.size(), this .map1.size());
051:
052: this .map1.put("key1", "value1");
053: assertEquals(1, this .map1.size());
054: assertEquals(this .map2.size(), this .map1.size());
055:
056: this .map2.put("key2", "value2");
057: assertEquals(2, this .map1.size());
058: assertEquals(this .map2.size(), this .map1.size());
059: }
060:
061: public void testIsEmpty() {
062: assertTrue(this .map1.isEmpty());
063: assertTrue(this .map2.isEmpty());
064:
065: this .map1.put("key", "value");
066:
067: assertFalse(this .map1.isEmpty());
068: assertFalse(this .map2.isEmpty());
069: }
070:
071: public void testContainsKey() {
072: assertFalse(this .map1.containsKey("key1"));
073: assertFalse(this .map2.containsKey("key1"));
074: this .map1.put("key1", "value");
075: assertTrue(this .map1.containsKey("key1"));
076: assertTrue(this .map2.containsKey("key1"));
077: this .map2.put("key2", "value");
078: assertTrue(this .map1.containsKey("key2"));
079: assertTrue(this .map2.containsKey("key2"));
080: }
081:
082: public void testContainsValue() {
083: assertFalse(this .map1.containsValue("value1"));
084: assertFalse(this .map2.containsValue("value1"));
085: this .map1.put("key1", "value1");
086: assertTrue(this .map1.containsValue("value1"));
087: assertTrue(this .map2.containsValue("value1"));
088: this .map2.put("key2", "value2");
089: assertTrue(this .map1.containsValue("value2"));
090: assertTrue(this .map2.containsValue("value2"));
091: }
092:
093: public void testPutAndGet() {
094: assertNull(this .map1.get("key1"));
095: assertNull(this .map2.get("key1"));
096: this .map1.put("key1", "value1");
097: assertNotNull(this .map1.get("key1"));
098: assertNotNull(this .map2.get("key1"));
099: this .map2.put("key2", "value2");
100: assertNotNull(this .map1.get("key2"));
101: assertNotNull(this .map2.get("key2"));
102: }
103:
104: public void testRemove() {
105: assertNull(this .map1.get("key1"));
106: assertNull(this .map2.get("key1"));
107: this .map1.put("key1", "value1");
108: this .map2.put("key2", "value2");
109: assertNotNull(this .map1.get("key1"));
110: assertNotNull(this .map2.get("key1"));
111: assertNotNull(this .map1.get("key2"));
112: assertNotNull(this .map2.get("key2"));
113:
114: this .map1.remove("key1");
115: assertNull(this .map1.get("key1"));
116: assertNull(this .map2.get("key1"));
117: assertNotNull(this .map1.get("key2"));
118: assertNotNull(this .map2.get("key2"));
119:
120: this .map2.remove("key2");
121: assertNull(this .map1.get("key2"));
122: assertNull(this .map2.get("key2"));
123: }
124:
125: public void testPutAll() {
126: Map all1 = new HashMap();
127: all1.put("key1", "value1");
128: all1.put("key2", "value2");
129: Map all2 = new HashMap();
130: all2.put("key3", "value3");
131: all2.put("key4", "value4");
132:
133: this .map1.putAll(all1);
134: assertEquals(2, this .map1.size());
135: assertEquals(2, this .map2.size());
136: this .map2.putAll(all2);
137: assertEquals(4, this .map1.size());
138: assertEquals(4, this .map2.size());
139:
140: assertTrue(this .map1.containsKey("key1"));
141: assertTrue(this .map1.containsKey("key2"));
142: assertTrue(this .map1.containsKey("key3"));
143: assertTrue(this .map1.containsKey("key4"));
144:
145: assertTrue(this .map2.containsKey("key1"));
146: assertTrue(this .map2.containsKey("key2"));
147: assertTrue(this .map2.containsKey("key3"));
148: assertTrue(this .map2.containsKey("key4"));
149: }
150:
151: public void testClear() {
152: assertTrue(this .map1.isEmpty());
153: assertTrue(this .map2.isEmpty());
154:
155: this .map1.put("key", "value");
156: assertFalse(this .map1.isEmpty());
157: assertFalse(this .map2.isEmpty());
158:
159: this .map1.clear();
160: assertTrue(this .map1.isEmpty());
161: assertTrue(this .map2.isEmpty());
162: this .map2.put("key", "value");
163: assertFalse(this .map1.isEmpty());
164: assertFalse(this .map2.isEmpty());
165:
166: this .map2.clear();
167: assertTrue(this .map1.isEmpty());
168: assertTrue(this .map2.isEmpty());
169: }
170:
171: public void testKeySet() {
172: Map all1 = new HashMap();
173: all1.put("key1", "value1");
174: all1.put("key2", "value2");
175: Map all2 = new HashMap();
176: all2.put("key3", "value3");
177: all2.put("key4", "value4");
178:
179: this .map1.putAll(all1);
180: assertEquals(all1.keySet(), this .map1.keySet());
181: assertEquals(all1.keySet(), this .map2.keySet());
182:
183: this .map2.putAll(all2);
184: all1.putAll(all2);
185: assertEquals(all1.keySet(), this .map1.keySet());
186: assertEquals(all1.keySet(), this .map2.keySet());
187: }
188:
189: public void testValues() {
190: Map all1 = new HashMap();
191: all1.put("key1", "value1");
192: all1.put("key2", "value2");
193: Map all2 = new HashMap();
194: all2.put("key3", "value3");
195: all2.put("key4", "value4");
196:
197: this.map1.putAll(all1);
198: assertTrue(this.map1.values().containsAll(all1.values()));
199: assertTrue(this.map2.values().containsAll(all1.values()));
200:
201: this.map2.putAll(all2);
202: all1.putAll(all2);
203: assertTrue(this.map1.values().containsAll(all1.values()));
204: assertTrue(this.map2.values().containsAll(all1.values()));
205: }
206:
207: }
|