1 18 package org.apache.activemq.kaha; 19 20 import java.io.IOException ; 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import junit.framework.TestCase; 29 30 35 public class StoreTest extends TestCase{ 36 37 protected String name; 38 protected Store store; 39 40 41 44 public void testClose() throws Exception { 45 store.close(); 46 try { 47 store.getListContainer("fred"); 49 assertTrue("Should have got a enception",false); 50 }catch(Exception e){ 51 52 } 53 } 54 55 58 public void testClear() throws Exception { 59 int count = 100; 60 ListContainer list = store.getListContainer("testClear"); 61 list.load(); 62 for (int i =0; i < count; i++){ 63 list.add("test " + i); 64 } 65 assertEquals(count,list.size()); 66 store.clear(); 67 assertTrue(list.isEmpty()); 68 } 69 70 71 72 75 public void testGetMapContainer() throws Exception { 76 String containerId = "test"; 77 MapContainer container = store.getMapContainer(containerId); 78 container.load(); 79 assertNotNull(container); 80 store.close(); 81 store = getStore(); 82 container = store.getMapContainer(containerId); 83 assertNotNull(container); 84 85 86 87 } 88 89 92 public void testDeleteMapContainer() throws Exception { 93 String containerId = "test"; 94 MapContainer container = store.getMapContainer(containerId); 95 assertNotNull(container); 96 store.deleteMapContainer(containerId); 97 assertFalse(store.doesMapContainerExist(containerId)); 98 store.close(); 99 store = getStore(); 100 assertFalse(store.doesMapContainerExist(containerId)); 101 } 102 103 104 105 106 109 public void testGetListContainer() throws Exception { 110 String containerId = "test"; 111 ListContainer container = store.getListContainer(containerId); 112 assertNotNull(container); 113 store.close(); 114 store = getStore(); 115 container = store.getListContainer(containerId); 116 assertNotNull(container); 117 } 118 119 122 public void testDeleteListContainer()throws Exception { 123 String containerId = "test"; 124 ListContainer container = store.getListContainer(containerId); 125 assertNotNull(container); 126 store.deleteListContainer(containerId); 127 assertFalse(store.doesListContainerExist(containerId)); 128 store.close(); 129 store = getStore(); 130 assertFalse(store.doesListContainerExist(containerId)); 131 } 132 133 134 public void testBasicAllocations() throws Exception { 135 Map testMap = new HashMap (); 136 int count = 1000; 137 for (int i =0; i<count; i++){ 138 String key = "key:"+i; 139 String value = "value:"+i; 140 testMap.put(key, value); 141 } 142 List testList = new ArrayList (); 143 for (int i = 0; i < count; i++){ 144 testList.add("value:"+i); 145 } 146 String listId = "testList"; 147 String mapId1 = "testMap"; 148 String mapId2 = "testMap2"; 149 MapContainer mapContainer1 = store.getMapContainer(mapId1); 150 mapContainer1.load(); 151 mapContainer1.putAll(testMap); 152 153 MapContainer mapContainer2 = store.getMapContainer(mapId2,mapId2); 154 mapContainer2.load(); 155 mapContainer2.putAll(testMap); 156 157 ListContainer listContainer = store.getListContainer(listId); 158 listContainer.load(); 159 160 listContainer.addAll(testList); 161 store.close(); 162 store = getStore(); 163 mapContainer1 = store.getMapContainer(mapId1); 164 mapContainer1.load(); 165 mapContainer2 = store.getMapContainer(mapId2,mapId2); 166 mapContainer2.load(); 167 listContainer = store.getListContainer(listId); 168 listContainer.load(); 169 for (Iterator i = testMap.keySet().iterator(); i.hasNext();){ 170 Object key = i.next(); 171 Object value = testMap.get(key); 172 assertTrue(mapContainer1.containsKey(key)); 173 assertEquals(value,mapContainer1.get(key)); 174 } 175 for (Iterator i = testMap.keySet().iterator(); i.hasNext();){ 176 Object key = i.next(); 177 Object value = testMap.get(key); 178 assertTrue(mapContainer2.containsKey(key)); 179 assertEquals(value,mapContainer2.get(key)); 180 } 181 assertEquals(testList.size(),listContainer.size()); 182 for (Iterator i = testList.iterator(), j = listContainer.iterator(); i.hasNext();){ 183 assertEquals(i.next(),j.next()); 184 } 185 } 186 187 188 protected Store getStore() throws IOException { 189 return StoreFactory.open(name, "rw"); 190 } 191 192 protected void setUp() throws Exception { 193 super.setUp(); 194 name = System.getProperty("basedir", ".")+"/target/activemq-data/store-test.db"; 195 store = getStore(); 196 } 197 198 protected void tearDown() throws Exception { 199 super.tearDown(); 200 if( store!=null ) { 201 store.close(); 202 store=null; 203 } 204 boolean rc = StoreFactory.delete(name); 205 assertTrue(rc); 206 } 207 } 208 | Popular Tags |