1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
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.config.Configuration.CacheMode; |
16 |
| import org.jboss.cache.factories.UnitTestCacheFactory; |
17 |
| import org.jboss.cache.pojo.test.Person; |
18 |
| import org.jboss.cache.pojo.test.Link; |
19 |
| import org.jboss.cache.pojo.test.NodeManager; |
20 |
| import org.jboss.cache.Fqn; |
21 |
| |
22 |
| import java.util.List; |
23 |
| import java.util.ArrayList; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| public class ReplicatedCircularGraphTest extends TestCase |
32 |
| { |
33 |
| Log log = LogFactory.getLog(ReplicatedCircularGraphTest.class); |
34 |
| PojoCache cache1; |
35 |
| PojoCache cache2; |
36 |
| |
37 |
10
| public ReplicatedCircularGraphTest(String name)
|
38 |
| { |
39 |
10
| super(name);
|
40 |
| } |
41 |
| |
42 |
10
| protected void setUp() throws Exception
|
43 |
| { |
44 |
10
| super.setUp();
|
45 |
10
| log.info("setUp() ....");
|
46 |
10
| cache1 = createCache("CacheGroup");
|
47 |
10
| cache2 = createCache("CacheGroup");
|
48 |
| } |
49 |
| |
50 |
10
| protected void tearDown() throws Exception
|
51 |
| { |
52 |
10
| super.tearDown();
|
53 |
10
| cache1.getCache().removeNode(Fqn.fromString("/"));
|
54 |
10
| cache1.stop();
|
55 |
10
| cache2.stop();
|
56 |
| } |
57 |
| |
58 |
20
| private PojoCache createCache(String name) throws Exception
|
59 |
| { |
60 |
20
| boolean toStart = false;
|
61 |
20
| PojoCache tree = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
|
62 |
20
| tree.start();
|
63 |
20
| return tree;
|
64 |
| } |
65 |
| |
66 |
| |
67 |
| |
68 |
0
| protected Person createPerson(String name, int age)
|
69 |
| { |
70 |
0
| Person p = new Person();
|
71 |
0
| p.setName(name);
|
72 |
0
| p.setAge(age);
|
73 |
0
| return p;
|
74 |
| } |
75 |
| |
76 |
2
| public void testCircularReference1() throws Exception
|
77 |
| { |
78 |
| |
79 |
2
| log.info("testCircularReference1() ...");
|
80 |
2
| Link parent = new Link("parent");
|
81 |
2
| Link child = new Link("child");
|
82 |
2
| parent.setLink(child);
|
83 |
2
| child.setLink(parent);
|
84 |
2
| cache1.attach("/link/parent", parent);
|
85 |
2
| TestingUtil.sleepThread(100);
|
86 |
2
| assertEquals("parent", ((Link) cache1.find("/link/parent")).getName());
|
87 |
2
| assertEquals("child", ((Link) cache1.find("/link/parent")).getLink().getName());
|
88 |
2
| assertEquals("parent", ((Link) cache2.find("/link/parent")).getName());
|
89 |
2
| assertEquals("child", ((Link) cache2.find("/link/parent")).getLink().getName());
|
90 |
2
| ((Link) cache2.find("/link/parent")).setLink(null);
|
91 |
2
| assertNull("Child should be null", ((Link) cache2.find("/link/parent")).getLink());
|
92 |
2
| Link link = (Link) cache1.detach("/link/parent");
|
93 |
2
| assertNotNull("Link should not be null ", link);
|
94 |
2
| System.out.println("Link: " + link);
|
95 |
| } |
96 |
| |
97 |
2
| public void testCircularReference2() throws Exception
|
98 |
| { |
99 |
| |
100 |
2
| log.info("testCircularReference2() ...");
|
101 |
2
| Link parent = new Link("parent");
|
102 |
2
| Link child = new Link("child");
|
103 |
2
| cache1.attach("/link/parent", parent);
|
104 |
2
| parent.setLink(child);
|
105 |
2
| child.setLink(parent);
|
106 |
2
| assertEquals("parent", ((Link) cache1.find("/link/parent")).getName());
|
107 |
2
| assertEquals("child", ((Link) cache1.find("/link/parent")).getLink().getName());
|
108 |
2
| assertEquals("parent", ((Link) cache2.find("/link/parent")).getName());
|
109 |
2
| assertEquals("child", ((Link) cache2.find("/link/parent")).getLink().getName());
|
110 |
2
| ((Link) cache2.find("/link/parent")).setLink(null);
|
111 |
2
| assertNull("Child should be null", ((Link) cache2.find("/link/parent")).getLink());
|
112 |
2
| Link link = (Link) cache1.detach("/link/parent");
|
113 |
2
| assertNotNull("Link should not be null ", link);
|
114 |
| } |
115 |
| |
116 |
2
| public void testCircularReference3() throws Exception
|
117 |
| { |
118 |
| |
119 |
2
| log.info("testCircularReference3() ...");
|
120 |
2
| Link parent = new Link("parent");
|
121 |
2
| Link child = new Link("child");
|
122 |
2
| cache1.attach("/link/parent", parent);
|
123 |
2
| cache1.attach("/link/child", child);
|
124 |
2
| TestingUtil.sleepThread(100);
|
125 |
2
| parent.setLink(child);
|
126 |
2
| child.setLink(parent);
|
127 |
| |
128 |
2
| Link p1 = (Link) cache1.find("/link/parent");
|
129 |
2
| Link c1 = (Link) cache1.find("/link/child");
|
130 |
2
| assertEquals("parent", p1.getName());
|
131 |
2
| assertEquals("child", p1.getLink().getName());
|
132 |
2
| assertEquals("child", c1.getName());
|
133 |
2
| assertEquals("parent", c1.getLink().getName());
|
134 |
| |
135 |
2
| Link p2 = (Link) cache1.find("/link/parent");
|
136 |
2
| Link c2 = (Link) cache1.find("/link/child");
|
137 |
| |
138 |
2
| assertEquals("parent", p2.getName());
|
139 |
2
| assertEquals("child", p2.getLink().getName());
|
140 |
2
| assertEquals("child", c2.getName());
|
141 |
2
| assertEquals("parent", c2.getLink().getName());
|
142 |
| |
143 |
2
| p2.setLink(null);
|
144 |
2
| assertNull("Child should be null", p2.getLink());
|
145 |
2
| Link link = (Link) cache1.detach("/link/parent");
|
146 |
2
| assertNotNull("Link should not be null ", link);
|
147 |
| } |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
2
| public void testCircularReference4() throws Exception
|
155 |
| { |
156 |
| |
157 |
2
| log.info("testCircularReference3() ...");
|
158 |
2
| Link parent = new Link("parent");
|
159 |
2
| Link child = new Link("child");
|
160 |
2
| parent.setLink(child);
|
161 |
2
| child.setLink(parent);
|
162 |
| |
163 |
2
| List<Link> list = new ArrayList<Link>();
|
164 |
2
| list.add(parent);
|
165 |
| |
166 |
2
| cache1.attach("/list", list);
|
167 |
2
| cache1.attach("/alias", list);
|
168 |
| |
169 |
2
| TestingUtil.sleepThread(100);
|
170 |
2
| List list1 = (List) cache2.find("/list");
|
171 |
2
| List list2 = (List) cache2.find("/alias");
|
172 |
| |
173 |
2
| assertEquals("parent", ((Link) list1.get(0)).getName());
|
174 |
2
| assertEquals("child", ((Link) list2.get(0)).getLink().getName());
|
175 |
| } |
176 |
| |
177 |
2
| public void testCircularAndSharedReferences() throws Exception
|
178 |
| { |
179 |
2
| log.info("testCircularAndSharedReferences() ...");
|
180 |
2
| NodeManager pm_ = new NodeManager();
|
181 |
| |
182 |
2
| pm_.setRootNode("root");
|
183 |
2
| pm_.addNode("root", "kanto");
|
184 |
2
| pm_.addNode("root.kanto", "tokyo");
|
185 |
2
| pm_.addNode("root.kanto", "kanagawa");
|
186 |
| |
187 |
2
| cache1.attach("/propagation", pm_);
|
188 |
2
| assertEquals("kanagawa", pm_.findNode("root.kanto.kanagawa").getNodeRDN());
|
189 |
2
| pm_.addNode("root.kanto.tokyo", "hadanshita");
|
190 |
2
| assertEquals("hadanshita", pm_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN());
|
191 |
| |
192 |
2
| NodeManager pm2_ = (NodeManager) cache2.find("/propagation");
|
193 |
2
| assertEquals("kanagawa", pm2_.findNode("root.kanto.kanagawa").getNodeRDN());
|
194 |
2
| assertEquals("hadanshita", pm2_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN());
|
195 |
| |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| } |
210 |
| |
211 |
2
| public static Test suite() throws Exception
|
212 |
| { |
213 |
2
| return new TestSuite(ReplicatedCircularGraphTest.class);
|
214 |
| } |
215 |
| |
216 |
0
| public static void main(String[] args) throws Exception
|
217 |
| { |
218 |
0
| junit.textui.TestRunner.run(ReplicatedCircularGraphTest.suite());
|
219 |
| } |
220 |
| |
221 |
| } |