KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > fjank > jcache > collection > MapAdapterTest


1 /* Open Source Java Caching Service
2 * Copyright (C) 2002 Frank Karlstrøm
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * The author can be contacted by email: fjankk@users.sourceforge.net
18 */

19 package org.fjank.jcache.collection;
20
21 import java.util.Collection JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.Map.Entry;
27 import javax.util.jcache.CacheAccessFactory;
28 import javax.util.jcache.CacheMap;
29 import junit.framework.TestCase;
30
31 /**Test class for the MapAdapter
32  * @author Frank Karlstrøm
33  */

34 public class MapAdapterTest extends TestCase {
35
36     
37     private CacheMap mapAccess;
38
39     /**
40      * Constructor for MapAdapterTest.
41      * @param arg0
42      */

43     public MapAdapterTest(String JavaDoc arg0) {
44         super(arg0);
45     }
46
47     public static void main(String JavaDoc[] args) {
48         junit.textui.TestRunner.run(MapAdapterTest.class);
49     }
50
51     final public void testSize() {
52         int size = mapAccess.size();
53         mapAccess.put("sizeTest", "sizeTestValue");
54         assertEquals(size+1, mapAccess.size());
55         mapAccess.remove("sizeTest");
56         assertEquals(size, mapAccess.size());
57     }
58
59     final public void testIsEmpty() {
60         int size = mapAccess.size();
61         if(size==0) {
62             assertEquals(true, mapAccess.isEmpty());
63             mapAccess.put("isEmptyTest", "isEmptyTestValue");
64             assertEquals(false, mapAccess.isEmpty());
65         }else {
66             assertEquals(false, mapAccess.isEmpty());
67             mapAccess.clear();
68             assertEquals(true, mapAccess.isEmpty());
69             
70         }
71     }
72
73     final public void testContainsKey() {
74         mapAccess.clear();
75         mapAccess.put("containsKeyTest", "containsKeyTestValue");
76         assertEquals(true, mapAccess.containsKey("containsKeyTest"));
77         mapAccess.clear();
78         assertEquals(false, mapAccess.containsKey("containsKeyTest"));
79         try {
80             mapAccess.containsKey(null);
81             fail("Should throw NullPointerException, but did not.");
82         }catch(NullPointerException JavaDoc e) {
83             //oko
84
}
85         
86     }
87
88     final public void testContainsValue() {
89         mapAccess.clear();
90         mapAccess.put("containsValueTest", "containsValueTestValue");
91         assertEquals(true, mapAccess.containsValue("containsValueTestValue"));
92         mapAccess.clear();
93         assertEquals(false, mapAccess.containsValue("containsValueTestValue"));
94         
95     }
96
97     final public void testGet() {
98         mapAccess.clear();
99         Object JavaDoc test = mapAccess.get("testGet");
100         assertNull(test);
101         String JavaDoc value = "testGetValue";
102         mapAccess.put("testGet", value);
103         test = mapAccess.get("testGet");
104         //Strings are special, so they have to be compared with equals.
105
assertEquals(value, test);
106         Object JavaDoc obj = new Object JavaDoc();
107         mapAccess.put("testGet", obj);
108         assertSame(obj, mapAccess.get("testGet"));
109         
110         
111     }
112
113     final public void testPut() {
114         Object JavaDoc one = new Object JavaDoc();
115         mapAccess.put("testPut", one);
116         Object JavaDoc two = mapAccess.put("testPut", new Object JavaDoc());
117         assertSame(one, two);
118         
119     }
120
121     final public void testRemove() {
122         Object JavaDoc one = new Object JavaDoc();
123         mapAccess.put("testRemove", one);
124         assertTrue(mapAccess.containsKey("testRemove"));
125         mapAccess.remove("testRemove");
126         assertFalse(mapAccess.containsKey("testRemove"));
127         mapAccess.remove("tut");
128         
129     }
130     final public void testPutString() {
131         Object JavaDoc one = new Object JavaDoc();
132         String JavaDoc group = "group";
133         mapAccess.put("testPutGroup", group, one);
134     }
135     final public void testRemoveString() {
136         Object JavaDoc one = "TestObj";
137         String JavaDoc group = "group";
138         mapAccess.put("testRemoveGroup", group, one);
139         Object JavaDoc removed = mapAccess.remove("testRemoveGroup", group);
140         assertEquals(one, removed);
141     }
142     final public void testPutAll() {
143         Integer JavaDoc one = new Integer JavaDoc(1);
144         Integer JavaDoc two = new Integer JavaDoc(2);
145         Integer JavaDoc three = new Integer JavaDoc(3);
146         Map JavaDoc map = new HashMap JavaDoc();
147         map.put("1", one);
148         map.put("2", two);
149         map.put("3", three);
150         mapAccess.clear();
151         mapAccess.putAll(map);
152         assertSame(one, mapAccess.get("1"));
153         assertSame(two, mapAccess.get("2"));
154         assertSame(three, mapAccess.get("3"));
155     }
156
157     final public void testClear() {
158         mapAccess.put("1", "1");
159         assertFalse(mapAccess.isEmpty());
160         mapAccess.clear();
161         assertTrue(mapAccess.isEmpty());
162     }
163
164     final public void testKeySet() {
165         mapAccess.clear();
166         Integer JavaDoc one = new Integer JavaDoc(1);
167         Integer JavaDoc two = new Integer JavaDoc(2);
168         Integer JavaDoc three = new Integer JavaDoc(3);
169         mapAccess.put("1", one);
170         mapAccess.put("2", two);
171         mapAccess.put("3", three);
172         Set JavaDoc keySet = mapAccess.keySet();
173         assertTrue(keySet.contains("1"));
174         assertTrue(keySet.contains("2"));
175         assertTrue(keySet.contains("3"));
176         assertFalse(keySet.contains(new Object JavaDoc()));
177     }
178
179     final public void testValues() {
180         mapAccess.clear();
181         Integer JavaDoc one = new Integer JavaDoc(1);
182         Integer JavaDoc two = new Integer JavaDoc(2);
183         Integer JavaDoc three = new Integer JavaDoc(3);
184         mapAccess.put("1", one);
185         mapAccess.put("2", two);
186         mapAccess.put("3", three);
187         Collection JavaDoc coll = mapAccess.values();
188         assertTrue(coll.contains(one));
189         assertTrue(coll.contains(two));
190         assertTrue(coll.contains(three));
191     }
192
193     final public void testEntrySet() {
194         mapAccess.clear();
195         Integer JavaDoc one = new Integer JavaDoc(1);
196         Integer JavaDoc two = new Integer JavaDoc(2);
197         Integer JavaDoc three = new Integer JavaDoc(3);
198         mapAccess.put("1", one);
199         mapAccess.put("2", two);
200         mapAccess.put("3", three);
201         Set JavaDoc set = mapAccess.entrySet();
202         Iterator JavaDoc iter = set.iterator();
203         Map.Entry JavaDoc entrOne=null, entrTwo=null,entrThree=null;
204         while(iter.hasNext()) {
205             Map.Entry JavaDoc entr = (Entry) iter.next();
206             if(entr.getKey().equals("1")) {
207                 entrOne=entr;
208             }else if(entr.getKey().equals("2")) {
209                 entrTwo=entr;
210                 set.remove(entr);
211             }else if(entr.getKey().equals("3")) {
212                 entrThree=entr;
213             }
214         }
215         assertTrue(set.contains(entrOne));
216         assertFalse(set.contains(entrTwo));
217         assertTrue(set.contains(entrThree));
218         assertTrue(mapAccess.containsKey(entrOne.getKey()));
219         assertFalse(mapAccess.containsKey(entrTwo.getKey()));
220         assertTrue(mapAccess.containsKey(entrThree.getKey()));
221     }
222
223     /**Saves the Map implementation as an instance variable to runm the
224      * tests with.
225      * @see junit.framework.TestCase#setUp()
226      */

227     protected void setUp() throws Exception JavaDoc {
228         super.setUp();
229         this.mapAccess = CacheAccessFactory.getInstance().getMapAccess();
230         
231         
232     }
233
234 }
235
Popular Tags