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; |
14 |
| import java.util.HashMap; |
15 |
| import java.util.HashSet; |
16 |
| import java.util.List; |
17 |
| import java.util.Map; |
18 |
| import java.util.Set; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| public class ObjectGraphTest extends TestCase |
27 |
| { |
28 |
| Log log = LogFactory.getLog(ObjectGraphTest.class); |
29 |
| PojoCache cache_; |
30 |
| |
31 |
18
| public ObjectGraphTest(String name)
|
32 |
| { |
33 |
18
| super(name);
|
34 |
| } |
35 |
| |
36 |
18
| protected void setUp() throws Exception
|
37 |
| { |
38 |
18
| super.setUp();
|
39 |
18
| log.info("setUp() ....");
|
40 |
18
| String configFile = "META-INF/local-service.xml";
|
41 |
18
| boolean toStart = false;
|
42 |
18
| cache_ = PojoCacheFactory.createCache(configFile, toStart);
|
43 |
18
| cache_.start();
|
44 |
| } |
45 |
| |
46 |
18
| protected void tearDown() throws Exception
|
47 |
| { |
48 |
18
| super.tearDown();
|
49 |
18
| cache_.stop();
|
50 |
| } |
51 |
| |
52 |
| |
53 |
| |
54 |
0
| protected Person createPerson(String name, int age)
|
55 |
| { |
56 |
0
| Person p = new Person();
|
57 |
0
| p.setName(name);
|
58 |
0
| p.setAge(age);
|
59 |
0
| return p;
|
60 |
| } |
61 |
| |
62 |
| |
63 |
2
| public void testListWithMultipleSharedObjccts() throws Exception
|
64 |
| { |
65 |
2
| log.info("testListWithMultipleSharedObjects() ....");
|
66 |
2
| List list1 = new ArrayList();
|
67 |
2
| Address addr = new Address();
|
68 |
2
| addr.setCity("San Jose");
|
69 |
2
| addr.setZip(95123);
|
70 |
2
| list1.add(addr);
|
71 |
| |
72 |
2
| List list2 = new ArrayList();
|
73 |
2
| List list3 = new ArrayList();
|
74 |
2
| list2.add(addr);
|
75 |
2
| list3.add(addr);
|
76 |
| |
77 |
2
| cache_.attach("/list1", list1);
|
78 |
2
| cache_.attach("/list2", list2);
|
79 |
2
| cache_.attach("/list3", list3);
|
80 |
| |
81 |
2
| list3 = (List) cache_.find("/list3");
|
82 |
2
| assertTrue("List size should not be 0 ", (list3.size() != 0));
|
83 |
2
| assertEquals("Address ", addr.getZip(), ((Address) list3.get(0)).getZip());
|
84 |
2
| addr.setCity("Sunnyvale");
|
85 |
2
| assertEquals("Address ", addr.getCity(), ((Address) list3.get(0)).getCity());
|
86 |
| |
87 |
2
| cache_.detach("/list1");
|
88 |
2
| cache_.detach("/list2");
|
89 |
2
| cache_.detach("/list3");
|
90 |
| } |
91 |
| |
92 |
2
| public void testRelationshipWithSharedList1() throws Exception
|
93 |
| { |
94 |
2
| log.info("testRelationshipWithList() ....");
|
95 |
2
| List list1 = new ArrayList();
|
96 |
2
| Address addr = new Address();
|
97 |
2
| addr.setCity("San Jose");
|
98 |
2
| addr.setZip(95123);
|
99 |
2
| list1.add(addr);
|
100 |
| |
101 |
2
| cache_.attach("/list", list1);
|
102 |
2
| list1 = (List) cache_.find("/list");
|
103 |
2
| assertEquals("List size should be ", 1, list1.size());
|
104 |
2
| cache_.attach("/alias", list1);
|
105 |
| |
106 |
2
| list1 = (List) cache_.find("/list");
|
107 |
2
| assertEquals("List size should be ", 1, list1.size());
|
108 |
2
| List list2 = (List) cache_.find("/alias");
|
109 |
| |
110 |
2
| assertEquals("List size should be ", 1, list2.size());
|
111 |
2
| assertEquals("Both lists should be equal ", list1, list2);
|
112 |
2
| assertEquals("Both list values should be equal ", list1.get(0), list2.get(0));
|
113 |
| } |
114 |
| |
115 |
2
| public void testRelationshipWithSharedList2() throws Exception
|
116 |
| { |
117 |
2
| log.info("testRelationshipWithList2() ....");
|
118 |
2
| List list1 = new ArrayList();
|
119 |
2
| Address addr = new Address();
|
120 |
2
| addr.setCity("San Jose");
|
121 |
2
| addr.setZip(95123);
|
122 |
2
| list1.add(addr);
|
123 |
| |
124 |
2
| List list2 = new ArrayList();
|
125 |
2
| list2.add(addr);
|
126 |
| |
127 |
2
| cache_.attach("/list1", list1);
|
128 |
2
| cache_.attach("/list2", list2);
|
129 |
2
| Address add2 = (Address) ((List) cache_.find("/list2")).get(0);
|
130 |
2
| Address add1 = (Address) ((List) cache_.find("/list1")).get(0);
|
131 |
2
| assertEquals("Address should be the same", add1, add2);
|
132 |
2
| assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip());
|
133 |
| } |
134 |
| |
135 |
2
| public void testListWithAttachAndDetach() throws Exception
|
136 |
| { |
137 |
2
| log.info("testListWithAttachAndDetach() ....");
|
138 |
2
| List list1 = new ArrayList();
|
139 |
2
| Address addr1 = new Address();
|
140 |
2
| addr1.setCity("San Jose");
|
141 |
2
| addr1.setZip(95123);
|
142 |
| |
143 |
2
| Address addr2 = new Address();
|
144 |
2
| addr2.setCity("Sunnyvale");
|
145 |
2
| addr2.setZip(94086);
|
146 |
2
| list1.add(addr2);
|
147 |
| |
148 |
2
| cache_.attach("/list", list1);
|
149 |
2
| list1 = (List) cache_.find("/list");
|
150 |
2
| assertEquals("List size should be ", 1, list1.size());
|
151 |
2
| cache_.attach("/alias", list1);
|
152 |
| |
153 |
2
| list1 = (List) cache_.find("/list");
|
154 |
2
| assertEquals("List size should be ", 1, list1.size());
|
155 |
2
| List list2 = (List) cache_.find("/alias");
|
156 |
| |
157 |
2
| assertEquals("List size should be ", 1, list2.size());
|
158 |
2
| assertEquals("Both lists should be equal ", list1, list2);
|
159 |
2
| assertEquals("Both list values should be equal ", list1.get(0), list2.get(0));
|
160 |
| } |
161 |
| |
162 |
2
| public void testRelationshipWithSharedSet1() throws Exception
|
163 |
| { |
164 |
2
| log.info("testRelationshipWithSet() ....");
|
165 |
2
| Set set1 = new HashSet();
|
166 |
2
| Address addr = new Address();
|
167 |
2
| addr.setCity("San Jose");
|
168 |
2
| addr.setZip(95123);
|
169 |
2
| set1.add(addr);
|
170 |
| |
171 |
| |
172 |
2
| cache_.attach("/set", set1);
|
173 |
| |
174 |
2
| set1 = (Set) cache_.find("/set");
|
175 |
2
| cache_.attach("/alias", set1);
|
176 |
| |
177 |
2
| set1 = (Set) cache_.find("/set");
|
178 |
2
| Set set2 = (Set) cache_.find("/alias");
|
179 |
2
| assertTrue("Set size should not be 0 ", (set2.size() != 0));
|
180 |
2
| assertEquals("Both sets should be equal ", set1, set2);
|
181 |
2
| Address add1 = (Address) set2.iterator().next();
|
182 |
2
| assertNotNull("Address should not be null", add1);
|
183 |
2
| assertEquals("Zip ", 95123, add1.getZip());
|
184 |
| } |
185 |
| |
186 |
2
| public void testRelationshipWithSharedSet2() throws Exception
|
187 |
| { |
188 |
2
| log.info("testRelationshipWithSet2() ....");
|
189 |
2
| Set set1 = new HashSet();
|
190 |
2
| Address addr = new Address();
|
191 |
2
| addr.setCity("San Jose");
|
192 |
2
| addr.setZip(95123);
|
193 |
2
| set1.add(addr);
|
194 |
| |
195 |
2
| Set set2 = new HashSet();
|
196 |
2
| set2.add(addr);
|
197 |
| |
198 |
2
| cache_.attach("/set1", set1);
|
199 |
2
| cache_.attach("/set2", set2);
|
200 |
| |
201 |
2
| Address add2 = (Address) ((Set) cache_.find("/set2")).iterator().next();
|
202 |
2
| Address add1 = (Address) ((Set) cache_.find("/set1")).iterator().next();
|
203 |
2
| assertEquals("Address should be the same", add1, add2);
|
204 |
2
| assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip());
|
205 |
| } |
206 |
| |
207 |
2
| public void testRelationshipWithSharedMap1() throws Exception
|
208 |
| { |
209 |
2
| log.info("testRelationshipWithMap() ....");
|
210 |
2
| Map map1 = new HashMap();
|
211 |
2
| Address addr = new Address();
|
212 |
2
| addr.setCity("San Jose");
|
213 |
2
| addr.setZip(95123);
|
214 |
2
| map1.put("key1", addr);
|
215 |
| |
216 |
2
| cache_.attach("/map", map1);
|
217 |
2
| cache_.attach("/alias", map1);
|
218 |
| |
219 |
2
| map1 = (Map) cache_.find("/map");
|
220 |
2
| Map map2 = (Map) cache_.find("/alias");
|
221 |
2
| assertTrue("Map size should not be 0 ", (map2.size() != 0));
|
222 |
2
| assertEquals("Both maps should be equal ", map1, map2);
|
223 |
2
| Address add1 = (Address) ((Map.Entry) map2.entrySet().iterator().next()).getValue();
|
224 |
2
| assertNotNull("Address should not be null", add1);
|
225 |
2
| assertEquals("Zip ", 95123, add1.getZip());
|
226 |
| } |
227 |
| |
228 |
2
| public void testRelationshipWithSharedMap2() throws Exception
|
229 |
| { |
230 |
2
| log.info("testRelationshipWithMap2() ....");
|
231 |
2
| Map map1 = new HashMap();
|
232 |
2
| Address addr = new Address();
|
233 |
2
| addr.setCity("San Jose");
|
234 |
2
| addr.setZip(95123);
|
235 |
2
| map1.put("key1", addr);
|
236 |
| |
237 |
2
| Map map2 = new HashMap();
|
238 |
2
| map2.put("key2", addr);
|
239 |
| |
240 |
| |
241 |
2
| cache_.attach("/map", map1);
|
242 |
2
| cache_.attach("/alias", map2);
|
243 |
| |
244 |
2
| map1 = (Map) cache_.find("/map");
|
245 |
2
| map2 = (Map) cache_.find("/alias");
|
246 |
2
| assertTrue("Map size should not be 0 ", (map2.size() != 0));
|
247 |
2
| Address add1 = (Address) ((Map.Entry) map2.entrySet().iterator().next()).getValue();
|
248 |
2
| assertNotNull("Address should not be null", add1);
|
249 |
2
| assertEquals("Zip ", 95123, add1.getZip());
|
250 |
| } |
251 |
| |
252 |
| |
253 |
2
| public void testObjectIdentity() throws Exception
|
254 |
| { |
255 |
| } |
256 |
| |
257 |
2
| public static Test suite() throws Exception
|
258 |
| { |
259 |
2
| return new TestSuite(ObjectGraphTest.class);
|
260 |
| } |
261 |
| |
262 |
0
| public static void main(String[] args) throws Exception
|
263 |
| { |
264 |
0
| junit.textui.TestRunner.run(suite());
|
265 |
| } |
266 |
| |
267 |
| } |
268 |
| |