KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > test > standAloneAop > ObjectGraphAopTest


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.test.cache.test.standAloneAop;
8
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12 import org.jboss.cache.aop.TreeCacheAop;
13 import org.jboss.logging.Logger;
14
15 /**
16  * Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
17  * @author Ben Wang
18  */

19
20 public class ObjectGraphAopTest extends TestCase
21 {
22    Logger log = Logger.getLogger(ObjectGraphAopTest.class);
23    TreeCacheAopTester tester1;
24
25    public ObjectGraphAopTest(String JavaDoc name)
26    {
27       super(name);
28    }
29
30    protected void setUp() throws Exception JavaDoc
31    {
32       super.setUp();
33       log.info("setUp() ....");
34       String JavaDoc configFile = "META-INF/local-service.xml";
35       tester1 = new TreeCacheAopTester(configFile);
36    }
37
38    protected void tearDown() throws Exception JavaDoc
39    {
40       super.tearDown();
41       tester1.stop();
42       tester1 = null;
43    }
44
45 // public void testDummy() {}
46

47    public void testSetup()
48    {
49       log.info("testSetup() ....");
50       try {
51          tester1.testSetup();
52       } catch (Exception JavaDoc ex) {
53          ex.printStackTrace();
54          fail("testSetup(): fails. " + ex.toString());
55       }
56    }
57
58    /**
59     * Test shared sub-object. In diagram, it is a forest
60     * where the sub-object is connected by two tree parents.
61     */

62    public void testMultipleReference() throws Exception JavaDoc
63    {
64       log.info("testMultipleReference() ...");
65       tester1.createPerson("/person/joe", "Joe Black", 31);
66       Person joe = (Person) tester1.getPerson("/person/joe");
67       tester1.createPerson("/person/ben", "Ben Hogan", 51);
68       Person ben = (Person) tester1.getPerson("/person/ben");
69
70       Address addr = tester1.createAddress();
71       addr.setStreet("123 Albert Ave.");
72       addr.setCity("Sunnyvale");
73       addr.setZip(94087);
74
75       // They share the sub-object: address
76
log.info("testMultipleReference(): set Joe address");
77       joe.setAddress(addr);
78       log.info("testMultipleReference(): set Ben address");
79       ben.setAddress(addr);
80
81       log.info("testMultipleReference(): verify");
82       assertEquals(tester1.getCity("/person/joe"), tester1.getCity("/person/ben"));
83       addr.setCity("Santa Clara");
84       assertEquals(tester1.getCity("/person/joe"), tester1.getCity("/person/ben"));
85    }
86
87    public void testRefCount() throws Exception JavaDoc
88    {
89 // try {Thread.sleep(10000); } catch (Exception e) {};
90
log.info("testRefCount() ...");
91       stage1();
92       stage2(tester1);
93
94       tester1.removePerson("/person/ben");
95       log.info("stage2(): " + tester1.printCacheDetails());
96    }
97
98    private void stage1()
99    {
100       tester1.createPerson("/person/joe", "Joe Black", 31);
101       Person joe = (Person) tester1.getPerson("/person/joe");
102       tester1.createPerson("/person/ben", "Ben Hogan", 51);
103       Person ben = (Person) tester1.getPerson("/person/ben");
104
105       Address addr = tester1.createAddress();
106       addr.setStreet("123 Albert Ave.");
107       addr.setCity("Sunnyvale");
108       addr.setZip(94087);
109
110       // They share the sub-object: address
111
joe.setAddress(addr);
112       assertEquals("Joe's address should still be valid ", "Sunnyvale", tester1.getCity("/person/joe"));
113       ben.setAddress(addr);
114       assertEquals("Ben's address should still be valid ", "Sunnyvale", tester1.getCity("/person/ben"));
115    }
116
117    private void stage2(TreeCacheAopTester tester)
118    {
119       //
120
tester.removePerson("/person/joe");
121       log.info("stage2(): " + tester.printCacheDetails());
122       assertEquals("Ben's address should still be valid ", "Sunnyvale", tester.getCity("/person/ben"));
123       Person ben = (Person) tester.getPerson("/person/ben");
124       Address addr = ben.getAddress();
125       addr.setCity("Santa Clara");
126       assertEquals("Ben's address should be changed ", "Santa Clara", tester.getCity("/person/ben"));
127    }
128
129    public void testCircularReference1() throws Exception JavaDoc
130    {
131 // try {Thread.sleep(10000); } catch (Exception e) {};
132
log.info("testCircularReference1() ...");
133       Link parent = new Link("parent");
134       Link child = new Link("child");
135       parent.setLink(child);
136       child.setLink(parent);
137       TreeCacheAop cache1 = tester1.getCache();
138       cache1.putObject("/link/parent", parent);
139       assertEquals("parent", ((Link) cache1.getObject("/link/parent")).getName());
140       assertEquals("child", ((Link) cache1.getObject("/link/parent")).getLink().getName());
141 // tester1.getCache().putObject("/link/parent/child", child);
142
log.info("testCircularReference1(): tester1 " + tester1.printCacheDetails());
143    }
144
145    public void testCircularReference2() throws Exception JavaDoc
146    {
147 // try {Thread.sleep(10000); } catch (Exception e) {};
148
log.info("testCircularReference2() ...");
149       Link parent = new Link("parent");
150       Link child = new Link("child");
151       tester1.getCache().putObject("/link/parent", parent);
152       parent.setLink(child);
153       child.setLink(parent);
154       TreeCacheAop cache1 = tester1.getCache();
155       assertEquals("parent", ((Link) cache1.getObject("/link/parent")).getName());
156       assertEquals("child", ((Link) cache1.getObject("/link/parent")).getLink().getName());
157       log.info("testCircularReference2(): tester1 " + tester1.printCacheDetails());
158    }
159
160    public void testCircularReference3() throws Exception JavaDoc
161    {
162 // try {Thread.sleep(10000); } catch (Exception e) {};
163
log.info("testCircularReference3() ...");
164       Link parent = new Link("parent");
165       Link child = new Link("child");
166       tester1.getCache().putObject("/link/parent", parent);
167       tester1.getCache().putObject("/link/child", child);
168       parent.setLink(child);
169       child.setLink(parent);
170       TreeCacheAop cache1 = tester1.getCache();
171       assertEquals("parent", ((Link) cache1.getObject("/link/parent")).getName());
172       assertEquals("child", ((Link) cache1.getObject("/link/parent")).getLink().getName());
173       assertEquals("child", ((Link) cache1.getObject("/link/child")).getName());
174       assertEquals("parent", ((Link) cache1.getObject("/link/child")).getLink().getName());
175 // tester1.getCache().putObject("/link/parent/child", child);
176
log.info("testCircularReference3(): tester1 " + tester1.printCacheDetails());
177    }
178
179    public void testObjectIdentity() throws Exception JavaDoc
180    {
181
182    }
183
184    public static Test suite() throws Exception JavaDoc
185    {
186       return new TestSuite(ObjectGraphAopTest.class);
187    }
188
189    public static void main(String JavaDoc[] args) throws Exception JavaDoc
190    {
191       junit.textui.TestRunner.run(suite());
192    }
193
194 }
195
196
Popular Tags