KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
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 JavaDoc name = "test";
36     protected Store store;
37     protected MapContainer container;
38     protected Map JavaDoc testMap;
39     protected static final int COUNT = 10;
40     
41     public void testBasicAllocations() throws Exception JavaDoc{
42         String JavaDoc key = "key";
43         Object JavaDoc 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     /*
54      * Test method for 'org.apache.activemq.kaha.MapContainer.size()'
55      */

56     public void testSize() throws Exception JavaDoc {
57         container.putAll(testMap);
58         assertTrue(container.size()==testMap.size());
59     }
60
61     /*
62      * Test method for 'org.apache.activemq.kaha.MapContainer.isEmpty()'
63      */

64     public void testIsEmpty() throws Exception JavaDoc {
65        assertTrue(container.isEmpty());
66     }
67
68     /*
69      * Test method for 'org.apache.activemq.kaha.MapContainer.clear()'
70      */

71     public void testClear() throws Exception JavaDoc {
72         container.putAll(testMap);
73         assertTrue(container.size()==testMap.size());
74         container.clear();
75         assertTrue(container.isEmpty());
76     }
77
78     /*
79      * Test method for 'org.apache.activemq.kaha.MapContainer.containsKey(Object)'
80      */

81     public void testContainsKeyObject() throws Exception JavaDoc {
82         container.putAll(testMap);
83         for (Iterator JavaDoc i = testMap.entrySet().iterator();i.hasNext();){
84             Map.Entry JavaDoc entry = (Entry) i.next();
85             assertTrue(container.containsKey(entry.getKey()));
86         }
87     }
88
89     /*
90      * Test method for 'org.apache.activemq.kaha.MapContainer.get(Object)'
91      */

92     public void testGetObject() throws Exception JavaDoc {
93         container.putAll(testMap);
94         for (Iterator JavaDoc i = testMap.entrySet().iterator();i.hasNext();){
95             Map.Entry JavaDoc entry = (Entry) i.next();
96             Object JavaDoc value = container.get(entry.getKey());
97             assertNotNull(value);
98             assertTrue(value.equals(entry.getValue()));
99         }
100     }
101
102     /*
103      * Test method for 'org.apache.activemq.kaha.MapContainer.containsValue(Object)'
104      */

105     public void testContainsValueObject() throws Exception JavaDoc {
106         container.putAll(testMap);
107         for (Iterator JavaDoc i = testMap.entrySet().iterator();i.hasNext();){
108             Map.Entry JavaDoc entry = (Entry) i.next();
109             assertTrue(container.containsValue(entry.getValue()));
110         }
111     }
112
113     /*
114      * Test method for 'org.apache.activemq.kaha.MapContainer.putAll(Map)'
115      */

116     public void testPutAllMap() throws Exception JavaDoc {
117         container.putAll(testMap);
118         for (Iterator JavaDoc i = testMap.entrySet().iterator();i.hasNext();){
119             Map.Entry JavaDoc entry = (Entry) i.next();
120             assertTrue(container.containsValue(entry.getValue()));
121             assertTrue(container.containsKey(entry.getKey()));
122         }
123     }
124
125     /*
126      * Test method for 'org.apache.activemq.kaha.MapContainer.keySet()'
127      */

128     public void testKeySet() throws Exception JavaDoc {
129         container.putAll(testMap);
130         Set JavaDoc keys = container.keySet();
131         assertTrue(keys.size()==testMap.size());
132         for (Iterator JavaDoc i = testMap.keySet().iterator();i.hasNext();){
133             Object JavaDoc key = i.next();
134             assertTrue(keys.contains(key));
135             keys.remove(key);
136         }
137         assertTrue(container.isEmpty());
138         
139     }
140
141     /*
142      * Test method for 'org.apache.activemq.kaha.MapContainer.values()'
143      */

144     public void testValues() throws Exception JavaDoc {
145         container.putAll(testMap);
146         Collection JavaDoc values = container.values();
147         assertTrue(values.size()==testMap.size());
148         for (Iterator JavaDoc i = testMap.values().iterator();i.hasNext();){
149             Object JavaDoc value = i.next();
150             assertTrue(values.contains(value));
151             assertTrue(values.remove(value));
152         }
153         assertTrue(container.isEmpty());
154     }
155
156     /*
157      * Test method for 'org.apache.activemq.kaha.MapContainer.entrySet()'
158      */

159     public void testEntrySet() throws Exception JavaDoc {
160         container.putAll(testMap);
161         Set JavaDoc entries = container.entrySet();
162         assertTrue(entries.size()==testMap.size());
163         for (Iterator JavaDoc i = entries.iterator();i.hasNext();){
164             Map.Entry JavaDoc entry = (Entry) i.next();
165             assertTrue(testMap.containsKey(entry.getKey()));
166             assertTrue(testMap.containsValue(entry.getValue()));
167             
168         }
169         
170     }
171
172     
173
174     /*
175      * Test method for 'org.apache.activemq.kaha.MapContainer.remove(Object)'
176      */

177     public void testRemoveObject() throws Exception JavaDoc {
178         container.putAll(testMap);
179         for (Iterator JavaDoc i = testMap.keySet().iterator();i.hasNext();){
180             container.remove(i.next());
181         }
182         assertTrue(container.isEmpty());
183     }
184
185     protected Store getStore() throws IOException JavaDoc{
186         return StoreFactory.open(name, "rw");
187     }
188     
189     protected void setUp() throws Exception JavaDoc{
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 JavaDoc();
196         for (int i =0; i < COUNT; i++){
197             String JavaDoc key = "key:" + i;
198             String JavaDoc value = "value:"+i;
199             testMap.put(key, value);
200         }
201         
202     }
203
204     protected void tearDown() throws Exception JavaDoc{
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