Search in sources :

Example 61 with Binding

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);
}
Also used : Binding(com.google.inject.Binding) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) Filter(javax.servlet.Filter) Injector(com.google.inject.Injector) ServletContext(javax.servlet.ServletContext)

Example 62 with Binding

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Binding(com.google.inject.Binding) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletResponse(javax.servlet.ServletResponse) Filter(javax.servlet.Filter) Injector(com.google.inject.Injector) FilterChain(javax.servlet.FilterChain) ServletContext(javax.servlet.ServletContext)

Example 63 with Binding

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);
}
Also used : Binding(com.google.inject.Binding) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) HttpServletRequest(javax.servlet.http.HttpServletRequest) Filter(javax.servlet.Filter) Injector(com.google.inject.Injector) ServletContext(javax.servlet.ServletContext)

Example 64 with Binding

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);
}
Also used : Binding(com.google.inject.Binding) Injector(com.google.inject.Injector)

Example 65 with Binding

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);
}
Also used : Binding(com.google.inject.Binding) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Injector(com.google.inject.Injector) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Aggregations

Binding (com.google.inject.Binding)92 Injector (com.google.inject.Injector)58 Key (com.google.inject.Key)36 AbstractModule (com.google.inject.AbstractModule)33 InstanceBinding (com.google.inject.spi.InstanceBinding)23 Map (java.util.Map)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)21 Module (com.google.inject.Module)18 Element (com.google.inject.spi.Element)18 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)17 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)16 HttpServlet (javax.servlet.http.HttpServlet)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)13 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)12 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)12 ServletContext (javax.servlet.ServletContext)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 HashMap (java.util.HashMap)11 TypeLiteral (com.google.inject.TypeLiteral)10 MapBinderBinding (com.google.inject.multibindings.MapBinderBinding)10