use of com.google.inject.Binding in project guice by google.
the class FilterDefinitionTest method testFilterCreateDispatchDestroy.
public final void testFilterCreateDispatchDestroy() throws ServletException, IOException {
Injector injector = createMock(Injector.class);
Binding binding = createMock(Binding.class);
HttpServletRequest request = createMock(HttpServletRequest.class);
final MockFilter mockFilter = new MockFilter();
expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
expect(injector.getBinding(Key.get(Filter.class))).andReturn(binding);
expect(injector.getInstance(Key.get(Filter.class))).andReturn(mockFilter).anyTimes();
expect(request.getRequestURI()).andReturn("/index.html");
expect(request.getContextPath()).andReturn("").anyTimes();
replay(injector, binding, request);
String pattern = "/*";
final FilterDefinition filterDef = new FilterDefinition(Key.get(Filter.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null);
//should fire on mockfilter now
filterDef.init(createMock(ServletContext.class), injector, Sets.<Filter>newIdentityHashSet());
assertTrue(filterDef.getFilter() instanceof MockFilter);
assertTrue("Init did not fire", mockFilter.isInit());
Filter matchingFilter = filterDef.getFilterIfMatching(request);
assertSame(mockFilter, matchingFilter);
final boolean[] proceed = new boolean[1];
matchingFilter.doFilter(request, null, new FilterChainInvocation(null, null, null) {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
proceed[0] = true;
}
});
assertTrue("Filter did not proceed down chain", proceed[0]);
filterDef.destroy(Sets.<Filter>newIdentityHashSet());
assertTrue("Destroy did not fire", mockFilter.isDestroy());
verify(injector, request);
}
use of com.google.inject.Binding in project guice by google.
the class FilterDefinitionTest method testGetFilterIfMatching.
public void testGetFilterIfMatching() throws ServletException {
String pattern = "/*";
final FilterDefinition filterDef = new FilterDefinition(Key.get(Filter.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null);
HttpServletRequest servletRequest = createMock(HttpServletRequest.class);
ServletContext servletContext = createMock(ServletContext.class);
Injector injector = createMock(Injector.class);
Binding binding = createMock(Binding.class);
final MockFilter mockFilter = new MockFilter() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
//suppress rest of chain...
}
};
expect(injector.getBinding(Key.get(Filter.class))).andReturn(binding);
expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
expect(injector.getInstance(Key.get(Filter.class))).andReturn(mockFilter).anyTimes();
expect(servletRequest.getContextPath()).andReturn("/a_context_path");
expect(servletRequest.getRequestURI()).andReturn("/a_context_path/test.html");
replay(servletRequest, binding, injector);
filterDef.init(servletContext, injector, Sets.<Filter>newIdentityHashSet());
Filter filter = filterDef.getFilterIfMatching(servletRequest);
assertSame(filter, mockFilter);
verify(servletRequest, binding, injector);
}
use of com.google.inject.Binding in project guice by google.
the class FilterDefinitionTest method testFilterCreateDispatchDestroySupressChain.
public final void testFilterCreateDispatchDestroySupressChain() throws ServletException, IOException {
Injector injector = createMock(Injector.class);
Binding binding = createMock(Binding.class);
HttpServletRequest request = createMock(HttpServletRequest.class);
final MockFilter mockFilter = new MockFilter() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
//suppress rest of chain...
}
};
expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
expect(injector.getBinding(Key.get(Filter.class))).andReturn(binding);
expect(injector.getInstance(Key.get(Filter.class))).andReturn(mockFilter).anyTimes();
expect(request.getRequestURI()).andReturn("/index.html");
expect(request.getContextPath()).andReturn("").anyTimes();
replay(injector, binding, request);
String pattern = "/*";
final FilterDefinition filterDef = new FilterDefinition(Key.get(Filter.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null);
//should fire on mockfilter now
filterDef.init(createMock(ServletContext.class), injector, Sets.<Filter>newIdentityHashSet());
assertTrue(filterDef.getFilter() instanceof MockFilter);
assertTrue("init did not fire", mockFilter.isInit());
Filter matchingFilter = filterDef.getFilterIfMatching(request);
assertSame(mockFilter, matchingFilter);
final boolean[] proceed = new boolean[1];
matchingFilter.doFilter(request, null, new FilterChainInvocation(null, null, null) {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
proceed[0] = true;
}
});
assertFalse("filter did not suppress chain", proceed[0]);
filterDef.destroy(Sets.<Filter>newIdentityHashSet());
assertTrue("destroy did not fire", mockFilter.isDestroy());
verify(injector, request);
}
use of com.google.inject.Binding in project guice by google.
the class ExtensionSpiTest method testSpiOnInjector.
public final void testSpiOnInjector() {
ServletSpiVisitor visitor = new ServletSpiVisitor(true);
int count = 0;
Injector injector = Guice.createInjector(new Module());
for (Binding binding : injector.getBindings().values()) {
assertEquals(count++, binding.acceptTargetVisitor(visitor));
}
validateVisitor(visitor);
}
use of com.google.inject.Binding in project guice by google.
the class ServletDefinitionPathsTest method servletPath.
private void servletPath(final String requestPath, String mapping, final String expectedServletPath) throws IOException, ServletException {
Injector injector = createMock(Injector.class);
Binding binding = createMock(Binding.class);
HttpServletRequest request = createMock(HttpServletRequest.class);
HttpServletResponse response = createMock(HttpServletResponse.class);
expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
expect(injector.getBinding(Key.get(HttpServlet.class))).andReturn(binding);
final boolean[] run = new boolean[1];
//get an instance of this servlet
expect(injector.getInstance(Key.get(HttpServlet.class))).andReturn(new HttpServlet() {
@Override
protected void service(HttpServletRequest servletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
final String path = servletRequest.getServletPath();
assertEquals(String.format("expected [%s] but was [%s]", expectedServletPath, path), expectedServletPath, path);
run[0] = true;
}
});
expect(request.getServletPath()).andReturn(requestPath);
replay(injector, binding, request);
ServletDefinition servletDefinition = new ServletDefinition(Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, mapping), new HashMap<String, String>(), null);
servletDefinition.init(null, injector, Sets.<HttpServlet>newIdentityHashSet());
servletDefinition.doService(request, response);
assertTrue("Servlet did not run!", run[0]);
verify(injector, binding, request);
}
Aggregations