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.impl.InternalConstant; |
11 |
| import org.jboss.cache.pojo.test.Person; |
12 |
| |
13 |
| import java.util.ArrayList; |
14 |
| import java.util.HashMap; |
15 |
| import java.util.HashSet; |
16 |
| import java.util.Map; |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| public class NewLocalTest extends TestCase |
25 |
| { |
26 |
| Log log_ = LogFactory.getLog(NewLocalTest.class); |
27 |
| PojoCache cache_; |
28 |
| |
29 |
6
| public NewLocalTest(String name)
|
30 |
| { |
31 |
6
| super(name);
|
32 |
| } |
33 |
| |
34 |
6
| protected void setUp() throws Exception
|
35 |
| { |
36 |
6
| super.setUp();
|
37 |
6
| log_.info("setUp() ....");
|
38 |
6
| String configFile = "META-INF/local-service.xml";
|
39 |
6
| boolean toStart = false;
|
40 |
6
| cache_ = PojoCacheFactory.createCache(configFile, toStart);
|
41 |
6
| cache_.start();
|
42 |
| } |
43 |
| |
44 |
6
| protected void tearDown() throws Exception
|
45 |
| { |
46 |
6
| super.tearDown();
|
47 |
6
| cache_.stop();
|
48 |
| } |
49 |
| |
50 |
| |
51 |
| |
52 |
0
| public void XtestBadFqn() throws Exception
|
53 |
| { |
54 |
0
| log_.info("testBadFqn() ....");
|
55 |
0
| Person test = new Person();
|
56 |
0
| test.setName("Ben");
|
57 |
0
| test.setAge(10);
|
58 |
0
| cache_.attach("/a", test);
|
59 |
0
| Person result = (Person) cache_.detach("/a");
|
60 |
0
| assertEquals(" ", test, result);
|
61 |
0
| result.setAge(20);
|
62 |
| |
63 |
0
| try
|
64 |
| { |
65 |
0
| cache_.attach(InternalConstant.JBOSS_INTERNAL_STRING, test);
|
66 |
0
| fail("putObject under JBoss_Internal should fail");
|
67 |
| } |
68 |
| catch (IllegalArgumentException iex) |
69 |
| { |
70 |
| |
71 |
| } |
72 |
| |
73 |
0
| try
|
74 |
| { |
75 |
0
| cache_.detach(InternalConstant.JBOSS_INTERNAL_STRING);
|
76 |
0
| fail("putObject under JBoss_Internal should fail");
|
77 |
| } |
78 |
| catch (IllegalArgumentException iex) |
79 |
| { |
80 |
| |
81 |
| } |
82 |
| } |
83 |
| |
84 |
1
| public void testPutRemove() throws Exception
|
85 |
| { |
86 |
1
| log_.info("testPutRemove() ....");
|
87 |
1
| Person test = new Person();
|
88 |
1
| test.setName("Ben");
|
89 |
1
| test.setAge(10);
|
90 |
1
| cache_.attach("/a", test);
|
91 |
1
| Person result = (Person) cache_.find("/a");
|
92 |
1
| assertEquals(" ", test, result);
|
93 |
1
| result.setAge(20);
|
94 |
1
| cache_.detach("/a");
|
95 |
1
| assertNull("Object should be null ", cache_.find("/a"));
|
96 |
1
| assertEquals("Age should be updated as ", 20, test.getAge());
|
97 |
| } |
98 |
| |
99 |
1
| public void testPutRemoveNodeExistence() throws Exception
|
100 |
| { |
101 |
1
| log_.info("testPutRemove() ....");
|
102 |
1
| Person test = new Person();
|
103 |
1
| test.setName("Ben");
|
104 |
1
| test.setAge(10);
|
105 |
1
| cache_.attach("person", test);
|
106 |
1
| Person result = (Person) cache_.find("person");
|
107 |
1
| assertEquals(" ", test, result);
|
108 |
1
| result.setAge(20);
|
109 |
1
| cache_.detach("person");
|
110 |
1
| assertNull("Object should be null ", cache_.find("person"));
|
111 |
1
| assertEquals("Age should be updated as ", 20, test.getAge());
|
112 |
| |
113 |
1
| assertNull("DataNode should not exisit ", cache_.getCache().getRoot().getChild(Fqn.fromString("person")));
|
114 |
| } |
115 |
| |
116 |
1
| public void testRemoveProxyList() throws Exception
|
117 |
| { |
118 |
1
| log_.info("testRemoveProxyList() ....");
|
119 |
1
| Person test = new Person();
|
120 |
1
| test.setName("Ben");
|
121 |
1
| test.setAge(10);
|
122 |
1
| ArrayList list = new ArrayList();
|
123 |
1
| list.add("English");
|
124 |
1
| list.add("Taiwanese");
|
125 |
1
| test.setLanguages(list);
|
126 |
1
| cache_.attach("/a", test);
|
127 |
1
| Person result = (Person) cache_.find("/a");
|
128 |
1
| assertEquals(" ", test, result);
|
129 |
| |
130 |
1
| assertTrue("Instance of proxyclass ", result.getLanguages() instanceof ClassProxy);
|
131 |
| |
132 |
1
| Person r1 = (Person) cache_.detach("/a");
|
133 |
| |
134 |
1
| assertEquals("Same instance ", result, r1);
|
135 |
1
| assertFalse("Instance of proxyclass ", result.getLanguages() instanceof ClassProxy);
|
136 |
1
| assertEquals("Same Collection instance", list, result.getLanguages());
|
137 |
| } |
138 |
| |
139 |
1
| public void testRemoveProxySet() throws Exception
|
140 |
| { |
141 |
1
| log_.info("testRemoveProxySet() ....");
|
142 |
1
| Person test = new Person();
|
143 |
1
| test.setName("Ben");
|
144 |
1
| test.setAge(10);
|
145 |
1
| HashSet set = new HashSet();
|
146 |
1
| set.add("Golf");
|
147 |
1
| set.add("Cooking");
|
148 |
1
| test.setSkills(set);
|
149 |
1
| cache_.attach("/a", test);
|
150 |
1
| Person result = (Person) cache_.find("/a");
|
151 |
1
| assertEquals(" ", test, result);
|
152 |
| |
153 |
1
| assertTrue("Instance of proxyclass ", result.getSkills() instanceof ClassProxy);
|
154 |
| |
155 |
1
| Person r1 = (Person) cache_.detach("/a");
|
156 |
| |
157 |
1
| assertEquals("Same instance ", result, r1);
|
158 |
1
| assertFalse("Instance of proxyclass ", result.getSkills() instanceof ClassProxy);
|
159 |
1
| assertEquals("Same Collection instance", set, result.getSkills());
|
160 |
| } |
161 |
| |
162 |
1
| public void testRemoveProxyMap() throws Exception
|
163 |
| { |
164 |
1
| log_.info("testRemoveProxyMap() ....");
|
165 |
1
| Person test = new Person();
|
166 |
1
| test.setName("Ben");
|
167 |
1
| test.setAge(10);
|
168 |
| |
169 |
1
| HashMap map = new HashMap();
|
170 |
1
| map.put("1", "Golf");
|
171 |
1
| map.put("2", "Surfing");
|
172 |
1
| test.setHobbies(map);
|
173 |
| |
174 |
1
| cache_.attach("/a", test);
|
175 |
1
| Person result = (Person) cache_.find("/a");
|
176 |
1
| assertEquals(" ", test, result);
|
177 |
| |
178 |
1
| assertTrue("Instance of proxyclass ", result.getHobbies() instanceof ClassProxy);
|
179 |
| |
180 |
1
| Person r1 = (Person) cache_.detach("/a");
|
181 |
| |
182 |
1
| assertEquals("Same instance ", result, r1);
|
183 |
1
| assertFalse("Instance of proxyclass ", result.getHobbies() instanceof ClassProxy);
|
184 |
1
| assertEquals("Same Collection instance", map, result.getHobbies());
|
185 |
| } |
186 |
| |
187 |
1
| public void testFindObjects() throws Exception
|
188 |
| { |
189 |
1
| log_.info("testFindObjects() ....");
|
190 |
1
| Map map = cache_.findAll("/");
|
191 |
1
| assertEquals("Objects size should be ", 0, map.size());
|
192 |
1
| Person ben = new Person();
|
193 |
1
| ben.setName("Ben");
|
194 |
1
| ben.setAge(10);
|
195 |
1
| cache_.attach("/a/b/c", ben);
|
196 |
1
| cache_.attach("/e", ben);
|
197 |
1
| Person joe = new Person();
|
198 |
1
| joe.setName("Joe");
|
199 |
1
| joe.setAge(10);
|
200 |
1
| cache_.attach("/f/joe", joe);
|
201 |
1
| map = cache_.findAll("/");
|
202 |
1
| assertEquals("Objects size should be ", 3, map.size());
|
203 |
| |
204 |
1
| map = cache_.findAll("/a");
|
205 |
1
| assertEquals("Objects size should be ", 1, map.size());
|
206 |
1
| cache_.detach("/e");
|
207 |
1
| map = cache_.findAll("/");
|
208 |
1
| assertEquals("Objects size should be ", 2, map.size());
|
209 |
| |
210 |
1
| map = cache_.findAll(null);
|
211 |
1
| assertEquals("Objects size should be ", 2, map.size());
|
212 |
| } |
213 |
| |
214 |
1
| public static Test suite() throws Exception
|
215 |
| { |
216 |
1
| return new TestSuite(NewLocalTest.class);
|
217 |
| } |
218 |
| |
219 |
| |
220 |
0
| public static void main(String[] args) throws Exception
|
221 |
| { |
222 |
0
| junit.textui.TestRunner.run(suite());
|
223 |
| } |
224 |
| |
225 |
| } |
226 |
| |