PageRenderTime 763ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/servlet/test/com/google/inject/servlet/VarargsServletDispatchIntegrationTest.java

https://code.google.com/
Java | 234 lines | 150 code | 54 blank | 30 comment | 17 complexity | c6d31a0e9f883c8feb8bfda1525ca3dc MD5 | raw file
Possible License(s): Apache-2.0
  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. package com.google.inject.servlet;
  17. import static org.easymock.EasyMock.createMock;
  18. import static org.easymock.EasyMock.expect;
  19. import static org.easymock.EasyMock.replay;
  20. import static org.easymock.EasyMock.verify;
  21. import com.google.inject.Guice;
  22. import com.google.inject.Injector;
  23. import com.google.inject.Key;
  24. import com.google.inject.Singleton;
  25. import junit.framework.TestCase;
  26. import java.io.IOException;
  27. import javax.servlet.Filter;
  28. import javax.servlet.FilterChain;
  29. import javax.servlet.FilterConfig;
  30. import javax.servlet.ServletConfig;
  31. import javax.servlet.ServletException;
  32. import javax.servlet.ServletRequest;
  33. import javax.servlet.ServletResponse;
  34. import javax.servlet.http.HttpServlet;
  35. import javax.servlet.http.HttpServletRequest;
  36. /**
  37. * Tests the FilterPipeline that dispatches to guice-managed servlets,
  38. * is a full integration test, with a real injector.
  39. *
  40. * @author Dhanji R. Prasanna (dhanji gmail com)
  41. */
  42. public class VarargsServletDispatchIntegrationTest extends TestCase {
  43. private static int inits, services, destroys, doFilters;
  44. @Override
  45. public void setUp() {
  46. inits = 0;
  47. services = 0;
  48. destroys = 0;
  49. doFilters = 0;
  50. GuiceFilter.reset();
  51. }
  52. public final void testDispatchRequestToManagedPipelineServlets()
  53. throws ServletException, IOException {
  54. final Injector injector = Guice.createInjector(new ServletModule() {
  55. @Override
  56. protected void configureServlets() {
  57. serve("/*", "/index.html").with(TestServlet.class);
  58. // These servets should never fire... (ordering test)
  59. serve("*.html", "/o/*", "/index/*", "*.jsp").with(Key.get(NeverServlet.class));
  60. }
  61. });
  62. final FilterPipeline pipeline = injector.getInstance(FilterPipeline.class);
  63. pipeline.initPipeline(null);
  64. //create ourselves a mock request with test URI
  65. HttpServletRequest requestMock = createMock(HttpServletRequest.class);
  66. expect(requestMock.getRequestURI())
  67. .andReturn("/index.html")
  68. .times(1);
  69. expect(requestMock.getContextPath())
  70. .andReturn("")
  71. .anyTimes();
  72. //dispatch request
  73. replay(requestMock);
  74. pipeline.dispatch(requestMock, null, createMock(FilterChain.class));
  75. pipeline.destroyPipeline();
  76. verify(requestMock);
  77. assertTrue("lifecycle states did not fire correct number of times-- inits: " + inits + "; dos: "
  78. + services + "; destroys: " + destroys,
  79. inits == 2 && services == 1 && destroys == 2);
  80. }
  81. public final void testVarargsSkipDispatchRequestToManagedPipelineServlets()
  82. throws ServletException, IOException {
  83. final Injector injector = Guice.createInjector(new ServletModule() {
  84. @Override
  85. protected void configureServlets() {
  86. serve("/notindex", "/&*", "/index.html").with(TestServlet.class);
  87. // These servets should never fire... (ordering test)
  88. serve("*.html", "/*", "/index/*", "*.jsp").with(Key.get(NeverServlet.class));
  89. }
  90. });
  91. final FilterPipeline pipeline = injector.getInstance(FilterPipeline.class);
  92. pipeline.initPipeline(null);
  93. //create ourselves a mock request with test URI
  94. HttpServletRequest requestMock = createMock(HttpServletRequest.class);
  95. expect(requestMock.getRequestURI())
  96. .andReturn("/index.html")
  97. .times(3);
  98. expect(requestMock.getContextPath())
  99. .andReturn("")
  100. .anyTimes();
  101. //dispatch request
  102. replay(requestMock);
  103. pipeline.dispatch(requestMock, null, createMock(FilterChain.class));
  104. pipeline.destroyPipeline();
  105. verify(requestMock);
  106. assertTrue("lifecycle states did not fire correct number of times-- inits: " + inits + "; dos: "
  107. + services + "; destroys: " + destroys,
  108. inits == 2 && services == 1 && destroys == 2);
  109. }
  110. public final void testDispatchRequestToManagedPipelineWithFilter()
  111. throws ServletException, IOException {
  112. final Injector injector = Guice.createInjector(new ServletModule() {
  113. @Override
  114. protected void configureServlets() {
  115. filter("/*").through(TestFilter.class);
  116. serve("/*").with(TestServlet.class);
  117. // These servets should never fire...
  118. serve("*.html", "/y/*", "/index/*", "*.jsp").with(Key.get(NeverServlet.class));
  119. }
  120. });
  121. final FilterPipeline pipeline = injector.getInstance(FilterPipeline.class);
  122. pipeline.initPipeline(null);
  123. //create ourselves a mock request with test URI
  124. HttpServletRequest requestMock = createMock(HttpServletRequest.class);
  125. expect(requestMock.getRequestURI())
  126. .andReturn("/index.html")
  127. .times(2);
  128. expect(requestMock.getContextPath())
  129. .andReturn("")
  130. .anyTimes();
  131. //dispatch request
  132. replay(requestMock);
  133. pipeline.dispatch(requestMock, null, createMock(FilterChain.class));
  134. pipeline.destroyPipeline();
  135. verify(requestMock);
  136. assertTrue("lifecycle states did not fire correct number of times-- inits: " + inits + "; dos: "
  137. + services + "; destroys: " + destroys,
  138. inits == 3 && services == 1 && destroys == 3 && doFilters == 1);
  139. }
  140. @Singleton
  141. public static class TestServlet extends HttpServlet {
  142. public void init(ServletConfig filterConfig) throws ServletException {
  143. inits++;
  144. }
  145. public void service(ServletRequest servletRequest, ServletResponse servletResponse)
  146. throws IOException, ServletException {
  147. services++;
  148. }
  149. public void destroy() {
  150. destroys++;
  151. }
  152. }
  153. @Singleton
  154. public static class NeverServlet extends HttpServlet {
  155. public void init(ServletConfig filterConfig) throws ServletException {
  156. inits++;
  157. }
  158. public void service(ServletRequest servletRequest, ServletResponse servletResponse)
  159. throws IOException, ServletException {
  160. assertTrue("NeverServlet was fired, when it should not have been.", false);
  161. }
  162. public void destroy() {
  163. destroys++;
  164. }
  165. }
  166. @Singleton
  167. public static class TestFilter implements Filter {
  168. public void init(FilterConfig filterConfig) throws ServletException {
  169. inits++;
  170. }
  171. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
  172. FilterChain filterChain) throws IOException, ServletException {
  173. doFilters++;
  174. filterChain.doFilter(servletRequest, servletResponse);
  175. }
  176. public void destroy() {
  177. destroys++;
  178. }
  179. }
  180. }