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