KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.cache.pojo;
9
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.jboss.cache.pojo.test.Address;
16 import org.jboss.cache.pojo.test.Person;
17
18 import javax.naming.Context JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 /**
24  * Test for attach with existing pojo for bulkd remove for speed.
25  *
26  * @author Ben Wang
27  */

28
29 public class ReplicatedPutWithBulkRemoveTest extends TestCase
30 {
31    Log log_ = LogFactory.getLog(ReplicatedPutWithBulkRemoveTest.class);
32    PojoCache cache_;
33    PojoCache cache1_;
34
35    public ReplicatedPutWithBulkRemoveTest(String JavaDoc name)
36    {
37       super(name);
38    }
39
40    protected void setUp() throws Exception JavaDoc
41    {
42       super.setUp();
43       Properties JavaDoc prop = new Properties JavaDoc();
44       prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
45       boolean toStart = false;
46       cache_ = PojoCacheFactory.createCache("META-INF/replSync-service.xml", toStart);
47       cache1_ = PojoCacheFactory.createCache("META-INF/replSync-service.xml", toStart);
48       cache_.start();
49       cache1_.start();
50    }
51
52    protected void tearDown() throws Exception JavaDoc
53    {
54       super.tearDown();
55       cache_.stop();
56       cache1_.stop();
57    }
58
59    public void testPutPutLocal() throws Exception JavaDoc
60    {
61       log_.info("testPutPut() ....");
62       Person test = new Person();
63       test.setName("Ben");
64       test.setAge(10);
65       Address addr = new Address();
66       addr.setZip(95123);
67       addr.setCity("Sunnyvale");
68       test.setAddress(addr);
69       cache_.attach("/a", test);
70       Person result = (Person) cache_.find("/a");
71       assertEquals(" ", test, result);
72
73       Person joe = new Person();
74       joe.setName("joe");
75       joe.setAge(20);
76       cache_.attach("/a", joe);
77       Person joe1 = (Person) cache_.find("/a");
78       assertEquals("Age should be ", 20, joe1.getAge());
79
80       assertEquals("Age should be ", 10, result.getAge());
81       assertEquals("Zip should be ", 95123, result.getAddress().getZip());
82
83       // Try to re-use the pojo
84
cache_.attach("/a", test);
85       Person result1 = (Person) cache_.find("/a");
86       assertEquals("Zip should be ", 95123, result1.getAddress().getZip());
87
88    }
89
90    public void testPutPut() throws Exception JavaDoc
91    {
92       log_.info("testPutPut() ....");
93       Person test = new Person();
94       test.setName("Ben");
95       test.setAge(10);
96       Address addr = new Address();
97       addr.setZip(95123);
98       addr.setCity("Sunnyvale");
99       test.setAddress(addr);
100       cache_.attach("/a", test);
101       Person result = (Person) cache_.find("/a");
102       assertEquals(" ", test, result);
103
104       Person result1 = (Person) cache1_.find("/a");
105       assertEquals("Age should be ", 10, result1.getAge());
106       assertEquals("Zip should be ", 95123, result1.getAddress().getZip());
107
108       Person joe = new Person();
109       joe.setName("joe");
110       joe.setAge(20);
111       cache_.attach("/a", joe);
112       Person joe1 = (Person) cache_.find("/a");
113       assertEquals("Age should be ", 20, joe1.getAge());
114
115       assertEquals("Age should be ", 10, result.getAge());
116       assertEquals("Zip should be ", 95123, result.getAddress().getZip());
117    }
118
119    Address getAddress(String JavaDoc city)
120    {
121       Address addr = new Address();
122       addr.setCity(city);
123       addr.setZip(95123);
124       addr.setStreet("Sunnyvale");
125       return addr;
126    }
127
128    public void testPutCollection() throws Exception JavaDoc
129    {
130       log_.info("testPutCollection() ....");
131       Person p1 = new Person();
132       p1.setName("Ben");
133       p1.setAge(10);
134       p1.setAddress(getAddress("Sunnyvale"));
135
136       List JavaDoc<Address> lang = new ArrayList JavaDoc<Address>();
137       lang.add(getAddress("Taipei"));
138       lang.add(getAddress("Tainan"));
139       p1.setLanguages(lang);
140
141       cache_.attach("/a", p1);
142       Person result = (Person) cache_.find("/a");
143       assertEquals(" ", p1, result);
144
145       Person result1 = (Person) cache1_.find("/a");
146       assertEquals("Age should be ", 10, result1.getAge());
147       assertEquals("Zip should be ", 95123, result1.getAddress().getZip());
148
149       Person p2 = new Person();
150       p2.setName("joe");
151       p2.setAge(20);
152       cache_.attach("/a", p2);
153       Person joe1 = (Person) cache_.find("/a");
154       assertEquals("Age should be ", 20, joe1.getAge());
155
156       // put p1 again
157
cache_.attach("/a", p1);
158
159       // remove p1
160
Person p3 = (Person) cache_.detach("/a");
161       assertEquals("Zip should be ", 95123, p3.getAddress().getZip());
162
163       assertEquals("Age should be ", 10, result.getAge());
164       assertEquals("Zip should be ", 95123, result.getAddress().getZip());
165    }
166
167    public static Test suite() throws Exception JavaDoc
168    {
169       return new TestSuite(ReplicatedPutWithBulkRemoveTest.class);
170    }
171
172
173    public static void main(String JavaDoc[] args) throws Exception JavaDoc
174    {
175       junit.textui.TestRunner.run(ReplicatedPutWithBulkRemoveTest.suite());
176    }
177
178 }
179
Popular Tags