KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

66    protected Person createPerson(String JavaDoc name, int age)
67    {
68       Person p = new Person();
69       p.setName(name);
70       p.setAge(age);
71       return p;
72    }
73
74    public void testCircularReference1() throws Exception JavaDoc
75    {
76 // try {Thread.sleep(10000); } catch (Exception e) {};
77
log.info("testCircularReference1() ...");
78       Link parent = new Link("parent");
79       Link child = new Link("child");
80       parent.setLink(child);
81       child.setLink(parent);
82       cache1.attach("/link/parent", parent);
83       TestingUtil.sleepThread(100);
84       assertEquals("parent", ((Link) cache1.find("/link/parent")).getName());
85       assertEquals("child", ((Link) cache1.find("/link/parent")).getLink().getName());
86       assertEquals("parent", ((Link) cache2.find("/link/parent")).getName());
87       assertEquals("child", ((Link) cache2.find("/link/parent")).getLink().getName());
88       ((Link) cache2.find("/link/parent")).setLink(null);
89       assertNull("Child should be null", ((Link) cache2.find("/link/parent")).getLink());
90       Link link = (Link) cache1.detach("/link/parent");
91       assertNotNull("Link should not be null ", link);
92       System.out.println("Link: " + link);
93    }
94
95    public void testCircularReference2() throws Exception JavaDoc
96    {
97 // try {Thread.sleep(10000); } catch (Exception e) {};
98
log.info("testCircularReference2() ...");
99       Link parent = new Link("parent");
100       Link child = new Link("child");
101       cache1.attach("/link/parent", parent);
102       parent.setLink(child);
103       child.setLink(parent);
104       assertEquals("parent", ((Link) cache1.find("/link/parent")).getName());
105       assertEquals("child", ((Link) cache1.find("/link/parent")).getLink().getName());
106       assertEquals("parent", ((Link) cache2.find("/link/parent")).getName());
107       assertEquals("child", ((Link) cache2.find("/link/parent")).getLink().getName());
108       ((Link) cache2.find("/link/parent")).setLink(null);
109       assertNull("Child should be null", ((Link) cache2.find("/link/parent")).getLink());
110       Link link = (Link) cache1.detach("/link/parent");
111       assertNotNull("Link should not be null ", link);
112    }
113
114    public void testCircularReference3() throws Exception JavaDoc
115    {
116 // try {Thread.sleep(10000); } catch (Exception e) {};
117
log.info("testCircularReference3() ...");
118       Link parent = new Link("parent");
119       Link child = new Link("child");
120       cache1.attach("/link/parent", parent);
121       cache1.attach("/link/child", child);
122       TestingUtil.sleepThread(100);
123       parent.setLink(child);
124       child.setLink(parent);
125
126       Link p1 = (Link) cache1.find("/link/parent");
127       Link c1 = (Link) cache1.find("/link/child");
128       assertEquals("parent", p1.getName());
129       assertEquals("child", p1.getLink().getName());
130       assertEquals("child", c1.getName());
131       assertEquals("parent", c1.getLink().getName());
132
133       Link p2 = (Link) cache1.find("/link/parent");
134       Link c2 = (Link) cache1.find("/link/child");
135
136       assertEquals("parent", p2.getName());
137       assertEquals("child", p2.getLink().getName());
138       assertEquals("child", c2.getName());
139       assertEquals("parent", c2.getLink().getName());
140
141       p2.setLink(null);
142       assertNull("Child should be null", p2.getLink());
143       Link link = (Link) cache1.detach("/link/parent");
144       assertNotNull("Link should not be null ", link);
145    }
146
147    /**
148     * Setting the circular relationship and also as a shared object.
149     *
150     * @throws Exception
151     */

152    public void testCircularReference4() throws Exception JavaDoc
153    {
154 // try {Thread.sleep(10000); } catch (Exception e) {};
155
log.info("testCircularReference3() ...");
156       Link parent = new Link("parent");
157       Link child = new Link("child");
158       parent.setLink(child);
159       child.setLink(parent);
160
161       List JavaDoc<Link> list = new ArrayList JavaDoc<Link>();
162       list.add(parent);
163
164       cache1.attach("/list", list);
165       cache1.attach("/alias", list);
166
167       TestingUtil.sleepThread(100);
168       List JavaDoc list1 = (List JavaDoc) cache2.find("/list");
169       List JavaDoc list2 = (List JavaDoc) cache2.find("/alias");
170
171       assertEquals("parent", ((Link) list1.get(0)).getName());
172       assertEquals("child", ((Link) list2.get(0)).getLink().getName());
173    }
174
175    public void testCircularAndSharedReferences() throws Exception JavaDoc
176    {
177       log.info("testCircularAndSharedReferences() ...");
178       NodeManager pm_ = new NodeManager();
179
180       pm_.setRootNode("root");
181       pm_.addNode("root", "kanto");
182       pm_.addNode("root.kanto", "tokyo");
183       pm_.addNode("root.kanto", "kanagawa");
184
185       cache1.attach("/propagation", pm_);
186       assertEquals("kanagawa", pm_.findNode("root.kanto.kanagawa").getNodeRDN());
187       pm_.addNode("root.kanto.tokyo", "hadanshita");
188       assertEquals("hadanshita", pm_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN());
189
190       NodeManager pm2_ = (NodeManager) cache2.find("/propagation");
191       assertEquals("kanagawa", pm2_.findNode("root.kanto.kanagawa").getNodeRDN());
192       assertEquals("hadanshita", pm2_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN());
193
194 /*
195       System.out.println("\n\n");
196       System.out.println("---------------------------------------------");
197       System.out.println("Initial pm state");
198       System.out.println("---------------------------------------------");
199       pm_.printNodes();
200
201       System.out.println("\n\n");
202       System.out.println("---------------------------------------------");
203       System.out.println("Initial cache content");
204       System.out.println(cache_.printDetails());
205       System.out.println("---------------------------------------------");
206 */

207    }
208
209    public static Test suite() throws Exception JavaDoc
210    {
211       return new TestSuite(ReplicatedCircularGraphTest.class);
212    }
213
214    public static void main(String JavaDoc[] args) throws Exception JavaDoc
215    {
216       junit.textui.TestRunner.run(ReplicatedCircularGraphTest.suite());
217    }
218
219 }
220
Popular Tags