now making sure that ids re prefixed by the config id.
[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.config.Transformation;
27
28 public class RobustTransformationTest {
29     private Transformation transformation;
30     private Transformation robust;
31     private DOMSource source;
32     private DOMSource resSource;
33
34     @Before
35     public void setUp() {
36         transformation = mock(Transformation.class);
37         robust = new RobustTransformation("transformation", transformation);
38         source = mock(DOMSource.class);
39         resSource = mock(DOMSource.class);
40     }
41
42     @Test
43     public void testNormalFlow() {
44         when(transformation.getFromType()).thenReturn("from");
45         when(transformation.getToType()).thenReturn("to");
46
47         when(transformation.transform(same(source))).thenReturn(resSource);
48
49         assertEquals("from", robust.getFromType());
50         assertEquals("to", robust.getToType());
51
52         assertSame(resSource, robust.transform(source));
53     }
54
55     @Test
56     public void testFromReturnsNull() {
57         when(transformation.getFromType()).thenReturn(null);
58         assertEquals(Constants.UNKNOWN_DOCUMENT_TYPE.toString(),
59             robust.getFromType());
60     }
61
62     @Test
63     public void testToReturnsNull() {
64         when(transformation.getToType()).thenReturn(null);
65         assertEquals(Constants.UNKNOWN_DOCUMENT_TYPE.toString(),
66             robust.getToType());
67     }
68
69     @Test
70     public void testFromThrowsException() {
71         doThrow(new RuntimeException("x")).when(transformation).getFromType();
72         assertEquals(Constants.UNKNOWN_DOCUMENT_TYPE.toString(),
73             robust.getFromType());
74     }
75
76     @Test
77     public void testToThrowsException() {
78         doThrow(new RuntimeException("x")).when(transformation).getFromType();
79         assertEquals(Constants.UNKNOWN_DOCUMENT_TYPE.toString(),
80             robust.getFromType());
81     }
82
83     @Test
84     public void testTransformReturnsNull() {
85         when(transformation.transform(any(DOMSource.class))).thenReturn(null);
86         assertEquals(null, robust.transform(source));
87     }
88
89     @Test
90     public void testTransformationThrowsException() {
91         doThrow(new RuntimeException("x")).when(transformation).transform(
92             any(DOMSource.class));
93         assertEquals(null, robust.transform(source));
94     }
95 }