KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > StoreTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.kaha;
19
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import junit.framework.TestCase;
29
30 /**
31 *Store test
32 *
33 * @version $Revision: 1.2 $
34 */

35 public class StoreTest extends TestCase{
36     
37     protected String JavaDoc name;
38     protected Store store;
39     
40
41     /*
42      * Test method for 'org.apache.activemq.kaha.Store.close()'
43      */

44     public void testClose() throws Exception JavaDoc{
45         store.close();
46         try {
47             //access should throw an exception
48
store.getListContainer("fred");
49             assertTrue("Should have got a enception",false);
50         }catch(Exception JavaDoc e){
51             
52         }
53     }
54
55     /*
56      * Test method for 'org.apache.activemq.kaha.Store.clear()'
57      */

58     public void testClear() throws Exception JavaDoc{
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     /*
73      * Test method for 'org.apache.activemq.kaha.Store.getMapContainer(Object)'
74      */

75     public void testGetMapContainer() throws Exception JavaDoc{
76         String JavaDoc 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     /*
90      * Test method for 'org.apache.activemq.kaha.Store.deleteMapContainer(Object)'
91      */

92     public void testDeleteMapContainer() throws Exception JavaDoc{
93         String JavaDoc 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     /*
107      * Test method for 'org.apache.activemq.kaha.Store.getListContainer(Object)'
108      */

109     public void testGetListContainer() throws Exception JavaDoc{
110         String JavaDoc 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     /*
120      * Test method for 'org.apache.activemq.kaha.Store.deleteListContainer(Object)'
121      */

122     public void testDeleteListContainer()throws Exception JavaDoc{
123         String JavaDoc 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 JavaDoc{
135         Map JavaDoc testMap = new HashMap JavaDoc();
136         int count = 1000;
137         for (int i =0; i<count; i++){
138             String JavaDoc key = "key:"+i;
139             String JavaDoc value = "value:"+i;
140             testMap.put(key, value);
141         }
142         List JavaDoc testList = new ArrayList JavaDoc();
143         for (int i = 0; i < count; i++){
144             testList.add("value:"+i);
145         }
146         String JavaDoc listId = "testList";
147         String JavaDoc mapId1 = "testMap";
148         String JavaDoc 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 JavaDoc i = testMap.keySet().iterator(); i.hasNext();){
170             Object JavaDoc key = i.next();
171             Object JavaDoc value = testMap.get(key);
172             assertTrue(mapContainer1.containsKey(key));
173             assertEquals(value,mapContainer1.get(key));
174         }
175         for (Iterator JavaDoc i = testMap.keySet().iterator(); i.hasNext();){
176             Object JavaDoc key = i.next();
177             Object JavaDoc 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 JavaDoc i = testList.iterator(), j = listContainer.iterator(); i.hasNext();){
183             assertEquals(i.next(),j.next());
184         }
185     }
186     
187     
188     protected Store getStore() throws IOException JavaDoc{
189         return StoreFactory.open(name, "rw");
190     }
191     
192     protected void setUp() throws Exception JavaDoc{
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 JavaDoc{
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