View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.guice.web;
20  
21  import java.util.Arrays;
22  
23  import javax.servlet.Filter;
24  import javax.servlet.FilterChain;
25  import javax.servlet.ServletRequest;
26  import javax.servlet.ServletResponse;
27  
28  import org.easymock.Capture;
29  import org.easymock.IMocksControl;
30  import org.junit.Test;
31  
32  import static org.easymock.EasyMock.and;
33  import static org.easymock.EasyMock.anyObject;
34  import static org.easymock.EasyMock.capture;
35  import static org.easymock.EasyMock.createStrictControl;
36  import static org.easymock.EasyMock.same;
37  
38  public class SimpleFilterChainTest {
39      @Test
40      public void testDoFilter() throws Exception {
41          IMocksControl ctrl = createStrictControl();
42  
43          FilterChain originalChain = ctrl.createMock(FilterChain.class);
44          Filter filter1 = ctrl.createMock("filter1", Filter.class);
45          Filter filter2 = ctrl.createMock("filter2", Filter.class);
46  
47          ServletRequest request = ctrl.createMock(ServletRequest.class);
48          ServletResponse response = ctrl.createMock(ServletResponse.class);
49  
50          Capture<FilterChain> fc1 = Capture.newInstance();
51          Capture<FilterChain> fc2 = Capture.newInstance();
52          filter1.doFilter(same(request), same(response), and(anyObject(FilterChain.class), capture(fc1)));
53          filter2.doFilter(same(request), same(response), and(anyObject(FilterChain.class), capture(fc2)));
54          originalChain.doFilter(request, response);
55  
56          ctrl.replay();
57  
58          SimpleFilterChain underTest = new SimpleFilterChain(originalChain, Arrays.asList(filter1, filter2).iterator());
59  
60          // all we actually care about is that, if we keep calling the filter chain, everything is called in the right
61          // order - we don't care what fc actually contains
62          underTest.doFilter(request, response);
63          fc1.getValue().doFilter(request, response);
64          fc2.getValue().doFilter(request, response);
65  
66          ctrl.verify();
67      }
68  }