KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > collection > ReplicatedSyncSetTest


1 package org.jboss.cache.pojo.collection;
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.pojo.PojoCache;
9 import org.jboss.cache.pojo.PojoCacheFactory;
10 import org.jboss.cache.pojo.test.Address;
11 import org.jboss.cache.pojo.test.Person;
12 import org.jboss.cache.Fqn;
13
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17
18 /**
19  * Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
20  *
21  * @author Ben Wang
22  */

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

62
63    protected Person createPerson(String JavaDoc name, int age)
64    {
65       Person p = new Person();
66       p.setName(name);
67       p.setAge(age);
68       return p;
69    }
70
71    /**
72     * Test attachment and then detachment and attachment.
73     *
74     * @throws Exception
75     */

76    public void testAttachDetach() throws Exception JavaDoc
77    {
78       log.info("testAttachDetach() ....");
79       Set JavaDoc set1 = new HashSet JavaDoc();
80       Address addr = new Address();
81       addr.setCity("San Jose");
82       addr.setZip(95123);
83       set1.add(addr);
84
85       Address addr2 = new Address();
86       addr2.setCity("Santa Clara");
87       addr2.setZip(95131);
88
89       Address addr3 = new Address();
90       addr3.setCity("Sunnyvale");
91       addr3.setZip(94086);
92
93       // Pure list
94
cache1.attach("/set", set1);
95       set1 = (Set JavaDoc) cache1.find("/set");
96       set1.add(addr2);
97       set1 = (Set JavaDoc)cache1.detach("/set");
98       assertEquals("Detached set should still be", 2, set1.size());
99       set1.add(addr3);
100       cache1.attach("/set", set1);
101
102       Set JavaDoc set2 = (Set JavaDoc) cache2.find("/set");
103       assertEquals("Set size should be ", 3, set2.size());
104    }
105
106    public void testRelationshipWithSharedSet1() throws Exception JavaDoc
107    {
108       log.info("testRelationshipWithSet() ....");
109       Set JavaDoc set1 = new HashSet JavaDoc();
110       Address addr = new Address();
111       addr.setCity("San Jose");
112       addr.setZip(95123);
113       set1.add(addr);
114
115       // Pure set
116
cache1.attach("/set", set1);
117       // We specifically need to use Proxy otherwise it won't work with multiple references
118
set1 = (Set JavaDoc) cache1.find("/set");
119       cache1.attach("/alias", set1);
120
121       Set JavaDoc set2 = (Set JavaDoc) cache1.find("/alias");
122       Address add1 = (Address) set2.iterator().next();
123       assertNotNull("Address should not be null", add1);
124       assertEquals("Zip ", 95123, add1.getZip());
125
126       set1 = (Set JavaDoc) cache2.find("/set");
127       set2 = (Set JavaDoc) cache2.find("/alias");
128       assertTrue("Set size should not be 0 ", (set2.size() != 0));
129       assertEquals("Both sets should be equal ", set1, set2);
130       add1 = (Address) set2.iterator().next();
131       assertNotNull("Address should not be null", add1);
132       assertEquals("Zip ", 95123, add1.getZip());
133    }
134
135    public void testRelationshipWithSharedSet2() throws Exception JavaDoc
136    {
137       log.info("testRelationshipWithSet2() ....");
138       Set JavaDoc set1 = new HashSet JavaDoc();
139       Address addr = new Address();
140       addr.setCity("San Jose");
141       addr.setZip(95123);
142       set1.add(addr);
143
144       Set JavaDoc set2 = new HashSet JavaDoc();
145       set2.add(addr);
146
147       cache1.attach("/set1", set1);
148       cache1.attach("/set2", set2);
149       Address add2 = (Address) ((Set JavaDoc) cache2.find("/set2")).iterator().next();
150       Address add1 = (Address) ((Set JavaDoc) cache2.find("/set1")).iterator().next();
151       assertEquals("Address should be the same", add1, add2);
152       assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip());
153    }
154
155    public void testNullWithSharedSet1() throws Exception JavaDoc
156    {
157       log.info("testNullWithSharedSet1() ....");
158       Set JavaDoc set1 = new HashSet JavaDoc();
159       set1.add("element 0");
160       set1.add(null); // element 1
161
set1.add("element 2");
162       assertTrue("contains test for null value", set1.contains(null));
163       Object JavaDoc a1[] = set1.toArray();
164       for (int looper = 0; looper < a1.length; looper++)
165       {
166          System.out.println("contained values:" + a1[looper]);
167       }
168
169       // Pure set
170
cache1.attach("/set", set1);
171       // We specifically need to use Proxy otherwise it won't work with multiple references
172
set1 = (Set JavaDoc) cache1.find("/set");
173       cache1.attach("/alias", set1);
174
175       Set JavaDoc set2 = (Set JavaDoc) cache1.find("/alias");
176
177       set1 = (Set JavaDoc) cache2.find("/set");
178       set2 = (Set JavaDoc) cache2.find("/alias");
179       assertTrue("Set size should not be 0 ", (set2.size() != 0));
180       assertEquals("Both sets should be equal ", set1, set2);
181
182       a1 = set1.toArray();
183       for (int looper = 0; looper < a1.length; looper++)
184       {
185          System.out.println("contained values:" + a1[looper]);
186       }
187       assertTrue("contains test for null value", set1.contains(null));
188       assertTrue("contains test for null value", set2.contains(null));
189
190       Iterator JavaDoc iter = set1.iterator();
191       while (iter.hasNext())
192       {
193          Object JavaDoc val = iter.next();
194          if ("element 2".equals(val))
195          {
196             iter.remove(); // remove element 2
197
}
198       }
199       assertFalse("element 2 is removed", set2.contains("element 2"));
200    }
201
202
203    /* This test won't pass since the recursive set will fail during replication as well
204    because of HashSet that calls out to HashMap hashCode. This causes the recursion.
205    public void testRecursion2() throws Exception
206    {
207       Set set = new HashSet();
208       set.add("1");
209       set.add("2");
210       set.add(set);
211
212       cache1.attach("set", set);
213    }
214    */

215
216    public static Test suite() throws Exception JavaDoc
217    {
218       return new TestSuite(ReplicatedSyncSetTest.class);
219    }
220
221    public static void main(String JavaDoc[] args) throws Exception JavaDoc
222    {
223       junit.textui.TestRunner.run(suite());
224    }
225
226 }
227
228
Popular Tags