KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > CircularGraphTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.cache.pojo;
9
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.jboss.cache.Fqn;
16 import org.jboss.cache.pojo.test.Link;
17 import org.jboss.cache.pojo.test.NodeManager;
18 import org.jboss.cache.pojo.test.Person;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 /**
24  * Test object graph handling in PojoCache, e.g., circular reference, multiple reference, link, etc.
25  *
26  * @author Ben Wang
27  */

28
29 public class CircularGraphTest extends TestCase
30 {
31    Log log = LogFactory.getLog(CircularGraphTest.class);
32    PojoCache cache_;
33
34    public CircularGraphTest(String JavaDoc name)
35    {
36       super(name);
37    }
38
39    protected void setUp() throws Exception JavaDoc
40    {
41       super.setUp();
42       log.info("setUp() ....");
43       String JavaDoc configFile = "META-INF/local-service.xml";
44       boolean toStart = false;
45       cache_ = PojoCacheFactory.createCache(configFile, toStart);
46       cache_.start();
47    }
48
49    protected void tearDown() throws Exception JavaDoc
50    {
51       super.tearDown();
52       cache_.stop();
53    }
54
55    // public void testDummy() {}
56

57    protected Person createPerson(String JavaDoc name, int age)
58    {
59       Person p = new Person();
60       p.setName(name);
61       p.setAge(age);
62       return p;
63    }
64
65    /**
66     * Pure parent child relationsip
67     *
68     * @throws Exception
69     */

70    public void testCircularReference1() throws Exception JavaDoc
71    {
72       log.info("testCircularReference1() ...");
73       Link parent = new Link("parent");
74       Link child = new Link("child");
75       parent.setLink(child);
76       child.setLink(parent);
77       cache_.attach("/link/parent", parent);
78       assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
79       assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
80    }
81
82    /**
83     * Pure parent child relationsip. Attach both parent and child.
84     *
85     * @throws Exception
86     */

87    public void testCircularReference2() throws Exception JavaDoc
88    {
89       log.info("testCircularReference2() ...");
90       Link parent = new Link("parent");
91       Link child = new Link("child");
92       parent.setLink(child);
93       child.setLink(parent);
94       cache_.attach("/link/parent", parent);
95       cache_.attach("/link/child", child);
96       assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
97       assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
98    }
99
100    /**
101     * Pure parent child relationsip with detach
102     *
103     * @throws Exception
104     */

105    public void testCircularReference3() throws Exception JavaDoc
106    {
107       log.info("testCircularReference3() ...");
108       Link parent = new Link("parent");
109       Link child = new Link("child");
110       parent.setLink(child);
111       child.setLink(parent);
112       cache_.attach("/link/parent", parent);
113       cache_.attach("/link/child", child);
114       assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
115       assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
116       Link link = (Link) cache_.detach("/link/parent");
117       cache_.detach("/link/child");
118       assertEquals("child", link.getLink().getName());
119       assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/parent"))));
120       assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/child"))));
121    }
122
123    /**
124     * Pure parent child relationsip with detach reversed
125     *
126     * @throws Exception
127     */

128    public void testCircularReference3a() throws Exception JavaDoc
129    {
130       log.info("testCircularReference3() ...");
131       Link parent = new Link("parent");
132       Link child = new Link("child");
133       parent.setLink(child);
134       child.setLink(parent);
135       cache_.attach("/link/parent", parent);
136       cache_.attach("/link/child", child);
137       assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
138       assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
139       cache_.detach("/link/child");
140       Link link = (Link) cache_.detach("/link/parent");
141       assertEquals("child", link.getLink().getName());
142       assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/parent"))));
143       assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/child"))));
144    }
145
146    /**
147     * cache managed first before put in the relationsip.
148     *
149     * @throws Exception
150     */

151    public void testCircularReference4() throws Exception JavaDoc
152    {
153       log.info("testCircularReference4() ...");
154       Link parent = new Link("parent");
155       Link child = new Link("child");
156       cache_.attach("/link/parent", parent);
157       cache_.attach("/link/child", child);
158       parent.setLink(child);
159       child.setLink(parent);
160       assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
161       assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
162       Link link = (Link) cache_.detach("/link/parent");
163       assertEquals("child", link.getLink().getName());
164       assertNull("Cache should be null ", cache_.getCache().getRoot().getChild(Fqn.fromString("/parent")));
165    }
166
167    /**
168     * Put first before settting the relationship
169     *
170     * @throws Exception
171     */

172    public void testCircularReference5() throws Exception JavaDoc
173    {
174       log.info("testCircularReference5() ...");
175       Link parent = new Link("parent");
176       Link child = new Link("child");
177       cache_.attach("/link/parent", parent);
178       cache_.attach("/link/child", child);
179       parent.setLink(child);
180       child.setLink(parent);
181       assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
182       assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
183       assertEquals("child", ((Link) cache_.find("/link/child")).getName());
184       assertEquals("parent", ((Link) cache_.find("/link/child")).getLink().getName());
185       Link link = (Link) cache_.detach("/link/parent");
186       assertEquals("child", link.getLink().getName());
187       assertNull("Cache should be null ", cache_.getCache().getRoot().getChild(Fqn.fromString("/parent")));
188    }
189
190    /**
191     * Put first before settting the relationship
192     *
193     * @throws Exception
194     */

195    public void testCircularReference6() throws Exception JavaDoc
196    {
197       log.info("testCircularReference6() ...");
198       Link parent = new Link("parent");
199       Link child = new Link("child");
200       cache_.attach("/link/parent", parent);
201       cache_.attach("/link/child", child);
202       parent.setLink(child);
203       child.setLink(parent);
204       assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
205       assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
206       assertEquals("child", ((Link) cache_.find("/link/child")).getName());
207       assertEquals("parent", ((Link) cache_.find("/link/child")).getLink().getName());
208       Link link = (Link) cache_.detach("/link/parent");
209       assertEquals("child", link.getLink().getName());
210       assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/parent"))));
211
212       // re-attach
213
cache_.attach("/link/parent", parent);
214    }
215
216    /**
217     * Setting the circular relationship and also as a shared object.
218     *
219     * @throws Exception
220     */

221    public void testCircularReference7() throws Exception JavaDoc
222    {
223       log.info("testCircularReference7() ...");
224       Link parent = new Link("parent");
225       Link child = new Link("child");
226       parent.setLink(child);
227       child.setLink(parent);
228
229       List JavaDoc<Link> list = new ArrayList JavaDoc<Link>();
230       list.add(parent);
231
232       cache_.attach("/list", list);
233       cache_.attach("/alias", list);
234
235       List JavaDoc<Link> list1 = (List JavaDoc<Link>) cache_.find("/list");
236       List JavaDoc<Link> list2 = (List JavaDoc<Link>) cache_.find("/alias");
237
238       assertEquals("parent", list1.get(0).getName());
239       assertEquals("child", list2.get(0).getLink().getName());
240    }
241
242    /**
243     * @throws Exception
244     */

245    public void testCircularReference8() throws Exception JavaDoc
246    {
247       log.info("testCircularReference8() ...");
248       Link parent = new Link("parent");
249       Link child = new Link("child");
250       parent.setLink(child);
251       child.setLink(parent);
252
253       cache_.attach("parent", parent);
254       parent.setLink(child);// again
255
child.setLink(parent);
256    }
257
258    public void testCircularAndSharedReferences() throws Exception JavaDoc
259    {
260       log.info("testCircularAndSharedReferences() ...");
261       NodeManager pm_ = new NodeManager();
262
263       pm_.setRootNode("root");
264       pm_.addNode("root", "kanto");
265       pm_.addNode("root.kanto", "tokyo");
266       pm_.addNode("root.kanto", "kanagawa");
267
268       assertEquals("kanagawa", pm_.findNode("root.kanto.kanagawa").getNodeRDN());
269       cache_.attach("/propagation", pm_);
270       pm_.addNode("root.kanto.tokyo", "hadanshita");
271       assertEquals("hadanshita", pm_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN());
272
273       List JavaDoc list = pm_.findNode("root").getChildren();
274       assertEquals("Root should have children of ", 1, list.size());
275       pm_.printNodes();
276    }
277
278    public void XtestObjectIdentity() throws Exception JavaDoc
279    {
280    }
281
282    public static Test suite() throws Exception JavaDoc
283    {
284       return new TestSuite(CircularGraphTest.class);
285    }
286
287    public static void main(String JavaDoc[] args) throws Exception JavaDoc
288    {
289       junit.textui.TestRunner.run(CircularGraphTest.suite());
290    }
291
292 }
293
Popular Tags