LazyInterceptorHandler implements InterceptorHandler { public void execute(InterceptorStack stack, ResourceMethod method, Object resourceInstance) throws InterceptionException { boolean accepts; try { accepts = getAcceptor().accepts(method); } catch (NullPointerException e) { throw new InterceptionException("StaticInterceptors should not use constructor parameters inside the accepts method", e); } if (accepts) { Interceptor interceptor = container.instanceFor(type); if (interceptor == null) { throw new InterceptionException("Unable to instantiate interceptor for " + type.getName() + ": the container returned null."); } logger.debug("Invoking interceptor {}", interceptor.getClass().getSimpleName()); interceptor.intercept(stack, method, resourceInstance); } else { stack.next(method, resourceInstance); } } LazyInterceptorHandler(Container container, Class<? extends Interceptor> type); void execute(InterceptorStack stack, ResourceMethod method, Object resourceInstance); @Override String toString();  }
@Test public void shouldUseContainerIfInterceptorAccepts() throws Exception { LazyInterceptorHandler handler = new LazyInterceptorHandler(container,AlwaysAcceptInterceptor.class); handler.execute(stack,method,instance); verify(container).instanceFor(AlwaysAcceptInterceptor.class); verify(mockInterceptor).intercept(stack, method, instance); verify(mockInterceptor, never()).accepts(any(ResourceMethod.class)); }
@Test public void shouldNotUseContainerIfInterceptorDoesntAccept() throws Exception { LazyInterceptorHandler handler = new LazyInterceptorHandler(container, NeverAcceptInterceptor.class); handler.execute(stack,method,instance); verify(container, never()).instanceFor(any(Class.class)); verify(stack).next(method, instance); }
@Test(expected=InterceptionException.class) public void shouldFailIfUsingConstructorParametersOnConstructor() throws Exception { LazyInterceptorHandler handler = new LazyInterceptorHandler(container, InterceptorUsingConstructorParameters.class); handler.execute(stack,method,instance); }
@Test(expected=InterceptionException.class) public void shouldFailIfUsingConstructorParametersOnAcceptsMethod() throws Exception { LazyInterceptorHandler handler = new LazyInterceptorHandler(container, InterceptorUsingConstructorParametersOnAccepts.class); handler.execute(stack,method,instance); }