Source code: org/activemq/filter/DestinationMapTest.java
1 /**
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.activemq.filter;
19
20 import junit.framework.TestCase;
21 import org.activemq.message.ActiveMQDestination;
22 import org.activemq.message.ActiveMQTopic;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Set;
29
30 /**
31 * @version $Revision: 1.1.1.1 $
32 */
33 public class DestinationMapTest extends TestCase {
34 protected DestinationMap map = new DestinationMap();
35
36 protected ActiveMQDestination d1 = createDestination("TEST.D1");
37 protected ActiveMQDestination d2 = createDestination("TEST.BAR.D2");
38 protected ActiveMQDestination d3 = createDestination("TEST.BAR.D3");
39 protected ActiveMQDestination compositeDestination1 = createDestination("TEST.D1,TEST.BAR.D2");
40 protected ActiveMQDestination compositeDestination2 = createDestination("TEST.D1,TEST.BAR.D3");
41
42 protected Object v1 = "value1";
43 protected Object v2 = "value2";
44 protected Object v3 = "value3";
45 protected Object v4 = "value4";
46 protected Object v5 = "value5";
47 protected Object v6 = "value6";
48
49
50 public void testSimpleDestinations() throws Exception {
51 map.put(d1, v1);
52 map.put(d2, v2);
53 map.put(d3, v3);
54
55 assertMapValue(d1, v1);
56 assertMapValue(d2, v2);
57 assertMapValue(d3, v3);
58 }
59
60 public void testSimpleDestinationsWithMultipleValues() throws Exception {
61 map.put(d1, v1);
62 map.put(d2, v2);
63 map.put(d2, v3);
64
65 assertMapValue(d1, v1);
66 assertMapValue("TEST.BAR.D2", v2, v3);
67 assertMapValue(d3, null);
68 }
69
70 public void testSimpleAndCompositeDestinations() throws Exception {
71 map.put(d1, v1);
72 map.put(compositeDestination1, v2);
73 map.put(compositeDestination2, v3);
74
75 assertMapValue("TEST.D1", v1, v2, v3);
76 assertMapValue(d2, v2);
77 assertMapValue(d3, v3);
78 assertMapValue(compositeDestination1.toString(), v1, v2, v3);
79 assertMapValue(compositeDestination2.toString(), v1, v2, v3);
80
81 map.remove(compositeDestination1, v2);
82 map.remove(compositeDestination2, v3);
83
84 assertMapValue("TEST.D1", v1);
85 }
86
87 public void testLookupOneStepWildcardDestinations() throws Exception {
88 map.put(d1, v1);
89 map.put(d2, v2);
90 map.put(d3, v3);
91
92 assertMapValue("TEST.D1", v1);
93 assertMapValue("TEST.*", v1);
94 assertMapValue("*.D1", v1);
95 assertMapValue("*.*", v1);
96
97 assertMapValue("TEST.BAR.D2", v2);
98 assertMapValue("TEST.*.D2", v2);
99 assertMapValue("*.BAR.D2", v2);
100 assertMapValue("*.*.D2", v2);
101
102 assertMapValue("TEST.BAR.D3", v3);
103 assertMapValue("TEST.*.D3", v3);
104 assertMapValue("*.BAR.D3", v3);
105 assertMapValue("*.*.D3", v3);
106
107 assertMapValue("TEST.BAR.D4", null);
108
109 assertMapValue("TEST.BAR.*", v2, v3);
110 }
111
112 public void testLookupMultiStepWildcardDestinations() throws Exception {
113 map.put(d1, v1);
114 map.put(d2, v2);
115 map.put(d3, v3);
116
117 List allValues = Arrays.asList(new Object[]{v1, v2, v3});
118
119 assertMapValue(">", allValues);
120 assertMapValue("TEST.>", allValues);
121 assertMapValue("*.>", allValues);
122
123 assertMapValue("FOO.>", null);
124 }
125
126
127 public void testStoreWildcardWithOneStepPath() throws Exception {
128 put("TEST.*", v1);
129 put("TEST.D1", v2);
130 put("TEST.BAR.*", v2);
131 put("TEST.BAR.D3", v3);
132
133 assertMapValue("FOO", null);
134 assertMapValue("TEST.FOO", v1);
135 assertMapValue("TEST.D1", v1, v2);
136
137 assertMapValue("TEST.FOO.FOO", null);
138 assertMapValue("TEST.BAR.FOO", v2);
139 assertMapValue("TEST.BAR.D3", v2, v3);
140
141 assertMapValue("TEST.*", v1, v2);
142 assertMapValue("*.D1", v1, v2);
143 assertMapValue("*.*", v1, v2);
144 assertMapValue("TEST.*.*", v2, v3);
145 assertMapValue("TEST.BAR.*", v2, v3);
146 assertMapValue("*.*.*", v2, v3);
147 assertMapValue("*.BAR.*", v2, v3);
148 assertMapValue("*.BAR.D3", v2, v3);
149 assertMapValue("*.*.D3", v2, v3);
150 }
151
152 public void testStoreWildcardInMiddleOfPath() throws Exception {
153 put("TEST.*", v1);
154 put("TEST.D1", v2);
155 put("TEST.BAR.*", v2);
156 put("TEST.XYZ.D3", v3);
157 put("TEST.XYZ.D4", v4);
158 put("TEST.BAR.D3", v5);
159 put("TEST.*.D2", v6);
160
161
162 assertMapValue("TEST.*.D3", v2, v3, v5);
163 assertMapValue("TEST.*.D4", v2, v4);
164
165 assertMapValue("TEST.*", v1, v2);
166 assertMapValue("TEST.*.*", v2, v3, v4, v5, v6);
167 assertMapValue("*.*.D3", v2, v3, v5);
168 assertMapValue("TEST.BAR.*", v2, v5, v6);
169
170 assertMapValue("TEST.BAR.D2", v2, v6);
171 assertMapValue("TEST.*.D2", v2, v6);
172 assertMapValue("TEST.BAR.*", v2, v5, v6);
173 }
174
175 public void testStoreAndLookupAllWildcards() throws Exception {
176 loadSample2();
177
178 assertSample2();
179
180 // lets remove everything and add it back
181 remove("TEST.FOO", v1);
182
183 assertMapValue("TEST.FOO", v2, v3, v4);
184 assertMapValue("TEST.*", v2, v3, v4, v6);
185 assertMapValue("*.*", v2, v3, v4, v6);
186
187 remove("TEST.XYZ", v6);
188
189 assertMapValue("TEST.*", v2, v3, v4);
190 assertMapValue("*.*", v2, v3, v4);
191
192 remove("TEST.*", v2);
193
194 assertMapValue("TEST.*", v3, v4);
195 assertMapValue("*.*", v3, v4);
196
197 remove(">", v4);
198
199 assertMapValue("TEST.*", v3);
200 assertMapValue("*.*", v3);
201
202 remove("TEST.>", v3);
203 remove("TEST.FOO.BAR", v5);
204
205 assertMapValue("FOO", null);
206 assertMapValue("TEST.FOO", null);
207 assertMapValue("TEST.D1", null);
208
209 assertMapValue("TEST.FOO.FOO", null);
210 assertMapValue("TEST.BAR.FOO", null);
211 assertMapValue("TEST.FOO.BAR", null);
212 assertMapValue("TEST.BAR.D3", null);
213
214 assertMapValue("TEST.*", null);
215 assertMapValue("*.*", null);
216 assertMapValue("*.D1", null);
217 assertMapValue("TEST.*.*", null);
218 assertMapValue("TEST.BAR.*", null);
219
220 loadSample2();
221
222 assertSample2();
223
224 remove(">", v4);
225 remove("TEST.*", v2);
226
227 assertMapValue("FOO", null);
228 assertMapValue("TEST.FOO", v1, v3);
229 assertMapValue("TEST.D1", v3);
230
231 assertMapValue("TEST.FOO.FOO", v3);
232 assertMapValue("TEST.BAR.FOO", v3);
233 assertMapValue("TEST.FOO.BAR", v3, v5);
234 assertMapValue("TEST.BAR.D3", v3);
235
236 assertMapValue("TEST.*", v1, v3, v6);
237 assertMapValue("*.*", v1, v3, v6);
238 assertMapValue("*.D1", v3);
239 assertMapValue("TEST.*.*", v3, v5);
240 assertMapValue("TEST.BAR.*", v3);
241 }
242
243 protected void loadSample2() {
244 put("TEST.FOO", v1);
245 put("TEST.*", v2);
246 put("TEST.>", v3);
247 put(">", v4);
248 put("TEST.FOO.BAR", v5);
249 put("TEST.XYZ", v6);
250 }
251
252 protected void assertSample2() {
253 assertMapValue("FOO", v4);
254 assertMapValue("TEST.FOO", v1, v2, v3, v4);
255 assertMapValue("TEST.D1", v2, v3, v4);
256
257 assertMapValue("TEST.FOO.FOO", v3, v4);
258 assertMapValue("TEST.BAR.FOO", v3, v4);
259 assertMapValue("TEST.FOO.BAR", v3, v4, v5);
260 assertMapValue("TEST.BAR.D3", v3, v4);
261
262 assertMapValue("TEST.*", v1, v2, v3, v4, v6);
263 assertMapValue("*.*", v1, v2, v3, v4, v6);
264 assertMapValue("*.D1", v2, v3, v4);
265 assertMapValue("TEST.*.*", v3, v4, v5);
266 assertMapValue("TEST.BAR.*", v3, v4);
267 }
268
269
270 protected void put(String name, Object value) {
271 map.put(createDestination(name), value);
272 }
273
274 protected void remove(String name, Object value) {
275 map.remove(createDestination(name), value);
276 }
277
278
279 protected void assertMapValue(String destinationName, Object expected) {
280 ActiveMQDestination destination = createDestination(destinationName);
281 assertMapValue(destination, expected);
282 }
283
284 protected void assertMapValue(String destinationName, Object expected1, Object expected2) {
285 assertMapValue(destinationName, Arrays.asList(new Object[]{expected1, expected2}));
286 }
287
288 protected void assertMapValue(String destinationName, Object expected1, Object expected2, Object expected3) {
289 assertMapValue(destinationName, Arrays.asList(new Object[]{expected1, expected2, expected3}));
290 }
291
292 protected void assertMapValue(String destinationName, Object expected1, Object expected2, Object expected3, Object expected4) {
293 assertMapValue(destinationName, Arrays.asList(new Object[]{expected1, expected2, expected3, expected4}));
294 }
295
296 protected void assertMapValue(String destinationName, Object expected1, Object expected2, Object expected3, Object expected4, Object expected5) {
297 assertMapValue(destinationName, Arrays.asList(new Object[]{expected1, expected2, expected3, expected4, expected5}));
298 }
299
300 protected void assertMapValue(ActiveMQDestination destination, Object expected) {
301 List expectedList = null;
302 if (expected == null) {
303 expectedList = Collections.EMPTY_LIST;
304 }
305 else if (expected instanceof List) {
306 expectedList = (List) expected;
307 }
308 else {
309 expectedList = new ArrayList();
310 expectedList.add(expected);
311 }
312 Collections.sort(expectedList);
313 Set actualSet = map.get(destination);
314 List actual = new ArrayList(actualSet);
315 Collections.sort(actual);
316 assertEquals("map value for destinationName: " + destination, expectedList, actual);
317 }
318
319 protected ActiveMQDestination createDestination(String name) {
320 return new ActiveMQTopic(name);
321 }
322 }