1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| package org.jboss.cache.pojo.notification; |
23 |
| |
24 |
| import java.util.ArrayList; |
25 |
| import java.util.List; |
26 |
| |
27 |
| import junit.framework.Test; |
28 |
| import junit.framework.TestCase; |
29 |
| import junit.framework.TestSuite; |
30 |
| |
31 |
| import org.jboss.cache.pojo.PojoCache; |
32 |
| import org.jboss.cache.pojo.PojoCacheFactory; |
33 |
| import org.jboss.cache.pojo.notification.event.AttachedEvent; |
34 |
| import org.jboss.cache.pojo.notification.event.DetachedEvent; |
35 |
| import org.jboss.cache.pojo.notification.event.ListModifiedEvent; |
36 |
| import org.jboss.cache.pojo.notification.event.Event; |
37 |
| import org.jboss.cache.pojo.test.Person; |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| public class ListTest extends TestCase |
47 |
| { |
48 |
| protected PojoCache cache; |
49 |
| protected Listener listener = new Listener(); |
50 |
| |
51 |
20
| public ListTest(String name)
|
52 |
| { |
53 |
20
| super(name);
|
54 |
| } |
55 |
| |
56 |
20
| protected void setUp() throws Exception
|
57 |
| { |
58 |
20
| super.setUp();
|
59 |
20
| String configFile = "META-INF/local-service.xml";
|
60 |
20
| boolean toStart = false;
|
61 |
20
| cache = PojoCacheFactory.createCache(configFile, toStart);
|
62 |
20
| cache.start();
|
63 |
20
| cache.addListener(listener);
|
64 |
| } |
65 |
| |
66 |
20
| protected void tearDown() throws Exception
|
67 |
| { |
68 |
20
| super.tearDown();
|
69 |
20
| cache.stop();
|
70 |
| } |
71 |
| |
72 |
120
| private <T extends Event> T takeNotification(Class<T> clazz)
|
73 |
| { |
74 |
120
| T notification = listener.take(clazz);
|
75 |
120
| verifyNotification(notification);
|
76 |
| |
77 |
120
| return notification;
|
78 |
| } |
79 |
| |
80 |
24
| protected void verifyNotification(Event notification)
|
81 |
| { |
82 |
24
| assertSame(cache, notification.getContext().getPojoCache());
|
83 |
24
| assertEquals(true, notification.isLocal());
|
84 |
| } |
85 |
| |
86 |
5
| public void testListAddNotification() throws Exception
|
87 |
| { |
88 |
5
| final String test1 = "test1";
|
89 |
5
| final String test2 = "test2";
|
90 |
| |
91 |
5
| List<String> list = new ArrayList<String>();
|
92 |
5
| list.add(test1);
|
93 |
5
| list.add(test2);
|
94 |
5
| cache.attach("a", list);
|
95 |
5
| list = (List) cache.find("a");
|
96 |
| |
97 |
| |
98 |
5
| AttachedEvent attach = (AttachedEvent) takeNotification(AttachedEvent.class);
|
99 |
5
| assertEquals(test1, attach.getSource());
|
100 |
| |
101 |
| |
102 |
5
| ListModifiedEvent modify = takeNotification(ListModifiedEvent.class);
|
103 |
5
| assertEquals(ListModifiedEvent.Operation.ADD, modify.getOperation());
|
104 |
5
| assertEquals(test1, modify.getValue());
|
105 |
5
| assertEquals(0, modify.getIndex());
|
106 |
| |
107 |
| |
108 |
5
| attach = takeNotification(AttachedEvent.class);
|
109 |
5
| assertEquals(test2, attach.getSource());
|
110 |
| |
111 |
| |
112 |
5
| modify = takeNotification(ListModifiedEvent.class);
|
113 |
5
| assertEquals(ListModifiedEvent.Operation.ADD, modify.getOperation());
|
114 |
5
| assertEquals(test2, modify.getValue());
|
115 |
5
| assertEquals(1, modify.getIndex());
|
116 |
| |
117 |
| |
118 |
5
| attach = takeNotification(AttachedEvent.class);
|
119 |
5
| assertEquals(list, attach.getSource());
|
120 |
| |
121 |
| } |
122 |
| |
123 |
5
| public void testListSetNotification() throws Exception
|
124 |
| { |
125 |
5
| final String test1 = "test1";
|
126 |
5
| final String test2 = "test2";
|
127 |
| |
128 |
5
| List<String> list = new ArrayList<String>();
|
129 |
5
| list.add(test1);
|
130 |
5
| cache.attach("a", list);
|
131 |
5
| list = (List) cache.find("a");
|
132 |
5
| list.set(0, test2);
|
133 |
| |
134 |
| |
135 |
5
| AttachedEvent attach = takeNotification(AttachedEvent.class);
|
136 |
5
| assertEquals(test1, attach.getSource());
|
137 |
| |
138 |
| |
139 |
5
| ListModifiedEvent modify = takeNotification(ListModifiedEvent.class);
|
140 |
5
| assertEquals(ListModifiedEvent.Operation.ADD, modify.getOperation());
|
141 |
5
| assertEquals(test1, modify.getValue());
|
142 |
5
| assertEquals(0, modify.getIndex());
|
143 |
| |
144 |
| |
145 |
5
| attach = takeNotification(AttachedEvent.class);
|
146 |
5
| assertEquals(list, attach.getSource());
|
147 |
| |
148 |
| |
149 |
5
| DetachedEvent detach = takeNotification(DetachedEvent.class);
|
150 |
5
| assertEquals(test1, detach.getSource());
|
151 |
| |
152 |
| |
153 |
5
| attach = takeNotification(AttachedEvent.class);
|
154 |
5
| assertEquals(test2, attach.getSource());
|
155 |
| |
156 |
| |
157 |
5
| modify = takeNotification(ListModifiedEvent.class);
|
158 |
5
| assertEquals(ListModifiedEvent.Operation.SET, modify.getOperation());
|
159 |
5
| assertEquals(test2, modify.getValue());
|
160 |
5
| assertEquals(0, modify.getIndex());
|
161 |
| } |
162 |
| |
163 |
5
| public void testListRemoveNotification() throws Exception
|
164 |
| { |
165 |
5
| final String test1 = "test1";
|
166 |
5
| final String test2 = "test2";
|
167 |
| |
168 |
5
| List<String> list = new ArrayList<String>();
|
169 |
5
| list.add(test1);
|
170 |
5
| list.add(test2);
|
171 |
5
| cache.attach("a", list);
|
172 |
5
| list = (List) cache.find("a");
|
173 |
5
| list.remove(1);
|
174 |
| |
175 |
| |
176 |
5
| AttachedEvent attach = takeNotification(AttachedEvent.class);
|
177 |
5
| assertEquals(test1, attach.getSource());
|
178 |
| |
179 |
| |
180 |
5
| ListModifiedEvent modify = takeNotification(ListModifiedEvent.class);
|
181 |
5
| assertEquals(ListModifiedEvent.Operation.ADD, modify.getOperation());
|
182 |
5
| assertEquals(test1, modify.getValue());
|
183 |
5
| assertEquals(0, modify.getIndex());
|
184 |
| |
185 |
| |
186 |
5
| attach = takeNotification(AttachedEvent.class);
|
187 |
5
| assertEquals(test2, attach.getSource());
|
188 |
| |
189 |
| |
190 |
5
| modify = takeNotification(ListModifiedEvent.class);
|
191 |
5
| assertEquals(ListModifiedEvent.Operation.ADD, modify.getOperation());
|
192 |
5
| assertEquals(test2, modify.getValue());
|
193 |
5
| assertEquals(1, modify.getIndex());
|
194 |
| |
195 |
| |
196 |
5
| attach = takeNotification(AttachedEvent.class);
|
197 |
5
| assertEquals(list, attach.getSource());
|
198 |
| |
199 |
| |
200 |
5
| modify = takeNotification(ListModifiedEvent.class);
|
201 |
5
| assertEquals(ListModifiedEvent.Operation.REMOVE, modify.getOperation());
|
202 |
5
| assertEquals(test2, modify.getValue());
|
203 |
5
| assertEquals(1, modify.getIndex());
|
204 |
| |
205 |
| |
206 |
5
| DetachedEvent detach = takeNotification(DetachedEvent.class);
|
207 |
5
| assertEquals(test2, detach.getSource());
|
208 |
| } |
209 |
| |
210 |
5
| public void testObjectListAdd() throws Exception
|
211 |
| { |
212 |
5
| final String english = "English";
|
213 |
5
| final String taiwanese = "Taiwanese";
|
214 |
| |
215 |
5
| Person test = new Person();
|
216 |
5
| test.setName("Ben");
|
217 |
5
| test.setAge(10);
|
218 |
| |
219 |
5
| ArrayList<String> list = new ArrayList<String>();
|
220 |
| |
221 |
5
| list.add(english);
|
222 |
5
| list.add(taiwanese);
|
223 |
5
| test.setLanguages(list);
|
224 |
| |
225 |
5
| cache.attach("a", test);
|
226 |
| |
227 |
| |
228 |
5
| AttachedEvent attach = takeNotification(AttachedEvent.class);
|
229 |
5
| assertEquals(english, attach.getSource());
|
230 |
| |
231 |
| |
232 |
5
| ListModifiedEvent modify = takeNotification(ListModifiedEvent.class);
|
233 |
5
| assertEquals(ListModifiedEvent.Operation.ADD, modify.getOperation());
|
234 |
5
| assertEquals(english, modify.getValue());
|
235 |
5
| assertEquals(0, modify.getIndex());
|
236 |
| |
237 |
| |
238 |
5
| attach = takeNotification(AttachedEvent.class);
|
239 |
5
| assertEquals(taiwanese, attach.getSource());
|
240 |
| |
241 |
| |
242 |
5
| modify = takeNotification(ListModifiedEvent.class);
|
243 |
5
| assertEquals(ListModifiedEvent.Operation.ADD, modify.getOperation());
|
244 |
5
| assertEquals(taiwanese, modify.getValue());
|
245 |
5
| assertEquals(1, modify.getIndex());
|
246 |
| |
247 |
| |
248 |
5
| attach = takeNotification(AttachedEvent.class);
|
249 |
5
| assertEquals(test.getLanguages(), attach.getSource());
|
250 |
| |
251 |
| |
252 |
5
| attach = takeNotification(AttachedEvent.class);
|
253 |
5
| assertEquals(test, attach.getSource());
|
254 |
| } |
255 |
| |
256 |
1
| public static Test suite() throws Exception
|
257 |
| { |
258 |
1
| return new TestSuite(ListTest.class);
|
259 |
| } |
260 |
| |
261 |
0
| public static void main(String[] args) throws Exception
|
262 |
| { |
263 |
0
| junit.textui.TestRunner.run(ListTest.suite());
|
264 |
| } |
265 |
| } |