1 |
| package org.jboss.cache.pojo; |
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.UnitTestCacheConfigurationFactory; |
10 |
| import org.jboss.cache.pojo.test.Person; |
11 |
| import org.jboss.cache.pojo.test.Student; |
12 |
| |
13 |
| import java.util.List; |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| public class ReplicatedTest extends TestCase |
21 |
| { |
22 |
| Log log = LogFactory.getLog(ReplicatedTest.class); |
23 |
| PojoCache cache, cache1; |
24 |
| |
25 |
| |
26 |
7
| public ReplicatedTest(String name)
|
27 |
| { |
28 |
7
| super(name);
|
29 |
| } |
30 |
| |
31 |
7
| protected void setUp() throws Exception
|
32 |
| { |
33 |
7
| super.setUp();
|
34 |
7
| log.info("setUp() ....");
|
35 |
7
| boolean toStart = false;
|
36 |
7
| cache = PojoCacheFactory.createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
|
37 |
7
| cache.start();
|
38 |
7
| cache1 = PojoCacheFactory.createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
|
39 |
7
| cache1.start();
|
40 |
| } |
41 |
| |
42 |
7
| protected void tearDown() throws Exception
|
43 |
| { |
44 |
7
| super.tearDown();
|
45 |
7
| cache.stop();
|
46 |
7
| cache1.stop();
|
47 |
| } |
48 |
| |
49 |
| |
50 |
| |
51 |
6
| private Person createPerson(String id, String name, int age)
|
52 |
| { |
53 |
6
| Person p = new Person();
|
54 |
6
| p.setName(name);
|
55 |
6
| p.setAge(age);
|
56 |
6
| cache.attach(id, p);
|
57 |
6
| return p;
|
58 |
| } |
59 |
| |
60 |
1
| private Student createStudent(String id, String name, int age, String grade)
|
61 |
| { |
62 |
1
| Student p = new Student();
|
63 |
1
| p.setName(name);
|
64 |
1
| p.setAge(age);
|
65 |
1
| p.setYear(grade);
|
66 |
1
| cache.attach(id, p);
|
67 |
1
| return p;
|
68 |
| } |
69 |
| |
70 |
1
| public void testSimple() throws Exception
|
71 |
| { |
72 |
1
| log.info("testSimple() ....");
|
73 |
1
| Person ben = createPerson("/person/test1", "Ben Wang", 40);
|
74 |
1
| assertEquals("Ben Wang", ben.getName());
|
75 |
1
| assertEquals("Ben Wang", ((Person) cache1.find("/person/test1")).getName());
|
76 |
1
| cache.detach("/person/test1");
|
77 |
| } |
78 |
| |
79 |
| |
80 |
1
| public void testDynamicRefSwapping() throws Exception
|
81 |
| { |
82 |
1
| Person person = createPerson("/person/test3", "Joe", 32);
|
83 |
1
| try
|
84 |
| { |
85 |
1
| person.setAge(30);
|
86 |
1
| List med = person.getMedication();
|
87 |
1
| assertNull("Medication should be null ", med);
|
88 |
1
| person.setAge(61);
|
89 |
1
| med = person.getMedication();
|
90 |
1
| assertEquals("Medication ", (Object) "Lipitor", (Object) med.get(0));
|
91 |
1
| assertEquals("Medication on cache1 ", "Lipitor",
|
92 |
| person.getMedication().get(0)); |
93 |
| |
94 |
1
| person.setAge(71);
|
95 |
1
| assertEquals("Medication ", "Vioxx", med.get(1));
|
96 |
1
| assertEquals("Medication on cache1 ", "Vioxx",
|
97 |
| ((Person) cache1.find("/person/test3")).getMedication().get(1)); |
98 |
1
| cache.detach("/person/test3");
|
99 |
| |
100 |
| } catch (Exception e) |
101 |
| { |
102 |
| |
103 |
| } |
104 |
| } |
105 |
| |
106 |
1
| public void testTransient() throws Exception
|
107 |
| { |
108 |
1
| log.info("testTransient() ....");
|
109 |
1
| Person ben = createPerson("/person/test1", "Ben Wang", 40);
|
110 |
1
| ben.setCurrentStatus("Idle");
|
111 |
1
| assertEquals("Cache 1 ", "Idle", ben.getCurrentStatus());
|
112 |
1
| assertEquals("Cache 2 ", "Active",
|
113 |
| ((Person) cache1.find("/person/test1")).getCurrentStatus()); |
114 |
1
| cache.detach("/person/test1");
|
115 |
| } |
116 |
| |
117 |
1
| public void testModification() throws Exception
|
118 |
| { |
119 |
1
| Person ben = createPerson("/person/test2", "Ben Wang", 40);
|
120 |
1
| ben.setName("Harald Gliebe");
|
121 |
1
| assertEquals(ben.getName(), "Harald Gliebe");
|
122 |
1
| assertEquals(((Person) cache1.find("/person/test2")).getName(), "Harald Gliebe");
|
123 |
1
| cache.detach("/person/test2");
|
124 |
| } |
125 |
| |
126 |
1
| public void testPostDetachModification() throws Exception
|
127 |
| { |
128 |
1
| Person ben = createPerson("/person/test2", "Ben Wang", 40);
|
129 |
1
| ben = (Person) cache1.find("/person/test2");
|
130 |
| |
131 |
| |
132 |
1
| cache.detach("/person/test2");
|
133 |
| |
134 |
1
| boolean passGet = false;
|
135 |
1
| try
|
136 |
| { |
137 |
1
| ben.getAge();
|
138 |
| } |
139 |
| catch (PojoCacheAlreadyDetachedException e) |
140 |
| { |
141 |
1
| passGet = true;
|
142 |
| } |
143 |
| |
144 |
1
| assertTrue("Expected PojoCacheAlreadyDetachedException!", passGet);
|
145 |
| |
146 |
1
| boolean passSet = false;
|
147 |
1
| try
|
148 |
| { |
149 |
1
| ben.setAge(10);
|
150 |
| } |
151 |
| catch (PojoCacheAlreadyDetachedException e) |
152 |
| { |
153 |
1
| passSet = true;
|
154 |
| } |
155 |
| |
156 |
1
| assertTrue("Expected PojoCacheAlreadyDetachedException!", passSet);
|
157 |
| } |
158 |
| |
159 |
1
| public void testStaleFieldDetach() throws Exception
|
160 |
| { |
161 |
1
| Person ben = createPerson("/person/test2", "Ben Wang", 40);
|
162 |
1
| ben = (Person) cache1.find("/person/test2");
|
163 |
| |
164 |
| |
165 |
1
| ben.setAge(99);
|
166 |
| |
167 |
| |
168 |
1
| ben = (Person) cache.detach("/person/test2");
|
169 |
| |
170 |
1
| assertEquals("Detach did not refresh field", 99, ben.getAge());
|
171 |
| } |
172 |
| |
173 |
1
| public void testInheritance() throws Exception
|
174 |
| { |
175 |
1
| Student joe = createStudent("/person/joe", "Joe", 32, "Senior");
|
176 |
1
| joe.setName("Joe Black");
|
177 |
1
| assertEquals(joe.getName(), "Joe Black");
|
178 |
1
| Student joe1 = (Student) cache1.find("/person/joe");
|
179 |
1
| assertEquals(joe1.getName(), "Joe Black");
|
180 |
1
| joe1.setYear("Junior");
|
181 |
1
| assertEquals(joe.getYear(), "Junior");
|
182 |
1
| assertEquals(joe1.getYear(), "Junior");
|
183 |
1
| cache.detach("/person/joe");
|
184 |
1
| cache.detach("/person/joe");
|
185 |
| } |
186 |
| |
187 |
| |
188 |
1
| public static Test suite() throws Exception
|
189 |
| { |
190 |
1
| return new TestSuite(ReplicatedTest.class);
|
191 |
| } |
192 |
| |
193 |
| |
194 |
0
| public static void main(String[] args) throws Exception
|
195 |
| { |
196 |
0
| junit.textui.TestRunner.run(suite());
|
197 |
| } |
198 |
| |
199 |
| } |
200 |
| |