1 18 package org.apache.activemq.kaha; 19 20 import java.io.IOException ; 21 import java.util.Collection ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import java.util.Map.Entry; 28 import org.apache.activemq.kaha.MapContainer; 29 import org.apache.activemq.kaha.Store; 30 import org.apache.activemq.kaha.StoreFactory; 31 import junit.framework.TestCase; 32 33 public class MapContainerTest extends TestCase{ 34 35 protected String name = "test"; 36 protected Store store; 37 protected MapContainer container; 38 protected Map testMap; 39 protected static final int COUNT = 10; 40 41 public void testBasicAllocations() throws Exception { 42 String key = "key"; 43 Object value = testMap; 44 MapContainer test = store.getMapContainer("test","test"); 45 test.put(key,value); 46 store.close(); 47 store = getStore(); 48 assertTrue(store.getMapContainerIds().isEmpty()==false); 49 test = store.getMapContainer("test","test"); 50 assertEquals(value,test.get(key)); 51 52 } 53 56 public void testSize() throws Exception { 57 container.putAll(testMap); 58 assertTrue(container.size()==testMap.size()); 59 } 60 61 64 public void testIsEmpty() throws Exception { 65 assertTrue(container.isEmpty()); 66 } 67 68 71 public void testClear() throws Exception { 72 container.putAll(testMap); 73 assertTrue(container.size()==testMap.size()); 74 container.clear(); 75 assertTrue(container.isEmpty()); 76 } 77 78 81 public void testContainsKeyObject() throws Exception { 82 container.putAll(testMap); 83 for (Iterator i = testMap.entrySet().iterator();i.hasNext();){ 84 Map.Entry entry = (Entry) i.next(); 85 assertTrue(container.containsKey(entry.getKey())); 86 } 87 } 88 89 92 public void testGetObject() throws Exception { 93 container.putAll(testMap); 94 for (Iterator i = testMap.entrySet().iterator();i.hasNext();){ 95 Map.Entry entry = (Entry) i.next(); 96 Object value = container.get(entry.getKey()); 97 assertNotNull(value); 98 assertTrue(value.equals(entry.getValue())); 99 } 100 } 101 102 105 public void testContainsValueObject() throws Exception { 106 container.putAll(testMap); 107 for (Iterator i = testMap.entrySet().iterator();i.hasNext();){ 108 Map.Entry entry = (Entry) i.next(); 109 assertTrue(container.containsValue(entry.getValue())); 110 } 111 } 112 113 116 public void testPutAllMap() throws Exception { 117 container.putAll(testMap); 118 for (Iterator i = testMap.entrySet().iterator();i.hasNext();){ 119 Map.Entry entry = (Entry) i.next(); 120 assertTrue(container.containsValue(entry.getValue())); 121 assertTrue(container.containsKey(entry.getKey())); 122 } 123 } 124 125 128 public void testKeySet() throws Exception { 129 container.putAll(testMap); 130 Set keys = container.keySet(); 131 assertTrue(keys.size()==testMap.size()); 132 for (Iterator i = testMap.keySet().iterator();i.hasNext();){ 133 Object key = i.next(); 134 assertTrue(keys.contains(key)); 135 keys.remove(key); 136 } 137 assertTrue(container.isEmpty()); 138 139 } 140 141 144 public void testValues() throws Exception { 145 container.putAll(testMap); 146 Collection values = container.values(); 147 assertTrue(values.size()==testMap.size()); 148 for (Iterator i = testMap.values().iterator();i.hasNext();){ 149 Object value = i.next(); 150 assertTrue(values.contains(value)); 151 assertTrue(values.remove(value)); 152 } 153 assertTrue(container.isEmpty()); 154 } 155 156 159 public void testEntrySet() throws Exception { 160 container.putAll(testMap); 161 Set entries = container.entrySet(); 162 assertTrue(entries.size()==testMap.size()); 163 for (Iterator i = entries.iterator();i.hasNext();){ 164 Map.Entry entry = (Entry) i.next(); 165 assertTrue(testMap.containsKey(entry.getKey())); 166 assertTrue(testMap.containsValue(entry.getValue())); 167 168 } 169 170 } 171 172 173 174 177 public void testRemoveObject() throws Exception { 178 container.putAll(testMap); 179 for (Iterator i = testMap.keySet().iterator();i.hasNext();){ 180 container.remove(i.next()); 181 } 182 assertTrue(container.isEmpty()); 183 } 184 185 protected Store getStore() throws IOException { 186 return StoreFactory.open(name, "rw"); 187 } 188 189 protected void setUp() throws Exception { 190 super.setUp(); 191 name = System.getProperty("basedir", ".")+"/target/activemq-data/map-container.db"; 192 store = getStore(); 193 container = store.getMapContainer("test","test",true); 194 container.load(); 195 testMap = new HashMap (); 196 for (int i =0; i < COUNT; i++){ 197 String key = "key:" + i; 198 String value = "value:"+i; 199 testMap.put(key, value); 200 } 201 202 } 203 204 protected void tearDown() throws Exception { 205 super.tearDown(); 206 if( store != null ) { 207 store.close(); 208 store=null; 209 } 210 assertTrue(StoreFactory.delete(name)); 211 } 212 213 214 215 216 } 217 | Popular Tags |