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.codehaus.activemq.filter;
19
20 import junit.framework.TestCase;
21 import org.codehaus.activemq.message.ActiveMQDestination;
22 import org.codehaus.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.6 $
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 testStoreAndLookupAllWildcards() throws Exception {
153 loadSample2();
154
155 assertSample2();
156
157
158 remove("TEST.FOO", v1);
159
160 assertMapValue("TEST.FOO", v2, v3, v4);
161 assertMapValue("TEST.*", v2, v3, v4, v6);
162 assertMapValue("*.*", v2, v3, v4, v6);
163
164 remove("TEST.XYZ", v6);
165
166 assertMapValue("TEST.*", v2, v3, v4);
167 assertMapValue("*.*", v2, v3, v4);
168
169 remove("TEST.*", v2);
170
171 assertMapValue("TEST.*", v3, v4);
172 assertMapValue("*.*", v3, v4);
173
174 remove(">", v4);
175
176 assertMapValue("TEST.*", v3);
177 assertMapValue("*.*", v3);
178
179 remove("TEST.>", v3);
180 remove("TEST.FOO.BAR", v5);
181
182 assertMapValue("FOO", null);
183 assertMapValue("TEST.FOO", null);
184 assertMapValue("TEST.D1", null);
185
186 assertMapValue("TEST.FOO.FOO", null);
187 assertMapValue("TEST.BAR.FOO", null);
188 assertMapValue("TEST.FOO.BAR", null);
189 assertMapValue("TEST.BAR.D3", null);
190
191 assertMapValue("TEST.*", null);
192 assertMapValue("*.*", null);
193 assertMapValue("*.D1", null);
194 assertMapValue("TEST.*.*", null);
195 assertMapValue("TEST.BAR.*", null);
196
197 loadSample2();
198
199 assertSample2();
200
201 remove(">", v4);
202 remove("TEST.*", v2);
203
204 assertMapValue("FOO", null);
205 assertMapValue("TEST.FOO", v1, v3);
206 assertMapValue("TEST.D1", v3);
207
208 assertMapValue("TEST.FOO.FOO", v3);
209 assertMapValue("TEST.BAR.FOO", v3);
210 assertMapValue("TEST.FOO.BAR", v3, v5);
211 assertMapValue("TEST.BAR.D3", v3);
212
213 assertMapValue("TEST.*", v1, v3, v6);
214 assertMapValue("*.*", v1, v3, v6);
215 assertMapValue("*.D1", v3);
216 assertMapValue("TEST.*.*", v3, v5);
217 assertMapValue("TEST.BAR.*", v3);
218 }
219
220 protected void loadSample2() {
221 put("TEST.FOO", v1);
222 put("TEST.*", v2);
223 put("TEST.>", v3);
224 put(">", v4);
225 put("TEST.FOO.BAR", v5);
226 put("TEST.XYZ", v6);
227 }
228
229 protected void assertSample2() {
230 assertMapValue("FOO", v4);
231 assertMapValue("TEST.FOO", v1, v2, v3, v4);
232 assertMapValue("TEST.D1", v2, v3, v4);
233
234 assertMapValue("TEST.FOO.FOO", v3, v4);
235 assertMapValue("TEST.BAR.FOO", v3, v4);
236 assertMapValue("TEST.FOO.BAR", v3, v4, v5);
237 assertMapValue("TEST.BAR.D3", v3, v4);
238
239 assertMapValue("TEST.*", v1, v2, v3, v4, v6);
240 assertMapValue("*.*", v1, v2, v3, v4, v6);
241 assertMapValue("*.D1", v2, v3, v4);
242 assertMapValue("TEST.*.*", v3, v4, v5);
243 assertMapValue("TEST.BAR.*", v3, v4);
244 }
245
246
247 protected void put(String name, Object value) {
248 map.put(createDestination(name), value);
249 }
250
251 protected void remove(String name, Object value) {
252 map.remove(createDestination(name), value);
253 }
254
255
256 protected void assertMapValue(String destinationName, Object expected) {
257 ActiveMQDestination destination = createDestination(destinationName);
258 assertMapValue(destination, expected);
259 }
260
261 protected void assertMapValue(String destinationName, Object expected1, Object expected2) {
262 assertMapValue(destinationName, Arrays.asList(new Object[]{expected1, expected2}));
263 }
264
265 protected void assertMapValue(String destinationName, Object expected1, Object expected2, Object expected3) {
266 assertMapValue(destinationName, Arrays.asList(new Object[]{expected1, expected2, expected3}));
267 }
268
269 protected void assertMapValue(String destinationName, Object expected1, Object expected2, Object expected3, Object expected4) {
270 assertMapValue(destinationName, Arrays.asList(new Object[]{expected1, expected2, expected3, expected4}));
271 }
272
273 protected void assertMapValue(String destinationName, Object expected1, Object expected2, Object expected3, Object expected4, Object expected5) {
274 assertMapValue(destinationName, Arrays.asList(new Object[]{expected1, expected2, expected3, expected4, expected5}));
275 }
276
277 protected void assertMapValue(ActiveMQDestination destination, Object expected) {
278 List expectedList = null;
279 if (expected == null) {
280 expectedList = Collections.EMPTY_LIST;
281 }
282 else if (expected instanceof List) {
283 expectedList = (List) expected;
284 }
285 else {
286 expectedList = new ArrayList();
287 expectedList.add(expected);
288 }
289 Collections.sort(expectedList);
290 Set actualSet = map.get(destination);
291 List actual = new ArrayList(actualSet);
292 Collections.sort(actual);
293 assertEquals("map value for destinationName: " + destination, expectedList, actual);
294 }
295
296 protected ActiveMQDestination createDestination(String name) {
297 return new ActiveMQTopic(name);
298 }
299 }