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