1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| package org.jboss.cache.pojo.passivation; |
24 |
| |
25 |
| import junit.framework.Test; |
26 |
| import junit.framework.TestCase; |
27 |
| import junit.framework.TestSuite; |
28 |
| import org.apache.commons.logging.Log; |
29 |
| import org.apache.commons.logging.LogFactory; |
30 |
| import org.jboss.cache.Fqn; |
31 |
| import org.jboss.cache.notifications.annotation.CacheListener; |
32 |
| import org.jboss.cache.notifications.annotation.NodeActivated; |
33 |
| import org.jboss.cache.notifications.annotation.NodeCreated; |
34 |
| import org.jboss.cache.notifications.annotation.NodeEvicted; |
35 |
| import org.jboss.cache.notifications.annotation.NodeModified; |
36 |
| import org.jboss.cache.notifications.annotation.NodePassivated; |
37 |
| import org.jboss.cache.notifications.event.Event; |
38 |
| import org.jboss.cache.pojo.PojoCache; |
39 |
| import org.jboss.cache.pojo.PojoCacheFactory; |
40 |
| import org.jboss.cache.pojo.test.Address; |
41 |
| import org.jboss.cache.pojo.test.Link; |
42 |
| import org.jboss.cache.pojo.test.Person; |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| public class ReplicatedTest extends TestCase |
51 |
| { |
52 |
| Log log = LogFactory.getLog(ReplicatedTest.class); |
53 |
| PojoCache cache_, cache1_; |
54 |
| MyCacheListener listener_; |
55 |
| |
56 |
5
| public ReplicatedTest(String name)
|
57 |
| { |
58 |
5
| super(name);
|
59 |
| } |
60 |
| |
61 |
5
| protected void setUp() throws Exception
|
62 |
| { |
63 |
5
| super.setUp();
|
64 |
5
| log.info("setUp() ....");
|
65 |
5
| String configFile = "META-INF/pojocache-passivation-service.xml";
|
66 |
5
| boolean toStart = false;
|
67 |
5
| cache_ = PojoCacheFactory.createCache(configFile, toStart);
|
68 |
5
| cache_.getCache().getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.BatchModeTransactionManagerLookup");
|
69 |
| |
70 |
5
| cache_.start();
|
71 |
| |
72 |
5
| cache1_ = PojoCacheFactory.createCache("META-INF/pojocache-passivation-service2.xml", toStart);
|
73 |
5
| cache1_.getCache().getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.BatchModeTransactionManagerLookup");
|
74 |
| |
75 |
5
| listener_ = new MyCacheListener();
|
76 |
5
| cache1_.getCache().addCacheListener(listener_);
|
77 |
| |
78 |
5
| cache1_.start();
|
79 |
| } |
80 |
| |
81 |
5
| protected void tearDown() throws Exception
|
82 |
| { |
83 |
5
| super.tearDown();
|
84 |
5
| cache_.getCache().removeNode(Fqn.fromString("/"));
|
85 |
5
| cache_.stop();
|
86 |
5
| cache1_.stop();
|
87 |
| } |
88 |
| |
89 |
| |
90 |
| |
91 |
6
| private Person createPerson(String name, int age)
|
92 |
| { |
93 |
6
| Person p = new Person();
|
94 |
6
| p.setName(name);
|
95 |
6
| p.setAge(age);
|
96 |
6
| Address add = new Address();
|
97 |
6
| add.setZip(95123);
|
98 |
6
| add.setCity("San Jose");
|
99 |
6
| p.setAddress(add);
|
100 |
6
| return p;
|
101 |
| } |
102 |
| |
103 |
2
| private void sanityCheck() throws InterruptedException
|
104 |
| { |
105 |
2
| Thread.sleep(100);
|
106 |
2
| if (listener_.getActivationCount() == 0 || listener_.getPassivationCount() == 0)
|
107 |
| { |
108 |
0
| fail("Sanity checking for passivation failed. Counters: activation - " + listener_.getActivationCount()
|
109 |
| + " passivation - " + listener_.getPassivationCount()); |
110 |
| } |
111 |
| |
112 |
2
| listener_.reset();
|
113 |
| } |
114 |
| |
115 |
1
| public void testFindAfterPassivation() throws Exception
|
116 |
| { |
117 |
1
| log.info("testFindAfterPassivation() ....");
|
118 |
1
| String id = "person";
|
119 |
1
| Person joe = createPerson("Joe Black", 20);
|
120 |
1
| cache_.attach(id, joe);
|
121 |
1
| Thread.sleep(9100);
|
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
1
| Person p = (Person) cache1_.find(id);
|
129 |
1
| assertNotNull("Person on remote node ", p);
|
130 |
| |
131 |
1
| sanityCheck();
|
132 |
| } |
133 |
| |
134 |
1
| public void testRemoteSetAfterPassivation() throws Exception
|
135 |
| { |
136 |
1
| log.info("testFindAfterPassivation() ....");
|
137 |
1
| String id = "person";
|
138 |
1
| Person joe = createPerson("Joe Black", 20);
|
139 |
1
| cache_.attach(id, joe);
|
140 |
| |
141 |
1
| Person p = (Person) cache1_.find(id);
|
142 |
1
| assertNotNull("Person on remote node ", p);
|
143 |
| |
144 |
1
| Thread.sleep(9100);
|
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
1
| Address addr = new Address();
|
151 |
1
| addr.setCity("Taipei");
|
152 |
1
| addr.setZip(106);
|
153 |
| |
154 |
1
| p.setAddress(addr);
|
155 |
| |
156 |
1
| sanityCheck();
|
157 |
| } |
158 |
| |
159 |
1
| public void testMultipleReference() throws Exception
|
160 |
| { |
161 |
1
| log.info("testMultipleReference() ...");
|
162 |
1
| String id1 = "/person/ben";
|
163 |
1
| cache_.attach(id1, createPerson("Ben Hogan", 51));
|
164 |
1
| Person joe = (Person) cache_.find(id1);
|
165 |
1
| String id = "/person/joe";
|
166 |
1
| cache_.attach(id, createPerson("Joe Black", 31));
|
167 |
1
| Person ben = (Person) cache_.find(id);
|
168 |
| |
169 |
1
| Address addr = new Address();
|
170 |
1
| addr.setStreet("123 Albert Ave.");
|
171 |
1
| addr.setCity("Sunnyvale");
|
172 |
1
| addr.setZip(94087);
|
173 |
| |
174 |
| |
175 |
1
| log.info("testMultipleReference(): set Joe address");
|
176 |
1
| joe.setAddress(addr);
|
177 |
1
| log.info("testMultipleReference(): set Ben address");
|
178 |
1
| ben.setAddress(addr);
|
179 |
| |
180 |
1
| log.info("testMultipleReference(): verify");
|
181 |
1
| Address add1 = (Address) ((Person) cache1_.find(id)).getAddress();
|
182 |
1
| Address add2 = (Address) ((Person) cache1_.find(id)).getAddress();
|
183 |
1
| assertEquals(add1.getCity(), add2.getCity());
|
184 |
1
| addr.setCity("Santa Clara");
|
185 |
1
| assertEquals(add1.getCity(), add2.getCity());
|
186 |
| |
187 |
1
| Thread.sleep(9100);
|
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
1
| assertEquals("City is ", "Santa Clara", add2.getCity());
|
194 |
| |
195 |
1
| Thread.sleep(9100);
|
196 |
1
| assertEquals("City is ", "Santa Clara", joe.getAddress().getCity());
|
197 |
| |
198 |
1
| cache_.detach(id);
|
199 |
1
| cache_.detach(id1);
|
200 |
| } |
201 |
| |
202 |
1
| public void testRemoveObject1() throws Exception
|
203 |
| { |
204 |
1
| log.info("testRemoveObject1() ...");
|
205 |
1
| cache_.attach("/person/joe", createPerson("Joe Black", 31));
|
206 |
1
| Person joe = (Person) cache_.find("/person/joe");
|
207 |
1
| cache_.attach("/person/ben", createPerson("Ben Hogan", 51));
|
208 |
1
| Person ben = (Person) cache_.find("/person/ben");
|
209 |
| |
210 |
1
| Address addr = new Address();
|
211 |
1
| addr.setStreet("123 Albert Ave.");
|
212 |
1
| addr.setCity("Sunnyvale");
|
213 |
1
| addr.setZip(94087);
|
214 |
| |
215 |
| |
216 |
1
| log.info("testMultipleReference(): set Joe address");
|
217 |
1
| joe.setAddress(addr);
|
218 |
1
| log.info("testMultipleReference(): set Ben address");
|
219 |
1
| ben.setAddress(addr);
|
220 |
| |
221 |
1
| Address add1 = (Address) ((Person) cache1_.find("/person/joe")).getAddress();
|
222 |
1
| Address add2 = (Address) ((Person) cache1_.find("/person/ben")).getAddress();
|
223 |
1
| assertEquals(add1.getCity(), add2.getCity());
|
224 |
1
| addr.setCity("Santa Clara");
|
225 |
1
| assertEquals(add1.getCity(), add2.getCity());
|
226 |
| |
227 |
1
| Thread.sleep(9100);
|
228 |
| |
229 |
1
| System.out.println("=== DETACH====");
|
230 |
1
| cache_.detach("/person/joe");
|
231 |
1
| Thread.sleep(3000);
|
232 |
1
| System.out.println("=== FIND === ");
|
233 |
1
| add2 = (Address) ((Person) cache1_.find("/person/ben")).getAddress();
|
234 |
| |
235 |
| } |
236 |
| |
237 |
1
| public void testCircularReference1() throws Exception
|
238 |
| { |
239 |
1
| log.info("testCircularReference1() ...");
|
240 |
1
| Link parent = new Link("parent");
|
241 |
1
| Link child = new Link("child");
|
242 |
1
| parent.setLink(child);
|
243 |
1
| child.setLink(parent);
|
244 |
1
| cache_.attach("/link/parent", parent);
|
245 |
| |
246 |
1
| Thread.sleep(9100);
|
247 |
1
| assertEquals("parent", ((Link) cache1_.find("/link/parent")).getName());
|
248 |
1
| assertEquals("child", ((Link) cache1_.find("/link/parent")).getLink().getName());
|
249 |
| } |
250 |
| |
251 |
| |
252 |
1
| public static Test suite() throws Exception
|
253 |
| { |
254 |
1
| return new TestSuite(ReplicatedTest.class);
|
255 |
| } |
256 |
| |
257 |
| |
258 |
0
| public static void main(String[] args) throws Exception
|
259 |
| { |
260 |
0
| junit.textui.TestRunner.run(suite());
|
261 |
| } |
262 |
| |
263 |
| @CacheListener |
264 |
| public class MyCacheListener |
265 |
| { |
266 |
| int activation = 0; |
267 |
| int passivation = 0; |
268 |
| |
269 |
2
| public int getActivationCount()
|
270 |
| { |
271 |
2
| return activation;
|
272 |
| } |
273 |
| |
274 |
2
| public int getPassivationCount()
|
275 |
| { |
276 |
2
| return passivation;
|
277 |
| } |
278 |
| |
279 |
2
| public void reset()
|
280 |
| { |
281 |
2
| activation = 0;
|
282 |
2
| passivation = 0;
|
283 |
| } |
284 |
| |
285 |
| |
286 |
778
| @NodeCreated
|
287 |
| @NodeEvicted |
288 |
| @NodeModified |
289 |
| public void nodeAccessed(Event e) |
290 |
| { |
291 |
778
| System.out.println("Event: " + e);
|
292 |
| } |
293 |
| |
294 |
83
| @NodeActivated
|
295 |
| public void nodeActivated(Event e) |
296 |
| { |
297 |
83
| nodeAccessed(e);
|
298 |
83
| if (!e.isPre())
|
299 |
| { |
300 |
30
| activation++;
|
301 |
| } |
302 |
| } |
303 |
| |
304 |
78
| @NodePassivated
|
305 |
| public void nodePassivated(Event e) |
306 |
| { |
307 |
78
| nodeAccessed(e);
|
308 |
78
| if (e.isPre())
|
309 |
| { |
310 |
39
| passivation++;
|
311 |
| } |
312 |
| } |
313 |
| } |
314 |
| } |