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