refactoring of the config interface towards more reuse in the implementation and...
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / RobustDocumentTypeTest.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.DocumentType;
28
29 public class RobustDocumentTypeTest {
30
31     private DocumentType documentType;
32     private DocumentType robust;
33     private DOMSource source;
34
35     @Before
36     public void setUp() {
37         documentType = mock(DocumentType.class);
38         robust = new RobustDocumentType(new Id<DocumentType>(10), documentType);
39         source = mock(DOMSource.class);
40     }
41
42     @Test
43     public void testNoEzception() {
44         when(documentType.isInstance(any(DOMSource.class))).thenReturn(true);
45         when(documentType.getName()).thenReturn("hello");
46         assertTrue(robust.isInstance(source));
47         assertEquals("hello", robust.getName());
48
49         verify(documentType).isInstance(same(source));
50
51         reset(documentType);
52         when(documentType.isInstance(any(DOMSource.class))).thenReturn(false);
53         assertFalse(robust.isInstance(source));
54         verify(documentType).isInstance(same(source));
55     }
56
57     @Test
58     public void testExceptionInIsInstance() {
59         doThrow(new RuntimeException("bla")).when(documentType).isInstance(
60             any(DOMSource.class));
61         assertFalse(robust.isInstance(source));
62         verify(documentType).isInstance(same(source));
63     }
64
65     @Test
66     public void testExceptionInValidate() {
67         doThrow(new RuntimeException("bla")).when(documentType).validate(
68             any(DOMSource.class));
69         assertFalse(robust.validate(source));
70         verify(documentType).validate(same(source));
71     }
72
73     @Test
74     public void testExceptionInGetName() {
75         doThrow(new RuntimeException("bla")).when(documentType).getName();
76         assertEquals(Constants.UNKNOWN_DOCUMENT_TYPE.toString(),
77             robust.getName());
78     }
79
80     @Test
81     public void testGetNameReturnsNull() {
82         when(documentType.getName()).thenReturn(null);
83         assertEquals(Constants.UNKNOWN_DOCUMENT_TYPE.toString(),
84             robust.getName());
85     }
86 }