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