KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > ReplicatedObjectGraphTest


1 package org.jboss.cache.pojo;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.jboss.cache.Fqn;
9 import org.jboss.cache.pojo.test.Address;
10 import org.jboss.cache.pojo.test.Person;
11
12 /**
13  * Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
14  *
15  * @author Ben Wang
16  */

17
18 public class ReplicatedObjectGraphTest extends TestCase
19 {
20    Log log = LogFactory.getLog(ReplicatedObjectGraphTest.class);
21    PojoCache cache1;
22    PojoCache cache2;
23
24    public ReplicatedObjectGraphTest(String JavaDoc name)
25    {
26       super(name);
27    }
28
29    protected void setUp() throws Exception JavaDoc
30    {
31       super.setUp();
32       log.info("setUp() ....");
33       cache1 = createCache("CacheGroup");
34       cache2 = createCache("CacheGroup");
35    }
36
37    protected void tearDown() throws Exception JavaDoc
38    {
39       super.tearDown();
40       cache1.getCache().removeNode(Fqn.fromString("/"));
41       cache1.stop();
42       cache2.stop();
43    }
44
45    private PojoCache createCache(String JavaDoc name) throws Exception JavaDoc
46    {
47       boolean toStart = false;
48       PojoCache tree = PojoCacheFactory.createCache("META-INF/replSync-service.xml", toStart);
49       tree.start();
50       return tree;
51    }
52
53    // public void testDummy() {}
54

55    protected Person createPerson(String JavaDoc name, int age)
56    {
57       Person p = new Person();
58       p.setName(name);
59       p.setAge(age);
60       return p;
61    }
62
63    private void stage0() throws Exception JavaDoc
64    {
65       cache1.attach("/person/joe", createPerson("Joe Black", 31));
66       Person joe = (Person) cache1.find("/person/joe");
67       cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
68       Person ben = (Person) cache1.find("/person/ben");
69
70       Address addr = new Address();
71       addr.setStreet("123 Albert Ave.");
72       addr.setCity("Sunnyvale");
73       addr.setZip(94087);
74       cache1.attach("/address", addr);
75
76       // They share the sub-object: address
77
joe.setAddress(addr);
78       ben.setAddress(addr);
79       assertEquals("Joe's address should still be valid ", "Sunnyvale", joe.getAddress().getCity());
80       assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity());
81    }
82
83    private void stage1() throws Exception JavaDoc
84    {
85       cache1.attach("/person/joe", createPerson("Joe Black", 31));
86       Person joe = (Person) cache1.find("/person/joe");
87       cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
88       Person ben = (Person) cache1.find("/person/ben");
89
90       Address addr = new Address();
91       addr.setStreet("123 Albert Ave.");
92       addr.setCity("Sunnyvale");
93       addr.setZip(94087);
94
95       // They share the sub-object: address
96
joe.setAddress(addr);
97       ben.setAddress(addr);
98       assertEquals("Joe's address should still be valid ", "Sunnyvale", joe.getAddress().getCity());
99       assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity());
100    }
101
102    private void stage2(PojoCache cache) throws Exception JavaDoc
103    {
104       //
105
cache.detach("/person/joe");
106       Person ben = (Person) cache.find("/person/ben");
107       assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity());
108       Address addr = ben.getAddress();
109       addr.setCity("Santa Clara");
110       assertEquals("Ben's address should be changed ", "Santa Clara", ben.getAddress().getCity());
111    }
112
113    /**
114     * Test whether repeated update on the ref count will change the replicated aop instances
115     *
116     * @throws Exception
117     */

118    public void testCheckReplInstance() throws Exception JavaDoc
119    {
120       log.info("testCheckReplInstance() ...");
121       stage0();
122       TestingUtil.sleepThread(100);
123       Person joe = (Person) cache1.find("/person/joe");
124       Person ben = (Person) cache1.find("/person/ben");
125       assertEquals("Ben and Joe's address should be the same ", joe.getAddress().getCity(),
126               ben.getAddress().getCity());
127
128       Address joe1 = (Address) cache2.find("/address");
129       assertEquals("Ben's address should not be changed ", joe.getAddress().getCity(), joe1.getCity());
130       ben = (Person) cache2.find("/person/ben");
131       cache2.detach("/person/ben");
132       Address joe2 = (Address) cache2.find("/address");
133       assertEquals("Joe's reference should be the same.", joe1, joe2);
134    }
135
136    public void testRefCountCheckRepl() throws Exception JavaDoc
137    {
138       log.info("testRefCountCheckRepl() ...");
139       stage1();
140       TestingUtil.sleepThread(100);
141       Person joe = (Person) cache1.find("/person/joe");
142       Person ben = (Person) cache1.find("/person/ben");
143       assertEquals("Ben and Joe's address should be the same ", joe.getAddress().getCity(),
144               ben.getAddress().getCity());
145       TestingUtil.sleepThread(100);
146       stage2(cache2);
147       assertEquals("Ben's address should be changed on cache1 as well ", "Santa Clara", ben.getAddress().getCity());
148       cache2.detach("/person/ben");
149    }
150
151
152    public void testdetach1() throws Exception JavaDoc
153    {
154       log.info("testdetach1() ...");
155       cache1.attach("/person/joe", createPerson("Joe Black", 31));
156       Person joe = (Person) cache1.find("/person/joe");
157       cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
158       Person ben = (Person) cache1.find("/person/ben");
159
160       Address addr = new Address();
161       addr.setStreet("123 Albert Ave.");
162       addr.setCity("Sunnyvale");
163       addr.setZip(94087);
164
165       // They share the sub-object: address
166
log.info("testMultipleReference(): set Joe address");
167       joe.setAddress(addr);
168       log.info("testMultipleReference(): set Ben address");
169       ben.setAddress(addr);
170
171       Address add1 = ((Person) cache2.find("/person/joe")).getAddress();
172       Address add2 = ((Person) cache2.find("/person/ben")).getAddress();
173       assertEquals(add1.getCity(), add2.getCity());
174       addr.setCity("Santa Clara");
175       assertEquals(add1.getCity(), add2.getCity());
176
177       // Remove pojo joe will relocate the address field to ben's
178
cache2.detach("/person/joe");
179       add2 = ((Person) cache2.find("/person/ben")).getAddress();
180
181       assertEquals("City ", "Santa Clara", add2.getCity());
182    }
183
184    public void testdetach2() throws Exception JavaDoc
185    {
186       log.info("testdetach2() ...");
187       cache1.attach("/person/joe", createPerson("Joe Black", 31));
188       Person joe = (Person) cache1.find("/person/joe");
189       cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
190       Person ben = (Person) cache1.find("/person/ben");
191       cache1.attach("/person/john", createPerson("John Daly", 41));
192       Person john = (Person) cache1.find("/person/john");
193
194       Address addr = new Address();
195       addr.setStreet("123 Albert Ave.");
196       addr.setCity("Sunnyvale");
197       addr.setZip(94087);
198
199       Address addr1 = new Address();
200       addr1.setStreet("123 Albert Ave.");
201       addr1.setCity("San Jose");
202       addr1.setZip(94087);
203
204       // They share the sub-object: address
205
log.info("testMultipleReference(): set Joe address");
206       joe.setAddress(addr);
207       log.info("testMultipleReference(): set Ben address");
208       ben.setAddress(addr);
209       john.setAddress(addr);
210
211       Address add1 = ((Person) cache2.find("/person/joe")).getAddress();
212       Address add2 = ((Person) cache2.find("/person/ben")).getAddress();
213       assertEquals(add1.getCity(), add2.getCity());
214       addr.setCity("Santa Clara");
215       assertEquals(add1.getCity(), add2.getCity());
216
217       // Remove pojo joe will relocate the address field to ben's
218
joe.setAddress(addr1);
219       add2 = ((Person) cache2.find("/person/joe")).getAddress();
220       assertEquals("City ", "San Jose", add2.getCity());
221       add2 = ((Person) cache2.find("/person/ben")).getAddress();
222       assertEquals("City ", "Santa Clara", add2.getCity());
223       add2 = ((Person) cache2.find("/person/john")).getAddress();
224       assertEquals("City ", "Santa Clara", add2.getCity());
225    }
226
227    public static Test suite() throws Exception JavaDoc
228    {
229       return new TestSuite(ReplicatedObjectGraphTest.class);
230    }
231
232    public static void main(String JavaDoc[] args) throws Exception JavaDoc
233    {
234       junit.textui.TestRunner.run(suite());
235    }
236
237 }
238
239
Popular Tags