KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > event > MapTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.cache.pojo.event;
9
10 import junit.framework.TestCase;
11 import junit.framework.Test;
12 import junit.framework.TestSuite;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.jboss.cache.pojo.PojoCache;
16 import org.jboss.cache.pojo.PojoCacheFactory;
17 import org.jboss.cache.pojo.PojoCacheListener;
18 import org.jboss.cache.pojo.test.Person;
19 import org.jboss.cache.pojo.test.Address;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.lang.reflect.Field JavaDoc;
24
25 /**
26  *
27  * @author Ben Wang
28  */

29
30 public class MapTest extends TestCase
31 {
32    Log log_ = LogFactory.getLog(MapTest.class);
33    PojoCache cache_;
34    static Throwable JavaDoc ex1_;
35    static boolean pre_;
36    static boolean post_;
37    static int counter_;
38
39    public MapTest(String JavaDoc name)
40    {
41       super(name);
42    }
43
44    protected void setUp() throws Exception JavaDoc
45    {
46       super.setUp();
47       log_.info("setUp() ....");
48       String JavaDoc configFile = "META-INF/local-service.xml";
49       boolean toStart = false;
50       cache_ = PojoCacheFactory.createCache(configFile, toStart);
51       cache_.start();
52
53       reset();
54    }
55
56    private void reset()
57    {
58       MapTest.ex1_ = null;
59       MapTest.pre_ = false;
60       MapTest.post_ = false;
61       MapTest.counter_ = 0;
62
63    }
64
65    protected void tearDown() throws Exception JavaDoc
66    {
67       super.tearDown();
68       cache_.stop();
69    }
70
71 // public void testDummy() {}
72

73    public void testAttachNotification1() throws Exception JavaDoc
74    {
75       log_.info("testAttachNotification1() ....");
76       MapTest.MyListener listener = new MapTest.MyListener();
77       cache_.addListener(listener);
78       Map JavaDoc map = new HashMap JavaDoc();
79       map.put("test1","test1");
80       map.put("test2","test2");
81       cache_.attach("a", map);
82       assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_);
83       assertTrue("pre-attach event is not emitted", MapTest.pre_);
84       assertTrue("post-attach event is not emitted", MapTest.post_);
85       // If not a POJO just a String, we should not emit the event.
86
assertEquals("Total number of event is ", 2, MapTest.counter_);
87
88       map = (Map JavaDoc)cache_.find("a");
89       map.remove("test2");
90       map.put("test3", "test3");
91       assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_);
92       assertTrue("pre-attach event is not emitted", MapTest.pre_);
93       assertTrue("post-attach event is not emitted", MapTest.post_);
94       // If not a POJO just a String, we should not emit the event.
95
assertEquals("Total number of event is ", 2, MapTest.counter_);
96
97       cache_.removeListener(listener);
98    }
99
100    public void testAttachNotification2() throws Exception JavaDoc
101    {
102       log_.info("testAttachNotification2() ....");
103       Person test = new Person();
104       test.setName("Ben");
105       test.setAge(10);
106       HashMap JavaDoc map = new HashMap JavaDoc();
107       map.put("test1", "English");
108       map.put("test2", "Taiwanese");
109       test.setHobbies(map);
110
111       MapTest.MyListener listener = new MapTest.MyListener();
112       cache_.addListener(listener);
113       cache_.attach("a", test);
114       assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_);
115       assertTrue("pre-attach event is not emitted", MapTest.pre_);
116       assertTrue("post-attach event is not emitted", MapTest.post_);
117       assertEquals("Total number of event is ", 4, MapTest.counter_);
118       cache_.removeListener(listener);
119    }
120
121    public void testAttachNotification3() throws Exception JavaDoc
122    {
123       log_.info("testAttachNotification3() ....");
124       MyListener listener = new MyListener();
125       cache_.addListener(listener);
126       Map JavaDoc map = new HashMap JavaDoc();
127       Address addr1 = new Address();
128       addr1.setCity("Taipei");
129
130       Address addr2 = new Address();
131       addr2.setCity("Taipei");
132
133       map.put("1", addr1);
134       map.put("2", addr2);
135       cache_.attach("a", map);
136       assertNull("Exception should be null but " + SetTest.ex1_, SetTest.ex1_);
137       assertTrue("pre-attach event is not emitted", MapTest.pre_);
138       assertTrue("post-attach event is not emitted", MapTest.post_);
139       // If not a POJO just a String, we should not emit the event.
140
assertEquals("Total number of event is ", 6, MapTest.counter_);
141
142       listener.reset();
143       map = (Map JavaDoc)cache_.find("a");
144       map.remove("2");
145
146       Address addr3 = new Address();
147       addr3.setCity("Taipei");
148       map.put("3", addr3);
149
150       assertNull("Exception should be null but " + ListTest.ex1_, MapTest.ex1_);
151       assertTrue("pre-attach event is not emitted", MapTest.pre_);
152       assertTrue("post-attach event is not emitted", MapTest.post_);
153       // If not a POJO just a String, we should not emit the event.
154
assertEquals("Total number of event is ", 4, MapTest.counter_);
155
156       cache_.removeListener(listener);
157    }
158
159    public void testDetachNotification1() throws Exception JavaDoc
160    {
161       log_.info("testDetachNotification1() ....");
162       MapTest.MyListener listener = new MapTest.MyListener();
163       cache_.addListener(listener);
164       HashMap JavaDoc map = new HashMap JavaDoc();
165       map.put("test1", "test1");
166       map.put("test2", "test2");
167       cache_.attach("a", map);
168       assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_);
169       assertTrue("pre-attach event is not emitted", MapTest.pre_);
170       assertTrue("post-attach event is not emitted", MapTest.post_);
171       // If not a POJO just a String, we should not emit the event.
172
assertEquals("Total number of event is ", 2, MapTest.counter_);
173
174       listener.reset();
175       cache_.detach("a");
176       assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_);
177       assertTrue("pre-attach event is not emitted", MapTest.pre_);
178       assertTrue("post-attach event is not emitted", MapTest.post_);
179       // If not a POJO just a String, we should not emit the event.
180
assertEquals("Total number of event is ", 2, MapTest.counter_);
181
182       cache_.removeListener(listener);
183    }
184
185    public void testDetachNotification2() throws Exception JavaDoc
186    {
187       log_.info("testDetachNotification2() ....");
188       Person test = new Person();
189       test.setName("Ben");
190       test.setAge(10);
191       HashMap JavaDoc map = new HashMap JavaDoc();
192       map.put("test1", "English");
193       map.put("test2", "Taiwanese");
194       test.setHobbies(map);
195
196       MapTest.MyListener listener = new MapTest.MyListener();
197       cache_.addListener(listener);
198       cache_.attach("a", test);
199       assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_);
200       assertTrue("pre-attach event is not emitted", MapTest.pre_);
201       assertTrue("post-attach event is not emitted", MapTest.post_);
202       assertEquals("Total number of event is ", 4, MapTest.counter_);
203
204       listener.reset();
205       cache_.detach("a");
206       assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_);
207       assertTrue("pre-attach event is not emitted", MapTest.pre_);
208       assertTrue("post-attach event is not emitted", MapTest.post_);
209       assertEquals("Total number of event is ", 4, MapTest.counter_);
210
211       cache_.removeListener(listener);
212    }
213
214    public void testDetachNotification3() throws Exception JavaDoc
215    {
216       log_.info("testAttachNotification3() ....");
217       MyListener listener = new MyListener();
218       cache_.addListener(listener);
219       Map JavaDoc map = new HashMap JavaDoc();
220       Address addr1 = new Address();
221       addr1.setCity("Taipei");
222
223       Address addr2 = new Address();
224       addr2.setCity("Taipei");
225
226       map.put("1", addr1);
227       map.put("2", addr2);
228       cache_.attach("a", map);
229       assertNull("Exception should be null but " + SetTest.ex1_, SetTest.ex1_);
230       assertTrue("pre-attach event is not emitted", MapTest.pre_);
231       assertTrue("post-attach event is not emitted", MapTest.post_);
232       // If not a POJO just a String, we should not emit the event.
233
assertEquals("Total number of event is ", 6, MapTest.counter_);
234
235       listener.reset();
236       map = (Map JavaDoc)cache_.find("a");
237       map.remove("2");
238
239       Address addr3 = new Address();
240       addr3.setCity("Taipei");
241       map.put("3", addr3);
242
243       assertNull("Exception should be null but " + ListTest.ex1_, MapTest.ex1_);
244       assertTrue("pre-attach event is not emitted", MapTest.pre_);
245       assertTrue("post-attach event is not emitted", MapTest.post_);
246       // If not a POJO just a String, we should not emit the event.
247
assertEquals("Total number of event is ", 4, MapTest.counter_);
248
249       listener.reset();
250       cache_.detach("a");
251
252       assertNull("Exception should be null but " + ListTest.ex1_, MapTest.ex1_);
253       assertTrue("pre-attach event is not emitted", MapTest.pre_);
254       assertTrue("post-attach event is not emitted", MapTest.post_);
255       // If not a POJO just a String, we should not emit the event.
256
assertEquals("Total number of event is ", 6, MapTest.counter_);
257
258       cache_.removeListener(listener);
259    }
260
261    public static Test suite() throws Exception JavaDoc
262    {
263       return new TestSuite(MapTest.class);
264    }
265
266
267    public static void main(String JavaDoc[] args) throws Exception JavaDoc
268    {
269       junit.textui.TestRunner.run(MapTest.suite());
270    }
271
272    public class MyListener implements PojoCacheListener
273    {
274       public MyListener()
275       {
276       }
277
278       public void reset()
279       {
280          MapTest.pre_ = false;
281          MapTest.post_ = false;
282          MapTest.counter_ = 0;
283       }
284
285       public void attach(Object JavaDoc pojo, boolean pre, boolean isLocal)
286       {
287          if(pre)
288          {
289             MapTest.pre_ = true;
290             MapTest.counter_++;
291          } else
292          {
293             MapTest.post_ = true;
294             MapTest.counter_++;
295          }
296       }
297
298       public void detach(Object JavaDoc pojo, boolean pre, boolean isLocal)
299       {
300          if(pre)
301          {
302             MapTest.pre_ = true;
303             MapTest.counter_++;
304          } else
305          {
306             MapTest.post_ = true;
307             MapTest.counter_++;
308          }
309       }
310
311       public void modify(Object JavaDoc pojo, Field JavaDoc field, boolean pre, boolean isLocal)
312       {
313          if(pre)
314          {
315             MapTest.pre_ = true;
316             MapTest.counter_++;
317          } else
318          {
319             MapTest.post_ = true;
320             MapTest.counter_++;
321          }
322       }
323
324       public void passivate(Object JavaDoc pojo, boolean pre)
325       {
326          throw new RuntimeException JavaDoc("passivate event not yet supported.");
327       }
328
329       public void evict(Object JavaDoc pojo, boolean pre)
330       {
331          throw new RuntimeException JavaDoc("evict event not yet supported.");
332       }
333
334       public void activate(Object JavaDoc pojo, boolean pre)
335       {
336          throw new RuntimeException JavaDoc("activate event not yet supported.");
337       }
338    }
339 }
340
Popular Tags