1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| package org.jboss.cache.pojo; |
24 |
| |
25 |
| import java.util.List; |
26 |
| import java.util.Set; |
27 |
| |
28 |
| import junit.framework.TestCase; |
29 |
| |
30 |
| import org.apache.commons.logging.Log; |
31 |
| import org.apache.commons.logging.LogFactory; |
32 |
| import org.jboss.cache.Fqn; |
33 |
| import org.jboss.cache.Node; |
34 |
| import org.jboss.cache.buddyreplication.NextMemberBuddyLocatorConfig; |
35 |
| import org.jboss.cache.config.BuddyReplicationConfig; |
36 |
| import org.jboss.cache.config.Configuration; |
37 |
| import org.jboss.cache.config.Configuration.CacheMode; |
38 |
| import org.jboss.cache.factories.UnitTestCacheConfigurationFactory; |
39 |
| import org.jboss.cache.pojo.impl.PojoReference; |
40 |
| import org.jboss.cache.pojo.test.Person; |
41 |
| import org.jboss.cache.pojo.test.Student; |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| public class BuddyReplicationTest extends TestCase |
50 |
| { |
51 |
| Log log = LogFactory.getLog(ReplicatedTest.class); |
52 |
| PojoCache cache, cache1; |
53 |
| |
54 |
4
| public BuddyReplicationTest(String name)
|
55 |
| { |
56 |
4
| super(name);
|
57 |
| } |
58 |
| |
59 |
4
| protected void setUp() throws Exception
|
60 |
| { |
61 |
4
| super.setUp();
|
62 |
4
| log.info("setUp() ....");
|
63 |
4
| boolean toStart = false;
|
64 |
4
| Configuration cfg1 = UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC);
|
65 |
4
| addBuddyReplication(cfg1);
|
66 |
4
| cache = PojoCacheFactory.createCache(cfg1, toStart);
|
67 |
4
| cache.start();
|
68 |
4
| Configuration cfg2 = UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC);
|
69 |
4
| addBuddyReplication(cfg2);
|
70 |
4
| cache1 = PojoCacheFactory.createCache(cfg2, toStart);
|
71 |
4
| cache1.start();
|
72 |
| } |
73 |
| |
74 |
4
| protected void tearDown() throws Exception
|
75 |
| { |
76 |
4
| super.tearDown();
|
77 |
4
| cache.stop();
|
78 |
4
| cache1.stop();
|
79 |
| } |
80 |
| |
81 |
8
| private void addBuddyReplication(Configuration cfg)
|
82 |
| { |
83 |
8
| BuddyReplicationConfig brc = new BuddyReplicationConfig();
|
84 |
8
| brc.setAutoDataGravitation(false);
|
85 |
8
| brc.setBuddyPoolName("test");
|
86 |
8
| brc.setEnabled(true);
|
87 |
8
| NextMemberBuddyLocatorConfig blc = new NextMemberBuddyLocatorConfig();
|
88 |
8
| brc.setBuddyLocatorConfig(blc);
|
89 |
| |
90 |
8
| cfg.setBuddyReplicationConfig(brc);
|
91 |
| } |
92 |
| |
93 |
4
| private Person createPerson(String id, String name, int age)
|
94 |
| { |
95 |
4
| Person p = new Person();
|
96 |
4
| p.setName(name);
|
97 |
4
| p.setAge(age);
|
98 |
4
| cache.attach(id, p);
|
99 |
4
| return p;
|
100 |
| } |
101 |
| |
102 |
0
| private Student createStudent(String id, String name, int age, String grade)
|
103 |
| { |
104 |
0
| Student p = new Student();
|
105 |
0
| p.setName(name);
|
106 |
0
| p.setAge(age);
|
107 |
0
| p.setYear(grade);
|
108 |
0
| cache.attach(id, p);
|
109 |
0
| return p;
|
110 |
| } |
111 |
| |
112 |
4
| private Person getReplicatedPerson(String id) throws PojoCacheException
|
113 |
| { |
114 |
4
| Person repl = (Person) cache1.find(id);
|
115 |
4
| assertNotNull("Person found at " + id, repl);
|
116 |
4
| return repl;
|
117 |
| } |
118 |
| |
119 |
1
| public void testSimple() throws Exception
|
120 |
| { |
121 |
1
| log.info("testSimple() ....");
|
122 |
1
| Person ben = createPerson("/person/test1", "Ben Wang", 40);
|
123 |
1
| assertEquals("Ben Wang", ben.getName());
|
124 |
1
| Person repl = getReplicatedPerson("/person/test1");
|
125 |
1
| assertEquals("Ben Wang", repl.getName());
|
126 |
1
| cache.detach("/person/test1");
|
127 |
| } |
128 |
| |
129 |
| |
130 |
1
| public void testDynamicRefSwapping() throws Exception
|
131 |
| { |
132 |
1
| Person person = createPerson("/person/test3", "Joe", 32);
|
133 |
1
| try
|
134 |
| { |
135 |
1
| person.setAge(30);
|
136 |
1
| List med = person.getMedication();
|
137 |
1
| assertNull("Medication should be null ", med);
|
138 |
1
| person.setAge(61);
|
139 |
1
| med = person.getMedication();
|
140 |
1
| assertEquals("Medication ", (Object) "Lipitor", (Object) med.get(0));
|
141 |
1
| assertEquals("Medication on cache1 ", "Lipitor",
|
142 |
| person.getMedication().get(0)); |
143 |
| |
144 |
1
| person.setAge(71);
|
145 |
1
| assertEquals("Medication ", "Vioxx", med.get(1));
|
146 |
1
| Person repl = getReplicatedPerson("/person/test3");
|
147 |
1
| assertEquals("Medication on cache1 ", "Vioxx",
|
148 |
| repl.getMedication().get(1)); |
149 |
1
| cache.detach("/person/test3");
|
150 |
| |
151 |
| } catch (Exception e) |
152 |
| { |
153 |
| |
154 |
| } |
155 |
| } |
156 |
| |
157 |
1
| public void testTransient() throws Exception
|
158 |
| { |
159 |
1
| log.info("testTransient() ....");
|
160 |
1
| Person ben = createPerson("/person/test1", "Ben Wang", 40);
|
161 |
1
| ben.setCurrentStatus("Idle");
|
162 |
1
| assertEquals("Cache 1 ", "Idle", ben.getCurrentStatus());
|
163 |
1
| Person repl = getReplicatedPerson("/person/test1");
|
164 |
1
| assertEquals("Cache 2 ", "Active",
|
165 |
| repl.getCurrentStatus()); |
166 |
1
| cache.detach("/person/test1");
|
167 |
| } |
168 |
| |
169 |
1
| public void testModification() throws Exception
|
170 |
| { |
171 |
1
| Person ben = createPerson("/person/test2", "Ben Wang", 40);
|
172 |
1
| ben.setName("Harald Gliebe");
|
173 |
1
| assertEquals(ben.getName(), "Harald Gliebe");
|
174 |
1
| Person repl = getReplicatedPerson("/person/test2");
|
175 |
1
| assertEquals("Harald Gliebe", repl.getName());
|
176 |
1
| cache.detach("/person/test2");
|
177 |
| } |
178 |
| } |