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 org.mockito.Matchers.*;
19 import static org.mockito.Mockito.*;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.logging.Level;
25 import javax.xml.transform.dom.DOMSource;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.wamblee.general.SystemClock;
30 import org.wamblee.xmlrouter.config.DocumentType;
31 import org.wamblee.xmlrouter.config.RouterConfig;
32 import org.wamblee.xmlrouter.config.Transformation;
33 import org.wamblee.xmlrouter.listener.EventInfo;
34 import org.wamblee.xmlrouter.listener.EventListener;
35 import org.wamblee.xmlrouter.listener.LoggingEventListener;
36 import org.wamblee.xmlrouter.subscribe.Destination;
38 public class XMLRouterFunctionTest {
40 private XMLRouterConfiguration config;
41 private XMLRouter routerService;
42 private XMLRouterConfigService configService;
44 private EventListener listener;
46 private DOMSource source1;
47 private DOMSource source2;
51 config = new XMLRouterConfigurationImpl();
52 EventListener logListener = new LoggingEventListener(Level.INFO);
53 listener = spy(logListener);
54 routerService = new XMLRouter(new SystemClock(), config, listener);
55 configService = new XMLRouterConfigService(config);
57 source1 = mock(DOMSource.class);
58 source2 = mock(DOMSource.class);
61 private RouterConfig registerDocumentType(String aType) {
62 DocumentType type = mock(DocumentType.class);
63 when(type.isInstance(any(DOMSource.class))).thenReturn(true);
64 when(type.getName()).thenReturn(aType);
65 RouterConfig routerConfig = configService.emptyConfig();
66 routerConfig.documentTypeConfig().add(type);
71 public void testOneTransformationOneDestination() {
72 RouterConfig routerConfig = registerDocumentType("any");
73 Transformation transformation = mock(Transformation.class);
74 when(transformation.getName()).thenReturn("trans");
75 when(transformation.getFromType()).thenReturn("any");
76 when(transformation.getToType()).thenReturn("bla");
77 when(transformation.transform(same(source1))).thenReturn(source2);
78 routerConfig.transformationConfig().add(transformation);
80 configService.apply(routerConfig, null);
82 Destination destination = mock(Destination.class);
84 destination.chooseFromTargetTypes((Collection<String>) anyObject()))
85 .thenReturn(Arrays.asList("bla"));
87 routerService.registerDestination(destination);
89 when(destination.receive(any(DOMSource.class))).thenReturn(true);
90 routerService.publish("bla", source1);
91 verify(listener).delivered(any(EventInfo.class),
92 anyListOf(Transformation.class), anyLong(), anyString(), eq(true));
94 verify(transformation).transform(source1);
95 verify(destination).receive(same(source2));
97 // now the same when the destination rejects the event.
98 when(destination.receive(any(DOMSource.class))).thenReturn(false);
99 routerService.publish("bla", source1);
100 verify(listener).notDelivered(any(EventInfo.class));