KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > aop > LocalAopTest


1 package org.jboss.cache.tests.aop;
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
10 import java.util.HashMap JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Set JavaDoc;
14
15 //import org.jboss.test.JBossTestCase;
16

17
18 /**
19  * LocalTestCase.java that uses standalone aop package.
20  * @author Ben Wang
21  */

22
23 public class LocalAopTest extends TestCase
24 {
25    // TODO No need to use the TreeCacheAopTester now.
26
// Category log = getLog();
27
Log log=LogFactory.getLog(LocalAopTest.class);
28    TreeCacheAopTester tester;
29
30
31    public LocalAopTest(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       tester = new TreeCacheAopTester(configFile);
42    }
43
44    protected void tearDown() throws Exception JavaDoc
45    {
46       super.tearDown();
47       tester.stop();
48       tester = null;
49    }
50
51 // public void testDummy() {}
52

53    public void testSetup()
54    {
55       log.info("testSetup() ....");
56       try {
57          tester.testSetup();
58       } catch (Exception JavaDoc ex) {
59          ex.printStackTrace();
60          fail("testSetup(): fails. " + ex.toString());
61       }
62    }
63
64    public void testSimple() throws Exception JavaDoc
65    {
66       log.info("testSimple() ....");
67       tester.createPerson("/person/test1", "Joe Black", 32);
68       assertEquals((Object JavaDoc) "Joe Black", (Object JavaDoc) tester.getName("/person/test1"));
69 // assertTrue(tester.getAge("/person/test1") == 32);
70
// tester.removePerson("/person/test1");
71
}
72
73    public void testMap() throws Exception JavaDoc
74    {
75       log.info("testMap() ....");
76       tester.createPerson("/person/test1", "Ben Wang", 40);
77       assertEquals((Object JavaDoc) "Ben Wang", (Object JavaDoc) tester.getName("/person/test1"));
78       Person ben = (Person)tester.getPerson("/person/test1");
79 // Map skills = new HashMap();
80
Map JavaDoc hobbies = ben.getHobbies();
81       if (hobbies == null) {
82          hobbies = new HashMap JavaDoc();
83          ben.setHobbies(hobbies);
84          // NB: it is neccessary to get hobbies again to get advised version
85
hobbies = ben.getHobbies();
86       }
87       hobbies.put("1", "English");
88       hobbies.put("2", "French");
89       if( !(hobbies instanceof ClassProxy)) {
90          fail("Hobbies is not an instance of ClassProxy");
91       }
92 // ben.setHobbies(skills);
93
// skills = ben.getHobbies();
94

95       hobbies = ben.getHobbies();
96       assertEquals("Hobbies size", 2, hobbies.size());
97       log.debug("Hobbies is " + hobbies.toString());
98    }
99
100    public void testModification() throws Exception JavaDoc
101    {
102       tester.createPerson("/person/test2", "Joe", 32);
103       tester.setName("/person/test2", "Joe Black");
104       assertEquals((Object JavaDoc) tester.getName("/person/test2"), (Object JavaDoc) "Joe Black");
105       tester.removePerson("/person/test2");
106    }
107
108    public void testRemove() throws Exception JavaDoc
109    {
110       tester.createPerson("/person/test3", "Joe", 32);
111       tester.removePerson("/person/test3");
112       try {
113          tester.getName("/person/test3");
114          fail("Object wasn't removed");
115       } catch (Exception JavaDoc e) {
116          // should be thrown
117
}
118    }
119
120    public void testDynamicRefSwapping() throws Exception JavaDoc
121    {
122       tester.createPerson("/person/test3", "Joe", 32);
123       Person person = (Person)tester.getPerson("/person/test3");
124       try {
125          person.setAge(30);
126          List JavaDoc med = person.getMedication();
127          assertNull("Medication should be null ", med);
128          person.setAge(60);
129          med = person.getMedication();
130          assertEquals("Medication ", (Object JavaDoc)"Lipitor", (Object JavaDoc)med.get(0));
131       } catch (Exception JavaDoc e) {
132          // should be thrown
133
}
134    }
135
136    public void testDependent() throws Exception JavaDoc
137    {
138       tester.createPerson("/person/test4", "Joe Black", 32);
139       tester.setCity("/person/test4", "Mannheim");
140       assertEquals((Object JavaDoc) tester.getCity("/person/test4"), (Object JavaDoc) "Mannheim");
141    }
142
143
144    public void testSerialization() throws Throwable JavaDoc
145    {
146
147       Person p = (Person) tester.testSerialization();
148 /*
149       if (p instanceof Advised)
150       {
151          InstanceAdvisor advisor = ((Advised) p)._getInstanceAdvisor();
152          Interceptor[] interceptors = advisor.getInterceptors();
153          for (int i = 0; i < interceptors.length; i++)
154          {
155             assertTrue("CacheInterceptor shouldn't be serialized",
156                 !(interceptors[i] instanceof CacheInterceptor));
157          }
158       }
159 */

160       assertEquals((Object JavaDoc) "Joe Black", (Object JavaDoc) p.getName());
161       assertEquals((Object JavaDoc) "Mannheim", (Object JavaDoc) p.getAddress().getCity());
162    }
163
164    public void testDeserialization() throws Throwable JavaDoc
165    {
166
167       Person p = new Person();
168       p.setName("test6");
169       tester.testDeserialization("/person/test6", p);
170       String JavaDoc name = tester.getName("/person/test6");
171       assertEquals((Object JavaDoc) "test6", (Object JavaDoc) name);
172    }
173
174    public void testMap2() throws Throwable JavaDoc
175    {
176       tester.createPerson("/person/test5", "Joe Black", 32);
177       tester.setHobby("/person/test5", "music", "guitar");
178       Object JavaDoc val = tester.getHobby("/person/test5", "music");
179       assertEquals("guitar", val);
180       tester.setHobby("/person/test5", "a", "b");
181       tester.getHobby("/person/test5", "a");
182       tester.printPerson("/person/test5");
183    }
184
185    public void testList() throws Throwable JavaDoc
186    {
187       tester.createPerson("/person/test6", "p6", 50);
188       tester.addLanguage("/person/test6", "German");
189       tester.addLanguage("/person/test6", "English");
190       tester.addLanguage("/person/test6", "French");
191       int size = tester.getLanguagesSize("/person/test6");
192       assertTrue(size == 3);
193       tester.printCache();
194       tester.addLanguage("/person/test6", "asdf");
195       tester.printCache();
196       tester.removeLanguage("/person/test6", "asdf");
197       tester.printCache();
198       size = tester.getLanguagesSize("/person/test6");
199       // assertTrue(size==3);
200
for (int i = 0; i < size; i++) {
201          log.debug("" + i + " : " + tester.getLanguage("/person/test6", i));
202       }
203       assertEquals(new Integer JavaDoc(3), new Integer JavaDoc(size));
204       String JavaDoc language = (String JavaDoc) tester.getLanguage("/person/test6", 1);
205       assertEquals((Object JavaDoc) "English", (Object JavaDoc) language);
206    }
207
208    public void testSet() throws Throwable JavaDoc
209    {
210       tester.createPerson("/person/test7", "p7", 27);
211       tester.addSkill("/person/test7", "Java");
212       tester.addSkill("/person/test7", "Java");
213       tester.addSkill("/person/test7", "Java");
214       Set JavaDoc skills = tester.getSkills("/person/test7");
215       assertEquals(new Integer JavaDoc(1), new Integer JavaDoc(skills.size()));
216       tester.removeSkill("/person/test7", "Java");
217       skills = tester.getSkills("/person/test7");
218       assertTrue(skills.isEmpty());
219       tester.addSkill("/person/test7", "Java");
220       tester.addSkill("/person/test7", "J2EE");
221       tester.addSkill("/person/test7", "JBoss");
222       skills = tester.getSkills("/person/test7");
223       assertEquals(new Integer JavaDoc(3), new Integer JavaDoc(skills.size()));
224    }
225
226    public void testFieldSynchronization() throws Throwable JavaDoc
227    {
228       String JavaDoc key = "/person/test8";
229       tester.createPerson(key, "p8", 8);
230       assertEquals(tester.getName(key), tester.getFieldValue(key, "name"));
231       assertEquals(new Integer JavaDoc(tester.getAge(key)), tester.getFieldValue(key, "age"));
232       tester.setName(key, "p8x");
233       assertEquals(tester.getName(key), tester.getFieldValue(key, "name"));
234       tester.setAge(key, 18);
235       assertEquals(new Integer JavaDoc(tester.getAge(key)), tester.getFieldValue(key, "age"));
236    }
237
238    public void testInheritance() throws Exception JavaDoc
239    {
240       tester.createStudent("/person/joe", "Joe", 32, "Senior");
241       tester.setName("/person/joe", "Joe Black");
242       assertEquals((Object JavaDoc) tester.getName("/person/joe"), (Object JavaDoc) "Joe Black");
243       tester.setYear("/person/joe", "Junior");
244       assertEquals((Object JavaDoc) tester.getYear("/person/joe"), (Object JavaDoc) "Junior");
245       tester.removePerson("/person/joe");
246    }
247
248
249    public static Test suite() throws Exception JavaDoc
250    {
251       return new TestSuite(LocalAopTest.class);
252    }
253
254
255    public static void main(String JavaDoc[] args) throws Exception JavaDoc
256    {
257       junit.textui.TestRunner.run(suite());
258    }
259
260 }
261
262
Popular Tags