initial versions.
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / RobustDestinationTest.java
1 package org.wamblee.xmlrouter.impl;
2
3 import static junit.framework.Assert.*;
4 import static org.mockito.Matchers.*;
5 import static org.mockito.Mockito.*;
6
7 import java.util.Arrays;
8 import java.util.Collection;
9 import java.util.Collections;
10
11 import javax.xml.transform.dom.DOMSource;
12
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.wamblee.xmlrouter.common.Id;
16 import org.wamblee.xmlrouter.subscribe.Destination;
17
18 public class RobustDestinationTest {
19
20     private Destination destination;
21     private Destination robust;
22     private DOMSource source;
23
24     @Before
25     public void setUp() {
26         destination = mock(Destination.class);
27         robust = new RobustDestination(new Id<Destination>(100), destination);
28         source = mock(DOMSource.class);
29     }
30
31     @Test
32     public void testNormalFlow() {
33         when(destination.getName()).thenReturn("name");
34         when(
35             destination.chooseFromTargetTypes((Collection<String>) anyObject()))
36             .thenReturn(Arrays.asList("x"));
37
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));
44
45         when(destination.receive(same(source))).thenReturn(false);
46         assertFalse(robust.receive(source));
47
48     }
49
50     @Test
51     public void testNameReturnsNull() {
52         when(destination.getName()).thenReturn(null);
53         assertEquals(Constants.UNKNOWN_DESTINATION_NAME.toString(),
54             robust.getName());
55     }
56
57     @Test
58     public void testNameThrowsException() {
59         doThrow(new RuntimeException("x")).when(destination).getName();
60         assertEquals(Constants.UNKNOWN_DESTINATION_NAME.toString(),
61             robust.getName());
62     }
63
64     @Test
65     public void testChooseFromTargetTypesReturnsNull() {
66         when(
67             destination.chooseFromTargetTypes((Collection<String>) anyObject()))
68             .thenReturn(null);
69         assertEquals(Collections.EMPTY_LIST,
70             robust.chooseFromTargetTypes(Arrays.asList("x")));
71     }
72
73     @Test
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")));
79     }
80
81     @Test
82     public void testChooseFromTargetTypesReturnsInvalidType() {
83         when(
84             destination.chooseFromTargetTypes((Collection<String>) anyObject()))
85             .thenReturn(Arrays.asList("x", "y"));
86         assertEquals(Arrays.asList("y"),
87             robust.chooseFromTargetTypes(Arrays.asList("y", "z")));
88     }
89
90     @Test
91     public void testReceiveThrowsException() {
92         doThrow(new RuntimeException("x")).when(destination).receive(
93             any(DOMSource.class));
94         assertEquals(false, robust.receive(source));
95     }
96 }