View Javadoc

1   /*
2    * $Id: DefaultActionSupport.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.dispatcher.ng;
22  
23  import com.opensymphony.xwork2.ActionContext;
24  import junit.framework.TestCase;
25  import org.apache.struts2.dispatcher.Dispatcher;
26  import org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter;
27  import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter;
28  import org.springframework.mock.web.*;
29  
30  import javax.servlet.*;
31  import java.io.IOException;
32  import java.util.LinkedList;
33  import java.util.Arrays;
34  
35  /***
36   * Integration tests for the filter
37   */
38  public class TwoFilterIntegrationTest extends TestCase {
39      StrutsExecuteFilter filterExecute;
40      StrutsPrepareFilter filterPrepare;
41      Filter failFilter;
42      private Filter stringFilter;
43  
44      public void setUp() {
45          filterPrepare = new StrutsPrepareFilter();
46          filterExecute = new StrutsExecuteFilter();
47          failFilter = new Filter() {
48              public void init(FilterConfig filterConfig) throws ServletException {}
49              public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
50                  fail("Should never get here");
51              }
52              public void destroy() {}
53          };
54          stringFilter = new Filter() {
55              public void init(FilterConfig filterConfig) throws ServletException {}
56              public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
57                  response.getWriter().write("content");
58                  assertNotNull(ActionContext.getContext());
59                  assertNotNull(Dispatcher.getInstance());
60              }
61              public void destroy() {}
62          };
63      }
64  
65      public void test404() throws ServletException, IOException {
66          MockHttpServletResponse response = run("/foo.action", filterPrepare, filterExecute, failFilter);
67          assertEquals(404, response.getStatus());
68      }
69  
70      public void test200() throws ServletException, IOException {
71          MockHttpServletResponse response = run("/hello.action", filterPrepare, filterExecute, failFilter);
72          assertEquals(200, response.getStatus());
73      }
74  
75      public void testStaticFallthrough() throws ServletException, IOException {
76          MockHttpServletResponse response = run("/foo.txt", filterPrepare, filterExecute, stringFilter);
77          assertEquals(200, response.getStatus());
78          assertEquals("content", response.getContentAsString());
79  
80      }
81  
82      public void testStaticExecute() throws ServletException, IOException {
83          MockHttpServletResponse response = run("/struts/utils.js", filterPrepare, filterExecute, failFilter);
84          assertEquals(200, response.getStatus());
85          assertTrue(response.getContentAsString().contains("StrutsUtils"));
86      }
87  
88      public void testFilterInMiddle() throws ServletException, IOException {
89          Filter middle = new Filter() {
90              public void init(FilterConfig filterConfig) throws ServletException {}
91              public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
92                  assertNotNull(ActionContext.getContext());
93                  assertNotNull(Dispatcher.getInstance());
94                  assertNull(ActionContext.getContext().getActionInvocation());
95                  chain.doFilter(request, response);
96                  assertEquals("hello", ActionContext.getContext().getActionInvocation().getProxy().getActionName());
97              }
98              public void destroy() {}
99          };
100         MockHttpServletResponse response = run("/hello.action", filterPrepare, middle, filterExecute, failFilter);
101         assertEquals(200, response.getStatus());
102     }
103 
104     private MockHttpServletResponse run(String uri, final Filter... filters) throws ServletException, IOException {
105         return run(uri, null, filters);
106     }
107     private MockHttpServletResponse run(String uri, ActionContext existingContext, final Filter... filters) throws ServletException, IOException {
108         final LinkedList<Filter> filterList = new LinkedList<Filter>(Arrays.asList(filters));
109         MockHttpServletRequest request = new MockHttpServletRequest();
110         MockHttpServletResponse response = new MockHttpServletResponse();
111         MockFilterConfig filterConfig = new MockFilterConfig();
112         MockFilterChain filterChain = new MockFilterChain() {
113             @Override
114             public void doFilter(ServletRequest req, ServletResponse res) {
115                 Filter next = (filterList.size() > 0 ? filterList.removeFirst() : null);
116                 if (next != null) {
117                     try {
118                         next.doFilter(req, res, this);
119                     } catch (IOException e) {
120                         throw new RuntimeException(e);
121                     } catch (ServletException e) {
122                         throw new RuntimeException(e);
123                     }
124                 }
125             }
126         };
127 
128         if (existingContext != null) {
129             request.setAttribute(PrepareOperations.CLEANUP_RECURSION_COUNTER, 1);
130         }
131         request.setRequestURI(uri);
132         for (Filter filter : filters) {
133             filter.init(filterConfig);
134         }
135 
136         ActionContext.setContext(existingContext);
137         filterList.removeFirst().doFilter(request, response, filterChain);
138         if (existingContext == null) {
139             assertNull(ActionContext.getContext());
140             assertNull(Dispatcher.getInstance());
141         } else {
142             assertEquals(Integer.valueOf(1), request.getAttribute(PrepareOperations.CLEANUP_RECURSION_COUNTER));
143         }
144         return response;
145     }
146 
147 
148 }