Home | History | Annotate | Download | only in servlet
      1 package com.google.inject.servlet;
      2 
      3 import static org.easymock.EasyMock.createMock;
      4 import static org.easymock.EasyMock.expect;
      5 import static org.easymock.EasyMock.replay;
      6 import static org.easymock.EasyMock.verify;
      7 
      8 import com.google.inject.Guice;
      9 import com.google.inject.Injector;
     10 import com.google.inject.Key;
     11 import com.google.inject.Singleton;
     12 
     13 import junit.framework.TestCase;
     14 
     15 import java.io.IOException;
     16 
     17 import javax.servlet.Filter;
     18 import javax.servlet.FilterChain;
     19 import javax.servlet.FilterConfig;
     20 import javax.servlet.ServletException;
     21 import javax.servlet.ServletRequest;
     22 import javax.servlet.ServletResponse;
     23 import javax.servlet.http.HttpServletRequest;
     24 
     25 /**
     26  *
     27  * This tests that filter stage of the pipeline dispatches
     28  * correctly to guice-managed filters with multiple modules.
     29  *
     30  * WARNING(dhanji): Non-parallelizable test =(
     31  *
     32  * @author dhanji (at) gmail.com (Dhanji R. Prasanna)
     33  */
     34 public class MultiModuleDispatchIntegrationTest extends TestCase {
     35     private static int inits, doFilters, destroys;
     36 
     37   @Override
     38   public final void setUp() {
     39     inits = 0;
     40     doFilters = 0;
     41     destroys = 0;
     42 
     43     GuiceFilter.reset();
     44   }
     45 
     46 
     47   public final void testDispatchRequestToManagedPipeline() throws ServletException, IOException {
     48     final Injector injector = Guice.createInjector(new ServletModule() {
     49 
     50       @Override
     51       protected void configureServlets() {
     52         filter("/*").through(TestFilter.class);
     53 
     54         // These filters should never fire
     55         filter("*.jsp").through(Key.get(TestFilter.class));
     56       }
     57 
     58     }, new ServletModule() {
     59 
     60       @Override
     61       protected void configureServlets() {
     62         filter("*.html").through(TestFilter.class);
     63         filter("/*").through(Key.get(TestFilter.class));
     64 
     65         // These filters should never fire
     66         filter("/index/*").through(Key.get(TestFilter.class));
     67       }
     68 
     69     });
     70 
     71     final FilterPipeline pipeline = injector.getInstance(FilterPipeline.class);
     72     pipeline.initPipeline(null);
     73 
     74     //create ourselves a mock request with test URI
     75     HttpServletRequest requestMock = createMock(HttpServletRequest.class);
     76 
     77     expect(requestMock.getRequestURI())
     78             .andReturn("/index.html")
     79             .anyTimes();
     80     expect(requestMock.getContextPath())
     81         .andReturn("")
     82         .anyTimes();
     83 
     84     //dispatch request
     85     replay(requestMock);
     86     pipeline.dispatch(requestMock, null, createMock(FilterChain.class));
     87     pipeline.destroyPipeline();
     88 
     89     verify(requestMock);
     90 
     91     assertTrue("lifecycle states did not"
     92         + " fire correct number of times-- inits: " + inits + "; dos: " + doFilters
     93         + "; destroys: " + destroys,
     94         inits == 1 && doFilters == 3 && destroys == 1);
     95   }
     96 
     97   @Singleton
     98   public static class TestFilter implements Filter {
     99     public void init(FilterConfig filterConfig) throws ServletException {
    100       inits++;
    101     }
    102 
    103     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
    104         FilterChain filterChain) throws IOException, ServletException {
    105       doFilters++;
    106       filterChain.doFilter(servletRequest, servletResponse);
    107     }
    108 
    109     public void destroy() {
    110       destroys++;
    111     }
    112   }
    113 }
    114