KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > collection > ReplicatedSyncListTest


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.pojo.PojoCache;
9 import org.jboss.cache.pojo.PojoCacheFactory;
10 import org.jboss.cache.pojo.test.Address;
11 import org.jboss.cache.pojo.test.Person;
12 import org.jboss.cache.Fqn;
13 import org.jboss.aop.proxy.ClassProxy;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.ListIterator JavaDoc;
18
19 /**
20  * Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
21  *
22  * @author Ben Wang
23  */

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

63
64    protected Person createPerson(String JavaDoc name, int age)
65    {
66       Person p = new Person();
67       p.setName(name);
68       p.setAge(age);
69       return p;
70    }
71
72    /**
73     * Test attachment and then detachment and attachment.
74     *
75     * @throws Exception
76     */

77    public void testAttachDetach() throws Exception JavaDoc
78    {
79       log.info("testAttachDetach() ....");
80       List JavaDoc list1 = new ArrayList JavaDoc();
81       Address addr = new Address();
82       addr.setCity("San Jose");
83       addr.setZip(95123);
84       list1.add(addr);
85
86       Address addr2 = new Address();
87       addr2.setCity("Santa Clara");
88       addr2.setZip(95131);
89
90       Address addr3 = new Address();
91       addr3.setCity("Sunnyvale");
92       addr3.setZip(94086);
93
94       // Pure list
95
cache1.attach("/list", list1);
96       list1 = (List JavaDoc) cache1.find("/list");
97       list1.add(addr2);
98       // The return value is the original reference.
99
list1 = (List JavaDoc)cache1.detach("/list");
100       assertEquals("Detached list should still be", 2, list1.size());
101       list1.add(addr3);
102       cache1.attach("/list", list1);
103
104       List JavaDoc list2 = (List JavaDoc) cache2.find("/list");
105       assertTrue("List size should not be 0 ", (list2.size() != 0));
106       assertEquals("Both list values should be equal ", ((Address) list1.get(0)).getZip(),
107               ((Address) list2.get(0)).getZip());
108    }
109
110    /**
111     * Two different keys share same list.
112     *
113     * @throws Exception
114     */

115    public void testRelationshipWithSharedList1() throws Exception JavaDoc
116    {
117       log.info("testRelationshipWithList() ....");
118       List JavaDoc list1 = new ArrayList JavaDoc();
119       Address addr = new Address();
120       addr.setCity("San Jose");
121       addr.setZip(95123);
122       list1.add(addr);
123
124       // Pure list
125
cache1.attach("/list", list1);
126       // We specifically need to use Proxy otherwise it won't work with multiple references
127
list1 = (List JavaDoc) cache1.find("/list");
128       cache1.attach("/alias", list1);
129
130       List JavaDoc list2 = (List JavaDoc) cache1.find("/alias");
131       Address add1 = (Address) list2.get(0);
132       assertNotNull("Address should not be null", add1);
133       assertEquals("Zip ", 95123, add1.getZip());
134
135       list1 = (List JavaDoc) cache2.find("/list");
136       list2 = (List JavaDoc) cache2.find("/alias");
137       assertTrue("List size should not be 0 ", (list2.size() != 0));
138       assertEquals("Both lists should be equal ", list1, list2);
139       assertEquals("Both list values should be equal ", list1.get(0), list2.get(0));
140    }
141
142    /**
143     * Shared object between two list item.
144     *
145     * @throws Exception
146     */

147    public void testRelationshipWithSharedList2() throws Exception JavaDoc
148    {
149       log.info("testRelationshipWithList2() ....");
150       // 2 lists with shared objects
151
List JavaDoc list1 = new ArrayList JavaDoc();
152       Address addr = new Address();
153       addr.setCity("San Jose");
154       addr.setZip(95123);
155       list1.add(addr);
156
157       List JavaDoc list2 = new ArrayList JavaDoc();
158       list2.add(addr);
159
160       cache1.attach("/list1", list1);
161       cache1.attach("/list2", list2);
162       Address add2 = (Address) ((List JavaDoc) cache2.find("/list2")).get(0);
163       Address add1 = (Address) ((List JavaDoc) cache2.find("/list1")).get(0);
164       assertEquals("Address should be the same", add1, add2);
165       assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip());
166    }
167
168    /**
169     * Shared object between regular POJO and List item.
170     *
171     * @throws Exception
172     */

173    public void testRelationshipWithSharedList3() throws Exception JavaDoc
174    {
175       log.info("testRelationshipWithList3() ....");
176       // 2 lists with shared objects
177
List JavaDoc list1 = new ArrayList JavaDoc();
178       Address addr = new Address();
179       addr.setCity("San Jose");
180       addr.setZip(95123);
181       list1.add(addr);
182
183       List JavaDoc list2 = new ArrayList JavaDoc();
184       list2.add(addr);
185
186       cache1.attach("/address", addr);
187       cache1.attach("/list1", list1);
188       Address add1 = (Address) ((List JavaDoc) cache2.find("/list1")).get(0);
189       Address add2 = (Address) cache2.find("/address");
190       assertEquals("Address should be the same", add1, add2);
191       assertEquals("Both shared object should be equal ", 95123, add1.getZip());
192    }
193
194    public void testNullWithSharedList1() throws Exception JavaDoc
195    {
196       log.info("testNullWithSharedList1() ....");
197       List JavaDoc list1 = new ArrayList JavaDoc();
198       list1.add("element 0");
199       list1.add(null); // element 1
200
list1.add("element 2");
201       list1.add(null); // element 3
202

203       // Pure set
204
cache1.attach("/list", list1);
205       // We specifically need to use Proxy otherwise it won't work with multiple references
206
list1 = (List JavaDoc) cache1.find("/list");
207       cache1.attach("/alias", list1);
208
209       List JavaDoc list2 = (List JavaDoc) cache1.find("/alias");
210
211       list1 = (List JavaDoc) cache2.find("/list");
212       list2 = (List JavaDoc) cache2.find("/alias");
213       assertTrue("List size should not be 0 ", (list2.size() != 0));
214       assertEquals("Both listss should be equal ", list1, list2);
215
216       Object JavaDoc a1[] = list1.toArray();
217       Object JavaDoc a2[] = list2.toArray();
218       assertTrue("element 1 is null", (a1[1] == null));
219       assertTrue("element 1 is null", (a2[1] == null));
220       assertTrue("element 3 is null", (a1[3] == null));
221       assertTrue("element 3 is null", (a2[3] == null));
222
223       assertTrue("contains test for null value", list1.contains(null));
224       assertTrue("contains test for null value", list2.contains(null));
225
226       assertTrue("index of null ", list2.indexOf(null) == 1);
227       assertTrue("last index of null ", list2.lastIndexOf(null) == 3);
228
229       list1.set(0, null); // set first element to null
230
assertTrue("set first item to null", list2.get(0) == null);
231       list1.set(0, "element 0");
232       assertTrue("set first item to 'element 0'", list2.get(0).equals("element 0"));
233
234
235       ListIterator JavaDoc listIter = list1.listIterator();
236       assertTrue("listiter has next", listIter.hasNext());
237       assertTrue("listiter 1st element is 'element 0'", listIter.next().equals("element 0"));
238       assertTrue("listiter has next", listIter.hasNext());
239       assertTrue("listiter 2nd element is null", listIter.next() == null);
240       listIter.remove();
241
242       assertTrue("2nd element should be 'element 2'", list2.get(1).equals("element 2"));
243
244    }
245
246    public void testRecursion1() throws Exception JavaDoc
247    {
248       List JavaDoc list = new ArrayList JavaDoc();
249       list.add("1");
250       list.add("2");
251       cache1.attach("list", list);
252       
253       list = (List JavaDoc)cache1.find("list");
254       list.add(list);
255
256       assertEquals("size ", 3, list.size());
257       List JavaDoc l2 = (List JavaDoc)list.get(2);
258       assertTrue("Instance of AopProxy", l2 instanceof ClassProxy);
259 // assertEquals("ClassProxy instance", list, l2);
260
}
261
262    public void testRecursion2() throws Exception JavaDoc
263    {
264       List JavaDoc list = new ArrayList JavaDoc();
265       list.add("1");
266       list.add("2");
267       list.add(list);
268
269       cache1.attach("list", list);
270
271       list = (List JavaDoc)cache1.find("list");
272       list.toString();
273    }
274
275    public static Test suite() throws Exception JavaDoc
276    {
277       return new TestSuite(ReplicatedSyncListTest.class);
278    }
279
280    public static void main(String JavaDoc[] args) throws Exception JavaDoc
281    {
282       junit.textui.TestRunner.run(suite());
283    }
284
285 }
286
287
Popular Tags