Turned the API packages into bundles.
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / RobustDestinationTest.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.xmlrouter.impl;
17
18 import static junit.framework.Assert.*;
19 import static org.mockito.Matchers.*;
20 import static org.mockito.Mockito.*;
21
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25
26 import javax.xml.transform.dom.DOMSource;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.wamblee.xmlrouter.common.Id;
31 import org.wamblee.xmlrouter.subscribe.Destination;
32
33 public class RobustDestinationTest {
34
35     private Destination destination;
36     private Destination robust;
37     private DOMSource source;
38
39     @Before
40     public void setUp() {
41         destination = mock(Destination.class);
42         robust = new RobustDestination(new Id<Destination>("100"), destination);
43         source = mock(DOMSource.class);
44     }
45
46     @Test
47     public void testNormalFlow() {
48         when(destination.getName()).thenReturn("name");
49         when(
50             destination.chooseFromTargetTypes((Collection<String>) anyObject()))
51             .thenReturn(Arrays.asList("x"));
52
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));
59
60         when(destination.receive(same(source))).thenReturn(false);
61         assertFalse(robust.receive(source));
62
63     }
64
65     @Test
66     public void testNameReturnsNull() {
67         when(destination.getName()).thenReturn(null);
68         assertEquals(Constants.UNKNOWN_DESTINATION_NAME.toString(),
69             robust.getName());
70     }
71
72     @Test
73     public void testNameThrowsException() {
74         doThrow(new RuntimeException("x")).when(destination).getName();
75         assertEquals(Constants.UNKNOWN_DESTINATION_NAME.toString(),
76             robust.getName());
77     }
78
79     @Test
80     public void testChooseFromTargetTypesReturnsNull() {
81         when(
82             destination.chooseFromTargetTypes((Collection<String>) anyObject()))
83             .thenReturn(null);
84         assertEquals(Collections.EMPTY_LIST,
85             robust.chooseFromTargetTypes(Arrays.asList("x")));
86     }
87
88     @Test
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")));
94     }
95
96     @Test
97     public void testChooseFromTargetTypesReturnsInvalidType() {
98         when(
99             destination.chooseFromTargetTypes((Collection<String>) anyObject()))
100             .thenReturn(Arrays.asList("x", "y"));
101         assertEquals(Arrays.asList("y"),
102             robust.chooseFromTargetTypes(Arrays.asList("y", "z")));
103     }
104
105     @Test
106     public void testReceiveThrowsException() {
107         doThrow(new RuntimeException("x")).when(destination).receive(
108             any(DOMSource.class));
109         assertEquals(false, robust.receive(source));
110     }
111 }