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.LinkedHashSet; |
25 |
| import java.util.Set; |
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.Event; |
36 |
| import org.jboss.cache.pojo.notification.event.SetModifiedEvent; |
37 |
| import org.jboss.cache.pojo.test.Person; |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| public class SetTest extends TestCase |
47 |
| { |
48 |
| private PojoCache cache; |
49 |
| private Listener listener = new Listener(); |
50 |
| |
51 |
3
| public SetTest(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 |
3
| protected void tearDown() throws Exception
|
67 |
| { |
68 |
3
| super.tearDown();
|
69 |
3
| cache.stop();
|
70 |
| } |
71 |
| |
72 |
18
| private <T extends Event> T takeNotification(Class<T> clazz)
|
73 |
| { |
74 |
18
| T notification = listener.take(clazz);
|
75 |
18
| verifyNotification(notification);
|
76 |
| |
77 |
18
| return notification;
|
78 |
| } |
79 |
| |
80 |
18
| protected void verifyNotification(Event notification)
|
81 |
| { |
82 |
18
| assertSame(cache, notification.getContext().getPojoCache());
|
83 |
18
| assertEquals(true, notification.isLocal());
|
84 |
| } |
85 |
| |
86 |
1
| @SuppressWarnings("unchecked")
|
87 |
| public void testSetAddNotification() throws Exception |
88 |
| { |
89 |
1
| final String test1 = "test1";
|
90 |
1
| final String test2 = "test2";
|
91 |
| |
92 |
1
| Set<String> set = new LinkedHashSet<String>();
|
93 |
1
| set.add(test1);
|
94 |
1
| set.add(test2);
|
95 |
1
| cache.attach("a", set);
|
96 |
1
| set = (Set) cache.find("a");
|
97 |
| |
98 |
| |
99 |
1
| AttachedEvent attach = takeNotification(AttachedEvent.class);
|
100 |
1
| assertEquals(test1, attach.getSource());
|
101 |
| |
102 |
| |
103 |
1
| SetModifiedEvent modify = takeNotification(SetModifiedEvent.class);
|
104 |
1
| assertEquals(SetModifiedEvent.Operation.ADD, modify.getOperation());
|
105 |
1
| assertEquals(test1, modify.getValue());
|
106 |
| |
107 |
| |
108 |
1
| attach = takeNotification(AttachedEvent.class);
|
109 |
1
| assertEquals(test2, attach.getSource());
|
110 |
| |
111 |
| |
112 |
1
| modify = takeNotification(SetModifiedEvent.class);
|
113 |
1
| assertEquals(SetModifiedEvent.Operation.ADD, modify.getOperation());
|
114 |
1
| assertEquals(test2, modify.getValue());
|
115 |
| |
116 |
| |
117 |
1
| attach = takeNotification(AttachedEvent.class);
|
118 |
1
| assertSame(set, attach.getSource());
|
119 |
| |
120 |
| } |
121 |
| |
122 |
1
| @SuppressWarnings("unchecked")
|
123 |
| public void testSetRemoveNotification() throws Exception |
124 |
| { |
125 |
1
| final String test1 = "test1";
|
126 |
1
| final String test2 = "test2";
|
127 |
| |
128 |
1
| Set<String> set = new LinkedHashSet<String>();
|
129 |
1
| set.add(test1);
|
130 |
1
| set.add(test2);
|
131 |
1
| cache.attach("a", set);
|
132 |
| |
133 |
1
| set = (Set<String>) cache.find("a");
|
134 |
1
| set.remove(test2);
|
135 |
| |
136 |
| |
137 |
1
| AttachedEvent attach = takeNotification(AttachedEvent.class);
|
138 |
1
| assertEquals(test1, attach.getSource());
|
139 |
| |
140 |
| |
141 |
1
| SetModifiedEvent modify = takeNotification(SetModifiedEvent.class);
|
142 |
1
| assertEquals(SetModifiedEvent.Operation.ADD, modify.getOperation());
|
143 |
1
| assertEquals(test1, modify.getValue());
|
144 |
| |
145 |
| |
146 |
1
| attach = takeNotification(AttachedEvent.class);
|
147 |
1
| assertEquals(test2, attach.getSource());
|
148 |
| |
149 |
| |
150 |
1
| modify = takeNotification(SetModifiedEvent.class);
|
151 |
1
| assertEquals(SetModifiedEvent.Operation.ADD, modify.getOperation());
|
152 |
1
| assertEquals(test2, modify.getValue());
|
153 |
| |
154 |
| |
155 |
1
| attach = takeNotification(AttachedEvent.class);
|
156 |
1
| assertSame(set, attach.getSource());
|
157 |
| |
158 |
| |
159 |
1
| modify = takeNotification(SetModifiedEvent.class);
|
160 |
1
| assertEquals(SetModifiedEvent.Operation.REMOVE, modify.getOperation());
|
161 |
1
| assertEquals(test2, modify.getValue());
|
162 |
| |
163 |
| |
164 |
1
| DetachedEvent detach = takeNotification(DetachedEvent.class);
|
165 |
1
| assertEquals(test2, detach.getSource());
|
166 |
| } |
167 |
| |
168 |
1
| public void testObjectSetAdd() throws Exception
|
169 |
| { |
170 |
1
| final String drumming = "druming";
|
171 |
1
| final String engineering = "engineering";
|
172 |
| |
173 |
1
| Person test = new Person();
|
174 |
1
| test.setName("Joe");
|
175 |
1
| test.setAge(30);
|
176 |
| |
177 |
1
| Set<String> set = new LinkedHashSet<String>();
|
178 |
1
| set.add(drumming);
|
179 |
1
| set.add(engineering);
|
180 |
1
| test.setSkills(set);
|
181 |
1
| cache.attach("a", test);
|
182 |
| |
183 |
| |
184 |
1
| AttachedEvent attach = takeNotification(AttachedEvent.class);
|
185 |
1
| assertEquals(drumming, attach.getSource());
|
186 |
| |
187 |
| |
188 |
1
| SetModifiedEvent modify = takeNotification(SetModifiedEvent.class);
|
189 |
1
| assertEquals(SetModifiedEvent.Operation.ADD, modify.getOperation());
|
190 |
1
| assertEquals(drumming, modify.getValue());
|
191 |
| |
192 |
| |
193 |
1
| attach = takeNotification(AttachedEvent.class);
|
194 |
1
| assertEquals(engineering, attach.getSource());
|
195 |
| |
196 |
| |
197 |
1
| modify = takeNotification(SetModifiedEvent.class);
|
198 |
1
| assertEquals(SetModifiedEvent.Operation.ADD, modify.getOperation());
|
199 |
1
| assertEquals(engineering, modify.getValue());
|
200 |
| |
201 |
| |
202 |
1
| attach = takeNotification(AttachedEvent.class);
|
203 |
1
| assertEquals(test.getSkills(), attach.getSource());
|
204 |
| |
205 |
| |
206 |
1
| attach = takeNotification(AttachedEvent.class);
|
207 |
1
| assertEquals(test, attach.getSource());
|
208 |
| } |
209 |
| |
210 |
1
| public static Test suite() throws Exception
|
211 |
| { |
212 |
1
| return new TestSuite(SetTest.class);
|
213 |
| } |
214 |
| |
215 |
0
| public static void main(String[] args) throws Exception
|
216 |
| { |
217 |
0
| junit.textui.TestRunner.run(SetTest.suite());
|
218 |
| } |
219 |
| } |