PageRenderTime 43ms CodeModel.GetById 36ms app.highlight 5ms 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
  1package com.google.inject.servlet;
  2
  3import static org.easymock.EasyMock.createMock;
  4import static org.easymock.EasyMock.expect;
  5import static org.easymock.EasyMock.replay;
  6import static org.easymock.EasyMock.verify;
  7
  8import com.google.inject.Guice;
  9import com.google.inject.Injector;
 10import com.google.inject.Key;
 11import com.google.inject.Singleton;
 12
 13import junit.framework.TestCase;
 14
 15import java.io.IOException;
 16
 17import javax.servlet.Filter;
 18import javax.servlet.FilterChain;
 19import javax.servlet.FilterConfig;
 20import javax.servlet.ServletException;
 21import javax.servlet.ServletRequest;
 22import javax.servlet.ServletResponse;
 23import javax.servlet.http.HttpServletRequest;
 24
 25/**
 26 *
 27 * This tests that filter stage of the pipeline dispatches
 28 * correctly to guice-managed filters.
 29 *
 30 * WARNING(dhanji): Non-parallelizable test =(
 31 *
 32 * @author dhanji@gmail.com (Dhanji R. Prasanna)
 33 */
 34public class VarargsFilterDispatchIntegrationTest 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        // This is actually a double match for "/*"
 53        filter("/*", "*.html", "/*").through(Key.get(TestFilter.class));
 54
 55        // These filters should never fire
 56        filter("/index/*").through(Key.get(TestFilter.class));
 57        filter("*.jsp").through(Key.get(TestFilter.class));
 58      }
 59    });
 60
 61    final FilterPipeline pipeline = injector.getInstance(FilterPipeline.class);
 62    pipeline.initPipeline(null);
 63
 64    //create ourselves a mock request with test URI
 65    HttpServletRequest requestMock = createMock(HttpServletRequest.class);
 66
 67    expect(requestMock.getRequestURI())
 68            .andReturn("/index.html")
 69            .anyTimes();
 70    expect(requestMock.getContextPath())
 71        .andReturn("")
 72        .anyTimes();
 73
 74    //dispatch request
 75    replay(requestMock);
 76    pipeline.dispatch(requestMock, null, createMock(FilterChain.class));
 77    pipeline.destroyPipeline();
 78
 79    verify(requestMock);
 80
 81    assertTrue("lifecycle states did not"
 82        + " fire correct number of times-- inits: " + inits + "; dos: " + doFilters
 83        + "; destroys: " + destroys,
 84        inits == 1 && doFilters == 3 && destroys == 1);
 85  }
 86
 87  public final void testDispatchThatNoFiltersFire() throws ServletException, IOException {
 88    final Injector injector = Guice.createInjector(new ServletModule() {
 89
 90      @Override
 91      protected void configureServlets() {
 92        filter("/public/*", "*.html", "*.xml").through(Key.get(TestFilter.class));
 93
 94        // These filters should never fire
 95        filter("/index/*").through(Key.get(TestFilter.class));
 96        filter("*.jsp").through(Key.get(TestFilter.class));
 97      }
 98    });
 99
100    final FilterPipeline pipeline = injector.getInstance(FilterPipeline.class);
101    pipeline.initPipeline(null);
102
103    //create ourselves a mock request with test URI
104    HttpServletRequest requestMock = createMock(HttpServletRequest.class);
105
106    expect(requestMock.getRequestURI())
107            .andReturn("/index.xhtml")
108            .anyTimes();
109    expect(requestMock.getContextPath())
110        .andReturn("")
111        .anyTimes();
112
113    //dispatch request
114    replay(requestMock);
115    pipeline.dispatch(requestMock, null, createMock(FilterChain.class));
116    pipeline.destroyPipeline();
117
118    verify(requestMock);
119
120    assertTrue("lifecycle states did not "
121        + "fire correct number of times-- inits: " + inits + "; dos: " + doFilters
122        + "; destroys: " + destroys,
123        inits == 1 && doFilters == 0 && destroys == 1);
124  }
125
126  public final void testDispatchFilterPipelineWithRegexMatching() throws ServletException,
127      IOException {
128
129    final Injector injector = Guice.createInjector(new ServletModule() {
130
131      @Override
132      protected void configureServlets() {
133        filterRegex("/[A-Za-z]*", "/index").through(TestFilter.class);
134
135        //these filters should never fire
136        filterRegex("\\w").through(Key.get(TestFilter.class));
137      }
138    });
139
140    final FilterPipeline pipeline = injector.getInstance(FilterPipeline.class);
141    pipeline.initPipeline(null);
142
143    //create ourselves a mock request with test URI
144    HttpServletRequest requestMock = createMock(HttpServletRequest.class);
145
146    expect(requestMock.getRequestURI())
147            .andReturn("/index")
148            .anyTimes();
149    expect(requestMock.getContextPath())
150        .andReturn("")
151        .anyTimes();
152
153    //dispatch request
154    replay(requestMock);
155    pipeline.dispatch(requestMock, null, createMock(FilterChain.class));
156    pipeline.destroyPipeline();
157
158    verify(requestMock);
159
160    assertTrue("lifecycle states did not fire "
161        + "correct number of times-- inits: " + inits + "; dos: " + doFilters
162        + "; destroys: " + destroys,
163        inits == 1 && doFilters == 2 && destroys == 1);
164  }
165
166  @Singleton
167  public static class TestFilter implements Filter {
168    public void init(FilterConfig filterConfig) throws ServletException {
169      inits++;
170    }
171
172    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
173        FilterChain filterChain) throws IOException, ServletException {
174      doFilters++;
175      filterChain.doFilter(servletRequest, servletResponse);
176    }
177
178    public void destroy() {
179      destroys++;
180    }
181  }
182}