KickJava   Java API By Example, From Geeks To Geeks.

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


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.aop.proxy.ClassProxy;
9 import org.jboss.cache.pojo.test.Address;
10 import org.jboss.cache.pojo.test.Person;
11 import org.jboss.cache.pojo.test.Student;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 /**
21  * Basic PojoCache test case.
22  *
23  * @author Ben Wang
24  */

25
26 public class LocalTest extends TestCase
27 {
28    Log log = LogFactory.getLog(LocalTest.class);
29    PojoCache cache_;
30
31    public LocalTest(String JavaDoc name)
32    {
33       super(name);
34    }
35
36    protected void setUp() throws Exception JavaDoc
37    {
38       super.setUp();
39       log.info("setUp() ....");
40       String JavaDoc configFile = "META-INF/local-service.xml";
41       boolean toStart = false;
42       cache_ = PojoCacheFactory.createCache(configFile, toStart);
43       cache_.start();
44    }
45
46    protected void tearDown() throws Exception JavaDoc
47    {
48       super.tearDown();
49       cache_.stop();
50    }
51
52    // public void testDummy() {}
53

54    private Person createPerson(String JavaDoc id, String JavaDoc name, int age)
55    {
56       Person p = new Person();
57       p.setName(name);
58       p.setAge(age);
59       Address add = new Address();
60       add.setZip(95123);
61       add.setCity("San Jose");
62       p.setAddress(add);
63       cache_.attach(id, p);
64       return p;
65    }
66
67    private Student createStudent(String JavaDoc id, String JavaDoc name, int age, String JavaDoc grade)
68    {
69       Student p = new Student();
70       p.setName(name);
71       p.setAge(age);
72       p.setYear(grade);
73       Address add = new Address();
74       add.setZip(95123);
75       add.setCity("San Jose");
76       p.setAddress(add);
77       cache_.attach(id, p);
78       return p;
79    }
80
81    public void testSimple() throws Exception JavaDoc
82    {
83       log.info("testSimple() ....");
84       Person p = createPerson("/person/test1", "Joe Black", 32);
85       assertEquals((Object JavaDoc) "Joe Black", p.getName());
86    }
87
88    public void testSimpleType() throws Exception JavaDoc
89    {
90       log.info("testSimpleType() ....");
91       String JavaDoc test = "This is a test";
92       cache_.attach("test", test);
93
94       assertEquals("String ", test, cache_.find("test"));
95    }
96
97    public void testModification() throws Exception JavaDoc
98    {
99       Person joe = createPerson("/person/test2", "Joe", 32);
100       joe.setName("Joe Black");
101       assertEquals(joe.getName(), "Joe Black");
102       cache_.detach("/person/test2");
103    }
104
105    public void testRemove() throws Exception JavaDoc
106    {
107       Person joe = createPerson("/person/test3", "Joe", 32);
108       cache_.detach("/person/test3");
109       // assertNull("Underlying cache content " + cache_.getCache().printDetails(),
110
// cache_.getCache().get("/person/test3"));
111
}
112
113    public void testDynamicRefSwapping() throws Exception JavaDoc
114    {
115       Person person = createPerson("/person/test3", "Joe", 32);
116       try
117       {
118          person.setAge(30);
119          List JavaDoc med = person.getMedication();
120          assertNull("Medication should be null ", med);
121          person.setAge(60);
122          med = person.getMedication();
123          assertEquals("Medication ", "Lipitor", med.get(0));
124       }
125       catch (Exception JavaDoc e)
126       {
127          // should be thrown
128
}
129    }
130
131    public void testMap() throws Exception JavaDoc
132    {
133       log.info("testMap() ....");
134       Person ben = createPerson("/person/test1", "Ben Wang", 40);
135       assertEquals((Object JavaDoc) "Ben Wang", ben.getName());
136       Map JavaDoc<String JavaDoc, String JavaDoc> hobbies = ben.getHobbies();
137       if (hobbies == null)
138       {
139          hobbies = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
140          ben.setHobbies(hobbies);
141          // NB: it is neccessary to get hobbies again to get advised version
142
hobbies = ben.getHobbies();
143       }
144       hobbies.put("1", "English");
145       hobbies.put("2", "French");
146       if (!(hobbies instanceof ClassProxy))
147       {
148          fail("Hobbies is not an instance of ClassProxy");
149       }
150
151       hobbies = ben.getHobbies();
152       assertEquals("Hobbies size", 2, hobbies.size());
153       log.debug("Hobbies is " + hobbies.toString());
154    }
155
156    public void testMapDetachAttach() throws Exception JavaDoc
157    {
158       log.info("testMapDetachATtach() ....");
159       Person ben = createPerson("/person/test1", "Ben Wang", 40);
160       assertEquals((Object JavaDoc) "Ben Wang", ben.getName());
161       Map JavaDoc<String JavaDoc, String JavaDoc> hobbies = ben.getHobbies();
162       if (hobbies == null)
163       {
164          hobbies = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
165          ben.setHobbies(hobbies);
166          // NB: it is neccessary to get hobbies again to get advised version
167
hobbies = ben.getHobbies();
168       }
169       hobbies.put("1", "English");
170       hobbies.put("2", "French");
171       if (!(hobbies instanceof ClassProxy))
172       {
173          fail("Hobbies is not an instance of ClassProxy");
174       }
175
176       hobbies = ben.getHobbies();
177       assertEquals("Hobbies size", 2, hobbies.size());
178       log.debug("Hobbies is " + hobbies.toString());
179
180       cache_.detach("/person/test1");
181       hobbies = ben.getHobbies();
182       if ((hobbies instanceof ClassProxy))
183       {
184          fail("Hobbies should not be an instance of ClassProxy");
185       }
186
187       cache_.attach("/person/1", ben);
188
189    }
190
191    public void testMap2() throws Throwable JavaDoc
192    {
193       Person joe = createPerson("/person/test5", "Joe Black", 32);
194       Map JavaDoc<String JavaDoc, String JavaDoc> hobby = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
195       hobby.put("music", "guitar");
196       joe.setHobbies(hobby);
197       Object JavaDoc val = joe.getHobbies().get("music");
198       assertEquals("guitar", val);
199       hobby = joe.getHobbies();
200       hobby.put("novel", "English");
201       assertEquals("Size of map ", 2, joe.getHobbies().size());
202    }
203
204    public void testList() throws Throwable JavaDoc
205    {
206       Person joe = createPerson("/person/test6", "Joe", 50);
207       List JavaDoc<String JavaDoc> language = new ArrayList JavaDoc<String JavaDoc>();
208       language.add("German");
209       language.add("English");
210       language.add("French");
211       joe.setLanguages(language);
212
213       assertEquals("Size of language ", 3, joe.getLanguages().size());
214       language = joe.getLanguages();
215       language.add("Mandarin");
216       language.add("Taiwanese");
217       language.add("Haka");
218       assertEquals("Size of language ", 6, joe.getLanguages().size());
219
220       String JavaDoc English = language.get(1);
221       assertEquals((Object JavaDoc) "English", English);
222       cache_.detach("/person/test6");
223    }
224
225    public void testListDetachAndAttach() throws Throwable JavaDoc
226    {
227       String JavaDoc id = "/person/test6";
228       Person joe = new Person();
229       List JavaDoc<String JavaDoc> language = new ArrayList JavaDoc<String JavaDoc>();
230       language.add("German");
231       language.add("English");
232       language.add("French");
233       joe.setLanguages(language);
234
235       cache_.attach(id, joe);
236       cache_.detach(id);
237       // TreeCacheProxyImpl impl = (TreeCacheProxyImpl)cache_.getCache();
238
// System.out.println("*** Here I ");
239
// System.out.println(impl.printDetails());
240

241       joe.getAge();
242       cache_.attach(id, joe);
243    }
244
245    public void testListDetachAndAttach2() throws Throwable JavaDoc
246    {
247       String JavaDoc id = "/person/test6";
248       Person joe = createPerson(id, "Joe", 50);
249       List JavaDoc<String JavaDoc> language = new ArrayList JavaDoc<String JavaDoc>();
250       language.add("German");
251       language.add("English");
252       language.add("French");
253       joe.setLanguages(language);
254
255       assertEquals("Size of language ", 3, joe.getLanguages().size());
256       language = joe.getLanguages();
257       language.add("Mandarin");
258       language.add("Taiwanese");
259       language.add("Haka");
260       assertEquals("Size of language ", 6, joe.getLanguages().size());
261
262       String JavaDoc English = language.get(1);
263       assertEquals((Object JavaDoc) "English", English);
264
265       if (!(language instanceof ClassProxy))
266       {
267          fail("Language is not an instance of ClassProxy");
268       }
269
270       cache_.detach(id);
271       joe.getAge();
272       language = joe.getLanguages();
273       if ((language instanceof ClassProxy))
274       {
275          fail("Language is an instance of ClassProxy");
276       }
277
278       cache_.attach(id, joe);
279    }
280
281    public void testSet() throws Throwable JavaDoc
282    {
283       Person joe = createPerson("/person/test7", "Joe", 27);
284       Set JavaDoc<String JavaDoc> skill = new HashSet JavaDoc<String JavaDoc>();
285       skill.add("Java");
286       skill.add("Java");
287       skill.add("Java");
288       joe.setSkills(skill);
289       skill = joe.getSkills();
290       assertEquals("Size of skill ", 1, skill.size());
291
292       skill.remove("Java");
293       assertTrue(skill.isEmpty());
294       skill.add("Java");
295       skill.add("J2EE");
296       skill.add("JBoss");
297       assertEquals(new Integer JavaDoc(3), new Integer JavaDoc(skill.size()));
298    }
299
300    public void testSetDetachAttach() throws Throwable JavaDoc
301    {
302       String JavaDoc id = "/person/test7";
303       Person joe = createPerson(id, "Joe", 27);
304       Set JavaDoc<String JavaDoc> skill = new HashSet JavaDoc<String JavaDoc>();
305       skill.add("Java");
306       skill.add("Java");
307       skill.add("Java");
308       joe.setSkills(skill);
309       skill = joe.getSkills();
310       assertEquals("Size of skill ", 1, skill.size());
311
312       skill.remove("Java");
313       assertTrue(skill.isEmpty());
314       skill.add("Java");
315       skill.add("J2EE");
316       skill.add("JBoss");
317       assertEquals(new Integer JavaDoc(3), new Integer JavaDoc(skill.size()));
318
319       if (!(skill instanceof ClassProxy))
320       {
321          fail("Skill is not an instance of ClassProxy");
322       }
323
324       cache_.detach(id);
325       joe.getAge();
326       skill = joe.getSkills();
327       if ((skill instanceof ClassProxy))
328       {
329          fail("Skill is an instance of ClassProxy");
330       }
331
332       cache_.attach(id, joe);
333    }
334
335    public void testInheritance() throws Exception JavaDoc
336    {
337       Student joe = createStudent("/person/joe", "Joe", 32, "Senior");
338       joe.setName("Joe Black");
339       assertEquals(joe.getName(), "Joe Black");
340       joe.setYear("Junior");
341       assertEquals(joe.getYear(), "Junior");
342       cache_.detach("/person/joe");
343    }
344
345
346    public static Test suite() throws Exception JavaDoc
347    {
348       return new TestSuite(LocalTest.class);
349    }
350
351
352    public static void main(String JavaDoc[] args) throws Exception JavaDoc
353    {
354       junit.textui.TestRunner.run(suite());
355    }
356
357 }
358
359
Popular Tags