KickJava   Java API By Example, From Geeks To Geeks.

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


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.Set JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.lang.reflect.Field JavaDoc;
24
25 /**
26  *
27  * @author Ben Wang
28  */

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