Restructured the project creating a router directory for the router bundle implementa...
[xmlrouter] / router / 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 java.util.UUID;
23
24 import javax.xml.transform.dom.DOMSource;
25
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.wamblee.xmlrouter.common.Id;
29 import org.wamblee.xmlrouter.config.Filter;
30
31 public class RobustFilterTest {
32
33     private Filter filter;
34     private Filter robust;
35     private DOMSource source;
36
37     @Before
38     public void setUp() {
39         filter = mock(Filter.class);
40         when(filter.getId()).thenReturn(
41             new Id<Filter>(UUID.randomUUID().toString()));
42         robust = new RobustFilter("filter", filter);
43         source = mock(DOMSource.class);
44     }
45
46     @Test
47     public void testNoEzception() {
48         when(filter.isAllowed(anyString(), any(DOMSource.class))).thenReturn(
49             true);
50         assertTrue(robust.isAllowed("xx", source));
51         verify(filter).isAllowed(eq("xx"), same(source));
52
53         reset(filter);
54         when(filter.isAllowed(anyString(), any(DOMSource.class))).thenReturn(
55             false);
56         assertFalse(robust.isAllowed("xx", source));
57         verify(filter).isAllowed(eq("xx"), same(source));
58     }
59
60     @Test
61     public void testException() {
62         doThrow(new RuntimeException("bla")).when(filter).isAllowed(
63             anyString(), any(DOMSource.class));
64         assertTrue(robust.isAllowed("xx", source));
65         verify(filter).isAllowed(eq("xx"), same(source));
66
67     }
68 }