removed the prefix argument from ConfigImpl.wrap().
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / RobustTransformationTest.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 javax.xml.transform.dom.DOMSource;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.wamblee.xmlrouter.common.Id;
27 import org.wamblee.xmlrouter.config.Transformation;
28
29 public class RobustTransformationTest {
30     private Transformation transformation;
31     private Transformation robust;
32     private DOMSource source;
33     private DOMSource resSource;
34
35     @Before
36     public void setUp() {
37         transformation = mock(Transformation.class);
38         when(transformation.getId()).thenReturn(new Id<Transformation>("t1"));
39         robust = new RobustTransformation(transformation);
40         source = mock(DOMSource.class);
41         resSource = mock(DOMSource.class);
42     }
43
44     @Test
45     public void testNormalFlow() {
46         when(transformation.getFromType()).thenReturn("from");
47         when(transformation.getToType()).thenReturn("to");
48
49         when(transformation.transform(same(source))).thenReturn(resSource);
50
51         assertEquals("from", robust.getFromType());
52         assertEquals("to", robust.getToType());
53
54         assertSame(resSource, robust.transform(source));
55     }
56
57     @Test
58     public void testFromReturnsNull() {
59         when(transformation.getFromType()).thenReturn(null);
60         assertEquals(Constants.UNKNOWN_DOCUMENT_TYPE.toString(),
61             robust.getFromType());
62     }
63
64     @Test
65     public void testToReturnsNull() {
66         when(transformation.getToType()).thenReturn(null);
67         assertEquals(Constants.UNKNOWN_DOCUMENT_TYPE.toString(),
68             robust.getToType());
69     }
70
71     @Test
72     public void testFromThrowsException() {
73         doThrow(new RuntimeException("x")).when(transformation).getFromType();
74         assertEquals(Constants.UNKNOWN_DOCUMENT_TYPE.toString(),
75             robust.getFromType());
76     }
77
78     @Test
79     public void testToThrowsException() {
80         doThrow(new RuntimeException("x")).when(transformation).getFromType();
81         assertEquals(Constants.UNKNOWN_DOCUMENT_TYPE.toString(),
82             robust.getFromType());
83     }
84
85     @Test
86     public void testTransformReturnsNull() {
87         when(transformation.transform(any(DOMSource.class))).thenReturn(null);
88         assertEquals(null, robust.transform(source));
89     }
90
91     @Test
92     public void testTransformationThrowsException() {
93         doThrow(new RuntimeException("x")).when(transformation).transform(
94             any(DOMSource.class));
95         assertEquals(null, robust.transform(source));
96     }
97 }