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;
9 import java.util.Collections;
11 import javax.xml.transform.dom.DOMSource;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.wamblee.xmlrouter.common.Id;
16 import org.wamblee.xmlrouter.subscribe.Destination;
18 public class RobustDestinationTest {
20 private Destination destination;
21 private Destination robust;
22 private DOMSource source;
26 destination = mock(Destination.class);
27 robust = new RobustDestination(new Id<Destination>(100), destination);
28 source = mock(DOMSource.class);
32 public void testNormalFlow() {
33 when(destination.getName()).thenReturn("name");
35 destination.chooseFromTargetTypes((Collection<String>) anyObject()))
36 .thenReturn(Arrays.asList("x"));
38 assertEquals("name", robust.getName());
39 assertEquals(Arrays.asList("x"),
40 robust.chooseFromTargetTypes(Arrays.asList("x", "y")));
41 when(destination.receive(same(source))).thenReturn(true);
42 assertTrue(robust.receive(source));
43 verify(destination).receive(same(source));
45 when(destination.receive(same(source))).thenReturn(false);
46 assertFalse(robust.receive(source));
51 public void testNameReturnsNull() {
52 when(destination.getName()).thenReturn(null);
53 assertEquals(Constants.UNKNOWN_DESTINATION_NAME.toString(),
58 public void testNameThrowsException() {
59 doThrow(new RuntimeException("x")).when(destination).getName();
60 assertEquals(Constants.UNKNOWN_DESTINATION_NAME.toString(),
65 public void testChooseFromTargetTypesReturnsNull() {
67 destination.chooseFromTargetTypes((Collection<String>) anyObject()))
69 assertEquals(Collections.EMPTY_LIST,
70 robust.chooseFromTargetTypes(Arrays.asList("x")));
74 public void testChooseFromTargetTypesThrowsException() {
75 doThrow(new RuntimeException("x")).when(destination)
76 .chooseFromTargetTypes(anyList());
77 assertEquals(Collections.EMPTY_LIST,
78 robust.chooseFromTargetTypes(Arrays.asList("x")));
82 public void testChooseFromTargetTypesReturnsInvalidType() {
84 destination.chooseFromTargetTypes((Collection<String>) anyObject()))
85 .thenReturn(Arrays.asList("x", "y"));
86 assertEquals(Arrays.asList("y"),
87 robust.chooseFromTargetTypes(Arrays.asList("y", "z")));
91 public void testReceiveThrowsException() {
92 doThrow(new RuntimeException("x")).when(destination).receive(
93 any(DOMSource.class));
94 assertEquals(false, robust.receive(source));