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.LinkedHashMap; |
25 |
| import java.util.Map; |
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.MapModifiedEvent; |
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 MapTest extends TestCase |
47 |
| { |
48 |
| private PojoCache cache; |
49 |
| private Listener listener = new Listener(); |
50 |
| |
51 |
3
| public MapTest(String name)
|
52 |
| { |
53 |
3
| super(name);
|
54 |
| } |
55 |
| |
56 |
3
| protected void setUp() throws Exception
|
57 |
| { |
58 |
3
| super.setUp();
|
59 |
3
| String configFile = "META-INF/local-service.xml";
|
60 |
3
| boolean toStart = false;
|
61 |
3
| cache = PojoCacheFactory.createCache(configFile, toStart);
|
62 |
3
| cache.start();
|
63 |
3
| cache.addListener(listener);
|
64 |
| } |
65 |
| |
66 |
| |
67 |
3
| protected void tearDown() throws Exception
|
68 |
| { |
69 |
3
| super.tearDown();
|
70 |
3
| cache.stop();
|
71 |
| } |
72 |
| |
73 |
18
| private <T extends Event> T takeNotification(Class<T> clazz)
|
74 |
| { |
75 |
18
| T notification = listener.take(clazz);
|
76 |
18
| verifyNotification(notification);
|
77 |
| |
78 |
18
| return notification;
|
79 |
| } |
80 |
| |
81 |
18
| protected void verifyNotification(Event notification)
|
82 |
| { |
83 |
18
| assertSame(cache, notification.getContext().getPojoCache());
|
84 |
18
| assertEquals(true, notification.isLocal());
|
85 |
| } |
86 |
| |
87 |
1
| public void testMapAddNotification() throws Exception
|
88 |
| { |
89 |
1
| final String key1 = "key1";
|
90 |
1
| final String key2 = "key2";
|
91 |
1
| final String test1 = "test1";
|
92 |
1
| final String test2 = "test2";
|
93 |
| |
94 |
1
| Map<String, String> map = new LinkedHashMap<String, String>();
|
95 |
1
| map.put(key1, test1);
|
96 |
1
| map.put(key2, test2);
|
97 |
1
| cache.attach("a", map);
|
98 |
1
| map = (Map) cache.find("a");
|
99 |
| |
100 |
| |
101 |
1
| AttachedEvent attach = takeNotification(AttachedEvent.class);
|
102 |
1
| assertEquals(test1, attach.getSource());
|
103 |
| |
104 |
| |
105 |
1
| MapModifiedEvent modify = takeNotification(MapModifiedEvent.class);
|
106 |
1
| assertEquals(MapModifiedEvent.Operation.PUT, modify.getOperation());
|
107 |
1
| assertEquals(key1, modify.getKey());
|
108 |
1
| assertEquals(test1, modify.getValue());
|
109 |
| |
110 |
| |
111 |
1
| attach = takeNotification(AttachedEvent.class);
|
112 |
1
| assertEquals(test2, attach.getSource());
|
113 |
| |
114 |
| |
115 |
1
| modify = takeNotification(MapModifiedEvent.class);
|
116 |
1
| assertEquals(MapModifiedEvent.Operation.PUT, modify.getOperation());
|
117 |
1
| assertEquals(key2, modify.getKey());
|
118 |
1
| assertEquals(test2, modify.getValue());
|
119 |
| |
120 |
| |
121 |
1
| attach = takeNotification(AttachedEvent.class);
|
122 |
1
| assertSame(map, attach.getSource());
|
123 |
| |
124 |
| } |
125 |
| |
126 |
1
| public void testMapRemoveNotification() throws Exception
|
127 |
| { |
128 |
1
| final String key1 = "key1";
|
129 |
1
| final String key2 = "key2";
|
130 |
1
| final String test1 = "test1";
|
131 |
1
| final String test2 = "test2";
|
132 |
| |
133 |
1
| Map<String, String> map = new LinkedHashMap<String, String>();
|
134 |
1
| map.put(key1, test1);
|
135 |
1
| map.put(key2, test2);
|
136 |
1
| cache.attach("a", map);
|
137 |
1
| map = (Map) cache.find("a");
|
138 |
1
| map.remove(key2);
|
139 |
| |
140 |
| |
141 |
1
| AttachedEvent attach = takeNotification(AttachedEvent.class);
|
142 |
1
| assertEquals(test1, attach.getSource());
|
143 |
| |
144 |
| |
145 |
1
| MapModifiedEvent modify = takeNotification(MapModifiedEvent.class);
|
146 |
1
| assertEquals(MapModifiedEvent.Operation.PUT, modify.getOperation());
|
147 |
1
| assertEquals(key1, modify.getKey());
|
148 |
1
| assertEquals(test1, modify.getValue());
|
149 |
| |
150 |
| |
151 |
1
| attach = takeNotification(AttachedEvent.class);
|
152 |
1
| assertEquals(test2, attach.getSource());
|
153 |
| |
154 |
| |
155 |
1
| modify = takeNotification(MapModifiedEvent.class);
|
156 |
1
| assertEquals(MapModifiedEvent.Operation.PUT, modify.getOperation());
|
157 |
1
| assertEquals(key2, modify.getKey());
|
158 |
1
| assertEquals(test2, modify.getValue());
|
159 |
| |
160 |
| |
161 |
1
| attach = takeNotification(AttachedEvent.class);
|
162 |
1
| assertSame(map, attach.getSource());
|
163 |
| |
164 |
| |
165 |
1
| modify = takeNotification(MapModifiedEvent.class);
|
166 |
1
| assertEquals(MapModifiedEvent.Operation.REMOVE, modify.getOperation());
|
167 |
1
| assertEquals(key2, modify.getKey());
|
168 |
1
| assertEquals(test2, modify.getValue());
|
169 |
| |
170 |
| |
171 |
1
| DetachedEvent detach = takeNotification(DetachedEvent.class);
|
172 |
1
| assertEquals(test2, detach.getSource());
|
173 |
| } |
174 |
| |
175 |
1
| public void testObjectMapAdd() throws Exception
|
176 |
| { |
177 |
1
| final String key1 = "key1";
|
178 |
1
| final String key2 = "key2";
|
179 |
1
| final String drumming = "druming";
|
180 |
1
| final String engineering = "engineering";
|
181 |
| |
182 |
1
| Person test = new Person();
|
183 |
1
| test.setName("Joe");
|
184 |
1
| test.setAge(30);
|
185 |
| |
186 |
1
| Map<String, String> map = new LinkedHashMap<String, String>();
|
187 |
1
| map.put(key1, drumming);
|
188 |
1
| map.put(key2, engineering);
|
189 |
1
| test.setHobbies(map);
|
190 |
1
| cache.attach("a", test);
|
191 |
| |
192 |
| |
193 |
1
| AttachedEvent attach = takeNotification(AttachedEvent.class);
|
194 |
1
| assertEquals(drumming, attach.getSource());
|
195 |
| |
196 |
| |
197 |
1
| MapModifiedEvent modify = takeNotification(MapModifiedEvent.class);
|
198 |
1
| assertEquals(MapModifiedEvent.Operation.PUT, modify.getOperation());
|
199 |
1
| assertEquals(key1, modify.getKey());
|
200 |
1
| assertEquals(drumming, modify.getValue());
|
201 |
| |
202 |
| |
203 |
1
| attach = takeNotification(AttachedEvent.class);
|
204 |
1
| assertEquals(engineering, attach.getSource());
|
205 |
| |
206 |
| |
207 |
1
| modify = takeNotification(MapModifiedEvent.class);
|
208 |
1
| assertEquals(key2, modify.getKey());
|
209 |
1
| assertEquals(MapModifiedEvent.Operation.PUT, modify.getOperation());
|
210 |
1
| assertEquals(engineering, modify.getValue());
|
211 |
| |
212 |
| |
213 |
1
| attach = takeNotification(AttachedEvent.class);
|
214 |
1
| assertEquals(test.getHobbies(), attach.getSource());
|
215 |
| |
216 |
| |
217 |
1
| attach = takeNotification(AttachedEvent.class);
|
218 |
1
| assertEquals(test, attach.getSource());
|
219 |
| } |
220 |
| |
221 |
1
| public static Test suite() throws Exception
|
222 |
| { |
223 |
1
| return new TestSuite(MapTest.class);
|
224 |
| } |
225 |
| |
226 |
0
| public static void main(String[] args) throws Exception
|
227 |
| { |
228 |
0
| junit.textui.TestRunner.run(MapTest.suite());
|
229 |
| } |
230 |
| } |