• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2008 Google Inc.
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.inject.servlet;
18 
19 import static org.easymock.EasyMock.createMock;
20 import static org.easymock.EasyMock.expect;
21 import static org.easymock.EasyMock.replay;
22 import static org.easymock.EasyMock.verify;
23 
24 import com.google.inject.Guice;
25 import com.google.inject.Injector;
26 import com.google.inject.Key;
27 import com.google.inject.Singleton;
28 
29 import junit.framework.TestCase;
30 
31 import java.io.IOException;
32 
33 import javax.servlet.Filter;
34 import javax.servlet.FilterChain;
35 import javax.servlet.FilterConfig;
36 import javax.servlet.ServletConfig;
37 import javax.servlet.ServletException;
38 import javax.servlet.ServletRequest;
39 import javax.servlet.ServletResponse;
40 import javax.servlet.http.HttpServlet;
41 import javax.servlet.http.HttpServletRequest;
42 
43 /**
44  * Tests the FilterPipeline that dispatches to guice-managed servlets,
45  * is a full integration test, with a real injector.
46  *
47  * @author Dhanji R. Prasanna (dhanji gmail com)
48  */
49 public class VarargsServletDispatchIntegrationTest extends TestCase {
50   private static int inits, services, destroys, doFilters;
51 
52   @Override
setUp()53   public void setUp() {
54     inits = 0;
55     services = 0;
56     destroys = 0;
57     doFilters = 0;
58 
59     GuiceFilter.reset();
60   }
61 
testDispatchRequestToManagedPipelineServlets()62   public final void testDispatchRequestToManagedPipelineServlets()
63       throws ServletException, IOException {
64     final Injector injector = Guice.createInjector(new ServletModule() {
65 
66       @Override
67       protected void configureServlets() {
68         serve("/*", "/index.html").with(TestServlet.class);
69 
70         // These servets should never fire... (ordering test)
71         serve("*.html", "/o/*", "/index/*", "*.jsp").with(Key.get(NeverServlet.class));
72       }
73     });
74 
75     final FilterPipeline pipeline = injector.getInstance(FilterPipeline.class);
76 
77     pipeline.initPipeline(null);
78 
79     //create ourselves a mock request with test URI
80     HttpServletRequest requestMock = createMock(HttpServletRequest.class);
81 
82     expect(requestMock.getRequestURI())
83         .andReturn("/index.html")
84         .times(1);
85     expect(requestMock.getContextPath())
86         .andReturn("")
87         .anyTimes();
88 
89     //dispatch request
90     replay(requestMock);
91 
92     pipeline.dispatch(requestMock, null, createMock(FilterChain.class));
93     pipeline.destroyPipeline();
94 
95     verify(requestMock);
96 
97     assertTrue("lifecycle states did not fire correct number of times-- inits: " + inits + "; dos: "
98             + services + "; destroys: " + destroys,
99         inits == 2 && services == 1 && destroys == 2);
100   }
101 
testVarargsSkipDispatchRequestToManagedPipelineServlets()102   public final void testVarargsSkipDispatchRequestToManagedPipelineServlets()
103       throws ServletException, IOException {
104     final Injector injector = Guice.createInjector(new ServletModule() {
105 
106       @Override
107       protected void configureServlets() {
108         serve("/notindex", "/&*", "/index.html").with(TestServlet.class);
109 
110         // These servets should never fire... (ordering test)
111         serve("*.html", "/*", "/index/*", "*.jsp").with(Key.get(NeverServlet.class));
112       }
113     });
114 
115     final FilterPipeline pipeline = injector.getInstance(FilterPipeline.class);
116 
117     pipeline.initPipeline(null);
118 
119     //create ourselves a mock request with test URI
120     HttpServletRequest requestMock = createMock(HttpServletRequest.class);
121 
122     expect(requestMock.getRequestURI())
123         .andReturn("/index.html")
124         .times(3);
125     expect(requestMock.getContextPath())
126         .andReturn("")
127         .anyTimes();
128 
129     //dispatch request
130     replay(requestMock);
131 
132     pipeline.dispatch(requestMock, null, createMock(FilterChain.class));
133     pipeline.destroyPipeline();
134 
135     verify(requestMock);
136 
137     assertTrue("lifecycle states did not fire correct number of times-- inits: " + inits + "; dos: "
138             + services + "; destroys: " + destroys,
139         inits == 2 && services == 1 && destroys == 2);
140   }
141 
testDispatchRequestToManagedPipelineWithFilter()142   public final void testDispatchRequestToManagedPipelineWithFilter()
143       throws ServletException, IOException {
144     final Injector injector = Guice.createInjector(new ServletModule() {
145 
146       @Override
147       protected void configureServlets() {
148         filter("/*").through(TestFilter.class);
149 
150         serve("/*").with(TestServlet.class);
151 
152         // These servets should never fire...
153         serve("*.html", "/y/*", "/index/*", "*.jsp").with(Key.get(NeverServlet.class));
154 
155       }
156     });
157 
158     final FilterPipeline pipeline = injector.getInstance(FilterPipeline.class);
159 
160     pipeline.initPipeline(null);
161 
162     //create ourselves a mock request with test URI
163     HttpServletRequest requestMock = createMock(HttpServletRequest.class);
164 
165     expect(requestMock.getRequestURI())
166         .andReturn("/index.html")
167         .times(2);
168     expect(requestMock.getContextPath())
169         .andReturn("")
170         .anyTimes();
171 
172     //dispatch request
173     replay(requestMock);
174 
175     pipeline.dispatch(requestMock, null, createMock(FilterChain.class));
176 
177     pipeline.destroyPipeline();
178 
179     verify(requestMock);
180 
181     assertTrue("lifecycle states did not fire correct number of times-- inits: " + inits + "; dos: "
182             + services + "; destroys: " + destroys,
183         inits == 3 && services == 1 && destroys == 3 && doFilters == 1);
184   }
185 
186   @Singleton
187   public static class TestServlet extends HttpServlet {
init(ServletConfig filterConfig)188     public void init(ServletConfig filterConfig) throws ServletException {
189       inits++;
190     }
191 
service(ServletRequest servletRequest, ServletResponse servletResponse)192     public void service(ServletRequest servletRequest, ServletResponse servletResponse)
193         throws IOException, ServletException {
194       services++;
195     }
196 
destroy()197     public void destroy() {
198       destroys++;
199     }
200   }
201 
202   @Singleton
203   public static class NeverServlet extends HttpServlet {
init(ServletConfig filterConfig)204     public void init(ServletConfig filterConfig) throws ServletException {
205       inits++;
206     }
207 
service(ServletRequest servletRequest, ServletResponse servletResponse)208     public void service(ServletRequest servletRequest, ServletResponse servletResponse)
209         throws IOException, ServletException {
210       fail("NeverServlet was fired, when it should not have been.");
211     }
212 
destroy()213     public void destroy() {
214       destroys++;
215     }
216   }
217 
218   @Singleton
219   public static class TestFilter implements Filter {
init(FilterConfig filterConfig)220     public void init(FilterConfig filterConfig) throws ServletException {
221       inits++;
222     }
223 
doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)224     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
225         FilterChain filterChain) throws IOException, ServletException {
226       doFilters++;
227       filterChain.doFilter(servletRequest, servletResponse);
228     }
229 
destroy()230     public void destroy() {
231       destroys++;
232     }
233   }
234 }
235