PageRenderTime 121ms CodeModel.GetById 2ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/metamorphiccode/guice
Java | 182 lines | 123 code | 40 blank | 19 comment | 15 complexity | 3a050035315842c4590f84b96704f753 MD5 | raw file
  1. package com.google.inject.servlet;
  2. import static org.easymock.EasyMock.createMock;
  3. import static org.easymock.EasyMock.expect;
  4. import static org.easymock.EasyMock.replay;
  5. import static org.easymock.EasyMock.verify;
  6. import com.google.inject.Guice;
  7. import com.google.inject.Injector;
  8. import com.google.inject.Key;
  9. import com.google.inject.Singleton;
  10. import junit.framework.TestCase;
  11. import java.io.IOException;
  12. import javax.servlet.Filter;
  13. import javax.servlet.FilterChain;
  14. import javax.servlet.FilterConfig;
  15. import javax.servlet.ServletException;
  16. import javax.servlet.ServletRequest;
  17. import javax.servlet.ServletResponse;
  18. import javax.servlet.http.HttpServletRequest;
  19. /**
  20. *
  21. * This tests that filter stage of the pipeline dispatches
  22. * correctly to guice-managed filters.
  23. *
  24. * WARNING(dhanji): Non-parallelizable test =(
  25. *
  26. * @author dhanji@gmail.com (Dhanji R. Prasanna)
  27. */
  28. public class VarargsFilterDispatchIntegrationTest extends TestCase {
  29. private static int inits, doFilters, destroys;
  30. @Override
  31. public final void setUp() {
  32. inits = 0;
  33. doFilters = 0;
  34. destroys = 0;
  35. GuiceFilter.reset();
  36. }
  37. public final void testDispatchRequestToManagedPipeline() throws ServletException, IOException {
  38. final Injector injector = Guice.createInjector(new ServletModule() {
  39. @Override
  40. protected void configureServlets() {
  41. // This is actually a double match for "/*"
  42. filter("/*", "*.html", "/*").through(Key.get(TestFilter.class));
  43. // These filters should never fire
  44. filter("/index/*").through(Key.get(TestFilter.class));
  45. filter("*.jsp").through(Key.get(TestFilter.class));
  46. }
  47. });
  48. final FilterPipeline pipeline = injector.getInstance(FilterPipeline.class);
  49. pipeline.initPipeline(null);
  50. //create ourselves a mock request with test URI
  51. HttpServletRequest requestMock = createMock(HttpServletRequest.class);
  52. expect(requestMock.getRequestURI())
  53. .andReturn("/index.html")
  54. .anyTimes();
  55. expect(requestMock.getContextPath())
  56. .andReturn("")
  57. .anyTimes();
  58. //dispatch request
  59. replay(requestMock);
  60. pipeline.dispatch(requestMock, null, createMock(FilterChain.class));
  61. pipeline.destroyPipeline();
  62. verify(requestMock);
  63. assertTrue("lifecycle states did not"
  64. + " fire correct number of times-- inits: " + inits + "; dos: " + doFilters
  65. + "; destroys: " + destroys,
  66. inits == 1 && doFilters == 3 && destroys == 1);
  67. }
  68. public final void testDispatchThatNoFiltersFire() throws ServletException, IOException {
  69. final Injector injector = Guice.createInjector(new ServletModule() {
  70. @Override
  71. protected void configureServlets() {
  72. filter("/public/*", "*.html", "*.xml").through(Key.get(TestFilter.class));
  73. // These filters should never fire
  74. filter("/index/*").through(Key.get(TestFilter.class));
  75. filter("*.jsp").through(Key.get(TestFilter.class));
  76. }
  77. });
  78. final FilterPipeline pipeline = injector.getInstance(FilterPipeline.class);
  79. pipeline.initPipeline(null);
  80. //create ourselves a mock request with test URI
  81. HttpServletRequest requestMock = createMock(HttpServletRequest.class);
  82. expect(requestMock.getRequestURI())
  83. .andReturn("/index.xhtml")
  84. .anyTimes();
  85. expect(requestMock.getContextPath())
  86. .andReturn("")
  87. .anyTimes();
  88. //dispatch request
  89. replay(requestMock);
  90. pipeline.dispatch(requestMock, null, createMock(FilterChain.class));
  91. pipeline.destroyPipeline();
  92. verify(requestMock);
  93. assertTrue("lifecycle states did not "
  94. + "fire correct number of times-- inits: " + inits + "; dos: " + doFilters
  95. + "; destroys: " + destroys,
  96. inits == 1 && doFilters == 0 && destroys == 1);
  97. }
  98. public final void testDispatchFilterPipelineWithRegexMatching() throws ServletException,
  99. IOException {
  100. final Injector injector = Guice.createInjector(new ServletModule() {
  101. @Override
  102. protected void configureServlets() {
  103. filterRegex("/[A-Za-z]*", "/index").through(TestFilter.class);
  104. //these filters should never fire
  105. filterRegex("\\w").through(Key.get(TestFilter.class));
  106. }
  107. });
  108. final FilterPipeline pipeline = injector.getInstance(FilterPipeline.class);
  109. pipeline.initPipeline(null);
  110. //create ourselves a mock request with test URI
  111. HttpServletRequest requestMock = createMock(HttpServletRequest.class);
  112. expect(requestMock.getRequestURI())
  113. .andReturn("/index")
  114. .anyTimes();
  115. expect(requestMock.getContextPath())
  116. .andReturn("")
  117. .anyTimes();
  118. //dispatch request
  119. replay(requestMock);
  120. pipeline.dispatch(requestMock, null, createMock(FilterChain.class));
  121. pipeline.destroyPipeline();
  122. verify(requestMock);
  123. assertTrue("lifecycle states did not fire "
  124. + "correct number of times-- inits: " + inits + "; dos: " + doFilters
  125. + "; destroys: " + destroys,
  126. inits == 1 && doFilters == 2 && destroys == 1);
  127. }
  128. @Singleton
  129. public static class TestFilter implements Filter {
  130. public void init(FilterConfig filterConfig) throws ServletException {
  131. inits++;
  132. }
  133. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
  134. FilterChain filterChain) throws IOException, ServletException {
  135. doFilters++;
  136. filterChain.doFilter(servletRequest, servletResponse);
  137. }
  138. public void destroy() {
  139. destroys++;
  140. }
  141. }
  142. }