1   /*
2    * Copyright 2004-2010 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  
17  package org.seasar.cubby.unit;
18  
19  import static org.easymock.EasyMock.anyObject;
20  import static org.easymock.EasyMock.createMock;
21  import static org.easymock.EasyMock.expect;
22  import static org.easymock.EasyMock.expectLastCall;
23  import static org.easymock.EasyMock.getCurrentArguments;
24  import static org.easymock.EasyMock.isA;
25  import static org.easymock.EasyMock.replay;
26  import static org.easymock.EasyMock.verify;
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertSame;
29  import static org.junit.Assert.assertTrue;
30  
31  import java.io.IOException;
32  import java.util.Arrays;
33  import java.util.Collection;
34  import java.util.HashMap;
35  import java.util.Hashtable;
36  import java.util.Map;
37  
38  import javax.servlet.Filter;
39  import javax.servlet.FilterChain;
40  import javax.servlet.FilterConfig;
41  import javax.servlet.ServletException;
42  import javax.servlet.ServletRequest;
43  import javax.servlet.ServletResponse;
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.http.HttpServletResponse;
46  
47  import org.easymock.IAnswer;
48  import org.junit.After;
49  import org.junit.Before;
50  import org.junit.Test;
51  import org.seasar.cubby.action.ActionContext;
52  import org.seasar.cubby.action.ActionResult;
53  import org.seasar.cubby.action.RequestParameter;
54  import org.seasar.cubby.controller.RequestParser;
55  import org.seasar.cubby.controller.impl.DefaultRequestParser;
56  import org.seasar.cubby.internal.controller.ThreadContext;
57  import org.seasar.cubby.plugin.PluginRegistry;
58  import org.seasar.cubby.plugins.BinderPlugin;
59  import org.seasar.cubby.routing.PathResolver;
60  import org.seasar.cubby.routing.impl.PathResolverImpl;
61  import org.seasar.cubby.routing.impl.PathTemplateParserImpl;
62  import org.seasar.cubby.spi.BeanDescProvider;
63  import org.seasar.cubby.spi.ContainerProvider;
64  import org.seasar.cubby.spi.ConverterProvider;
65  import org.seasar.cubby.spi.PathResolverProvider;
66  import org.seasar.cubby.spi.RequestParserProvider;
67  import org.seasar.cubby.spi.beans.impl.DefaultBeanDescProvider;
68  import org.seasar.cubby.spi.container.Container;
69  import org.seasar.cubby.spi.container.LookupException;
70  import org.seasar.cubby.spi.impl.AbstractRequestParserProvider;
71  
72  public class CubbyRunnerFilterTest {
73  
74  	private PluginRegistry pluginRegistry = PluginRegistry.getInstance();
75  
76  	private final Hashtable<String, Object> attributes = new Hashtable<String, Object>();
77  
78  	private final Map<String, String[]> parameterMap = new HashMap<String, String[]>();
79  
80  	private MockAction mockAction = new MockAction();
81  
82  	private static ActionResult expectActionResult = new ActionResult() {
83  
84  		public void execute(ActionContext actionContext,
85  				HttpServletRequest request, HttpServletResponse response)
86  				throws Exception {
87  		}
88  
89  	};
90  
91  	@Before
92  	public void before() {
93  		PathResolver pathResolver = new PathResolverImpl(
94  				new PathTemplateParserImpl());
95  		pathResolver.add(MockAction.class);
96  
97  		BinderPlugin binderPlugin = new BinderPlugin();
98  		binderPlugin.bind(RequestParserProvider.class).toInstance(
99  				new AbstractRequestParserProvider() {
100 
101 					@Override
102 					protected Collection<RequestParser> getRequestParsers() {
103 						return Arrays
104 								.asList(new RequestParser[]{new DefaultRequestParser()});
105 					}
106 
107 				});
108 		binderPlugin.bind(ContainerProvider.class).toInstance(
109 				new MockContainerProvider(new Container() {
110 
111 					public <T> T lookup(Class<T> type) throws LookupException {
112 						if (MockAction.class.equals(type)) {
113 							return type.cast(mockAction);
114 						}
115 						throw new LookupException("type:" + type);
116 					}
117 
118 				}));
119 		binderPlugin.bind(BeanDescProvider.class).toInstance(
120 				new DefaultBeanDescProvider());
121 		binderPlugin.bind(ConverterProvider.class).toInstance(
122 				new MockConverterProvider());
123 		binderPlugin.bind(PathResolverProvider.class).toInstance(
124 				new MockPathResolverProvider(pathResolver));
125 		pluginRegistry.register(binderPlugin);
126 	}
127 
128 	@After
129 	public void after() {
130 		pluginRegistry.clear();
131 	}
132 
133 	@Test
134 	public void processActionWithFilter() throws Exception {
135 		parameterMap.put("name", new String[]{"abcdefg"});
136 
137 		HttpServletRequest request = createMock(HttpServletRequest.class);
138 		request.setAttribute(isA(String.class), anyObject());
139 		expectLastCall().andStubAnswer(new IAnswer<Void>() {
140 
141 			public Void answer() throws Throwable {
142 				attributes.put((String) getCurrentArguments()[0],
143 						getCurrentArguments()[1]);
144 				return null;
145 			}
146 
147 		});
148 		expect(request.getAttribute(isA(String.class))).andStubAnswer(
149 				new IAnswer<Object>() {
150 
151 					public Object answer() throws Throwable {
152 						return attributes.get(getCurrentArguments()[0]);
153 					}
154 
155 				});
156 		request.removeAttribute(isA(String.class));
157 		expectLastCall().andStubAnswer(new IAnswer<Void>() {
158 
159 			public Void answer() throws Throwable {
160 				attributes.remove(getCurrentArguments()[0]);
161 				return null;
162 			}
163 
164 		});
165 		expect(request.getServletPath()).andReturn("/mock/execute");
166 		expect(request.getPathInfo()).andReturn(null);
167 		expect(request.getParameterMap()).andReturn(parameterMap);
168 		expect(request.getRequestURI()).andReturn("/context/mock/execute");
169 		expect(request.getMethod()).andReturn("GET");
170 		expect(request.getCharacterEncoding()).andReturn("UTF-8");
171 		expect(request.getSession(false)).andReturn(null);
172 		HttpServletResponse response = createMock(HttpServletResponse.class);
173 		replay(request, response);
174 
175 		MockFilter filter = new MockFilter();
176 		ActionResult result = CubbyRunner.processAction(request, response,
177 				filter);
178 		assertSame(expectActionResult, result);
179 		assertEquals("abcdefg", mockAction.getName());
180 
181 		verify(request, response);
182 	}
183 
184 	public static class MockAction {
185 		private String name;
186 
187 		@RequestParameter
188 		public void setName(String name) {
189 			this.name = name;
190 		}
191 
192 		public String getName() {
193 			return name;
194 		}
195 
196 		public ActionResult execute() {
197 			HttpServletRequest request = ThreadContext.getCurrentContext()
198 					.getRequest();
199 			assertTrue((Boolean) request.getAttribute("filtered"));
200 			return expectActionResult;
201 		}
202 	}
203 
204 	public static class MockFilter implements Filter {
205 
206 		public void init(FilterConfig filterConfig) throws ServletException {
207 		}
208 
209 		public void destroy() {
210 		}
211 
212 		public void doFilter(ServletRequest request, ServletResponse response,
213 				FilterChain chain) throws IOException, ServletException {
214 			request.setAttribute("filtered", true);
215 			chain.doFilter(request, response);
216 		}
217 
218 	}
219 
220 }