protected void applyScoping(Binding<?> binding, final ScopedBindingBuilder scopedBindingBuilder) { binding.acceptScopingVisitor(new BindingScopingVisitor<Void>() { public Void visitEagerSingleton() { if (scopedBindingBuilder != null) { scopedBindingBuilder.asEagerSingleton(); } return null; } public Void visitScope(Scope scope) { scopedBindingBuilder.in(scope); return null; } public Void visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) { scopedBindingBuilder.in(scopeAnnotation); return null; } public Void visitNoScoping() { // do nothing return null; } }); }
private static boolean isSingleton(Binding<?> binding) { return binding.acceptScopingVisitor(new BindingScopingVisitor<Boolean>() { @Override public Boolean visitNoScoping() { return false; } @Override public Boolean visitScopeAnnotation(Class<? extends Annotation> visitedAnnotation) { return visitedAnnotation.equals(Singleton.class); } @Override public Boolean visitScope(Scope visitedScope) { return visitedScope.equals(Scopes.SINGLETON); } @Override public Boolean visitEagerSingleton() { return true; } }); }
public static boolean isWorkerScope(Binding<?> binding) { return binding.acceptScopingVisitor(new BindingScopingVisitor<Boolean>() { public Boolean visitNoScoping() { return false; } public Boolean visitScopeAnnotation(Class<? extends Annotation> visitedAnnotation) { return visitedAnnotation == Worker.class; } public Boolean visitScope(Scope visitedScope) { return visitedScope.getClass() == WorkerPoolScope.class; } public Boolean visitEagerSingleton() { return false; } }); }
private static String scoped(final Binding<?> binding, final String statement) { return binding.acceptScopingVisitor(new BindingScopingVisitor<String>() { @Override public String visitEagerSingleton() { if (binding instanceof InstanceBinding<?>) { return statement; } throw new DeguicifierException(); } @Override public String visitScope(Scope scope) { return scopeFieldName(scope) + ".scope(null, " + guicify(statement) + ")"; } @Override public String visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) { throw new DeguicifierException(); } @Override public String visitNoScoping() { return statement; } }); }
private ComponentScope getComponentScope(Key<?> key, Injector i) { return i.getBinding(key).acceptScopingVisitor(new BindingScopingVisitor<ComponentScope>() { @Override public ComponentScope visitEagerSingleton() { return ComponentScope.Singleton; } @Override public ComponentScope visitScope(Scope theScope) { ComponentScope cs = scopeMap.get(theScope); return (cs != null) ? cs : ComponentScope.Undefined; } @Override public ComponentScope visitScopeAnnotation(Class scopeAnnotation) { // This method is not invoked for Injector bindings throw new UnsupportedOperationException(); } @Override public ComponentScope visitNoScoping() { return ComponentScope.PerRequest; } }); }
public static Scoping forAnnotation(final Class<? extends Annotation> scopingAnnotation) { if (scopingAnnotation == Singleton.class || scopingAnnotation == javax.inject.Singleton.class) { return SINGLETON_ANNOTATION; } return new Scoping() { @Override public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitScopeAnnotation(scopingAnnotation); } @Override public Class<? extends Annotation> getScopeAnnotation() { return scopingAnnotation; } @Override public String toString() { return scopingAnnotation.getName(); } @Override public void applyTo(ScopedBindingBuilder scopedBindingBuilder) { scopedBindingBuilder.in(scopingAnnotation); } }; }
public static Scoping forInstance(final Scope scope) { if (scope == Scopes.SINGLETON) { return SINGLETON_INSTANCE; } return new Scoping() { @Override public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitScope(scope); } @Override public Scope getScopeInstance() { return scope; } @Override public String toString() { return scope.toString(); } @Override public void applyTo(ScopedBindingBuilder scopedBindingBuilder) { scopedBindingBuilder.in(scope); } }; }
public static Scoping forAnnotation(final Class<? extends Annotation> scopingAnnotation) { if (scopingAnnotation == Singleton.class || scopingAnnotation == javax.inject.Singleton.class) { return SINGLETON_ANNOTATION; } return new Scoping() { public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitScopeAnnotation(scopingAnnotation); } @Override public Class<? extends Annotation> getScopeAnnotation() { return scopingAnnotation; } @Override public String toString() { return scopingAnnotation.getName(); } public void applyTo(ScopedBindingBuilder scopedBindingBuilder) { scopedBindingBuilder.in(scopingAnnotation); } }; }
public static Scoping forInstance(final Scope scope) { if (scope == Scopes.SINGLETON) { return SINGLETON_INSTANCE; } return new Scoping() { public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitScope(scope); } @Override public Scope getScopeInstance() { return scope; } @Override public String toString() { return scope.toString(); } public void applyTo(ScopedBindingBuilder scopedBindingBuilder) { scopedBindingBuilder.in(scope); } }; }
public static Scoping forInstance(final Scope scope) { if (scope == Scopes.SINGLETON) { return SINGLETON_INSTANCE; } else if (scope == Scopes.NO_SCOPE) { return EXPLICITLY_UNSCOPED; } return new Scoping() { @Override public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitScope(scope); } @Override public Scope getScopeInstance() { return scope; } @Override public String toString() { return scope.toString(); } @Override public void applyTo(ScopedBindingBuilder scopedBindingBuilder) { scopedBindingBuilder.in(scope); } }; }
public static ServiceDependencyManager buildServiceDependencyManager( final Injector injector, Iterable<Key<? extends BaseService<?>>> serviceKeys ) { final Multimap<ResolvedBinding<BaseService<?>>, ResolvedBinding<? extends BaseService<?>>> graph = computeInterdependencies( serviceKeys, injector); for (ResolvedBinding<? extends BaseService<?>> binding : graph.values()) { checkArgument(binding.getBinding().acceptScopingVisitor(new BindingScopingVisitor<Boolean>() { public Boolean visitEagerSingleton() { return true; } public Boolean visitScope(Scope scope) { return scope == Scopes.SINGLETON; } public Boolean visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) { return scopeAnnotation.equals(Singleton.class); } public Boolean visitNoScoping() { return false; } }), "Managed service must be bound as Singleton", binding); } return ServiceDependencyManager.buildServicesWithDependencies( ResolvedBinding.<BaseService<?>>resolver(injector) .andThen(ResolvedBinding::get) .transform(serviceKeys) .toList(), Functional.funPairs(graph.entries()) .mapKeys(ResolvedBinding::get) .mapValues((F<ResolvedBinding<? extends BaseService<?>>, BaseService<?>>) binding -> binding.get()) .toList() ); }
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); }
private void pathInfoWithServletStyleMatching( final String requestUri, final String contextPath, String mapping, final String expectedPathInfo, final String servletPath) 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.getPathInfo(); if (null == expectedPathInfo) { assertNull( String.format("expected [%s] but was [%s]", expectedPathInfo, path), path); } else { assertEquals( String.format("expected [%s] but was [%s]", expectedPathInfo, path), expectedPathInfo, path); } //assert memoizer //noinspection StringEquality assertSame("memo field did not work", path, servletRequest.getPathInfo()); run[0] = true; } }); expect(request.getRequestURI()).andReturn(requestUri); expect(request.getServletPath()).andReturn(servletPath).anyTimes(); expect(request.getContextPath()).andReturn(contextPath); expect(request.getAttribute(REQUEST_DISPATCHER_REQUEST)).andReturn(null); 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); }
public final void pathInfoWithRegexMatching( final String requestUri, final String contextPath, String mapping, final String expectedPathInfo, final String servletPath) 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.getPathInfo(); if (null == expectedPathInfo) { assertNull( String.format("expected [%s] but was [%s]", expectedPathInfo, path), path); } else { assertEquals( String.format("expected [%s] but was [%s]", expectedPathInfo, path), expectedPathInfo, path); } //assert memoizer //noinspection StringEquality assertSame("memo field did not work", path, servletRequest.getPathInfo()); run[0] = true; } }); expect(request.getRequestURI()).andReturn(requestUri); expect(request.getServletPath()).andReturn(servletPath).anyTimes(); expect(request.getContextPath()).andReturn(contextPath); expect(request.getAttribute(REQUEST_DISPATCHER_REQUEST)).andReturn(null); replay(injector, binding, request); ServletDefinition servletDefinition = new ServletDefinition( Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.REGEX, 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); }
public final void testIncludeManagedServlet() throws IOException, ServletException { String pattern = "blah.html"; final ServletDefinition servletDefinition = new ServletDefinition( Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null); final Injector injector = createMock(Injector.class); final Binding binding = createMock(Binding.class); final HttpServletRequest requestMock = createMock(HttpServletRequest.class); expect(requestMock.getAttribute(A_KEY)).andReturn(A_VALUE); requestMock.setAttribute(REQUEST_DISPATCHER_REQUEST, true); requestMock.removeAttribute(REQUEST_DISPATCHER_REQUEST); final boolean[] run = new boolean[1]; final HttpServlet mockServlet = new HttpServlet() { @Override protected void service( HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException { run[0] = true; final Object o = request.getAttribute(A_KEY); assertEquals("Wrong attrib returned - " + o, A_VALUE, o); } }; expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true); expect(injector.getBinding(Key.get(HttpServlet.class))).andReturn(binding); expect(injector.getInstance(HTTP_SERLVET_KEY)).andReturn(mockServlet); final Key<ServletDefinition> servetDefsKey = Key.get(TypeLiteral.get(ServletDefinition.class)); Binding<ServletDefinition> mockBinding = createMock(Binding.class); expect(injector.findBindingsByType(eq(servetDefsKey.getTypeLiteral()))) .andReturn(ImmutableList.<Binding<ServletDefinition>>of(mockBinding)); Provider<ServletDefinition> bindingProvider = Providers.of(servletDefinition); expect(mockBinding.getProvider()).andReturn(bindingProvider); replay(injector, binding, requestMock, mockBinding); // Have to init the Servlet before we can dispatch to it. servletDefinition.init(null, injector, Sets.<HttpServlet>newIdentityHashSet()); final RequestDispatcher dispatcher = new ManagedServletPipeline(injector).getRequestDispatcher(pattern); assertNotNull(dispatcher); dispatcher.include(requestMock, createMock(HttpServletResponse.class)); assertTrue("Include did not dispatch to our servlet!", run[0]); verify(injector, requestMock, mockBinding); }
public final void testForwardToManagedServlet() throws IOException, ServletException { String pattern = "blah.html"; final ServletDefinition servletDefinition = new ServletDefinition( Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null); final Injector injector = createMock(Injector.class); final Binding binding = createMock(Binding.class); final HttpServletRequest requestMock = createMock(HttpServletRequest.class); final HttpServletResponse mockResponse = createMock(HttpServletResponse.class); expect(requestMock.getAttribute(A_KEY)).andReturn(A_VALUE); requestMock.setAttribute(REQUEST_DISPATCHER_REQUEST, true); requestMock.removeAttribute(REQUEST_DISPATCHER_REQUEST); expect(mockResponse.isCommitted()).andReturn(false); mockResponse.resetBuffer(); expectLastCall().once(); final List<String> paths = new ArrayList<>(); final HttpServlet mockServlet = new HttpServlet() { @Override protected void service( HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException { paths.add(request.getRequestURI()); final Object o = request.getAttribute(A_KEY); assertEquals("Wrong attrib returned - " + o, A_VALUE, o); } }; expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true); expect(injector.getBinding(Key.get(HttpServlet.class))).andReturn(binding); expect(injector.getInstance(HTTP_SERLVET_KEY)).andReturn(mockServlet); final Key<ServletDefinition> servetDefsKey = Key.get(TypeLiteral.get(ServletDefinition.class)); Binding<ServletDefinition> mockBinding = createMock(Binding.class); expect(injector.findBindingsByType(eq(servetDefsKey.getTypeLiteral()))) .andReturn(ImmutableList.<Binding<ServletDefinition>>of(mockBinding)); Provider<ServletDefinition> bindingProvider = Providers.of(servletDefinition); expect(mockBinding.getProvider()).andReturn(bindingProvider); replay(injector, binding, requestMock, mockResponse, mockBinding); // Have to init the Servlet before we can dispatch to it. servletDefinition.init(null, injector, Sets.<HttpServlet>newIdentityHashSet()); final RequestDispatcher dispatcher = new ManagedServletPipeline(injector).getRequestDispatcher(pattern); assertNotNull(dispatcher); dispatcher.forward(requestMock, mockResponse); assertTrue("Include did not dispatch to our servlet!", paths.contains(pattern)); verify(injector, requestMock, mockResponse, mockBinding); }
public final void forwardToManagedServletFailureOnCommittedBuffer() throws IOException, ServletException { String pattern = "blah.html"; final ServletDefinition servletDefinition = new ServletDefinition( Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null); final Injector injector = createMock(Injector.class); final Binding binding = createMock(Binding.class); final HttpServletRequest mockRequest = createMock(HttpServletRequest.class); final HttpServletResponse mockResponse = createMock(HttpServletResponse.class); expect(mockResponse.isCommitted()).andReturn(true); final HttpServlet mockServlet = new HttpServlet() { @Override protected void service( HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException { final Object o = request.getAttribute(A_KEY); assertEquals("Wrong attrib returned - " + o, A_VALUE, o); } }; expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true); expect(injector.getBinding(Key.get(HttpServlet.class))).andReturn(binding); expect(injector.getInstance(Key.get(HttpServlet.class))).andReturn(mockServlet); final Key<ServletDefinition> servetDefsKey = Key.get(TypeLiteral.get(ServletDefinition.class)); Binding<ServletDefinition> mockBinding = createMock(Binding.class); expect(injector.findBindingsByType(eq(servetDefsKey.getTypeLiteral()))) .andReturn(ImmutableList.<Binding<ServletDefinition>>of(mockBinding)); Provider<ServletDefinition> bindingProvider = Providers.of(servletDefinition); expect(mockBinding.getProvider()).andReturn(bindingProvider); replay(injector, binding, mockRequest, mockResponse, mockBinding); // Have to init the Servlet before we can dispatch to it. servletDefinition.init(null, injector, Sets.<HttpServlet>newIdentityHashSet()); final RequestDispatcher dispatcher = new ManagedServletPipeline(injector).getRequestDispatcher(pattern); assertNotNull(dispatcher); try { dispatcher.forward(mockRequest, mockResponse); } finally { verify(injector, mockRequest, mockResponse, mockBinding); } }
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); }
@Override public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitNoScoping(); }
@Override public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitScopeAnnotation(Singleton.class); }
@Override public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitScope(Scopes.SINGLETON); }
@Override public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) { return visitor.visitEagerSingleton(); }
@Override public <V> V acceptScopingVisitor(BindingScopingVisitor<V> visitor) { return scoping.acceptVisitor(visitor); }
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(mapping, Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, mapping), new HashMap<String, String>(), null); servletDefinition.init(null, injector, Sets.newSetFromMap(Maps.<HttpServlet, Boolean>newIdentityHashMap())); servletDefinition.doService(request, response); assertTrue("Servlet did not run!", run[0]); verify(injector, binding, request); }
private void pathInfoWithServletStyleMatching(final String requestUri, final String contextPath, String mapping, final String expectedPathInfo, final String servletPath) 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.getPathInfo(); if (null == expectedPathInfo) { assertNull(String.format("expected [%s] but was [%s]", expectedPathInfo, path), path); } else { assertEquals(String.format("expected [%s] but was [%s]", expectedPathInfo, path), expectedPathInfo, path); } //assert memoizer //noinspection StringEquality assertSame("memo field did not work", path, servletRequest.getPathInfo()); run[0] = true; } }); expect(request.getRequestURI()) .andReturn(requestUri); expect(request.getServletPath()) .andReturn(servletPath) .anyTimes(); expect(request.getContextPath()) .andReturn(contextPath); expect(request.getAttribute(REQUEST_DISPATCHER_REQUEST)).andReturn(null); replay(injector, binding, request); ServletDefinition servletDefinition = new ServletDefinition(mapping, Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, mapping), new HashMap<String, String>(), null); servletDefinition.init(null, injector, Sets.newSetFromMap(Maps.<HttpServlet, Boolean>newIdentityHashMap())); servletDefinition.doService(request, response); assertTrue("Servlet did not run!", run[0]); verify(injector, binding, request); }
public final void pathInfoWithRegexMatching(final String requestUri, final String contextPath, String mapping, final String expectedPathInfo, final String servletPath) 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.getPathInfo(); if (null == expectedPathInfo) { assertNull(String.format("expected [%s] but was [%s]", expectedPathInfo, path), path); } else { assertEquals(String.format("expected [%s] but was [%s]", expectedPathInfo, path), expectedPathInfo, path); } //assert memoizer //noinspection StringEquality assertSame("memo field did not work", path, servletRequest.getPathInfo()); run[0] = true; } }); expect(request.getRequestURI()) .andReturn(requestUri); expect(request.getServletPath()) .andReturn(servletPath) .anyTimes(); expect(request.getContextPath()) .andReturn(contextPath); expect(request.getAttribute(REQUEST_DISPATCHER_REQUEST)).andReturn(null); replay(injector, binding, request); ServletDefinition servletDefinition = new ServletDefinition(mapping, Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.REGEX, mapping), new HashMap<String, String>(), null); servletDefinition.init(null, injector, Sets.newSetFromMap(Maps.<HttpServlet, Boolean>newIdentityHashMap())); servletDefinition.doService(request, response); assertTrue("Servlet did not run!", run[0]); verify(injector, binding, request); }
public final void testIncludeManagedServlet() throws IOException, ServletException { String pattern = "blah.html"; final ServletDefinition servletDefinition = new ServletDefinition(pattern, Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null); final Injector injector = createMock(Injector.class); final Binding binding = createMock(Binding.class); final HttpServletRequest requestMock = createMock(HttpServletRequest.class); expect(requestMock.getAttribute(A_KEY)) .andReturn(A_VALUE); requestMock.setAttribute(REQUEST_DISPATCHER_REQUEST, true); requestMock.removeAttribute(REQUEST_DISPATCHER_REQUEST); final boolean[] run = new boolean[1]; final HttpServlet mockServlet = new HttpServlet() { protected void service(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException { run[0] = true; final Object o = request.getAttribute(A_KEY); assertEquals("Wrong attrib returned - " + o, A_VALUE, o); } }; expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())) .andReturn(true); expect(injector.getBinding(Key.get(HttpServlet.class))) .andReturn(binding); expect(injector.getInstance(HTTP_SERLVET_KEY)) .andReturn(mockServlet); final Key<ServletDefinition> servetDefsKey = Key .get(TypeLiteral.get(ServletDefinition.class)); Binding<ServletDefinition> mockBinding = createMock(Binding.class); expect(injector.findBindingsByType(eq(servetDefsKey.getTypeLiteral()))) .andReturn(ImmutableList.<Binding<ServletDefinition>>of(mockBinding)); Provider<ServletDefinition> bindingProvider = Providers.of(servletDefinition); expect(mockBinding.getProvider()) .andReturn(bindingProvider); replay(injector, binding, requestMock, mockBinding); // Have to init the Servlet before we can dispatch to it. servletDefinition.init(null, injector, Sets.newSetFromMap(Maps.<HttpServlet, Boolean>newIdentityHashMap())); final RequestDispatcher dispatcher = new ManagedServletPipeline( injector) .getRequestDispatcher(pattern); assertNotNull(dispatcher); dispatcher.include(requestMock, createMock(HttpServletResponse.class)); assertTrue("Include did not dispatch to our servlet!", run[0]); verify(injector, requestMock, mockBinding); }
public final void testForwardToManagedServlet() throws IOException, ServletException { String pattern = "blah.html"; final ServletDefinition servletDefinition = new ServletDefinition(pattern, Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null); final Injector injector = createMock(Injector.class); final Binding binding = createMock(Binding.class); final HttpServletRequest requestMock = createMock(HttpServletRequest.class); final HttpServletResponse mockResponse = createMock(HttpServletResponse.class); expect(requestMock.getAttribute(A_KEY)) .andReturn(A_VALUE); requestMock.setAttribute(REQUEST_DISPATCHER_REQUEST, true); requestMock.removeAttribute(REQUEST_DISPATCHER_REQUEST); expect(mockResponse.isCommitted()) .andReturn(false); mockResponse.resetBuffer(); expectLastCall().once(); final List<String> paths = new ArrayList<String>(); final HttpServlet mockServlet = new HttpServlet() { protected void service(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException { paths.add(request.getRequestURI()); final Object o = request.getAttribute(A_KEY); assertEquals("Wrong attrib returned - " + o, A_VALUE, o); } }; expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())) .andReturn(true); expect(injector.getBinding(Key.get(HttpServlet.class))) .andReturn(binding); expect(injector.getInstance(HTTP_SERLVET_KEY)) .andReturn(mockServlet); final Key<ServletDefinition> servetDefsKey = Key .get(TypeLiteral.get(ServletDefinition.class)); Binding<ServletDefinition> mockBinding = createMock(Binding.class); expect(injector.findBindingsByType(eq(servetDefsKey.getTypeLiteral()))) .andReturn(ImmutableList.<Binding<ServletDefinition>>of(mockBinding)); Provider<ServletDefinition> bindingProvider = Providers.of(servletDefinition); expect(mockBinding.getProvider()) .andReturn(bindingProvider); replay(injector, binding, requestMock, mockResponse, mockBinding); // Have to init the Servlet before we can dispatch to it. servletDefinition.init(null, injector, Sets.newSetFromMap(Maps.<HttpServlet, Boolean>newIdentityHashMap())); final RequestDispatcher dispatcher = new ManagedServletPipeline(injector) .getRequestDispatcher(pattern); assertNotNull(dispatcher); dispatcher.forward(requestMock, mockResponse); assertTrue("Include did not dispatch to our servlet!", paths.contains(pattern)); verify(injector, requestMock, mockResponse, mockBinding); }
public final void forwardToManagedServletFailureOnCommittedBuffer() throws IOException, ServletException { String pattern = "blah.html"; final ServletDefinition servletDefinition = new ServletDefinition(pattern, Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null); final Injector injector = createMock(Injector.class); final Binding binding = createMock(Binding.class); final HttpServletRequest mockRequest = createMock(HttpServletRequest.class); final HttpServletResponse mockResponse = createMock(HttpServletResponse.class); expect(mockResponse.isCommitted()) .andReturn(true); final HttpServlet mockServlet = new HttpServlet() { protected void service(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException { final Object o = request.getAttribute(A_KEY); assertEquals("Wrong attrib returned - " + o, A_VALUE, o); } }; expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())) .andReturn(true); expect(injector.getBinding(Key.get(HttpServlet.class))) .andReturn(binding); expect(injector.getInstance(Key.get(HttpServlet.class))) .andReturn(mockServlet); final Key<ServletDefinition> servetDefsKey = Key .get(TypeLiteral.get(ServletDefinition.class)); Binding<ServletDefinition> mockBinding = createMock(Binding.class); expect(injector.findBindingsByType(eq(servetDefsKey.getTypeLiteral()))) .andReturn(ImmutableList.<Binding<ServletDefinition>>of(mockBinding)); Provider<ServletDefinition> bindingProvider = Providers.of(servletDefinition); expect(mockBinding.getProvider()) .andReturn(bindingProvider); replay(injector, binding, mockRequest, mockResponse, mockBinding); // Have to init the Servlet before we can dispatch to it. servletDefinition.init(null, injector, Sets.newSetFromMap(Maps.<HttpServlet, Boolean>newIdentityHashMap())); final RequestDispatcher dispatcher = new ManagedServletPipeline(injector) .getRequestDispatcher(pattern); assertNotNull(dispatcher); try { dispatcher.forward(mockRequest, mockResponse); } finally { verify(injector, mockRequest, mockResponse, mockBinding); } }
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(pattern, 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.newSetFromMap(Maps.<Filter, Boolean>newIdentityHashMap())); 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.newSetFromMap(Maps.<Filter, Boolean>newIdentityHashMap())); assertTrue("Destroy did not fire", mockFilter.isDestroy()); verify(injector, request); }