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.config.Configuration.CacheMode; |
9 |
| import org.jboss.cache.factories.UnitTestCacheFactory; |
10 |
| import org.jboss.cache.pojo.PojoCache; |
11 |
| import org.jboss.cache.pojo.PojoCacheFactory; |
12 |
| import org.jboss.cache.pojo.test.Address; |
13 |
| import org.jboss.cache.pojo.test.Person; |
14 |
| import org.jboss.cache.Fqn; |
15 |
| |
16 |
| import java.util.HashSet; |
17 |
| import java.util.Iterator; |
18 |
| import java.util.Set; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| public class ReplicatedSyncSetTest extends TestCase |
27 |
| { |
28 |
| Log log = LogFactory.getLog(ReplicatedSyncSetTest.class); |
29 |
| PojoCache cache1; |
30 |
| PojoCache cache2; |
31 |
| |
32 |
8
| public ReplicatedSyncSetTest(String name)
|
33 |
| { |
34 |
8
| super(name);
|
35 |
| } |
36 |
| |
37 |
8
| protected void setUp() throws Exception
|
38 |
| { |
39 |
8
| super.setUp();
|
40 |
8
| log.info("setUp() ....");
|
41 |
8
| cache1 = createCache("CacheGroup");
|
42 |
8
| cache2 = createCache("CacheGroup");
|
43 |
| } |
44 |
| |
45 |
8
| protected void tearDown() throws Exception
|
46 |
| { |
47 |
8
| super.tearDown();
|
48 |
8
| cache1.getCache().removeNode(Fqn.fromString("/"));
|
49 |
8
| cache1.stop();
|
50 |
8
| cache2.stop();
|
51 |
| } |
52 |
| |
53 |
16
| private PojoCache createCache(String name) throws Exception
|
54 |
| { |
55 |
16
| boolean toStart = false;
|
56 |
16
| PojoCache cache = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
|
57 |
16
| cache.start();
|
58 |
16
| return cache;
|
59 |
| } |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
0
| protected Person createPerson(String name, int age)
|
65 |
| { |
66 |
0
| Person p = new Person();
|
67 |
0
| p.setName(name);
|
68 |
0
| p.setAge(age);
|
69 |
0
| return p;
|
70 |
| } |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
2
| public void testAttachDetach() throws Exception
|
78 |
| { |
79 |
2
| log.info("testAttachDetach() ....");
|
80 |
2
| Set set1 = new HashSet();
|
81 |
2
| Address addr = new Address();
|
82 |
2
| addr.setCity("San Jose");
|
83 |
2
| addr.setZip(95123);
|
84 |
2
| set1.add(addr);
|
85 |
| |
86 |
2
| Address addr2 = new Address();
|
87 |
2
| addr2.setCity("Santa Clara");
|
88 |
2
| addr2.setZip(95131);
|
89 |
| |
90 |
2
| Address addr3 = new Address();
|
91 |
2
| addr3.setCity("Sunnyvale");
|
92 |
2
| addr3.setZip(94086);
|
93 |
| |
94 |
| |
95 |
2
| cache1.attach("/set", set1);
|
96 |
2
| set1 = (Set) cache1.find("/set");
|
97 |
2
| set1.add(addr2);
|
98 |
2
| set1 = (Set)cache1.detach("/set");
|
99 |
2
| assertEquals("Detached set should still be", 2, set1.size());
|
100 |
2
| set1.add(addr3);
|
101 |
2
| cache1.attach("/set", set1);
|
102 |
| |
103 |
2
| Set set2 = (Set) cache2.find("/set");
|
104 |
2
| assertEquals("Set size should be ", 3, set2.size());
|
105 |
| } |
106 |
| |
107 |
2
| public void testRelationshipWithSharedSet1() throws Exception
|
108 |
| { |
109 |
2
| log.info("testRelationshipWithSet() ....");
|
110 |
2
| Set set1 = new HashSet();
|
111 |
2
| Address addr = new Address();
|
112 |
2
| addr.setCity("San Jose");
|
113 |
2
| addr.setZip(95123);
|
114 |
2
| set1.add(addr);
|
115 |
| |
116 |
| |
117 |
2
| cache1.attach("/set", set1);
|
118 |
| |
119 |
2
| set1 = (Set) cache1.find("/set");
|
120 |
2
| cache1.attach("/alias", set1);
|
121 |
| |
122 |
2
| Set set2 = (Set) cache1.find("/alias");
|
123 |
2
| Address add1 = (Address) set2.iterator().next();
|
124 |
2
| assertNotNull("Address should not be null", add1);
|
125 |
2
| assertEquals("Zip ", 95123, add1.getZip());
|
126 |
| |
127 |
2
| set1 = (Set) cache2.find("/set");
|
128 |
2
| set2 = (Set) cache2.find("/alias");
|
129 |
2
| assertTrue("Set size should not be 0 ", (set2.size() != 0));
|
130 |
2
| assertEquals("Both sets should be equal ", set1, set2);
|
131 |
0
| add1 = (Address) set2.iterator().next();
|
132 |
0
| assertNotNull("Address should not be null", add1);
|
133 |
0
| assertEquals("Zip ", 95123, add1.getZip());
|
134 |
| } |
135 |
| |
136 |
2
| public void testRelationshipWithSharedSet2() throws Exception
|
137 |
| { |
138 |
2
| log.info("testRelationshipWithSet2() ....");
|
139 |
2
| Set set1 = new HashSet();
|
140 |
2
| Address addr = new Address();
|
141 |
2
| addr.setCity("San Jose");
|
142 |
2
| addr.setZip(95123);
|
143 |
2
| set1.add(addr);
|
144 |
| |
145 |
2
| Set set2 = new HashSet();
|
146 |
2
| set2.add(addr);
|
147 |
| |
148 |
2
| cache1.attach("/set1", set1);
|
149 |
2
| cache1.attach("/set2", set2);
|
150 |
2
| Address add2 = (Address) ((Set) cache2.find("/set2")).iterator().next();
|
151 |
2
| Address add1 = (Address) ((Set) cache2.find("/set1")).iterator().next();
|
152 |
2
| assertEquals("Address should be the same", add1, add2);
|
153 |
2
| assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip());
|
154 |
| } |
155 |
| |
156 |
2
| public void testNullWithSharedSet1() throws Exception
|
157 |
| { |
158 |
2
| log.info("testNullWithSharedSet1() ....");
|
159 |
2
| Set set1 = new HashSet();
|
160 |
2
| set1.add("element 0");
|
161 |
2
| set1.add(null);
|
162 |
2
| set1.add("element 2");
|
163 |
2
| assertTrue("contains test for null value", set1.contains(null));
|
164 |
2
| Object a1[] = set1.toArray();
|
165 |
2
| for (int looper = 0; looper < a1.length; looper++)
|
166 |
| { |
167 |
6
| System.out.println("contained values:" + a1[looper]);
|
168 |
| } |
169 |
| |
170 |
| |
171 |
2
| cache1.attach("/set", set1);
|
172 |
| |
173 |
2
| set1 = (Set) cache1.find("/set");
|
174 |
2
| cache1.attach("/alias", set1);
|
175 |
| |
176 |
2
| Set set2 = (Set) cache1.find("/alias");
|
177 |
| |
178 |
2
| set1 = (Set) cache2.find("/set");
|
179 |
2
| set2 = (Set) cache2.find("/alias");
|
180 |
2
| assertTrue("Set size should not be 0 ", (set2.size() != 0));
|
181 |
2
| assertEquals("Both sets should be equal ", set1, set2);
|
182 |
| |
183 |
2
| a1 = set1.toArray();
|
184 |
2
| for (int looper = 0; looper < a1.length; looper++)
|
185 |
| { |
186 |
6
| System.out.println("contained values:" + a1[looper]);
|
187 |
| } |
188 |
2
| assertTrue("contains test for null value", set1.contains(null));
|
189 |
2
| assertTrue("contains test for null value", set2.contains(null));
|
190 |
| |
191 |
2
| Iterator iter = set1.iterator();
|
192 |
2
| while (iter.hasNext())
|
193 |
| { |
194 |
6
| Object val = iter.next();
|
195 |
6
| if ("element 2".equals(val))
|
196 |
| { |
197 |
2
| iter.remove();
|
198 |
| } |
199 |
| } |
200 |
2
| assertFalse("element 2 is removed", set2.contains("element 2"));
|
201 |
| } |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
2
| public static Test suite() throws Exception
|
218 |
| { |
219 |
2
| return new TestSuite(ReplicatedSyncSetTest.class);
|
220 |
| } |
221 |
| |
222 |
0
| public static void main(String[] args) throws Exception
|
223 |
| { |
224 |
0
| junit.textui.TestRunner.run(suite());
|
225 |
| } |
226 |
| |
227 |
| } |
228 |
| |