View Javadoc

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