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 com.google.common.collect.Iterators;
22  import org.easymock.Capture;
23  import org.easymock.IMocksControl;
24  import org.junit.Test;
25  
26  import javax.servlet.Filter;
27  import javax.servlet.FilterChain;
28  import javax.servlet.ServletRequest;
29  import javax.servlet.ServletResponse;
30  
31  import static org.easymock.EasyMock.*;
32  
33  public class SimpleFilterChainTest {
34      @Test
35      public void testDoFilter() throws Exception {
36          IMocksControl ctrl = createStrictControl();
37  
38          FilterChain originalChain = ctrl.createMock(FilterChain.class);
39          Filter filter1 = ctrl.createMock("filter1", Filter.class);
40          Filter filter2 = ctrl.createMock("filter2", Filter.class);
41  
42          ServletRequest request = ctrl.createMock(ServletRequest.class);
43          ServletResponse response = ctrl.createMock(ServletResponse.class);
44  
45          Capture<FilterChain> fc1 = new Capture<FilterChain>();
46          Capture<FilterChain> fc2 = new Capture<FilterChain>();
47          filter1.doFilter(same(request), same(response), and(anyObject(FilterChain.class), capture(fc1)));
48          filter2.doFilter(same(request), same(response), and(anyObject(FilterChain.class), capture(fc2)));
49          originalChain.doFilter(request, response);
50  
51          ctrl.replay();
52  
53          SimpleFilterChain underTest = new SimpleFilterChain(originalChain, Iterators.forArray(filter1, filter2));
54  
55          // all we actually care about is that, if we keep calling the filter chain, everything is called in the right
56          // order - we don't care what fc actually contains
57          underTest.doFilter(request, response);
58          fc1.getValue().doFilter(request, response);
59          fc2.getValue().doFilter(request, response);
60  
61          ctrl.verify();
62      }
63  }