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.filter;
18  
19  import static org.easymock.EasyMock.anyObject;
20  import static org.easymock.EasyMock.createNiceMock;
21  import static org.easymock.EasyMock.expect;
22  import static org.easymock.EasyMock.replay;
23  import static org.easymock.EasyMock.verify;
24  
25  import java.io.IOException;
26  import java.util.Collections;
27  import java.util.Map;
28  
29  import javax.servlet.FilterChain;
30  import javax.servlet.FilterConfig;
31  import javax.servlet.ServletException;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  
35  import org.junit.Test;
36  import org.seasar.cubby.internal.controller.ActionProcessor;
37  import org.seasar.cubby.internal.controller.ActionResultWrapper;
38  import org.seasar.cubby.routing.PathInfo;
39  import org.seasar.cubby.routing.PathResolver;
40  import org.seasar.cubby.routing.Routing;
41  
42  public class CubbyFilterLifecycleTest {
43  
44  	@Test
45  	public void unmatchAnyAction() throws ServletException, IOException {
46  		final FilterConfig filterConfig = createNiceMock(FilterConfig.class);
47  		expect(filterConfig.getInitParameter("ignorePathPattern")).andReturn(
48  				"/js/.*,/img/.*");
49  		final FilterChain filterChain = createNiceMock(FilterChain.class);
50  		final HttpServletRequest request = createNiceMock(HttpServletRequest.class);
51  		expect(request.getServletPath()).andStubReturn("/a/b");
52  		expect(request.getMethod()).andStubReturn("GET");
53  		expect(request.getCharacterEncoding()).andStubReturn("UTF-8");
54  		final HttpServletResponse response = createNiceMock(HttpServletResponse.class);
55  		final PathResolver pathResolver = createNiceMock(PathResolver.class);
56  		replay(filterConfig, filterChain, request, response, pathResolver);
57  
58  		final CubbyFilter cubbyFilter = new CubbyFilter() {
59  
60  			@Override
61  			protected PathResolver createPathResolver() {
62  				return pathResolver;
63  			}
64  
65  		};
66  		cubbyFilter.init(filterConfig);
67  		cubbyFilter.doFilter(request, response, filterChain);
68  		cubbyFilter.destroy();
69  
70  		verify(filterConfig, filterChain, request, response, pathResolver);
71  	}
72  
73  	@Test
74  	public void matchAnyAction() throws Exception {
75  		final FilterConfig filterConfig = createNiceMock(FilterConfig.class);
76  		expect(filterConfig.getInitParameter("ignorePathPattern")).andReturn(
77  				"/js/.*,/img/.*");
78  		final FilterChain filterChain = createNiceMock(FilterChain.class);
79  		final HttpServletRequest request = createNiceMock(HttpServletRequest.class);
80  		expect(request.getServletPath()).andStubReturn("/a/b");
81  		expect(request.getMethod()).andStubReturn("GET");
82  		expect(request.getCharacterEncoding()).andStubReturn("UTF-8");
83  		final HttpServletResponse response = createNiceMock(HttpServletResponse.class);
84  		final PathInfo pathInfo = createNiceMock(PathInfo.class);
85  		final PathResolver pathResolver = createNiceMock(PathResolver.class);
86  		expect(pathResolver.getPathInfo("/a/b", "GET", "UTF-8")).andReturn(
87  				pathInfo);
88  		final ActionResultWrapper actionResultWrapper = createNiceMock(ActionResultWrapper.class);
89  		final ActionProcessor actionProcessor = createNiceMock(ActionProcessor.class);
90  		expect(
91  				actionProcessor.process((HttpServletRequest) anyObject(),
92  						(HttpServletResponse) anyObject(),
93  						(Routing) anyObject())).andStubReturn(
94  				actionResultWrapper);
95  		replay(filterConfig, filterChain, request, response, pathResolver,
96  				pathInfo, actionProcessor);
97  
98  		final CubbyFilter cubbyFilter = new CubbyFilter() {
99  
100 			@Override
101 			protected PathResolver createPathResolver() {
102 				return pathResolver;
103 			}
104 
105 			@Override
106 			protected Map<String, Object[]> parseRequest(
107 					HttpServletRequest request) {
108 				return Collections.emptyMap();
109 			}
110 
111 			@Override
112 			protected ActionProcessor createActionProcessor() {
113 				return actionProcessor;
114 			}
115 
116 		};
117 		cubbyFilter.init(filterConfig);
118 		cubbyFilter.doFilter(request, response, filterChain);
119 		cubbyFilter.destroy();
120 
121 		verify(filterConfig, filterChain, request, response, pathResolver,
122 				pathInfo, actionProcessor);
123 	}
124 }