KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > passivation > LocalTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
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  * Basic PojoCache test case.
25  *
26  * @author Ben Wang
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    public LocalTest(String JavaDoc name)
36    {
37       super(name);
38    }
39
40    protected void setUp() throws Exception JavaDoc
41    {
42       super.setUp();
43       log.info("setUp() ....");
44       String JavaDoc configFile = "META-INF/pojocache-passivation-service.xml";
45       boolean toStart = false;
46       cache_ = PojoCacheFactory.createCache(configFile, toStart);
47       cache_.getCache().getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.BatchModeTransactionManagerLookup");
48
49       listener_ = new MyCacheListener();
50       cache_.getCache().addCacheListener(listener_);
51
52       cache_.start();
53    }
54
55    protected void tearDown() throws Exception JavaDoc
56    {
57       super.tearDown();
58       cache_.getCache().removeNode(Fqn.fromString("/"));
59       cache_.getCache().removeCacheListener(listener_);
60       cache_.stop();
61       Thread.sleep(1000);
62    }
63
64 // public void testDummy() {}
65

66    private Person createPerson(String JavaDoc name, int age)
67    {
68       Person p = new Person();
69       p.setName(name);
70       p.setAge(age);
71       Address add = new Address();
72       add.setZip(95123);
73       add.setCity("San Jose");
74       p.setAddress(add);
75       return p;
76    }
77
78    private void sanityCheck()
79    {
80       if (listener_.getActivationCount() == 0 || listener_.getPassivationCount() == 0)
81       {
82          fail("Sanity checking for passivation failed. Counters: activation - " + listener_.getActivationCount()
83                  + " passivation - " + listener_.getPassivationCount());
84       }
85
86       listener_.reset();
87    }
88
89    public void testSimple() throws Exception JavaDoc
90    {
91       log.info("testSimple() ....");
92       String JavaDoc id = "person";
93       Person joe = createPerson("Joe Black", 20);
94       cache_.attach(id, joe);
95       Thread.sleep(9100); // default is 3 seconds so joe should have been passivated.
96

97 // assertFalse("Node should be evicted ",
98
// cache_.getCache().exists(id));
99
assertFalse("Node should be evicted ",
100               cache_.getCache().getRoot().hasChild(new Fqn(id)));
101
102       assertEquals("age ", 20, joe.getAge());
103       joe.setAge(30);
104       assertEquals("age ", 30, joe.getAge());
105
106       sanityCheck();
107    }
108
109    public void testSetAddress() throws Exception JavaDoc
110    {
111       log.info("testAddress() ....");
112       String JavaDoc id = "person";
113       Person joe = createPerson("Joe Black", 20);
114       cache_.attach(id, joe);
115       Thread.sleep(9100); // default is 3 seconds so joe should have been passivated.
116

117       assertFalse("Node should be evicted ",
118               cache_.getCache().getRoot().hasChild(new Fqn(id)));
119
120       Address addr = new Address();
121       addr.setCity("Taipei");
122       addr.setZip(106);
123
124       joe.setAddress(addr);
125
126       sanityCheck();
127    }
128
129    public void testFindAgain() throws Exception JavaDoc
130    {
131       log.info("testFindAgain() ....");
132       String JavaDoc id = "person";
133       Person joe = createPerson("Joe Black", 20);
134       cache_.attach(id, joe);
135       Thread.sleep(9100); // default is 3 seconds so joe should have been passivated.
136

137 // assertFalse("Node should be evicted ",
138
// cache_.getCache().exists(id));
139
assertFalse("Node should be evicted ",
140               cache_.getCache().getRoot().hasChild(new Fqn(id)));
141
142 // assertFalse("Node should be evicted ",
143
// cache_.getCache().exists(id));
144
assertFalse("Node should be evicted ",
145               cache_.getCache().getRoot().hasChild(new Fqn(id)));
146
147       Person p = (Person) cache_.find(id);
148
149       assertEquals("age ", 20, joe.getAge());
150       joe.setAge(30);
151       assertEquals("age ", 30, joe.getAge());
152
153       assertEquals("age ", p.getAge(), joe.getAge());
154
155       assertFalse("Instance not equal (this is known side effect) ", joe == p);
156
157       sanityCheck();
158    }
159
160    public void testMultipleReference() throws Exception JavaDoc
161    {
162       log.info("testMultipleReference() ...");
163       String JavaDoc id1 = "/person/ben";
164       cache_.attach(id1, createPerson("Ben Hogan", 51));
165       Person joe = (Person) cache_.find(id1);
166       String JavaDoc id = "/person/joe";
167       cache_.attach(id, createPerson("Joe Black", 31));
168       Person ben = (Person) cache_.find(id);
169
170       Address addr = new Address();
171       addr.setStreet("123 Albert Ave.");
172       addr.setCity("Sunnyvale");
173       addr.setZip(94087);
174
175       // They share the sub-object: address
176
log.info("testMultipleReference(): set Joe address");
177       joe.setAddress(addr);
178       log.info("testMultipleReference(): set Ben address");
179       ben.setAddress(addr);
180
181       log.info("testMultipleReference(): verify");
182       Address add1 = (Address) ((Person) cache_.find(id)).getAddress();
183       Address add2 = (Address) ((Person) cache_.find(id)).getAddress();
184       assertEquals(add1.getCity(), add2.getCity());
185       addr.setCity("Santa Clara");
186       assertEquals(add1.getCity(), add2.getCity());
187
188       Thread.sleep(9100); // default is 3 seconds so joe should have been passivated.
189
// assertFalse("Node should be evicted ",
190
// cache_.getCache().exists("/person/joe"));
191
assertFalse("Node should be evicted ",
192               cache_.getCache().getRoot().hasChild(new Fqn(id)));
193
194       assertEquals("City is ", "Santa Clara", add2.getCity());
195
196       Thread.sleep(9100); // default is 3 seconds so joe should have been passivated.
197
assertEquals("City is ", "Santa Clara", joe.getAddress().getCity());
198
199       cache_.detach(id);
200       cache_.detach(id1);
201    }
202
203    public void testRemoveObject1() throws Exception JavaDoc
204    {
205       log.info("testRemoveObject1() ...");
206       cache_.attach("/person/joe", createPerson("Joe Black", 31));
207       Person joe = (Person) cache_.find("/person/joe");
208       cache_.attach("/person/ben", createPerson("Ben Hogan", 51));
209       Person ben = (Person) cache_.find("/person/ben");
210
211       Address addr = new Address();
212       addr.setStreet("123 Albert Ave.");
213       addr.setCity("Sunnyvale");
214       addr.setZip(94087);
215
216       // They share the sub-object: address
217
log.info("testMultipleReference(): set Joe address");
218       joe.setAddress(addr);
219       log.info("testMultipleReference(): set Ben address");
220       ben.setAddress(addr);
221
222       Address add1 = (Address) ((Person) cache_.find("/person/joe")).getAddress();
223       Address add2 = (Address) ((Person) cache_.find("/person/ben")).getAddress();
224       assertEquals(add1.getCity(), add2.getCity());
225       addr.setCity("Santa Clara");
226       assertEquals(add1.getCity(), add2.getCity());
227
228       Thread.sleep(9100);
229       // Remove pojo joe will relocate the address field to ben's
230
cache_.detach("/person/joe");
231       add2 = (Address) ((Person) cache_.find("/person/ben")).getAddress();
232       assertEquals("City ", "Santa Clara", add2.getCity());
233    }
234
235    public void testCircularReference1() throws Exception JavaDoc
236    {
237       log.info("testCircularReference1() ...");
238       Link parent = new Link("parent");
239       Link child = new Link("child");
240       parent.setLink(child);
241       child.setLink(parent);
242       cache_.attach("/link/parent", parent);
243
244       Thread.sleep(9100);
245       assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
246       assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
247    }
248
249
250    public static Test suite() throws Exception JavaDoc
251    {
252       return new TestSuite(org.jboss.cache.pojo.passivation.LocalTest.class);
253    }
254
255
256    public static void main(String JavaDoc[] args) throws Exception JavaDoc
257    {
258       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       public int getActivationCount()
267       {
268          return activation;
269       }
270
271       public int getPassivationCount()
272       {
273          return passivation;
274       }
275
276       public void reset()
277       {
278          activation = 0;
279          passivation = 0;
280       }
281
282       public void nodeActivated(Fqn fqn, boolean pre)
283       {
284          if (!pre)
285          {
286             System.out.println("nodeActivated: " + fqn);
287             activation++;
288          }
289       }
290
291       public void nodePassivated(Fqn fqn, boolean pre)
292       {
293          if (pre)
294          {
295             System.out.println("nodePassivated: " + fqn);
296             passivation++;
297          }
298       }
299    }
300 }
301
Popular Tags