1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
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; |
22 |
| import java.util.Map; |
23 |
| import java.lang.reflect.Field; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| public class MapTest extends TestCase |
31 |
| { |
32 |
| Log log_ = LogFactory.getLog(MapTest.class); |
33 |
| PojoCache cache_; |
34 |
| static Throwable ex1_; |
35 |
| static boolean pre_; |
36 |
| static boolean post_; |
37 |
| static int counter_; |
38 |
| |
39 |
12
| public MapTest(String name)
|
40 |
| { |
41 |
12
| super(name);
|
42 |
| } |
43 |
| |
44 |
12
| protected void setUp() throws Exception
|
45 |
| { |
46 |
12
| super.setUp();
|
47 |
12
| log_.info("setUp() ....");
|
48 |
12
| String configFile = "META-INF/local-service.xml";
|
49 |
12
| boolean toStart = false;
|
50 |
12
| cache_ = PojoCacheFactory.createCache(configFile, toStart);
|
51 |
12
| cache_.start();
|
52 |
| |
53 |
12
| reset();
|
54 |
| } |
55 |
| |
56 |
12
| private void reset()
|
57 |
| { |
58 |
12
| MapTest.ex1_ = null;
|
59 |
12
| MapTest.pre_ = false;
|
60 |
12
| MapTest.post_ = false;
|
61 |
12
| MapTest.counter_ = 0;
|
62 |
| |
63 |
| } |
64 |
| |
65 |
12
| protected void tearDown() throws Exception
|
66 |
| { |
67 |
12
| super.tearDown();
|
68 |
12
| cache_.stop();
|
69 |
| } |
70 |
| |
71 |
| |
72 |
| |
73 |
2
| public void testAttachNotification1() throws Exception
|
74 |
| { |
75 |
2
| log_.info("testAttachNotification1() ....");
|
76 |
2
| MapTest.MyListener listener = new MapTest.MyListener();
|
77 |
2
| cache_.addListener(listener);
|
78 |
2
| Map map = new HashMap();
|
79 |
2
| map.put("test1","test1");
|
80 |
2
| map.put("test2","test2");
|
81 |
2
| cache_.attach("a", map);
|
82 |
2
| assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_);
|
83 |
2
| assertTrue("pre-attach event is not emitted", MapTest.pre_);
|
84 |
2
| assertTrue("post-attach event is not emitted", MapTest.post_);
|
85 |
| |
86 |
2
| assertEquals("Total number of event is ", 2, MapTest.counter_);
|
87 |
| |
88 |
2
| map = (Map)cache_.find("a");
|
89 |
2
| map.remove("test2");
|
90 |
2
| map.put("test3", "test3");
|
91 |
2
| assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_);
|
92 |
2
| assertTrue("pre-attach event is not emitted", MapTest.pre_);
|
93 |
2
| assertTrue("post-attach event is not emitted", MapTest.post_);
|
94 |
| |
95 |
2
| assertEquals("Total number of event is ", 2, MapTest.counter_);
|
96 |
| |
97 |
2
| cache_.removeListener(listener);
|
98 |
| } |
99 |
| |
100 |
2
| public void testAttachNotification2() throws Exception
|
101 |
| { |
102 |
2
| log_.info("testAttachNotification2() ....");
|
103 |
2
| Person test = new Person();
|
104 |
2
| test.setName("Ben");
|
105 |
2
| test.setAge(10);
|
106 |
2
| HashMap map = new HashMap();
|
107 |
2
| map.put("test1", "English");
|
108 |
2
| map.put("test2", "Taiwanese");
|
109 |
2
| test.setHobbies(map);
|
110 |
| |
111 |
2
| MapTest.MyListener listener = new MapTest.MyListener();
|
112 |
2
| cache_.addListener(listener);
|
113 |
2
| cache_.attach("a", test);
|
114 |
2
| assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_);
|
115 |
2
| assertTrue("pre-attach event is not emitted", MapTest.pre_);
|
116 |
2
| assertTrue("post-attach event is not emitted", MapTest.post_);
|
117 |
2
| assertEquals("Total number of event is ", 4, MapTest.counter_);
|
118 |
2
| cache_.removeListener(listener);
|
119 |
| } |
120 |
| |
121 |
2
| public void testAttachNotification3() throws Exception
|
122 |
| { |
123 |
2
| log_.info("testAttachNotification3() ....");
|
124 |
2
| MyListener listener = new MyListener();
|
125 |
2
| cache_.addListener(listener);
|
126 |
2
| Map map = new HashMap();
|
127 |
2
| Address addr1 = new Address();
|
128 |
2
| addr1.setCity("Taipei");
|
129 |
| |
130 |
2
| Address addr2 = new Address();
|
131 |
2
| addr2.setCity("Taipei");
|
132 |
| |
133 |
2
| map.put("1", addr1);
|
134 |
2
| map.put("2", addr2);
|
135 |
2
| cache_.attach("a", map);
|
136 |
2
| assertNull("Exception should be null but " + SetTest.ex1_, SetTest.ex1_);
|
137 |
2
| assertTrue("pre-attach event is not emitted", MapTest.pre_);
|
138 |
2
| assertTrue("post-attach event is not emitted", MapTest.post_);
|
139 |
| |
140 |
2
| assertEquals("Total number of event is ", 6, MapTest.counter_);
|
141 |
| |
142 |
2
| listener.reset();
|
143 |
2
| map = (Map)cache_.find("a");
|
144 |
2
| map.remove("2");
|
145 |
| |
146 |
2
| Address addr3 = new Address();
|
147 |
2
| addr3.setCity("Taipei");
|
148 |
2
| map.put("3", addr3);
|
149 |
| |
150 |
2
| assertNull("Exception should be null but " + ListTest.ex1_, MapTest.ex1_);
|
151 |
2
| assertTrue("pre-attach event is not emitted", MapTest.pre_);
|
152 |
2
| assertTrue("post-attach event is not emitted", MapTest.post_);
|
153 |
| |
154 |
2
| assertEquals("Total number of event is ", 4, MapTest.counter_);
|
155 |
| |
156 |
2
| cache_.removeListener(listener);
|
157 |
| } |
158 |
| |
159 |
2
| public void testDetachNotification1() throws Exception
|
160 |
| { |
161 |
2
| log_.info("testDetachNotification1() ....");
|
162 |
2
| MapTest.MyListener listener = new MapTest.MyListener();
|
163 |
2
| cache_.addListener(listener);
|
164 |
2
| HashMap map = new HashMap();
|
165 |
2
| map.put("test1", "test1");
|
166 |
2
| map.put("test2", "test2");
|
167 |
2
| cache_.attach("a", map);
|
168 |
2
| assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_);
|
169 |
2
| assertTrue("pre-attach event is not emitted", MapTest.pre_);
|
170 |
2
| assertTrue("post-attach event is not emitted", MapTest.post_);
|
171 |
| |
172 |
2
| assertEquals("Total number of event is ", 2, MapTest.counter_);
|
173 |
| |
174 |
2
| listener.reset();
|
175 |
2
| cache_.detach("a");
|
176 |
2
| assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_);
|
177 |
2
| assertTrue("pre-attach event is not emitted", MapTest.pre_);
|
178 |
2
| assertTrue("post-attach event is not emitted", MapTest.post_);
|
179 |
| |
180 |
2
| assertEquals("Total number of event is ", 2, MapTest.counter_);
|
181 |
| |
182 |
2
| cache_.removeListener(listener);
|
183 |
| } |
184 |
| |
185 |
2
| public void testDetachNotification2() throws Exception
|
186 |
| { |
187 |
2
| log_.info("testDetachNotification2() ....");
|
188 |
2
| Person test = new Person();
|
189 |
2
| test.setName("Ben");
|
190 |
2
| test.setAge(10);
|
191 |
2
| HashMap map = new HashMap();
|
192 |
2
| map.put("test1", "English");
|
193 |
2
| map.put("test2", "Taiwanese");
|
194 |
2
| test.setHobbies(map);
|
195 |
| |
196 |
2
| MapTest.MyListener listener = new MapTest.MyListener();
|
197 |
2
| cache_.addListener(listener);
|
198 |
2
| cache_.attach("a", test);
|
199 |
2
| assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_);
|
200 |
2
| assertTrue("pre-attach event is not emitted", MapTest.pre_);
|
201 |
2
| assertTrue("post-attach event is not emitted", MapTest.post_);
|
202 |
2
| assertEquals("Total number of event is ", 4, MapTest.counter_);
|
203 |
| |
204 |
2
| listener.reset();
|
205 |
2
| cache_.detach("a");
|
206 |
2
| assertNull("Exception should be null but " + MapTest.ex1_, MapTest.ex1_);
|
207 |
2
| assertTrue("pre-attach event is not emitted", MapTest.pre_);
|
208 |
2
| assertTrue("post-attach event is not emitted", MapTest.post_);
|
209 |
2
| assertEquals("Total number of event is ", 4, MapTest.counter_);
|
210 |
| |
211 |
2
| cache_.removeListener(listener);
|
212 |
| } |
213 |
| |
214 |
2
| public void testDetachNotification3() throws Exception
|
215 |
| { |
216 |
2
| log_.info("testAttachNotification3() ....");
|
217 |
2
| MyListener listener = new MyListener();
|
218 |
2
| cache_.addListener(listener);
|
219 |
2
| Map map = new HashMap();
|
220 |
2
| Address addr1 = new Address();
|
221 |
2
| addr1.setCity("Taipei");
|
222 |
| |
223 |
2
| Address addr2 = new Address();
|
224 |
2
| addr2.setCity("Taipei");
|
225 |
| |
226 |
2
| map.put("1", addr1);
|
227 |
2
| map.put("2", addr2);
|
228 |
2
| cache_.attach("a", map);
|
229 |
2
| assertNull("Exception should be null but " + SetTest.ex1_, SetTest.ex1_);
|
230 |
2
| assertTrue("pre-attach event is not emitted", MapTest.pre_);
|
231 |
2
| assertTrue("post-attach event is not emitted", MapTest.post_);
|
232 |
| |
233 |
2
| assertEquals("Total number of event is ", 6, MapTest.counter_);
|
234 |
| |
235 |
2
| listener.reset();
|
236 |
2
| map = (Map)cache_.find("a");
|
237 |
2
| map.remove("2");
|
238 |
| |
239 |
2
| Address addr3 = new Address();
|
240 |
2
| addr3.setCity("Taipei");
|
241 |
2
| map.put("3", addr3);
|
242 |
| |
243 |
2
| assertNull("Exception should be null but " + ListTest.ex1_, MapTest.ex1_);
|
244 |
2
| assertTrue("pre-attach event is not emitted", MapTest.pre_);
|
245 |
2
| assertTrue("post-attach event is not emitted", MapTest.post_);
|
246 |
| |
247 |
2
| assertEquals("Total number of event is ", 4, MapTest.counter_);
|
248 |
| |
249 |
2
| listener.reset();
|
250 |
2
| cache_.detach("a");
|
251 |
| |
252 |
2
| assertNull("Exception should be null but " + ListTest.ex1_, MapTest.ex1_);
|
253 |
2
| assertTrue("pre-attach event is not emitted", MapTest.pre_);
|
254 |
2
| assertTrue("post-attach event is not emitted", MapTest.post_);
|
255 |
| |
256 |
2
| assertEquals("Total number of event is ", 6, MapTest.counter_);
|
257 |
| |
258 |
2
| cache_.removeListener(listener);
|
259 |
| } |
260 |
| |
261 |
2
| public static Test suite() throws Exception
|
262 |
| { |
263 |
2
| return new TestSuite(MapTest.class);
|
264 |
| } |
265 |
| |
266 |
| |
267 |
0
| public static void main(String[] args) throws Exception
|
268 |
| { |
269 |
0
| junit.textui.TestRunner.run(MapTest.suite());
|
270 |
| } |
271 |
| |
272 |
| public class MyListener implements PojoCacheListener |
273 |
| { |
274 |
12
| public MyListener()
|
275 |
| { |
276 |
| } |
277 |
| |
278 |
10
| public void reset()
|
279 |
| { |
280 |
10
| MapTest.pre_ = false;
|
281 |
10
| MapTest.post_ = false;
|
282 |
10
| MapTest.counter_ = 0;
|
283 |
| } |
284 |
| |
285 |
56
| public void attach(Object pojo, boolean pre, boolean isLocal)
|
286 |
| { |
287 |
56
| if(pre)
|
288 |
| { |
289 |
28
| MapTest.pre_ = true;
|
290 |
28
| MapTest.counter_++;
|
291 |
| } else |
292 |
| { |
293 |
28
| MapTest.post_ = true;
|
294 |
28
| MapTest.counter_++;
|
295 |
| } |
296 |
| } |
297 |
| |
298 |
32
| public void detach(Object pojo, boolean pre, boolean isLocal)
|
299 |
| { |
300 |
32
| if(pre)
|
301 |
| { |
302 |
16
| MapTest.pre_ = true;
|
303 |
16
| MapTest.counter_++;
|
304 |
| } else |
305 |
| { |
306 |
16
| MapTest.post_ = true;
|
307 |
16
| MapTest.counter_++;
|
308 |
| } |
309 |
| } |
310 |
| |
311 |
0
| public void modify(Object pojo, Field field, boolean pre, boolean isLocal)
|
312 |
| { |
313 |
0
| if(pre)
|
314 |
| { |
315 |
0
| MapTest.pre_ = true;
|
316 |
0
| MapTest.counter_++;
|
317 |
| } else |
318 |
| { |
319 |
0
| MapTest.post_ = true;
|
320 |
0
| MapTest.counter_++;
|
321 |
| } |
322 |
| } |
323 |
| |
324 |
0
| public void passivate(Object pojo, boolean pre)
|
325 |
| { |
326 |
0
| throw new RuntimeException("passivate event not yet supported.");
|
327 |
| } |
328 |
| |
329 |
0
| public void evict(Object pojo, boolean pre)
|
330 |
| { |
331 |
0
| throw new RuntimeException("evict event not yet supported.");
|
332 |
| } |
333 |
| |
334 |
0
| public void activate(Object pojo, boolean pre)
|
335 |
| { |
336 |
0
| throw new RuntimeException("activate event not yet supported.");
|
337 |
| } |
338 |
| } |
339 |
| } |