2 * Copyright 2005-2011 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.xmlrouter.impl;
18 import static junit.framework.Assert.*;
19 import static org.mockito.Matchers.*;
20 import static org.mockito.Mockito.*;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.logging.Level;
26 import javax.xml.transform.dom.DOMSource;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.wamblee.xmlrouter.common.Id;
31 import org.wamblee.xmlrouter.config.DocumentType;
32 import org.wamblee.xmlrouter.config.Transformation;
33 import org.wamblee.xmlrouter.listener.EventListener;
34 import org.wamblee.xmlrouter.listener.LoggingEventListener;
35 import org.wamblee.xmlrouter.subscribe.Destination;
37 public class XMLRouterTest {
39 public static class MyDestination implements Destination {
41 private boolean receiveResult;
42 private Collection<String> types;
44 public MyDestination(boolean aReceiveResult, Collection<String> aTypes) {
45 receiveResult = aReceiveResult;
50 public Collection<String> chooseFromTargetTypes(
51 Collection<String> aPossibleTargetTypes) {
56 public String getName() {
61 public boolean receive(DOMSource aEvent) {
66 private XMLRouter router;
67 private DOMSource source1;
68 private DOMSource source2;
69 private DOMSource source3;
71 private Destination destinationSpy;
72 private Id<Destination> destinationId;
73 private EventListener listener;
77 EventListener logListener = new LoggingEventListener(Level.INFO);
78 listener = spy(logListener);
79 router = new XMLRouter(listener);
80 source1 = mock(DOMSource.class);
81 source2 = mock(DOMSource.class);
82 source3 = mock(DOMSource.class);
86 public void testNoInputDocumentsRegistered() {
87 Destination destination = new MyDestination(true, Arrays.asList("any"));
88 destinationSpy = spy(destination);
90 destinationId = router.registerDestination(destinationSpy);
91 assertFalse(router.publish("any", source1));
95 public void testOneDestinationNoTransformationSuccess() {
96 destinationSpy = registerDestination(true, "any");
97 registerDocumentType("any");
99 assertTrue(router.publish("any", source1));
100 verify(destinationSpy).receive(same(source1));
102 // Unregister the destination.
103 router.unregisterDestination(destinationId);
104 reset(destinationSpy);
105 assertFalse(router.publish("any", source2));
106 verifyNoMoreInteractions(destinationSpy);
109 private void registerDocumentType(String aType) {
110 DocumentType type = mock(DocumentType.class);
111 when(type.isInstance(any(DOMSource.class))).thenReturn(true);
112 when(type.getName()).thenReturn(aType);
113 Id<DocumentType> typeId = router.addDocumentType(type);
116 private void registerDocumentType(String aType, DOMSource aSource) {
117 DocumentType type = mock(DocumentType.class);
118 when(type.isInstance(same(aSource))).thenReturn(true);
119 when(type.getName()).thenReturn(aType);
120 Id<DocumentType> typeId = router.addDocumentType(type);
123 private Destination registerDestination(boolean aResult, String... types) {
124 Destination destination = new MyDestination(aResult,
125 Arrays.asList(types));
126 Destination myspy = spy(destination);
127 destinationId = router.registerDestination(myspy);
132 public void testOneDestinationNotMatches() {
133 destinationSpy = registerDestination(true);
134 registerDocumentType("any");
136 assertFalse(router.publish("any", source1));
137 verify(destinationSpy, never()).receive(any(DOMSource.class));
141 public void testOneDestinationThrowsException() {
142 destinationSpy = registerDestination(true, "any");
143 registerDocumentType("any");
145 doThrow(new RuntimeException()).when(destinationSpy).receive(
146 any(DOMSource.class));
148 assertFalse(router.publish("any", source1));
149 verify(destinationSpy).receive(same(source1));
153 public void testOneDestinationThrowsExceptionSecondDestinationStillHandled() {
154 testOneDestinationThrowsException();
155 Destination destination2 = new MyDestination(true, Arrays.asList("any"));
156 Destination destinationSpy2 = spy(destination2);
157 Id<Destination> destinationId2 = router
158 .registerDestination(destinationSpy2);
160 assertTrue(router.publish("any", source1));
161 verify(destinationSpy2).receive(same(source1));
165 public void testDestinationChooseFromTargetTypesThrowsException() {
166 destinationSpy = registerDestination(true, "any");
167 registerDocumentType("any");
169 doThrow(new RuntimeException()).when(destinationSpy)
170 .chooseFromTargetTypes((Collection<String>) anyObject());
172 assertFalse(router.publish("any", source1));
173 verify(destinationSpy, never()).receive(same(source1));
177 public void testDestinationChooseFromTargetTypesThrowsExceptionSecondDestinationStillOk() {
178 testDestinationChooseFromTargetTypesThrowsException();
180 Destination destination2 = new MyDestination(true, Arrays.asList("any"));
181 Destination destinationSpy2 = spy(destination2);
182 Id<Destination> destinationId2 = router
183 .registerDestination(destinationSpy2);
184 assertTrue(router.publish("any", source1));
185 verify(destinationSpy, never()).receive(same(source1));
186 verify(destinationSpy2).receive(same(source1));
190 public void testDestinationChooseFromTargetTypesReturnsNull() {
191 destinationSpy = registerDestination(true, "any");
192 registerDocumentType("any");
196 .chooseFromTargetTypes((Collection<String>) anyObject()))
199 assertFalse(router.publish("any", source1));
200 verify(destinationSpy, never()).receive(same(source1));
204 public void testDestinationChooseFromTargetTypesReturnsNullSecondDestinationStillOk() {
205 testDestinationChooseFromTargetTypesReturnsNull();
207 Destination destination2 = new MyDestination(true, Arrays.asList("any"));
208 Destination destinationSpy2 = spy(destination2);
209 Id<Destination> destinationId2 = router
210 .registerDestination(destinationSpy2);
211 assertTrue(router.publish("any", source1));
212 verify(destinationSpy, never()).receive(same(source1));
213 verify(destinationSpy2).receive(same(source1));
217 public void testOneTransformationOneDestination() {
218 registerDocumentType("any");
219 Transformation transformation = mock(Transformation.class);
220 when(transformation.getFromType()).thenReturn("any");
221 when(transformation.getToType()).thenReturn("bla");
222 when(transformation.transform(same(source1))).thenReturn(source2);
223 router.addTransformation(transformation);
225 Destination destination = mock(Destination.class);
227 destination.chooseFromTargetTypes((Collection<String>) anyObject()))
228 .thenReturn(Arrays.asList("bla"));
230 router.registerDestination(destination);
232 when(destination.receive(any(DOMSource.class))).thenReturn(true);
233 assertTrue(router.publish("bla", source1));
235 verify(transformation).transform(source1);
236 verify(destination).receive(same(source2));
238 // now the same when the destination rejects the event.
239 when(destination.receive(any(DOMSource.class))).thenReturn(false);
240 assertFalse(router.publish("bla", source1));
243 private Transformation createTransformation(String aFrom, String aTo,
244 DOMSource aSource, DOMSource aTarget) {
245 Transformation transformation = mock(Transformation.class);
246 when(transformation.getFromType()).thenReturn(aFrom);
247 when(transformation.getToType()).thenReturn(aTo);
248 when(transformation.transform(same(aSource))).thenReturn(aTarget);
249 return transformation;
253 public void testOneTransformationReturnsNull() {
254 registerDocumentType("any");
255 Transformation transformation = createTransformation("any", "bla",
258 router.addTransformation(transformation);
260 Destination destination = mock(Destination.class);
262 destination.chooseFromTargetTypes((Collection<String>) anyObject()))
263 .thenReturn(Arrays.asList("bla"));
264 router.registerDestination(destination);
266 assertFalse(router.publish("bla", source1));
268 verify(transformation).transform(source1);
269 verify(destination, never()).receive(any(DOMSource.class));
271 // add second transformation that behaves normally
272 Transformation transformation2 = createTransformation("any", "bla2",
275 router.addTransformation(transformation2);
277 destination.chooseFromTargetTypes((Collection<String>) anyObject()))
278 .thenReturn(Arrays.asList("bla", "bla2"));
280 reset(transformation);
281 when(transformation.getFromType()).thenReturn("any");
282 when(transformation.getToType()).thenReturn("bla");
283 when(transformation.transform(same(source1))).thenReturn(null);
285 when(destination.receive(any(DOMSource.class))).thenReturn(true);
286 assertTrue(router.publish("bla", source1));
288 verify(transformation).transform(source1);
289 verify(transformation2).transform(source1);
291 verify(destination).receive(same(source2));
296 public void testChooseMultipleDestinationsOneType() {
297 Destination dest1 = registerDestination(true, "any");
298 Destination dest2 = registerDestination(true, "any");
299 registerDocumentType("any");
301 assertTrue(router.publish("source", source1));
303 verify(dest1).receive(same(source1));
304 verify(dest2).receive(same(source1));
308 public void testMultipleDeliveryToOneDestination() {
309 Destination dest = registerDestination(true, "any", "other");
310 registerDocumentType("any", source1);
311 registerDocumentType("other", source2);
312 Transformation transformation = createTransformation("any", "other",
314 router.addTransformation(transformation);
316 assertTrue(router.publish("source", source1));
318 verify(dest).receive(same(source1));
319 verify(dest).receive(same(source2));
323 public void testMultipleTransformations() {
324 Destination dest = registerDestination(true, "any", "other");
325 registerDocumentType("any", source1);
326 registerDocumentType("other", source3);
328 Transformation t1 = createTransformation("any", "intermediate",
330 router.addTransformation(t1);
331 Transformation t2 = createTransformation("intermediate", "other",
333 router.addTransformation(t2);
335 assertTrue(router.publish("source", source1));
337 verify(dest).receive(same(source3));