View Javadoc

1   /*
2    * $Id: ActionContextCleanUpTest.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  
22  package org.apache.struts2.dispatcher;
23  
24  import java.io.IOException;
25  import java.util.HashMap;
26  import java.util.LinkedHashMap;
27  import java.util.Map;
28  
29  import javax.servlet.ServletContext;
30  import javax.servlet.ServletException;
31  import javax.servlet.ServletRequest;
32  import javax.servlet.ServletResponse;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  import org.apache.struts2.dispatcher.mapper.ActionMapping;
37  import org.springframework.mock.web.MockFilterConfig;
38  import org.springframework.mock.web.MockHttpServletRequest;
39  import org.springframework.mock.web.MockHttpServletResponse;
40  import org.springframework.mock.web.MockServletContext;
41  
42  import com.mockobjects.servlet.MockFilterChain;
43  
44  import junit.framework.TestCase;
45  
46  /***
47   * @version $Date: 2008-04-27 08:41:38 -0500 (Sun, 27 Apr 2008) $ $Id: ActionContextCleanUpTest.java 651946 2008-04-27 13:41:38Z apetrelli $
48   */
49  public class ActionContextCleanUpTest extends TestCase {
50  
51  
52      protected MockFilterConfig filterConfig;
53      protected MockHttpServletRequest request;
54      protected MockHttpServletResponse response;
55      protected MockFilterChain filterChain;
56      protected MockFilterChain filterChain2;
57      protected MockServletContext servletContext;
58  
59      protected Counter counter;
60      protected Map<String, Integer> _tmpStore;
61      protected InnerDispatcher _dispatcher;
62      protected InnerDispatcher _dispatcher2;
63      protected ActionContextCleanUp cleanUp;
64      protected ActionContextCleanUp cleanUp2;
65  
66  
67      @Override
68      protected void tearDown() throws Exception {
69          filterConfig = null;
70          request = null;
71          response = null;
72          filterChain = null;
73          filterChain2 = null;
74          servletContext = null;
75          counter = null;
76          _tmpStore = null;
77          _dispatcher = null;
78          _dispatcher2 = null;
79          cleanUp = null;
80          cleanUp2 = null;
81      }
82  
83      @Override
84      protected void setUp() throws Exception {
85          Dispatcher.setInstance(null);
86  
87          counter = new Counter();
88          _tmpStore = new LinkedHashMap<String, Integer>();
89  
90          filterConfig = new MockFilterConfig();
91          request = new MockHttpServletRequest();
92          response = new MockHttpServletResponse();
93          servletContext = new MockServletContext();
94          _dispatcher = new InnerDispatcher(servletContext) {
95              @Override
96              public String toString() {
97                  return "dispatcher";
98              }
99          };
100         _dispatcher2 = new InnerDispatcher(servletContext){
101             @Override
102             public String toString() {
103                 return "dispatcher2";
104             }
105         };
106 
107 
108         filterChain = new MockFilterChain() {
109             @Override
110             public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
111                 _tmpStore.put("counter"+(counter.count++), (Integer) request.getAttribute("__cleanup_recursion_counter"));
112             }
113         };
114 
115         cleanUp = new ActionContextCleanUp();
116         cleanUp2 = new ActionContextCleanUp();
117         filterChain2 = new MockFilterChain() {
118             @Override
119             public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
120                 _tmpStore.put("counter"+(counter.count++), (Integer) request.getAttribute("__cleanup_recursion_counter"));
121                 cleanUp2.doFilter(request, response, filterChain);
122             }
123         };
124     }
125 
126 
127     public void testSingle() throws Exception {
128         assertNull(request.getAttribute("__cleanup_recursion_counter"));
129 
130         cleanUp.init(filterConfig);
131         cleanUp.doFilter(request, response, filterChain);
132         cleanUp.destroy();
133 
134         assertEquals(_tmpStore.size(), 1);
135         assertEquals(_tmpStore.get("counter0"), new Integer(1));
136 
137         assertEquals(request.getAttribute("__cleanup_recursion_counter"), new Integer("0"));
138     }
139 
140     public void testMultiple() throws Exception {
141         assertNull(request.getAttribute("__cleanup_recursion_counter"));
142 
143         cleanUp.init(filterConfig);
144         cleanUp2.init(filterConfig);
145         cleanUp.doFilter(request, response, filterChain2);
146         cleanUp2.destroy();
147         cleanUp.destroy();
148 
149         assertEquals(_tmpStore.size(), 2);
150         assertEquals(_tmpStore.get("counter0"), new Integer(1));
151         assertEquals(_tmpStore.get("counter1"), new Integer(2));
152 
153         assertEquals(request.getAttribute("__cleanup_recursion_counter"), new Integer("0"));
154     }
155 
156 
157     class InnerDispatcher extends Dispatcher {
158         public boolean prepare = false;
159         public boolean wrapRequest = false;
160         public boolean service = false;
161 
162         public InnerDispatcher(ServletContext servletContext) {
163             super(servletContext, new HashMap<String,String>());
164         }
165 
166         @Override
167         public void prepare(HttpServletRequest request, HttpServletResponse response) {
168             prepare = true;
169         }
170 
171         @Override
172         public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext) throws IOException {
173             wrapRequest = true;
174             return request;
175         }
176 
177         @Override
178         public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException {
179             service = true;
180         }
181     }
182 
183     class Counter {
184         public int count=0;
185     }
186 }