KickJava   Java API By Example, From Geeks To Geeks.

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


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.Fqn;
10 import org.jboss.cache.pojo.test.Person;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Map JavaDoc;
16
17 /**
18  * Additional basic tests
19  *
20  * @author Ben Wang
21  */

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

51    public void XtestBadFqn() throws Exception JavaDoc
52    {
53       log_.info("testBadFqn() ....");
54       Person test = new Person();
55       test.setName("Ben");
56       test.setAge(10);
57       cache_.attach("/a", test);
58       Person result = (Person) cache_.detach("/a");
59       assertEquals(" ", test, result);
60       result.setAge(20);
61
62       try
63       {
64          cache_.attach(InternalConstant.JBOSS_INTERNAL_STRING, test);
65          fail("putObject under JBoss_Internal should fail");
66       }
67       catch (IllegalArgumentException JavaDoc iex)
68       {
69          // ok
70
}
71
72       try
73       {
74          cache_.detach(InternalConstant.JBOSS_INTERNAL_STRING);
75          fail("putObject under JBoss_Internal should fail");
76       }
77       catch (IllegalArgumentException JavaDoc iex)
78       {
79          // ok
80
}
81    }
82
83    public void testPutRemove() throws Exception JavaDoc
84    {
85       log_.info("testPutRemove() ....");
86       Person test = new Person();
87       test.setName("Ben");
88       test.setAge(10);
89       cache_.attach("/a", test);
90       Person result = (Person) cache_.find("/a");
91       assertEquals(" ", test, result);
92       result.setAge(20);
93       cache_.detach("/a");
94       assertNull("Object should be null ", cache_.find("/a"));
95       assertEquals("Age should be updated as ", 20, test.getAge());
96    }
97
98    public void testPutRemoveNodeExistence() throws Exception JavaDoc
99    {
100       log_.info("testPutRemove() ....");
101       Person test = new Person();
102       test.setName("Ben");
103       test.setAge(10);
104       cache_.attach("person", test);
105       Person result = (Person) cache_.find("person");
106       assertEquals(" ", test, result);
107       result.setAge(20);
108       cache_.detach("person");
109       assertNull("Object should be null ", cache_.find("person"));
110       assertEquals("Age should be updated as ", 20, test.getAge());
111
112       assertNull("DataNode should not exisit ", cache_.getCache().getRoot().getChild(Fqn.fromString("person")));
113    }
114
115    public void testRemoveProxyList() throws Exception JavaDoc
116    {
117       log_.info("testRemoveProxyList() ....");
118       Person test = new Person();
119       test.setName("Ben");
120       test.setAge(10);
121       ArrayList JavaDoc list = new ArrayList JavaDoc();
122       list.add("English");
123       list.add("Taiwanese");
124       test.setLanguages(list);
125       cache_.attach("/a", test);
126       Person result = (Person) cache_.find("/a");
127       assertEquals(" ", test, result);
128
129       assertTrue("Instance of proxyclass ", result.getLanguages() instanceof ClassProxy);
130
131       Person r1 = (Person) cache_.detach("/a");
132
133       assertEquals("Same instance ", result, r1);
134       assertFalse("Instance of proxyclass ", result.getLanguages() instanceof ClassProxy);
135       assertEquals("Same Collection instance", list, result.getLanguages());
136    }
137
138    public void testRemoveProxySet() throws Exception JavaDoc
139    {
140       log_.info("testRemoveProxySet() ....");
141       Person test = new Person();
142       test.setName("Ben");
143       test.setAge(10);
144       HashSet JavaDoc set = new HashSet JavaDoc();
145       set.add("Golf");
146       set.add("Cooking");
147       test.setSkills(set);
148       cache_.attach("/a", test);
149       Person result = (Person) cache_.find("/a");
150       assertEquals(" ", test, result);
151
152       assertTrue("Instance of proxyclass ", result.getSkills() instanceof ClassProxy);
153
154       Person r1 = (Person) cache_.detach("/a");
155
156       assertEquals("Same instance ", result, r1);
157       assertFalse("Instance of proxyclass ", result.getSkills() instanceof ClassProxy);
158       assertEquals("Same Collection instance", set, result.getSkills());
159    }
160
161    public void testRemoveProxyMap() throws Exception JavaDoc
162    {
163       log_.info("testRemoveProxyMap() ....");
164       Person test = new Person();
165       test.setName("Ben");
166       test.setAge(10);
167
168       HashMap JavaDoc map = new HashMap JavaDoc();
169       map.put("1", "Golf");
170       map.put("2", "Surfing");
171       test.setHobbies(map);
172
173       cache_.attach("/a", test);
174       Person result = (Person) cache_.find("/a");
175       assertEquals(" ", test, result);
176
177       assertTrue("Instance of proxyclass ", result.getHobbies() instanceof ClassProxy);
178
179       Person r1 = (Person) cache_.detach("/a");
180
181       assertEquals("Same instance ", result, r1);
182       assertFalse("Instance of proxyclass ", result.getHobbies() instanceof ClassProxy);
183       assertEquals("Same Collection instance", map, result.getHobbies());
184    }
185
186    public void testFindObjects() throws Exception JavaDoc
187    {
188       log_.info("testFindObjects() ....");
189       Map JavaDoc map = cache_.findAll("/");
190       assertEquals("Objects size should be ", 0, map.size());
191       Person ben = new Person();
192       ben.setName("Ben");
193       ben.setAge(10);
194       cache_.attach("/a/b/c", ben);
195       cache_.attach("/e", ben); // multiple keys, same pojo
196
Person joe = new Person();
197       joe.setName("Joe");
198       joe.setAge(10);
199       cache_.attach("/f/joe", joe);
200       map = cache_.findAll("/");
201       assertEquals("Objects size should be ", 3, map.size());
202
203       map = cache_.findAll("/a");
204       assertEquals("Objects size should be ", 1, map.size());
205       cache_.detach("/e");
206       map = cache_.findAll("/");
207       assertEquals("Objects size should be ", 2, map.size());
208
209       map = cache_.findAll(null); // should everything.
210
assertEquals("Objects size should be ", 2, map.size());
211    }
212
213    public static Test suite() throws Exception JavaDoc
214    {
215       return new TestSuite(NewLocalTest.class);
216    }
217
218
219    public static void main(String JavaDoc[] args) throws Exception JavaDoc
220    {
221       junit.textui.TestRunner.run(suite());
222    }
223
224 }
225
226
Popular Tags