KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > NewReplicatedTest


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.Fqn;
9 import org.jboss.cache.pojo.test.Person;
10 import org.jboss.cache.pojo.test.SpecialSerializedAddress;
11
12 import javax.naming.Context JavaDoc;
13 import java.util.Properties JavaDoc;
14
15
16 /**
17  * Replication PojoCache test. This is the real beef as local mode for PojoCache really does
18  * not do anything.
19  *
20  * @author Ben Wang
21  */

22
23 public class NewReplicatedTest extends TestCase
24 {
25    Log log_ = LogFactory.getLog(NewReplicatedTest.class);
26    PojoCache cache_;
27    PojoCache cache1_;
28
29    public NewReplicatedTest(String JavaDoc name)
30    {
31       super(name);
32    }
33
34    protected void setUp() throws Exception JavaDoc
35    {
36       super.setUp();
37       Properties JavaDoc prop = new Properties JavaDoc();
38       prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
39       boolean toStart = false;
40       cache_ = PojoCacheFactory.createCache("META-INF/replSync-service.xml", toStart);
41       cache1_ = PojoCacheFactory.createCache("META-INF/replSync-service.xml", toStart);
42       cache_.start();
43       cache1_.start();
44    }
45
46    protected void tearDown() throws Exception JavaDoc
47    {
48       super.tearDown();
49       cache_.stop();
50       cache1_.stop();
51    }
52
53    /*
54    public void testObjectSizePut() throws Exception
55    {
56       org.jboss.cache.data.Student student = (org.jboss.cache.data.Student)constructObject();
57       cache_.putObject("/joe", student);
58    }
59
60    static Object constructObject()
61    {
62       org.jboss.cache.data.Student joe = new org.jboss.cache.data.Student();
63       joe.setName("Joe");
64
65       Address add = new Address();
66       add.setZip(94086);
67       add.setCity("Sunnyvale)");
68       add.setStreet("Albertson");
69       joe.setAddress(add);
70
71       String str;
72       for(int i=0; i < 100; i++)
73       {
74          Course course = new Course();
75          str = RandomString.randomstring(10,20);
76          course.setInstructor(str);
77          str = RandomString.randomstring(10,20);
78          course.setTitle(str);
79          str = RandomString.randomstring(10,20);
80          course.setRoom(str);
81          joe.addCourse(course);
82       }
83
84       return joe;
85    }
86    */

87
88    public void testRemoteRemove() throws Exception JavaDoc
89    {
90       log_.info("testRemoteRemove() ....");
91       Person test = new Person();
92       test.setName("Ben");
93       test.setAge(10);
94       cache_.attach("/a", test);
95       Person result = (Person) cache_.find("/a");
96       assertEquals(" ", test, result);
97
98       Person remote = (Person) cache1_.find("/a");
99       assertEquals("Age should be ", 10, remote.getAge());
100
101       // Remote remove
102
cache1_.detach("/a");
103       assertNull("Object should be null ", cache_.find("/a"));
104
105       assertNull("Object should be null ", cache1_.find("/a"));
106       // It is 0 since it will be un-initialized.
107
assertEquals("Age should be ", 0, remote.getAge());
108    }
109
110    public void testRemoteRemove2() throws Exception JavaDoc
111    {
112       log_.info("testRemoteRemove() ....");
113       Person test = new Person();
114       test.setName("Ben");
115       test.setAge(10);
116       cache_.attach("/a", test);
117       Person result = (Person) cache_.find("/a");
118       assertEquals(" ", test, result);
119
120       Person remote = (Person) cache1_.find("/a");
121       assertEquals("Age should be ", 10, remote.getAge());
122
123       // Remote remove
124
cache_.detach("/a");
125       assertNull("Object should be null ", cache_.find("/a"));
126
127       assertNull("Object should be null ", cache1_.find("/a"));
128       // this will trigger the PojoCacheAlreadyDetachedException
129
try
130       {
131          remote.getAge();
132          fail("Should throw out exception here.");
133       }
134       catch (PojoCacheAlreadyDetachedException pe)
135       {
136       }
137    }
138
139    /**
140     * Test for pojo detachment and then serialization.
141     *
142     * @throws Exception
143     */

144    public void testRemoteDetach() throws Exception JavaDoc
145    {
146       log_.info("testRemoteDetach() ....");
147       SpecialSerializedAddress addr = new SpecialSerializedAddress();
148       addr.setZip(95123);
149       addr.addResidents("Ben");
150       addr.addResidents("Joe");
151       // Test serialization first
152
Fqn fqn = new Fqn("/plain");
153       cache_.getCache().put(fqn, "test", addr);
154       cache_.getCache().remove(fqn, "test");
155
156       cache_.attach("/a", addr);
157       SpecialSerializedAddress result = (SpecialSerializedAddress) cache_.find("/a");
158       assertEquals(" ", addr, result);
159
160       // Remote remove
161
cache_.detach("/a");
162       assertNull("Object should be null ", cache_.find("/a"));
163
164       // Test serialization after detach
165
cache_.getCache().put(fqn, "test", addr);
166
167       SpecialSerializedAddress remote = (SpecialSerializedAddress)
168               cache1_.getCache().get(fqn, "test");
169       assertEquals("Name should be ", 95123, remote.getZip());
170    }
171
172    public void testPutArray1() throws Exception JavaDoc
173    {
174       log_.info("testPutArray1() ....");
175       long[] arr = new long[]{1, 2};
176       cache_.attach("array", arr);
177
178       long[] a2 = (long[]) cache1_.find("array");
179       assertEquals("arr 0", 1, a2[0]);
180    }
181
182    public void testPutArray2() throws Exception JavaDoc
183    {
184       log_.info("testPutArray2() ....");
185       Person p1 = new Person();
186       p1.setName("Ben");
187       p1.setAge(10);
188
189       Person p2 = new Person();
190       p2.setName("Joe");
191       p2.setAge(20);
192       Person[] arr = new Person[]{p1, p2};
193
194       cache_.attach("array", arr);
195
196       Person[] a2 = (Person[]) cache1_.find("array");
197       assertEquals("arr 0", "Ben", a2[0].getName());
198    }
199
200
201    /**
202     * JBCACHE-200.
203     *
204     * @throws Exception
205     */

206    public void testStateTransfer() throws Exception JavaDoc
207    {
208       log_.info("testStateTransfer() ....");
209       Person test = new Person();
210       test.setName("Ben");
211       test.setAge(10);
212       cache_.attach("/a", test);
213       Person result = (Person) cache_.find("/a");
214       assertEquals(" ", test, result);
215
216       // restart cache1_
217
cache1_.stop();
218       cache1_.getCache().removeNode(Fqn.fromString("/a"));
219       cache1_.start();
220       // Start from scratch for initial state transfer
221
Person remote = (Person) cache1_.find("/a");
222       assertEquals("Age should be ", 10, remote.getAge());
223    }
224
225    public static Test suite() throws Exception JavaDoc
226    {
227       return new TestSuite(NewReplicatedTest.class);
228    }
229
230
231    public static void main(String JavaDoc[] args) throws Exception JavaDoc
232    {
233       junit.textui.TestRunner.run(suite());
234    }
235
236 }
237
238
Popular Tags