3a948eb8c9c29cb650dc0f64827e2a019e4c2a34
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / RobustFilterTest.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.Filter;
28
29 public class RobustFilterTest {
30
31     private Filter filter;
32     private Filter robust;
33     private DOMSource source;
34
35     @Before
36     public void setUp() {
37         filter = mock(Filter.class);
38         robust = new RobustFilter(new Id<Filter>(10), filter);
39         source = mock(DOMSource.class);
40     }
41
42     @Test
43     public void testNoEzception() {
44         when(filter.isAllowed(anyString(), any(DOMSource.class))).thenReturn(
45             true);
46         assertTrue(robust.isAllowed("xx", source));
47         verify(filter).isAllowed(eq("xx"), same(source));
48
49         reset(filter);
50         when(filter.isAllowed(anyString(), any(DOMSource.class))).thenReturn(
51             false);
52         assertFalse(robust.isAllowed("xx", source));
53         verify(filter).isAllowed(eq("xx"), same(source));
54     }
55
56     @Test
57     public void testException() {
58         doThrow(new RuntimeException("bla")).when(filter).isAllowed(
59             anyString(), any(DOMSource.class));
60         assertTrue(robust.isAllowed("xx", source));
61         verify(filter).isAllowed(eq("xx"), same(source));
62
63     }
64 }