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.lang.reflect.Field; |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| public class LocalTest extends TestCase |
30 |
| { |
31 |
| Log log_ = LogFactory.getLog(LocalTest.class); |
32 |
| PojoCache cache_; |
33 |
| static Throwable ex1_; |
34 |
| static boolean pre_; |
35 |
| static boolean post_; |
36 |
| static int counter_; |
37 |
| |
38 |
8
| public LocalTest(String name)
|
39 |
| { |
40 |
8
| super(name);
|
41 |
| } |
42 |
| |
43 |
8
| protected void setUp() throws Exception
|
44 |
| { |
45 |
8
| super.setUp();
|
46 |
8
| log_.info("setUp() ....");
|
47 |
8
| String configFile = "META-INF/local-service.xml";
|
48 |
8
| boolean toStart = false;
|
49 |
8
| cache_ = PojoCacheFactory.createCache(configFile, toStart);
|
50 |
8
| cache_.start();
|
51 |
| |
52 |
8
| reset();
|
53 |
| } |
54 |
| |
55 |
12
| private void reset()
|
56 |
| { |
57 |
12
| ex1_ = null;
|
58 |
12
| pre_ = false;
|
59 |
12
| post_ = false;
|
60 |
12
| counter_ = 0;
|
61 |
| |
62 |
| } |
63 |
| |
64 |
8
| protected void tearDown() throws Exception
|
65 |
| { |
66 |
8
| super.tearDown();
|
67 |
8
| cache_.stop();
|
68 |
| } |
69 |
| |
70 |
| |
71 |
| |
72 |
2
| public void testAttachNotification() throws Exception
|
73 |
| { |
74 |
2
| log_.info("testAttachNotification() ....");
|
75 |
2
| Person test = new Person();
|
76 |
2
| test.setName("Ben");
|
77 |
2
| test.setAge(10);
|
78 |
2
| MyListener listener = new MyListener(test);
|
79 |
2
| cache_.addListener(listener);
|
80 |
2
| cache_.attach("/a", test);
|
81 |
2
| assertNull("Exception should be null but " +ex1_, ex1_);
|
82 |
2
| assertTrue("pre-attach event is not emitted", pre_);
|
83 |
2
| assertTrue("post-attach event is not emitted", post_);
|
84 |
2
| assertEquals("Total number of event is ", 2, counter_);
|
85 |
2
| cache_.removeListener(listener);
|
86 |
| } |
87 |
| |
88 |
2
| public void testAttachNotification2() throws Exception
|
89 |
| { |
90 |
2
| log_.info("testAttachNotification2() ....");
|
91 |
2
| Person test = new Person();
|
92 |
2
| test.setName("Ben");
|
93 |
2
| test.setAge(10);
|
94 |
2
| Address addr = new Address();
|
95 |
2
| test.setAddress(addr);
|
96 |
| |
97 |
2
| MyListener listener = new MyListener(test);
|
98 |
2
| cache_.addListener(listener);
|
99 |
2
| cache_.attach("/a", test);
|
100 |
2
| assertNull("Exception should be null but " +ex1_, ex1_);
|
101 |
2
| assertTrue("pre-attach event is not emitted", pre_);
|
102 |
2
| assertTrue("post-attach event is not emitted", post_);
|
103 |
| |
104 |
2
| assertEquals("Total number of event is ", 4, counter_);
|
105 |
2
| cache_.removeListener(listener);
|
106 |
| } |
107 |
| |
108 |
2
| public void testDetachNotification() throws Exception
|
109 |
| { |
110 |
2
| log_.info("testDetachNotification() ....");
|
111 |
2
| Person test = new Person();
|
112 |
2
| test.setName("Ben");
|
113 |
2
| test.setAge(10);
|
114 |
2
| MyListener listener = new MyListener(test);
|
115 |
2
| cache_.addListener(listener);
|
116 |
2
| cache_.attach("/a", test);
|
117 |
2
| assertNull("Exception should be null but " +ex1_, ex1_);
|
118 |
2
| assertTrue("pre-attach event is not emitted", pre_);
|
119 |
2
| assertTrue("post-attach event is not emitted", post_);
|
120 |
2
| reset();
|
121 |
| |
122 |
2
| cache_.detach("/a");
|
123 |
2
| assertNull("Exception should be null but " +ex1_, ex1_);
|
124 |
2
| assertTrue("pre-detach event is not emitted", pre_);
|
125 |
2
| assertTrue("post-detach event is not emitted", post_);
|
126 |
2
| cache_.removeListener(listener);
|
127 |
| } |
128 |
| |
129 |
2
| public void testFieldNotification() throws Exception
|
130 |
| { |
131 |
2
| log_.info("testAttachNotification() ....");
|
132 |
2
| Person test = new Person();
|
133 |
2
| test.setName("Ben");
|
134 |
2
| test.setAge(10);
|
135 |
2
| MyListener listener = new MyListener(test);
|
136 |
2
| cache_.addListener(listener);
|
137 |
2
| cache_.attach("/a", test);
|
138 |
2
| assertNull("Exception should be null but " +ex1_, ex1_);
|
139 |
2
| assertTrue("pre-attach event is not emitted", pre_);
|
140 |
2
| assertTrue("post-attach event is not emitted", post_);
|
141 |
| |
142 |
2
| reset();
|
143 |
| |
144 |
2
| test.setAge(20);
|
145 |
2
| assertNull("Exception should be null but " +ex1_, ex1_);
|
146 |
2
| assertTrue("pre-attach event is not emitted", pre_);
|
147 |
2
| assertTrue("post-attach event is not emitted", post_);
|
148 |
2
| assertEquals("Total number of event is ", 2, counter_);
|
149 |
| |
150 |
2
| cache_.removeListener(listener);
|
151 |
| } |
152 |
| |
153 |
2
| public static Test suite() throws Exception
|
154 |
| { |
155 |
2
| return new TestSuite(LocalTest.class);
|
156 |
| } |
157 |
| |
158 |
| |
159 |
0
| public static void main(String[] args) throws Exception
|
160 |
| { |
161 |
0
| junit.textui.TestRunner.run(LocalTest.suite());
|
162 |
| } |
163 |
| |
164 |
| public class MyListener implements PojoCacheListener |
165 |
| { |
166 |
| Object pojo; |
167 |
| |
168 |
8
| public MyListener(Object pojo)
|
169 |
| { |
170 |
8
| this.pojo = pojo;
|
171 |
| } |
172 |
| |
173 |
20
| public void attach(Object pojo, boolean pre, boolean isLocal)
|
174 |
| { |
175 |
20
| if(pre)
|
176 |
| { |
177 |
10
| pre_ = true;
|
178 |
10
| counter_++;
|
179 |
| } else |
180 |
| { |
181 |
10
| post_ = true;
|
182 |
10
| counter_++;
|
183 |
| } |
184 |
| } |
185 |
| |
186 |
4
| public void detach(Object pojo, boolean pre, boolean isLocal)
|
187 |
| { |
188 |
4
| if(pre)
|
189 |
| { |
190 |
2
| pre_ = true;
|
191 |
2
| counter_++;
|
192 |
| } else |
193 |
| { |
194 |
2
| post_ = true;
|
195 |
2
| counter_++;
|
196 |
| } |
197 |
| } |
198 |
| |
199 |
4
| public void modify(Object pojo, Field field, boolean pre, boolean isLocal)
|
200 |
| { |
201 |
4
| if(pre)
|
202 |
| { |
203 |
2
| pre_ = true;
|
204 |
2
| counter_++;
|
205 |
| } else |
206 |
| { |
207 |
2
| post_ = true;
|
208 |
2
| counter_++;
|
209 |
| } |
210 |
| } |
211 |
| |
212 |
0
| public void passivate(Object pojo, boolean pre)
|
213 |
| { |
214 |
0
| throw new RuntimeException("passivate event not yet supported.");
|
215 |
| } |
216 |
| |
217 |
0
| public void evict(Object pojo, boolean pre)
|
218 |
| { |
219 |
0
| throw new RuntimeException("evict event not yet supported.");
|
220 |
| } |
221 |
| |
222 |
0
| public void activate(Object pojo, boolean pre)
|
223 |
| { |
224 |
0
| throw new RuntimeException("activate event not yet supported.");
|
225 |
| } |
226 |
| } |
227 |
| } |