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.config.Configuration.CacheMode; |
10 |
| import org.jboss.cache.factories.UnitTestCacheFactory; |
11 |
| import org.jboss.cache.pojo.test.Address; |
12 |
| import org.jboss.cache.pojo.test.Person; |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| public class ReplicatedObjectGraphTest extends TestCase |
21 |
| { |
22 |
| Log log = LogFactory.getLog(ReplicatedObjectGraphTest.class); |
23 |
| PojoCache cache1; |
24 |
| PojoCache cache2; |
25 |
| |
26 |
8
| public ReplicatedObjectGraphTest(String name)
|
27 |
| { |
28 |
8
| super(name);
|
29 |
| } |
30 |
| |
31 |
8
| protected void setUp() throws Exception
|
32 |
| { |
33 |
8
| super.setUp();
|
34 |
8
| log.info("setUp() ....");
|
35 |
8
| cache1 = createCache("CacheGroup");
|
36 |
8
| cache2 = createCache("CacheGroup");
|
37 |
| } |
38 |
| |
39 |
8
| protected void tearDown() throws Exception
|
40 |
| { |
41 |
8
| super.tearDown();
|
42 |
8
| cache1.getCache().removeNode(Fqn.fromString("/"));
|
43 |
8
| cache1.stop();
|
44 |
8
| cache2.stop();
|
45 |
| } |
46 |
| |
47 |
16
| private PojoCache createCache(String name) throws Exception
|
48 |
| { |
49 |
16
| boolean toStart = false;
|
50 |
16
| PojoCache tree = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
|
51 |
16
| tree.start();
|
52 |
16
| return tree;
|
53 |
| } |
54 |
| |
55 |
| |
56 |
| |
57 |
18
| protected Person createPerson(String name, int age)
|
58 |
| { |
59 |
18
| Person p = new Person();
|
60 |
18
| p.setName(name);
|
61 |
18
| p.setAge(age);
|
62 |
18
| return p;
|
63 |
| } |
64 |
| |
65 |
2
| private void stage0() throws Exception
|
66 |
| { |
67 |
2
| cache1.attach("/person/joe", createPerson("Joe Black", 31));
|
68 |
2
| Person joe = (Person) cache1.find("/person/joe");
|
69 |
2
| cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
|
70 |
2
| Person ben = (Person) cache1.find("/person/ben");
|
71 |
| |
72 |
2
| Address addr = new Address();
|
73 |
2
| addr.setStreet("123 Albert Ave.");
|
74 |
2
| addr.setCity("Sunnyvale");
|
75 |
2
| addr.setZip(94087);
|
76 |
2
| cache1.attach("/address", addr);
|
77 |
| |
78 |
| |
79 |
2
| joe.setAddress(addr);
|
80 |
2
| ben.setAddress(addr);
|
81 |
2
| assertEquals("Joe's address should still be valid ", "Sunnyvale", joe.getAddress().getCity());
|
82 |
2
| assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity());
|
83 |
| } |
84 |
| |
85 |
2
| private void stage1() throws Exception
|
86 |
| { |
87 |
2
| cache1.attach("/person/joe", createPerson("Joe Black", 31));
|
88 |
2
| Person joe = (Person) cache1.find("/person/joe");
|
89 |
2
| cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
|
90 |
2
| Person ben = (Person) cache1.find("/person/ben");
|
91 |
| |
92 |
2
| Address addr = new Address();
|
93 |
2
| addr.setStreet("123 Albert Ave.");
|
94 |
2
| addr.setCity("Sunnyvale");
|
95 |
2
| addr.setZip(94087);
|
96 |
| |
97 |
| |
98 |
2
| joe.setAddress(addr);
|
99 |
2
| ben.setAddress(addr);
|
100 |
2
| assertEquals("Joe's address should still be valid ", "Sunnyvale", joe.getAddress().getCity());
|
101 |
2
| assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity());
|
102 |
| } |
103 |
| |
104 |
2
| private void stage2(PojoCache cache) throws Exception
|
105 |
| { |
106 |
| |
107 |
2
| cache.detach("/person/joe");
|
108 |
2
| Person ben = (Person) cache.find("/person/ben");
|
109 |
2
| assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity());
|
110 |
2
| Address addr = ben.getAddress();
|
111 |
2
| addr.setCity("Santa Clara");
|
112 |
2
| assertEquals("Ben's address should be changed ", "Santa Clara", ben.getAddress().getCity());
|
113 |
| } |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
2
| public void testCheckReplInstance() throws Exception
|
121 |
| { |
122 |
2
| log.info("testCheckReplInstance() ...");
|
123 |
2
| stage0();
|
124 |
2
| TestingUtil.sleepThread(100);
|
125 |
2
| Person joe = (Person) cache1.find("/person/joe");
|
126 |
2
| Person ben = (Person) cache1.find("/person/ben");
|
127 |
2
| assertEquals("Ben and Joe's address should be the same ", joe.getAddress().getCity(),
|
128 |
| ben.getAddress().getCity()); |
129 |
| |
130 |
2
| Address joe1 = (Address) cache2.find("/address");
|
131 |
2
| assertEquals("Ben's address should not be changed ", joe.getAddress().getCity(), joe1.getCity());
|
132 |
2
| ben = (Person) cache2.find("/person/ben");
|
133 |
2
| cache2.detach("/person/ben");
|
134 |
2
| Address joe2 = (Address) cache2.find("/address");
|
135 |
2
| assertEquals("Joe's reference should be the same.", joe1, joe2);
|
136 |
| } |
137 |
| |
138 |
2
| public void testRefCountCheckRepl() throws Exception
|
139 |
| { |
140 |
2
| log.info("testRefCountCheckRepl() ...");
|
141 |
2
| stage1();
|
142 |
2
| TestingUtil.sleepThread(100);
|
143 |
2
| Person joe = (Person) cache1.find("/person/joe");
|
144 |
2
| Person ben = (Person) cache1.find("/person/ben");
|
145 |
2
| assertEquals("Ben and Joe's address should be the same ", joe.getAddress().getCity(),
|
146 |
| ben.getAddress().getCity()); |
147 |
2
| TestingUtil.sleepThread(100);
|
148 |
2
| stage2(cache2);
|
149 |
2
| assertEquals("Ben's address should be changed on cache1 as well ", "Santa Clara", ben.getAddress().getCity());
|
150 |
2
| cache2.detach("/person/ben");
|
151 |
| } |
152 |
| |
153 |
| |
154 |
2
| public void testdetach1() throws Exception
|
155 |
| { |
156 |
2
| log.info("testdetach1() ...");
|
157 |
2
| cache1.attach("/person/joe", createPerson("Joe Black", 31));
|
158 |
2
| Person joe = (Person) cache1.find("/person/joe");
|
159 |
2
| cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
|
160 |
2
| Person ben = (Person) cache1.find("/person/ben");
|
161 |
| |
162 |
2
| Address addr = new Address();
|
163 |
2
| addr.setStreet("123 Albert Ave.");
|
164 |
2
| addr.setCity("Sunnyvale");
|
165 |
2
| addr.setZip(94087);
|
166 |
| |
167 |
| |
168 |
2
| log.info("testMultipleReference(): set Joe address");
|
169 |
2
| joe.setAddress(addr);
|
170 |
2
| log.info("testMultipleReference(): set Ben address");
|
171 |
2
| ben.setAddress(addr);
|
172 |
| |
173 |
2
| Address add1 = ((Person) cache2.find("/person/joe")).getAddress();
|
174 |
2
| Address add2 = ((Person) cache2.find("/person/ben")).getAddress();
|
175 |
2
| assertEquals(add1.getCity(), add2.getCity());
|
176 |
2
| addr.setCity("Santa Clara");
|
177 |
2
| assertEquals(add1.getCity(), add2.getCity());
|
178 |
| |
179 |
| |
180 |
2
| cache2.detach("/person/joe");
|
181 |
2
| add2 = ((Person) cache2.find("/person/ben")).getAddress();
|
182 |
| |
183 |
2
| assertEquals("City ", "Santa Clara", add2.getCity());
|
184 |
| } |
185 |
| |
186 |
2
| public void testdetach2() throws Exception
|
187 |
| { |
188 |
2
| log.info("testdetach2() ...");
|
189 |
2
| cache1.attach("/person/joe", createPerson("Joe Black", 31));
|
190 |
2
| Person joe = (Person) cache1.find("/person/joe");
|
191 |
2
| cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
|
192 |
2
| Person ben = (Person) cache1.find("/person/ben");
|
193 |
2
| cache1.attach("/person/john", createPerson("John Daly", 41));
|
194 |
2
| Person john = (Person) cache1.find("/person/john");
|
195 |
| |
196 |
2
| Address addr = new Address();
|
197 |
2
| addr.setStreet("123 Albert Ave.");
|
198 |
2
| addr.setCity("Sunnyvale");
|
199 |
2
| addr.setZip(94087);
|
200 |
| |
201 |
2
| Address addr1 = new Address();
|
202 |
2
| addr1.setStreet("123 Albert Ave.");
|
203 |
2
| addr1.setCity("San Jose");
|
204 |
2
| addr1.setZip(94087);
|
205 |
| |
206 |
| |
207 |
2
| log.info("testMultipleReference(): set Joe address");
|
208 |
2
| joe.setAddress(addr);
|
209 |
2
| log.info("testMultipleReference(): set Ben address");
|
210 |
2
| ben.setAddress(addr);
|
211 |
2
| john.setAddress(addr);
|
212 |
| |
213 |
2
| Address add1 = ((Person) cache2.find("/person/joe")).getAddress();
|
214 |
2
| Address add2 = ((Person) cache2.find("/person/ben")).getAddress();
|
215 |
2
| assertEquals(add1.getCity(), add2.getCity());
|
216 |
2
| addr.setCity("Santa Clara");
|
217 |
2
| assertEquals(add1.getCity(), add2.getCity());
|
218 |
| |
219 |
| |
220 |
2
| joe.setAddress(addr1);
|
221 |
2
| add2 = ((Person) cache2.find("/person/joe")).getAddress();
|
222 |
2
| assertEquals("City ", "San Jose", add2.getCity());
|
223 |
2
| add2 = ((Person) cache2.find("/person/ben")).getAddress();
|
224 |
2
| assertEquals("City ", "Santa Clara", add2.getCity());
|
225 |
2
| add2 = ((Person) cache2.find("/person/john")).getAddress();
|
226 |
2
| assertEquals("City ", "Santa Clara", add2.getCity());
|
227 |
| } |
228 |
| |
229 |
2
| public static Test suite() throws Exception
|
230 |
| { |
231 |
2
| return new TestSuite(ReplicatedObjectGraphTest.class);
|
232 |
| } |
233 |
| |
234 |
0
| public static void main(String[] args) throws Exception
|
235 |
| { |
236 |
0
| junit.textui.TestRunner.run(suite());
|
237 |
| } |
238 |
| |
239 |
| } |
240 |
| |