001: /*
002: * $Id: ActionContextCleanUpTest.java 478625 2006-11-23 17:31:52Z wsmoak $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.dispatcher;
022:
023: import java.io.IOException;
024: import java.util.HashMap;
025: import java.util.LinkedHashMap;
026: import java.util.Map;
027:
028: import javax.servlet.ServletContext;
029: import javax.servlet.ServletException;
030: import javax.servlet.ServletRequest;
031: import javax.servlet.ServletResponse;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: import org.apache.struts2.dispatcher.mapper.ActionMapping;
036: import org.springframework.mock.web.MockFilterConfig;
037: import org.springframework.mock.web.MockHttpServletRequest;
038: import org.springframework.mock.web.MockHttpServletResponse;
039: import org.springframework.mock.web.MockServletContext;
040:
041: import com.mockobjects.servlet.MockFilterChain;
042:
043: import junit.framework.TestCase;
044:
045: /**
046: * @version $Date: 2006-11-23 12:31:52 -0500 (Thu, 23 Nov 2006) $ $Id: ActionContextCleanUpTest.java 478625 2006-11-23 17:31:52Z wsmoak $
047: */
048: public class ActionContextCleanUpTest extends TestCase {
049:
050: protected MockFilterConfig filterConfig;
051: protected MockHttpServletRequest request;
052: protected MockHttpServletResponse response;
053: protected MockFilterChain filterChain;
054: protected MockFilterChain filterChain2;
055: protected MockServletContext servletContext;
056:
057: protected Counter counter;
058: protected Map<String, Integer> _tmpStore;
059: protected InnerDispatcher _dispatcher;
060: protected InnerDispatcher _dispatcher2;
061: protected ActionContextCleanUp cleanUp;
062: protected ActionContextCleanUp cleanUp2;
063:
064: @Override
065: protected void tearDown() throws Exception {
066: filterConfig = null;
067: request = null;
068: response = null;
069: filterChain = null;
070: filterChain2 = null;
071: servletContext = null;
072: counter = null;
073: _tmpStore = null;
074: _dispatcher = null;
075: _dispatcher2 = null;
076: cleanUp = null;
077: cleanUp2 = null;
078: }
079:
080: @Override
081: protected void setUp() throws Exception {
082: Dispatcher.setInstance(null);
083:
084: counter = new Counter();
085: _tmpStore = new LinkedHashMap<String, Integer>();
086:
087: filterConfig = new MockFilterConfig();
088: request = new MockHttpServletRequest();
089: response = new MockHttpServletResponse();
090: servletContext = new MockServletContext();
091: _dispatcher = new InnerDispatcher(servletContext) {
092: @Override
093: public String toString() {
094: return "dispatcher";
095: }
096: };
097: _dispatcher2 = new InnerDispatcher(servletContext) {
098: @Override
099: public String toString() {
100: return "dispatcher2";
101: }
102: };
103:
104: filterChain = new MockFilterChain() {
105: @Override
106: public void doFilter(ServletRequest request,
107: ServletResponse response) throws IOException,
108: ServletException {
109: _tmpStore
110: .put(
111: "counter" + (counter.count++),
112: (Integer) request
113: .getAttribute("__cleanup_recursion_counter"));
114: }
115: };
116:
117: cleanUp = new ActionContextCleanUp();
118: cleanUp2 = new ActionContextCleanUp();
119: filterChain2 = new MockFilterChain() {
120: @Override
121: public void doFilter(ServletRequest request,
122: ServletResponse response) throws IOException,
123: ServletException {
124: _tmpStore
125: .put(
126: "counter" + (counter.count++),
127: (Integer) request
128: .getAttribute("__cleanup_recursion_counter"));
129: cleanUp2.doFilter(request, response, filterChain);
130: }
131: };
132: }
133:
134: public void testSingle() throws Exception {
135: assertNull(request.getAttribute("__cleanup_recursion_counter"));
136:
137: cleanUp.init(filterConfig);
138: cleanUp.doFilter(request, response, filterChain);
139: cleanUp.destroy();
140:
141: assertEquals(_tmpStore.size(), 1);
142: assertEquals(_tmpStore.get("counter0"), new Integer(1));
143:
144: assertEquals(request
145: .getAttribute("__cleanup_recursion_counter"),
146: new Integer("0"));
147: }
148:
149: public void testMultiple() throws Exception {
150: assertNull(request.getAttribute("__cleanup_recursion_counter"));
151:
152: cleanUp.init(filterConfig);
153: cleanUp2.init(filterConfig);
154: cleanUp.doFilter(request, response, filterChain2);
155: cleanUp2.destroy();
156: cleanUp.destroy();
157:
158: assertEquals(_tmpStore.size(), 2);
159: assertEquals(_tmpStore.get("counter0"), new Integer(1));
160: assertEquals(_tmpStore.get("counter1"), new Integer(2));
161:
162: assertEquals(request
163: .getAttribute("__cleanup_recursion_counter"),
164: new Integer("0"));
165: }
166:
167: class InnerDispatcher extends Dispatcher {
168: public boolean prepare = false;
169: public boolean wrapRequest = false;
170: public boolean service = false;
171:
172: public InnerDispatcher(ServletContext servletContext) {
173: super (servletContext, new HashMap<String, String>());
174: }
175:
176: @Override
177: public void prepare(HttpServletRequest request,
178: HttpServletResponse response) {
179: prepare = true;
180: }
181:
182: @Override
183: public HttpServletRequest wrapRequest(
184: HttpServletRequest request,
185: ServletContext servletContext) throws IOException {
186: wrapRequest = true;
187: return request;
188: }
189:
190: @Override
191: public void serviceAction(HttpServletRequest request,
192: HttpServletResponse response, ServletContext context,
193: ActionMapping mapping) throws ServletException {
194: service = true;
195: }
196: }
197:
198: class Counter {
199: public int count = 0;
200: }
201: }
|