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