KickJava   Java API By Example, From Geeks To Geeks.

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


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
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 /**
21  * Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
22  *
23  * @author Ben Wang
24  */

25
26 public class ObjectGraphTest extends TestCase
27 {
28    Log log = LogFactory.getLog(ObjectGraphTest.class);
29    PojoCache cache_;
30
31    public ObjectGraphTest(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       String JavaDoc configFile = "META-INF/local-service.xml";
41       boolean toStart = false;
42       cache_ = PojoCacheFactory.createCache(configFile, toStart);
43       cache_.start();
44    }
45
46    protected void tearDown() throws Exception JavaDoc
47    {
48       super.tearDown();
49       cache_.stop();
50    }
51
52 // public void testDummy() {}
53

54    protected Person createPerson(String JavaDoc name, int age)
55    {
56       Person p = new Person();
57       p.setName(name);
58       p.setAge(age);
59       return p;
60    }
61
62
63    public void testListWithMultipleSharedObjccts() throws Exception JavaDoc
64    {
65       log.info("testListWithMultipleSharedObjects() ....");
66       List JavaDoc list1 = new ArrayList JavaDoc();
67       Address addr = new Address();
68       addr.setCity("San Jose");
69       addr.setZip(95123);
70       list1.add(addr);
71
72       List JavaDoc list2 = new ArrayList JavaDoc();
73       List JavaDoc list3 = new ArrayList JavaDoc();
74       list2.add(addr);
75       list3.add(addr);
76
77       cache_.attach("/list1", list1);
78       cache_.attach("/list2", list2);
79       cache_.attach("/list3", list3);
80
81       list3 = (List JavaDoc) cache_.find("/list3");
82       assertTrue("List size should not be 0 ", (list3.size() != 0));
83       assertEquals("Address ", addr.getZip(), ((Address) list3.get(0)).getZip());
84       addr.setCity("Sunnyvale");
85       assertEquals("Address ", addr.getCity(), ((Address) list3.get(0)).getCity());
86
87       cache_.detach("/list1");
88       cache_.detach("/list2");
89       cache_.detach("/list3");
90    }
91
92    public void testRelationshipWithSharedList1() throws Exception JavaDoc
93    {
94       log.info("testRelationshipWithList() ....");
95       List JavaDoc list1 = new ArrayList JavaDoc();
96       Address addr = new Address();
97       addr.setCity("San Jose");
98       addr.setZip(95123);
99       list1.add(addr);
100
101       cache_.attach("/list", list1);
102       list1 = (List JavaDoc) cache_.find("/list");
103       assertEquals("List size should be ", 1, list1.size());
104       cache_.attach("/alias", list1);
105
106       list1 = (List JavaDoc) cache_.find("/list");
107       assertEquals("List size should be ", 1, list1.size());
108       List JavaDoc list2 = (List JavaDoc) cache_.find("/alias");
109 // System.out.println(cache_.printDetails());
110
assertEquals("List size should be ", 1, list2.size());
111       assertEquals("Both lists should be equal ", list1, list2);
112       assertEquals("Both list values should be equal ", list1.get(0), list2.get(0));
113    }
114
115    public void testRelationshipWithSharedList2() throws Exception JavaDoc
116    {
117       log.info("testRelationshipWithList2() ....");
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       List JavaDoc list2 = new ArrayList JavaDoc();
125       list2.add(addr);
126
127       cache_.attach("/list1", list1);
128       cache_.attach("/list2", list2);
129       Address add2 = (Address) ((List JavaDoc) cache_.find("/list2")).get(0);
130       Address add1 = (Address) ((List JavaDoc) cache_.find("/list1")).get(0);
131       assertEquals("Address should be the same", add1, add2);
132       assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip());
133    }
134
135    public void testListWithAttachAndDetach() throws Exception JavaDoc
136    {
137       log.info("testListWithAttachAndDetach() ....");
138       List JavaDoc list1 = new ArrayList JavaDoc();
139       Address addr1 = new Address();
140       addr1.setCity("San Jose");
141       addr1.setZip(95123);
142
143       Address addr2 = new Address();
144       addr2.setCity("Sunnyvale");
145       addr2.setZip(94086);
146       list1.add(addr2);
147
148       cache_.attach("/list", list1);
149       list1 = (List JavaDoc) cache_.find("/list");
150       assertEquals("List size should be ", 1, list1.size());
151       cache_.attach("/alias", list1);
152
153       list1 = (List JavaDoc) cache_.find("/list");
154       assertEquals("List size should be ", 1, list1.size());
155       List JavaDoc list2 = (List JavaDoc) cache_.find("/alias");
156 // System.out.println(cache_.printDetails());
157
assertEquals("List size should be ", 1, list2.size());
158       assertEquals("Both lists should be equal ", list1, list2);
159       assertEquals("Both list values should be equal ", list1.get(0), list2.get(0));
160    }
161
162    public void testRelationshipWithSharedSet1() throws Exception JavaDoc
163    {
164       log.info("testRelationshipWithSet() ....");
165       Set JavaDoc set1 = new HashSet JavaDoc();
166       Address addr = new Address();
167       addr.setCity("San Jose");
168       addr.setZip(95123);
169       set1.add(addr);
170
171       // Pure set
172
cache_.attach("/set", set1);
173       // We specifically need to use Proxy otherwise it won't work with multiple references
174
set1 = (Set JavaDoc) cache_.find("/set");
175       cache_.attach("/alias", set1);
176
177       set1 = (Set JavaDoc) cache_.find("/set");
178       Set JavaDoc set2 = (Set JavaDoc) cache_.find("/alias");
179       assertTrue("Set size should not be 0 ", (set2.size() != 0));
180       assertEquals("Both sets should be equal ", set1, set2);
181       Address add1 = (Address) set2.iterator().next();
182       assertNotNull("Address should not be null", add1);
183       assertEquals("Zip ", 95123, add1.getZip());
184    }
185
186    public void testRelationshipWithSharedSet2() throws Exception JavaDoc
187    {
188       log.info("testRelationshipWithSet2() ....");
189       Set JavaDoc set1 = new HashSet JavaDoc();
190       Address addr = new Address();
191       addr.setCity("San Jose");
192       addr.setZip(95123);
193       set1.add(addr);
194
195       Set JavaDoc set2 = new HashSet JavaDoc();
196       set2.add(addr);
197
198       cache_.attach("/set1", set1);
199       cache_.attach("/set2", set2);
200
201       Address add2 = (Address) ((Set JavaDoc) cache_.find("/set2")).iterator().next();
202       Address add1 = (Address) ((Set JavaDoc) cache_.find("/set1")).iterator().next();
203       assertEquals("Address should be the same", add1, add2);
204       assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip());
205    }
206
207    public void testRelationshipWithSharedMap1() throws Exception JavaDoc
208    {
209       log.info("testRelationshipWithMap() ....");
210       Map JavaDoc map1 = new HashMap JavaDoc();
211       Address addr = new Address();
212       addr.setCity("San Jose");
213       addr.setZip(95123);
214       map1.put("key1", addr);
215
216       cache_.attach("/map", map1);
217       cache_.attach("/alias", map1);
218
219       map1 = (Map JavaDoc) cache_.find("/map");
220       Map JavaDoc map2 = (Map JavaDoc) cache_.find("/alias");
221       assertTrue("Map size should not be 0 ", (map2.size() != 0));
222       assertEquals("Both maps should be equal ", map1, map2);
223       Address add1 = (Address) ((Map.Entry JavaDoc) map2.entrySet().iterator().next()).getValue();
224       assertNotNull("Address should not be null", add1);
225       assertEquals("Zip ", 95123, add1.getZip());
226    }
227
228    public void testRelationshipWithSharedMap2() throws Exception JavaDoc
229    {
230       log.info("testRelationshipWithMap2() ....");
231       Map JavaDoc map1 = new HashMap JavaDoc();
232       Address addr = new Address();
233       addr.setCity("San Jose");
234       addr.setZip(95123);
235       map1.put("key1", addr);
236
237       Map JavaDoc map2 = new HashMap JavaDoc();
238       map2.put("key2", addr);
239
240       // Pure map
241
cache_.attach("/map", map1);
242       cache_.attach("/alias", map2);
243
244       map1 = (Map JavaDoc) cache_.find("/map");
245       map2 = (Map JavaDoc) cache_.find("/alias");
246       assertTrue("Map size should not be 0 ", (map2.size() != 0));
247       Address add1 = (Address) ((Map.Entry JavaDoc) map2.entrySet().iterator().next()).getValue();
248       assertNotNull("Address should not be null", add1);
249       assertEquals("Zip ", 95123, add1.getZip());
250    }
251
252
253    public void testObjectIdentity() throws Exception JavaDoc
254    {
255    }
256
257    public static Test suite() throws Exception JavaDoc
258    {
259       return new TestSuite(ObjectGraphTest.class);
260    }
261
262    public static void main(String JavaDoc[] args) throws Exception JavaDoc
263    {
264       junit.textui.TestRunner.run(suite());
265    }
266
267 }
268
269
Popular Tags