KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > filter > DestinationMapTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.activemq.filter;
20
21 import junit.framework.TestCase;
22
23 import org.apache.activemq.command.ActiveMQDestination;
24 import org.apache.activemq.command.ActiveMQQueue;
25 import org.apache.activemq.command.ActiveMQTopic;
26 import org.apache.activemq.filter.DestinationMap;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Set JavaDoc;
33
34 public class DestinationMapTest extends TestCase {
35     protected DestinationMap map = new DestinationMap();
36
37     protected ActiveMQDestination d1 = createDestination("TEST.D1");
38     protected ActiveMQDestination d2 = createDestination("TEST.BAR.D2");
39     protected ActiveMQDestination d3 = createDestination("TEST.BAR.D3");
40     protected ActiveMQDestination compositeDestination1 = createDestination("TEST.D1,TEST.BAR.D2");
41     protected ActiveMQDestination compositeDestination2 = createDestination("TEST.D1,TEST.BAR.D3");
42
43     protected Object JavaDoc v1 = "value1";
44     protected Object JavaDoc v2 = "value2";
45     protected Object JavaDoc v3 = "value3";
46     protected Object JavaDoc v4 = "value4";
47     protected Object JavaDoc v5 = "value5";
48     protected Object JavaDoc v6 = "value6";
49
50     public void testCompositeDestinations() throws Exception JavaDoc {
51         ActiveMQDestination d1 = createDestination("TEST.BAR.D2");
52         ActiveMQDestination d2 = createDestination("TEST.BAR.D3");
53         map.put(d1, d1);
54         map.put(d2, d2);
55         map.get(createDestination("TEST.BAR.D2,TEST.BAR.D3"));
56
57     }
58
59     public void testSimpleDestinations() throws Exception JavaDoc {
60         map.put(d1, v1);
61         map.put(d2, v2);
62         map.put(d3, v3);
63
64         assertMapValue(d1, v1);
65         assertMapValue(d2, v2);
66         assertMapValue(d3, v3);
67     }
68
69     public void testQueueAndTopicWithSameName() throws Exception JavaDoc {
70         ActiveMQQueue q1 = new ActiveMQQueue("foo");
71         ActiveMQTopic t1 = new ActiveMQTopic("foo");
72
73         map.put(q1, v1);
74         map.put(t1, v2);
75
76         assertMapValue(q1, v1);
77         assertMapValue(t1, v2);
78     }
79
80     public void testSimpleDestinationsWithMultipleValues() throws Exception JavaDoc {
81         map.put(d1, v1);
82         map.put(d2, v2);
83         map.put(d2, v3);
84
85         assertMapValue(d1, v1);
86         assertMapValue("TEST.BAR.D2", v2, v3);
87         assertMapValue(d3, null);
88     }
89
90     public void testSimpleAndCompositeDestinations() throws Exception JavaDoc {
91         map.put(d1, v1);
92         map.put(compositeDestination1, v2);
93         map.put(compositeDestination2, v3);
94
95         assertMapValue("TEST.D1", v1, v2, v3);
96         assertMapValue(d2, v2);
97         assertMapValue(d3, v3);
98         assertMapValue(compositeDestination1.toString(), v1, v2, v3);
99         assertMapValue(compositeDestination2.toString(), v1, v2, v3);
100
101         map.remove(compositeDestination1, v2);
102         map.remove(compositeDestination2, v3);
103
104         assertMapValue("TEST.D1", v1);
105     }
106
107     public void testLookupOneStepWildcardDestinations() throws Exception JavaDoc {
108         map.put(d1, v1);
109         map.put(d2, v2);
110         map.put(d3, v3);
111
112         assertMapValue("TEST.D1", v1);
113         assertMapValue("TEST.*", v1);
114         assertMapValue("*.D1", v1);
115         assertMapValue("*.*", v1);
116
117         assertMapValue("TEST.BAR.D2", v2);
118         assertMapValue("TEST.*.D2", v2);
119         assertMapValue("*.BAR.D2", v2);
120         assertMapValue("*.*.D2", v2);
121
122         assertMapValue("TEST.BAR.D3", v3);
123         assertMapValue("TEST.*.D3", v3);
124         assertMapValue("*.BAR.D3", v3);
125         assertMapValue("*.*.D3", v3);
126
127         assertMapValue("TEST.BAR.D4", null);
128
129         assertMapValue("TEST.BAR.*", v2, v3);
130     }
131
132     public void testLookupMultiStepWildcardDestinations() throws Exception JavaDoc {
133         map.put(d1, v1);
134         map.put(d2, v2);
135         map.put(d3, v3);
136
137         List JavaDoc allValues = Arrays.asList(new Object JavaDoc[] { v1, v2, v3 });
138
139         assertMapValue(">", allValues);
140         assertMapValue("TEST.>", allValues);
141         assertMapValue("*.>", allValues);
142
143         assertMapValue("FOO.>", null);
144     }
145
146     public void testStoreWildcardWithOneStepPath() throws Exception JavaDoc {
147         put("TEST.*", v1);
148         put("TEST.D1", v2);
149         put("TEST.BAR.*", v2);
150         put("TEST.BAR.D3", v3);
151
152         assertMapValue("FOO", null);
153         assertMapValue("TEST.FOO", v1);
154         assertMapValue("TEST.D1", v1, v2);
155
156         assertMapValue("TEST.FOO.FOO", null);
157         assertMapValue("TEST.BAR.FOO", v2);
158         assertMapValue("TEST.BAR.D3", v2, v3);
159
160         assertMapValue("TEST.*", v1, v2);
161         assertMapValue("*.D1", v1, v2);
162         assertMapValue("*.*", v1, v2);
163         assertMapValue("TEST.*.*", v2, v3);
164         assertMapValue("TEST.BAR.*", v2, v3);
165         assertMapValue("*.*.*", v2, v3);
166         assertMapValue("*.BAR.*", v2, v3);
167         assertMapValue("*.BAR.D3", v2, v3);
168         assertMapValue("*.*.D3", v2, v3);
169     }
170
171     public void testStoreWildcardInMiddleOfPath() throws Exception JavaDoc {
172         put("TEST.*", v1);
173         put("TEST.D1", v2);
174         put("TEST.BAR.*", v2);
175         put("TEST.XYZ.D3", v3);
176         put("TEST.XYZ.D4", v4);
177         put("TEST.BAR.D3", v5);
178         put("TEST.*.D2", v6);
179
180         assertMapValue("TEST.*.D3", v2, v3, v5);
181         assertMapValue("TEST.*.D4", v2, v4);
182
183         assertMapValue("TEST.*", v1, v2);
184         assertMapValue("TEST.*.*", v2, v3, v4, v5, v6);
185         assertMapValue("TEST.*.>", v1, v2, v3, v4, v5, v6);
186         assertMapValue("TEST.>", v1, v2, v3, v4, v5, v6);
187         assertMapValue("TEST.>.>", v1, v2, v3, v4, v5, v6);
188         assertMapValue("*.*.D3", v2, v3, v5);
189         assertMapValue("TEST.BAR.*", v2, v5, v6);
190
191         assertMapValue("TEST.BAR.D2", v2, v6);
192         assertMapValue("TEST.*.D2", v2, v6);
193         assertMapValue("TEST.BAR.*", v2, v5, v6);
194     }
195
196     public void testDoubleWildcardDoesNotMatchLongerPattern() throws Exception JavaDoc {
197         put("TEST.*", v1);
198         put("TEST.BAR.D3", v2);
199
200         assertMapValue("*.*.D3", v2);
201     }
202
203     public void testWildcardAtEndOfPathAndAtBeginningOfSearch() throws Exception JavaDoc {
204         put("TEST.*", v1);
205
206         assertMapValue("*.D1", v1);
207     }
208
209     public void testAnyPathWildcardInMap() throws Exception JavaDoc {
210         put("TEST.FOO.>", v1);
211
212         assertMapValue("TEST.FOO.BAR.WHANOT.A.B.C", v1);
213         assertMapValue("TEST.FOO.BAR.WHANOT", v1);
214         assertMapValue("TEST.FOO.BAR", v1);
215
216         assertMapValue("TEST.*.*", v1);
217         assertMapValue("TEST.BAR", null);
218
219         assertMapValue("TEST.FOO", v1);
220     }
221
222     public void testSimpleAddRemove() throws Exception JavaDoc {
223         put("TEST.D1", v2);
224
225         assertEquals("Root child count", 1, map.getTopicRootChildCount());
226
227         assertMapValue("TEST.D1", v2);
228
229         remove("TEST.D1", v2);
230
231         assertEquals("Root child count", 0, map.getTopicRootChildCount());
232         assertMapValue("TEST.D1", null);
233     }
234
235     public void testStoreAndLookupAllWildcards() throws Exception JavaDoc {
236         loadSample2();
237
238         assertSample2();
239
240         // lets remove everything and add it back
241
remove("TEST.FOO", v1);
242
243         assertMapValue("TEST.FOO", v2, v3, v4);
244         assertMapValue("TEST.*", v2, v3, v4, v6);
245         assertMapValue("*.*", v2, v3, v4, v6);
246
247         remove("TEST.XYZ", v6);
248
249         assertMapValue("TEST.*", v2, v3, v4);
250         assertMapValue("*.*", v2, v3, v4);
251
252         remove("TEST.*", v2);
253
254         assertMapValue("TEST.*", v3, v4);
255         assertMapValue("*.*", v3, v4);
256
257         remove(">", v4);
258
259         assertMapValue("TEST.*", v3);
260         assertMapValue("*.*", v3);
261
262         remove("TEST.>", v3);
263         remove("TEST.FOO.BAR", v5);
264
265         assertMapValue("FOO", null);
266         assertMapValue("TEST.FOO", null);
267         assertMapValue("TEST.D1", null);
268
269         assertMapValue("TEST.FOO.FOO", null);
270         assertMapValue("TEST.BAR.FOO", null);
271         assertMapValue("TEST.FOO.BAR", null);
272         assertMapValue("TEST.BAR.D3", null);
273
274         assertMapValue("TEST.*", null);
275         assertMapValue("*.*", null);
276         assertMapValue("*.D1", null);
277         assertMapValue("TEST.*.*", null);
278         assertMapValue("TEST.BAR.*", null);
279
280         loadSample2();
281
282         assertSample2();
283
284         remove(">", v4);
285         remove("TEST.*", v2);
286
287         assertMapValue("FOO", null);
288         assertMapValue("TEST.FOO", v1, v3);
289         assertMapValue("TEST.D1", v3);
290
291         assertMapValue("TEST.FOO.FOO", v3);
292         assertMapValue("TEST.BAR.FOO", v3);
293         assertMapValue("TEST.FOO.BAR", v3, v5);
294         assertMapValue("TEST.BAR.D3", v3);
295
296         assertMapValue("TEST.*", v1, v3, v6);
297         assertMapValue("*.*", v1, v3, v6);
298         assertMapValue("*.D1", v3);
299         assertMapValue("TEST.*.*", v3, v5);
300         assertMapValue("TEST.BAR.*", v3);
301     }
302     
303     public void testAddAndRemove() throws Exception JavaDoc {
304         
305         put("FOO.A", v1);
306         assertMapValue("FOO.>", v1);
307         
308         put("FOO.B", v2);
309         assertMapValue("FOO.>", v1, v2);
310         
311         Set JavaDoc set = map.removeAll(createDestination("FOO.A"));
312         
313         assertMapValue("FOO.>", v2);
314         
315     }
316
317
318     protected void loadSample2() {
319         put("TEST.FOO", v1);
320         put("TEST.*", v2);
321         put("TEST.>", v3);
322         put(">", v4);
323         put("TEST.FOO.BAR", v5);
324         put("TEST.XYZ", v6);
325     }
326
327     protected void assertSample2() {
328         assertMapValue("FOO", v4);
329         assertMapValue("TEST.FOO", v1, v2, v3, v4);
330         assertMapValue("TEST.D1", v2, v3, v4);
331
332         assertMapValue("TEST.FOO.FOO", v3, v4);
333         assertMapValue("TEST.BAR.FOO", v3, v4);
334         assertMapValue("TEST.FOO.BAR", v3, v4, v5);
335         assertMapValue("TEST.BAR.D3", v3, v4);
336
337         assertMapValue("TEST.*", v1, v2, v3, v4, v6);
338         assertMapValue("*.*", v1, v2, v3, v4, v6);
339         assertMapValue("*.D1", v2, v3, v4);
340         assertMapValue("TEST.*.*", v3, v4, v5);
341         assertMapValue("TEST.BAR.*", v3, v4);
342     }
343
344     protected void put(String JavaDoc name, Object JavaDoc value) {
345         map.put(createDestination(name), value);
346     }
347
348     protected void remove(String JavaDoc name, Object JavaDoc value) {
349         ActiveMQDestination destination = createDestination(name);
350         map.remove(destination, value);
351     }
352
353     protected void assertMapValue(String JavaDoc destinationName, Object JavaDoc expected) {
354         ActiveMQDestination destination = createDestination(destinationName);
355         assertMapValue(destination, expected);
356     }
357
358     protected void assertMapValue(String JavaDoc destinationName, Object JavaDoc expected1, Object JavaDoc expected2) {
359         assertMapValue(destinationName, Arrays.asList(new Object JavaDoc[] { expected1, expected2 }));
360     }
361
362     protected void assertMapValue(String JavaDoc destinationName, Object JavaDoc expected1, Object JavaDoc expected2, Object JavaDoc expected3) {
363         assertMapValue(destinationName, Arrays.asList(new Object JavaDoc[] { expected1, expected2, expected3 }));
364     }
365
366     protected void assertMapValue(String JavaDoc destinationName, Object JavaDoc expected1, Object JavaDoc expected2, Object JavaDoc expected3, Object JavaDoc expected4) {
367         assertMapValue(destinationName, Arrays.asList(new Object JavaDoc[] { expected1, expected2, expected3, expected4 }));
368     }
369
370     protected void assertMapValue(String JavaDoc destinationName, Object JavaDoc expected1, Object JavaDoc expected2, Object JavaDoc expected3, Object JavaDoc expected4, Object JavaDoc expected5) {
371         assertMapValue(destinationName, Arrays.asList(new Object JavaDoc[] { expected1, expected2, expected3, expected4, expected5 }));
372     }
373     
374     protected void assertMapValue(String JavaDoc destinationName, Object JavaDoc expected1, Object JavaDoc expected2, Object JavaDoc expected3, Object JavaDoc expected4, Object JavaDoc expected5, Object JavaDoc expected6) {
375         assertMapValue(destinationName, Arrays.asList(new Object JavaDoc[] { expected1, expected2, expected3, expected4, expected5, expected6 }));
376     }
377
378     protected void assertMapValue(ActiveMQDestination destination, Object JavaDoc expected) {
379         List JavaDoc expectedList = null;
380         if (expected == null) {
381             expectedList = Collections.EMPTY_LIST;
382         }
383         else if (expected instanceof List JavaDoc) {
384             expectedList = (List JavaDoc) expected;
385         }
386         else {
387             expectedList = new ArrayList JavaDoc();
388             expectedList.add(expected);
389         }
390         Collections.sort(expectedList);
391         Set JavaDoc actualSet = map.get(destination);
392         List JavaDoc actual = new ArrayList JavaDoc(actualSet);
393         Collections.sort(actual);
394         assertEquals("map value for destinationName: " + destination, expectedList, actual);
395     }
396
397     protected ActiveMQDestination createDestination(String JavaDoc name) {
398         return new ActiveMQTopic(name);
399     }
400 }
401
Popular Tags