Search in sources :

Example 1 with ConstructorBinding

use of com.google.inject.spi.ConstructorBinding in project roboguice by roboguice.

the class MethodInterceptionTest method testSpiAccessToInterceptors.

public void testSpiAccessToInterceptors() throws NoSuchMethodException {
    final MethodInterceptor countingInterceptor = new CountingInterceptor();
    final MethodInterceptor returnNullInterceptor = new ReturnNullInterceptor();
    Injector injector = Guice.createInjector(new AbstractModule() {

        protected void configure() {
            bindInterceptor(Matchers.any(), Matchers.returns(only(Foo.class)), countingInterceptor);
            bindInterceptor(Matchers.any(), Matchers.returns(only(Foo.class).or(only(Bar.class))), returnNullInterceptor);
        }
    });
    ConstructorBinding<?> interceptedBinding = (ConstructorBinding<?>) injector.getBinding(Interceptable.class);
    Method barMethod = Interceptable.class.getMethod("bar");
    Method fooMethod = Interceptable.class.getMethod("foo");
    assertEquals(ImmutableMap.<Method, List<MethodInterceptor>>of(fooMethod, ImmutableList.of(countingInterceptor, returnNullInterceptor), barMethod, ImmutableList.of(returnNullInterceptor)), interceptedBinding.getMethodInterceptors());
    ConstructorBinding<?> nonInterceptedBinding = (ConstructorBinding<?>) injector.getBinding(Foo.class);
    assertEquals(ImmutableMap.<Method, List<MethodInterceptor>>of(), nonInterceptedBinding.getMethodInterceptors());
    injector.getInstance(Interceptable.class).foo();
    assertEquals("expected counting interceptor to be invoked first", 1, count.get());
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ConstructorBinding(com.google.inject.spi.ConstructorBinding) Method(java.lang.reflect.Method)

Example 2 with ConstructorBinding

use of com.google.inject.spi.ConstructorBinding in project guice by google.

the class MethodInterceptionTest method testSpiAccessToInterceptors.

@Test
public void testSpiAccessToInterceptors() throws NoSuchMethodException {
    final MethodInterceptor countingInterceptor = new CountingInterceptor();
    final MethodInterceptor returnNullInterceptor = new ReturnNullInterceptor();
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bindInterceptor(Matchers.any(), Matchers.returns(only(Foo.class)), countingInterceptor);
            bindInterceptor(Matchers.any(), Matchers.returns(only(Foo.class).or(only(Bar.class))), returnNullInterceptor);
        }
    });
    ConstructorBinding<?> interceptedBinding = (ConstructorBinding<?>) injector.getBinding(Interceptable.class);
    Method barMethod = Interceptable.class.getMethod("bar");
    Method fooMethod = Interceptable.class.getMethod("foo");
    assertEquals(ImmutableMap.<Method, List<MethodInterceptor>>of(fooMethod, ImmutableList.of(countingInterceptor, returnNullInterceptor), barMethod, ImmutableList.of(returnNullInterceptor)), interceptedBinding.getMethodInterceptors());
    ConstructorBinding<?> nonInterceptedBinding = (ConstructorBinding<?>) injector.getBinding(Foo.class);
    assertEquals(ImmutableMap.<Method, List<MethodInterceptor>>of(), nonInterceptedBinding.getMethodInterceptors());
    injector.getInstance(Interceptable.class).foo();
    assertEquals("expected counting interceptor to be invoked first", 1, count.get());
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ConstructorBinding(com.google.inject.spi.ConstructorBinding) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

ConstructorBinding (com.google.inject.spi.ConstructorBinding)2 Method (java.lang.reflect.Method)2 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)2 Test (org.junit.Test)1