1 package org.wamblee.xmlrouter.impl;
3 import static junit.framework.Assert.*;
4 import static org.mockito.Matchers.*;
5 import static org.mockito.Mockito.*;
7 import java.util.Arrays;
8 import java.util.Collection;
10 import javax.xml.transform.dom.DOMSource;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.wamblee.xmlrouter.common.Id;
15 import org.wamblee.xmlrouter.config.DocumentType;
16 import org.wamblee.xmlrouter.config.Transformation;
17 import org.wamblee.xmlrouter.subscribe.Destination;
19 public class XMLRouterTest {
21 public static class MyDestination implements Destination {
23 private boolean receiveResult;
24 private Collection<String> types;
26 public MyDestination(boolean aReceiveResult, Collection<String> aTypes) {
27 receiveResult = aReceiveResult;
32 public Collection<String> chooseFromTargetTypes(
33 Collection<String> aPossibleTargetTypes) {
38 public String getName() {
43 public boolean receive(DOMSource aEvent) {
48 private XMLRouter router;
49 private DOMSource source1;
50 private DOMSource source2;
51 private DOMSource source3;
53 private Destination destinationSpy;
54 private Id<Destination> destinationId;
58 router = new XMLRouter();
59 source1 = mock(DOMSource.class);
60 source2 = mock(DOMSource.class);
61 source3 = mock(DOMSource.class);
65 public void testNoInputDocumentsRegistered() {
66 Destination destination = new MyDestination(true, Arrays.asList("any"));
67 destinationSpy = spy(destination);
69 destinationId = router.registerDestination(destinationSpy);
70 assertFalse(router.publish("any", source1));
74 public void testOneDestinationNoTransformationSuccess() {
75 destinationSpy = registerDestination(true, "any");
76 registerDocumentType("any");
78 assertTrue(router.publish("any", source1));
79 verify(destinationSpy).receive(same(source1));
81 // Unregister the destination.
82 router.unregisterDestination(destinationId);
83 reset(destinationSpy);
84 assertFalse(router.publish("any", source2));
85 verifyNoMoreInteractions(destinationSpy);
88 private void registerDocumentType(String aType) {
89 DocumentType type = mock(DocumentType.class);
90 when(type.isInstance(any(DOMSource.class))).thenReturn(true);
91 when(type.getName()).thenReturn(aType);
92 Id<DocumentType> typeId = router.addDocumentType(type);
95 private void registerDocumentType(String aType, DOMSource aSource) {
96 DocumentType type = mock(DocumentType.class);
97 when(type.isInstance(same(aSource))).thenReturn(true);
98 when(type.getName()).thenReturn(aType);
99 Id<DocumentType> typeId = router.addDocumentType(type);
102 private Destination registerDestination(boolean aResult, String... types) {
103 Destination destination = new MyDestination(aResult,
104 Arrays.asList(types));
105 Destination myspy = spy(destination);
106 destinationId = router.registerDestination(myspy);
111 public void testOneDestinationNotMatches() {
112 destinationSpy = registerDestination(true);
113 registerDocumentType("any");
115 assertFalse(router.publish("any", source1));
116 verify(destinationSpy, never()).receive(any(DOMSource.class));
120 public void testOneDestinationThrowsException() {
121 destinationSpy = registerDestination(true, "any");
122 registerDocumentType("any");
124 doThrow(new RuntimeException()).when(destinationSpy).receive(
125 any(DOMSource.class));
127 assertFalse(router.publish("any", source1));
128 verify(destinationSpy).receive(same(source1));
132 public void testOneDestinationThrowsExceptionSecondDestinationStillHandled() {
133 testOneDestinationThrowsException();
134 Destination destination2 = new MyDestination(true, Arrays.asList("any"));
135 Destination destinationSpy2 = spy(destination2);
136 Id<Destination> destinationId2 = router
137 .registerDestination(destinationSpy2);
139 assertTrue(router.publish("any", source1));
140 verify(destinationSpy2).receive(same(source1));
144 public void testDestinationChooseFromTargetTypesThrowsException() {
145 destinationSpy = registerDestination(true, "any");
146 registerDocumentType("any");
148 doThrow(new RuntimeException()).when(destinationSpy)
149 .chooseFromTargetTypes((Collection<String>) anyObject());
151 assertFalse(router.publish("any", source1));
152 verify(destinationSpy, never()).receive(same(source1));
156 public void testDestinationChooseFromTargetTypesThrowsExceptionSecondDestinationStillOk() {
157 testDestinationChooseFromTargetTypesThrowsException();
159 Destination destination2 = new MyDestination(true, Arrays.asList("any"));
160 Destination destinationSpy2 = spy(destination2);
161 Id<Destination> destinationId2 = router
162 .registerDestination(destinationSpy2);
163 assertTrue(router.publish("any", source1));
164 verify(destinationSpy, never()).receive(same(source1));
165 verify(destinationSpy2).receive(same(source1));
169 public void testDestinationChooseFromTargetTypesReturnsNull() {
170 destinationSpy = registerDestination(true, "any");
171 registerDocumentType("any");
175 .chooseFromTargetTypes((Collection<String>) anyObject()))
178 assertFalse(router.publish("any", source1));
179 verify(destinationSpy, never()).receive(same(source1));
183 public void testDestinationChooseFromTargetTypesReturnsNullSecondDestinationStillOk() {
184 testDestinationChooseFromTargetTypesReturnsNull();
186 Destination destination2 = new MyDestination(true, Arrays.asList("any"));
187 Destination destinationSpy2 = spy(destination2);
188 Id<Destination> destinationId2 = router
189 .registerDestination(destinationSpy2);
190 assertTrue(router.publish("any", source1));
191 verify(destinationSpy, never()).receive(same(source1));
192 verify(destinationSpy2).receive(same(source1));
196 public void testOneTransformationOneDestination() {
197 registerDocumentType("any");
198 Transformation transformation = mock(Transformation.class);
199 when(transformation.getFromType()).thenReturn("any");
200 when(transformation.getToType()).thenReturn("bla");
201 when(transformation.transform(same(source1))).thenReturn(source2);
202 router.addTransformation(transformation);
204 Destination destination = mock(Destination.class);
206 destination.chooseFromTargetTypes((Collection<String>) anyObject()))
207 .thenReturn(Arrays.asList("bla"));
209 router.registerDestination(destination);
211 when(destination.receive(any(DOMSource.class))).thenReturn(true);
212 assertTrue(router.publish("bla", source1));
214 verify(transformation).transform(source1);
215 verify(destination).receive(same(source2));
217 // now the same when the destination rejects the event.
218 when(destination.receive(any(DOMSource.class))).thenReturn(false);
219 assertFalse(router.publish("bla", source1));
222 private Transformation createTransformation(String aFrom, String aTo,
223 DOMSource aSource, DOMSource aTarget) {
224 Transformation transformation = mock(Transformation.class);
225 when(transformation.getFromType()).thenReturn(aFrom);
226 when(transformation.getToType()).thenReturn(aTo);
227 when(transformation.transform(same(aSource))).thenReturn(aTarget);
228 return transformation;
232 public void testOneTransformationReturnsNull() {
233 registerDocumentType("any");
234 Transformation transformation = createTransformation("any", "bla",
237 router.addTransformation(transformation);
239 Destination destination = mock(Destination.class);
241 destination.chooseFromTargetTypes((Collection<String>) anyObject()))
242 .thenReturn(Arrays.asList("bla"));
243 router.registerDestination(destination);
245 assertFalse(router.publish("bla", source1));
247 verify(transformation).transform(source1);
248 verify(destination, never()).receive(any(DOMSource.class));
250 // add second transformation that behaves normally
251 Transformation transformation2 = createTransformation("any", "bla2",
254 router.addTransformation(transformation2);
256 destination.chooseFromTargetTypes((Collection<String>) anyObject()))
257 .thenReturn(Arrays.asList("bla", "bla2"));
259 reset(transformation);
260 when(transformation.getFromType()).thenReturn("any");
261 when(transformation.getToType()).thenReturn("bla");
262 when(transformation.transform(same(source1))).thenReturn(null);
264 when(destination.receive(any(DOMSource.class))).thenReturn(true);
265 assertTrue(router.publish("bla", source1));
267 verify(transformation).transform(source1);
268 verify(transformation2).transform(source1);
270 verify(destination).receive(same(source2));
275 public void testChooseMultipleDestinationsOneType() {
276 Destination dest1 = registerDestination(true, "any");
277 Destination dest2 = registerDestination(true, "any");
278 registerDocumentType("any");
280 assertTrue(router.publish("source", source1));
282 verify(dest1).receive(same(source1));
283 verify(dest2).receive(same(source1));
287 public void testMultipleDeliveryToOneDestination() {
288 Destination dest = registerDestination(true, "any", "other");
289 registerDocumentType("any", source1);
290 registerDocumentType("other", source2);
291 Transformation transformation = createTransformation("any", "other",
293 router.addTransformation(transformation);
295 assertTrue(router.publish("source", source1));
297 verify(dest).receive(same(source1));
298 verify(dest).receive(same(source2));
302 public void testMultipleTransformations() {
303 Destination dest = registerDestination(true, "any", "other");
304 registerDocumentType("any", source1);
305 registerDocumentType("other", source3);
307 Transformation t1 = createTransformation("any", "intermediate",
309 router.addTransformation(t1);
310 Transformation t2 = createTransformation("intermediate", "other",
312 router.addTransformation(t2);
314 assertTrue(router.publish("source", source1));
316 verify(dest).receive(same(source3));