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.Collections;
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.subscribe.Destination;
33 public class RobustDestinationTest {
35 private Destination destination;
36 private Destination robust;
37 private DOMSource source;
41 destination = mock(Destination.class);
42 robust = new RobustDestination(new Id<Destination>("100"), destination);
43 source = mock(DOMSource.class);
47 public void testNormalFlow() {
48 when(destination.getName()).thenReturn("name");
50 destination.chooseFromTargetTypes((Collection<String>) anyObject()))
51 .thenReturn(Arrays.asList("x"));
53 assertEquals("name", robust.getName());
54 assertEquals(Arrays.asList("x"),
55 robust.chooseFromTargetTypes(Arrays.asList("x", "y")));
56 when(destination.receive(same(source))).thenReturn(true);
57 assertTrue(robust.receive(source));
58 verify(destination).receive(same(source));
60 when(destination.receive(same(source))).thenReturn(false);
61 assertFalse(robust.receive(source));
66 public void testNameReturnsNull() {
67 when(destination.getName()).thenReturn(null);
68 assertEquals(Constants.UNKNOWN_DESTINATION_NAME.toString(),
73 public void testNameThrowsException() {
74 doThrow(new RuntimeException("x")).when(destination).getName();
75 assertEquals(Constants.UNKNOWN_DESTINATION_NAME.toString(),
80 public void testChooseFromTargetTypesReturnsNull() {
82 destination.chooseFromTargetTypes((Collection<String>) anyObject()))
84 assertEquals(Collections.EMPTY_LIST,
85 robust.chooseFromTargetTypes(Arrays.asList("x")));
89 public void testChooseFromTargetTypesThrowsException() {
90 doThrow(new RuntimeException("x")).when(destination)
91 .chooseFromTargetTypes(anyList());
92 assertEquals(Collections.EMPTY_LIST,
93 robust.chooseFromTargetTypes(Arrays.asList("x")));
97 public void testChooseFromTargetTypesReturnsInvalidType() {
99 destination.chooseFromTargetTypes((Collection<String>) anyObject()))
100 .thenReturn(Arrays.asList("x", "y"));
101 assertEquals(Arrays.asList("y"),
102 robust.chooseFromTargetTypes(Arrays.asList("y", "z")));
106 public void testReceiveThrowsException() {
107 doThrow(new RuntimeException("x")).when(destination).receive(
108 any(DOMSource.class));
109 assertEquals(false, robust.receive(source));