KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > aop > ObjectGraphAopTest


1 package org.jboss.cache.tests.aop;
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.aop.TreeCacheAop;
9
10 /**
11  * Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
12  * @author Ben Wang
13  */

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

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

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