1 |
| package org.jboss.cache.pojo.collection; |
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.aop.proxy.ClassProxy; |
9 |
| import org.jboss.cache.Fqn; |
10 |
| import org.jboss.cache.config.Configuration.CacheMode; |
11 |
| import org.jboss.cache.factories.UnitTestCacheConfigurationFactory; |
12 |
| import org.jboss.cache.pojo.PojoCache; |
13 |
| import org.jboss.cache.pojo.PojoCacheAlreadyDetachedException; |
14 |
| import org.jboss.cache.pojo.PojoCacheFactory; |
15 |
| import org.jboss.cache.pojo.test.Address; |
16 |
| import org.jboss.cache.pojo.test.Person; |
17 |
| |
18 |
| import java.util.ArrayList; |
19 |
| import java.util.List; |
20 |
| import java.util.ListIterator; |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| public class ReplicatedSyncListTest extends TestCase |
29 |
| { |
30 |
| Log log = LogFactory.getLog(ReplicatedSyncListTest.class); |
31 |
| PojoCache cache1; |
32 |
| PojoCache cache2; |
33 |
| |
34 |
8
| public ReplicatedSyncListTest(String name)
|
35 |
| { |
36 |
8
| super(name);
|
37 |
| } |
38 |
| |
39 |
8
| protected void setUp() throws Exception
|
40 |
| { |
41 |
8
| super.setUp();
|
42 |
8
| log.info("setUp() ....");
|
43 |
8
| cache1 = createCache("CacheGroup");
|
44 |
8
| cache2 = createCache("CacheGroup");
|
45 |
| } |
46 |
| |
47 |
8
| protected void tearDown() throws Exception
|
48 |
| { |
49 |
8
| super.tearDown();
|
50 |
8
| cache1.getCache().removeNode(Fqn.fromString("/"));
|
51 |
8
| cache1.stop();
|
52 |
8
| cache2.stop();
|
53 |
| } |
54 |
| |
55 |
16
| private PojoCache createCache(String name) throws Exception
|
56 |
| { |
57 |
16
| boolean toStart = false;
|
58 |
16
| PojoCache cache = PojoCacheFactory.createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
|
59 |
16
| cache.start();
|
60 |
16
| return cache;
|
61 |
| } |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
0
| protected Person createPerson(String name, int age)
|
67 |
| { |
68 |
0
| Person p = new Person();
|
69 |
0
| p.setName(name);
|
70 |
0
| p.setAge(age);
|
71 |
0
| return p;
|
72 |
| } |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
1
| public void testAttachDetach() throws Exception
|
80 |
| { |
81 |
1
| log.info("testAttachDetach() ....");
|
82 |
1
| List list1 = new ArrayList();
|
83 |
1
| Address addr = new Address();
|
84 |
1
| addr.setCity("San Jose");
|
85 |
1
| addr.setZip(95123);
|
86 |
1
| list1.add(addr);
|
87 |
| |
88 |
1
| Address addr2 = new Address();
|
89 |
1
| addr2.setCity("Santa Clara");
|
90 |
1
| addr2.setZip(95131);
|
91 |
| |
92 |
1
| Address addr3 = new Address();
|
93 |
1
| addr3.setCity("Sunnyvale");
|
94 |
1
| addr3.setZip(94086);
|
95 |
| |
96 |
| |
97 |
1
| cache1.attach("/list", list1);
|
98 |
1
| list1 = (List) cache1.find("/list");
|
99 |
1
| list1.add(addr2);
|
100 |
| |
101 |
1
| list1 = (List) cache1.detach("/list");
|
102 |
1
| assertEquals("Detached list should still be", 2, list1.size());
|
103 |
1
| list1.add(addr3);
|
104 |
1
| cache1.attach("/list", list1);
|
105 |
| |
106 |
1
| List list2 = (List) cache2.find("/list");
|
107 |
1
| assertTrue("List size should not be 0 ", (list2.size() != 0));
|
108 |
1
| assertEquals("Both list values should be equal ", ((Address) list1.get(0)).getZip(),
|
109 |
| ((Address) list2.get(0)).getZip()); |
110 |
| } |
111 |
| |
112 |
1
| public void testRemoteDetach() throws Exception
|
113 |
| { |
114 |
1
| List<Object> list = new ArrayList<Object>();
|
115 |
1
| Address addr = new Address();
|
116 |
1
| addr.setCity("San Jose");
|
117 |
1
| addr.setZip(95123);
|
118 |
1
| list.add(addr);
|
119 |
| |
120 |
1
| Address addr2 = new Address();
|
121 |
1
| addr2.setCity("Santa Clara");
|
122 |
1
| addr2.setZip(95131);
|
123 |
| |
124 |
1
| cache1.attach("/list", list);
|
125 |
1
| list = (List<Object>) cache2.find("/list");
|
126 |
1
| cache1.detach("/list");
|
127 |
| |
128 |
1
| boolean pass = false;
|
129 |
1
| try
|
130 |
| { |
131 |
1
| list.add(addr);
|
132 |
| } |
133 |
| catch (PojoCacheAlreadyDetachedException e) |
134 |
| { |
135 |
1
| pass = true;
|
136 |
| } |
137 |
| |
138 |
1
| assertTrue("Expected PojoCacheAlreadyDetachedExcpetion!", pass);
|
139 |
| } |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
1
| public void testRelationshipWithSharedList1() throws Exception
|
147 |
| { |
148 |
1
| log.info("testRelationshipWithList() ....");
|
149 |
1
| List list1 = new ArrayList();
|
150 |
1
| Address addr = new Address();
|
151 |
1
| addr.setCity("San Jose");
|
152 |
1
| addr.setZip(95123);
|
153 |
1
| list1.add(addr);
|
154 |
| |
155 |
| |
156 |
1
| cache1.attach("/list", list1);
|
157 |
| |
158 |
1
| list1 = (List) cache1.find("/list");
|
159 |
1
| cache1.attach("/alias", list1);
|
160 |
| |
161 |
1
| List list2 = (List) cache1.find("/alias");
|
162 |
1
| Address add1 = (Address) list2.get(0);
|
163 |
1
| assertNotNull("Address should not be null", add1);
|
164 |
1
| assertEquals("Zip ", 95123, add1.getZip());
|
165 |
| |
166 |
1
| list1 = (List) cache2.find("/list");
|
167 |
1
| list2 = (List) cache2.find("/alias");
|
168 |
1
| assertTrue("List size should not be 0 ", (list2.size() != 0));
|
169 |
1
| assertEquals("Both lists should be equal ", list1, list2);
|
170 |
1
| assertEquals("Both list values should be equal ", list1.get(0), list2.get(0));
|
171 |
| } |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
1
| public void testRelationshipWithSharedList2() throws Exception
|
179 |
| { |
180 |
1
| log.info("testRelationshipWithList2() ....");
|
181 |
| |
182 |
1
| List list1 = new ArrayList();
|
183 |
1
| Address addr = new Address();
|
184 |
1
| addr.setCity("San Jose");
|
185 |
1
| addr.setZip(95123);
|
186 |
1
| list1.add(addr);
|
187 |
| |
188 |
1
| List list2 = new ArrayList();
|
189 |
1
| list2.add(addr);
|
190 |
| |
191 |
1
| cache1.attach("/list1", list1);
|
192 |
1
| cache1.attach("/list2", list2);
|
193 |
1
| Address add2 = (Address) ((List) cache2.find("/list2")).get(0);
|
194 |
1
| Address add1 = (Address) ((List) cache2.find("/list1")).get(0);
|
195 |
1
| assertEquals("Address should be the same", add1, add2);
|
196 |
1
| assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip());
|
197 |
| } |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
1
| public void testRelationshipWithSharedList3() throws Exception
|
205 |
| { |
206 |
1
| log.info("testRelationshipWithList3() ....");
|
207 |
| |
208 |
1
| List list1 = new ArrayList();
|
209 |
1
| Address addr = new Address();
|
210 |
1
| addr.setCity("San Jose");
|
211 |
1
| addr.setZip(95123);
|
212 |
1
| list1.add(addr);
|
213 |
| |
214 |
1
| List list2 = new ArrayList();
|
215 |
1
| list2.add(addr);
|
216 |
| |
217 |
1
| cache1.attach("/address", addr);
|
218 |
1
| cache1.attach("/list1", list1);
|
219 |
1
| Address add1 = (Address) ((List) cache2.find("/list1")).get(0);
|
220 |
1
| Address add2 = (Address) cache2.find("/address");
|
221 |
1
| assertEquals("Address should be the same", add1, add2);
|
222 |
1
| assertEquals("Both shared object should be equal ", 95123, add1.getZip());
|
223 |
| } |
224 |
| |
225 |
1
| public void testNullWithSharedList1() throws Exception
|
226 |
| { |
227 |
1
| log.info("testNullWithSharedList1() ....");
|
228 |
1
| List list1 = new ArrayList();
|
229 |
1
| list1.add("element 0");
|
230 |
1
| list1.add(null);
|
231 |
1
| list1.add("element 2");
|
232 |
1
| list1.add(null);
|
233 |
| |
234 |
| |
235 |
1
| cache1.attach("/list", list1);
|
236 |
| |
237 |
1
| list1 = (List) cache1.find("/list");
|
238 |
1
| cache1.attach("/alias", list1);
|
239 |
| |
240 |
1
| List list2 = (List) cache1.find("/alias");
|
241 |
| |
242 |
1
| list1 = (List) cache2.find("/list");
|
243 |
1
| list2 = (List) cache2.find("/alias");
|
244 |
1
| assertTrue("List size should not be 0 ", (list2.size() != 0));
|
245 |
1
| assertEquals("Both listss should be equal ", list1, list2);
|
246 |
| |
247 |
1
| Object a1[] = list1.toArray();
|
248 |
1
| Object a2[] = list2.toArray();
|
249 |
1
| assertTrue("element 1 is null", (a1[1] == null));
|
250 |
1
| assertTrue("element 1 is null", (a2[1] == null));
|
251 |
1
| assertTrue("element 3 is null", (a1[3] == null));
|
252 |
1
| assertTrue("element 3 is null", (a2[3] == null));
|
253 |
| |
254 |
1
| assertTrue("contains test for null value", list1.contains(null));
|
255 |
1
| assertTrue("contains test for null value", list2.contains(null));
|
256 |
| |
257 |
1
| assertTrue("index of null ", list2.indexOf(null) == 1);
|
258 |
1
| assertTrue("last index of null ", list2.lastIndexOf(null) == 3);
|
259 |
| |
260 |
1
| list1.set(0, null);
|
261 |
1
| assertTrue("set first item to null", list2.get(0) == null);
|
262 |
1
| list1.set(0, "element 0");
|
263 |
1
| assertTrue("set first item to 'element 0'", list2.get(0).equals("element 0"));
|
264 |
| |
265 |
| |
266 |
1
| ListIterator listIter = list1.listIterator();
|
267 |
1
| assertTrue("listiter has next", listIter.hasNext());
|
268 |
1
| assertTrue("listiter 1st element is 'element 0'", listIter.next().equals("element 0"));
|
269 |
1
| assertTrue("listiter has next", listIter.hasNext());
|
270 |
1
| assertTrue("listiter 2nd element is null", listIter.next() == null);
|
271 |
1
| listIter.remove();
|
272 |
| |
273 |
1
| assertTrue("2nd element should be 'element 2'", list2.get(1).equals("element 2"));
|
274 |
| |
275 |
| } |
276 |
| |
277 |
1
| public void testRecursion1() throws Exception
|
278 |
| { |
279 |
1
| List list = new ArrayList();
|
280 |
1
| list.add("1");
|
281 |
1
| list.add("2");
|
282 |
1
| cache1.attach("list", list);
|
283 |
| |
284 |
1
| list = (List) cache1.find("list");
|
285 |
1
| list.add(list);
|
286 |
| |
287 |
1
| assertEquals("size ", 3, list.size());
|
288 |
1
| List l2 = (List) list.get(2);
|
289 |
1
| assertTrue("Instance of AopProxy", l2 instanceof ClassProxy);
|
290 |
| |
291 |
| } |
292 |
| |
293 |
1
| public void testRecursion2() throws Exception
|
294 |
| { |
295 |
1
| List list = new ArrayList();
|
296 |
1
| list.add("1");
|
297 |
1
| list.add("2");
|
298 |
1
| list.add(list);
|
299 |
| |
300 |
1
| cache1.attach("list", list);
|
301 |
| |
302 |
1
| list = (List) cache1.find("list");
|
303 |
1
| list.toString();
|
304 |
| } |
305 |
| |
306 |
1
| public static Test suite() throws Exception
|
307 |
| { |
308 |
1
| return new TestSuite(ReplicatedSyncListTest.class);
|
309 |
| } |
310 |
| |
311 |
0
| public static void main(String[] args) throws Exception
|
312 |
| { |
313 |
0
| junit.textui.TestRunner.run(suite());
|
314 |
| } |
315 |
| |
316 |
| } |
317 |
| |