Search in sources :

Example 16 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project spring-data-commons by spring-projects.

the class ProjectingMethodInterceptorUnitTests method wrapsDelegateResultInProxyIfTypesDontMatch.

// DATAREST-221
@Test
public void wrapsDelegateResultInProxyIfTypesDontMatch() throws Throwable {
    MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(new ProxyProjectionFactory(), interceptor, conversionService);
    when(invocation.getMethod()).thenReturn(Helper.class.getMethod("getHelper"));
    when(interceptor.invoke(invocation)).thenReturn("Foo");
    assertThat(methodInterceptor.invoke(invocation)).isInstanceOf(Helper.class);
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Test(org.junit.Test)

Example 17 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project spring-data-commons by spring-projects.

the class ProjectingMethodInterceptorUnitTests method returnsNullAsIs.

// DATAREST-221
@Test
public void returnsNullAsIs() throws Throwable {
    MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(factory, interceptor, conversionService);
    when(interceptor.invoke(invocation)).thenReturn(null);
    assertThat(methodInterceptor.invoke(invocation)).isNull();
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Test(org.junit.Test)

Example 18 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project spring-data-commons by spring-projects.

the class ProjectingMethodInterceptorUnitTests method returnsSingleElementCollectionForTargetThatReturnsNonCollection.

@Test
public void returnsSingleElementCollectionForTargetThatReturnsNonCollection() throws Throwable {
    MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(new ProxyProjectionFactory(), interceptor, conversionService);
    Helper reference = mock(Helper.class);
    Object result = methodInterceptor.invoke(mockInvocationOf("getHelperCollection", reference));
    assertThat(result).isInstanceOf(Collection.class);
    Collection<?> collection = (Collection<?>) result;
    assertThat(collection).hasSize(1);
    assertThat(collection).hasOnlyElementsOfType(HelperProjection.class);
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Collection(java.util.Collection) Test(org.junit.Test)

Example 19 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project guice by google.

the class MethodInterceptionTest method testGetElements_interceptorBindings.

@Test
public void testGetElements_interceptorBindings() throws Exception {
    @SuppressWarnings("rawtypes") Matcher<Class> classMatcher = Matchers.subclassesOf(List.class);
    Matcher<Method> methodMatcher = Matchers.returns(Matchers.identicalTo(int.class));
    MethodInterceptor interceptor = new MethodInterceptor() {

        @Override
        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            return null;
        }
    };
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bindInterceptor(classMatcher, methodMatcher, interceptor);
        }
    });
    final List<InterceptorBinding> interceptorBindings = new ArrayList<>();
    for (Element element : injector.getElements()) {
        element.acceptVisitor(new DefaultElementVisitor<Void>() {

            @Override
            public Void visit(InterceptorBinding interceptorBinding) {
                interceptorBindings.add(interceptorBinding);
                return null;
            }
        });
    }
    assertThat(interceptorBindings).hasSize(1);
    InterceptorBinding extractedBinding = interceptorBindings.get(0);
    assertSame(classMatcher, extractedBinding.getClassMatcher());
    assertSame(methodMatcher, extractedBinding.getMethodMatcher());
    assertSame(interceptor, extractedBinding.getInterceptors().get(0));
}
Also used : Element(com.google.inject.spi.Element) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) InterceptorBinding(com.google.inject.spi.InterceptorBinding) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Test(org.junit.Test)

Example 20 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project roboguice by roboguice.

the class BindingTest method testToConstructorAndMethodInterceptors.

/*if[AOP]*/
public void testToConstructorAndMethodInterceptors() throws NoSuchMethodException {
    final Constructor<D> constructor = D.class.getConstructor(Stage.class);
    final AtomicInteger count = new AtomicInteger();
    final MethodInterceptor countingInterceptor = new MethodInterceptor() {

        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            count.incrementAndGet();
            return methodInvocation.proceed();
        }
    };
    Injector injector = Guice.createInjector(new AbstractModule() {

        protected void configure() {
            bind(Object.class).toConstructor(constructor);
            bindInterceptor(Matchers.any(), Matchers.any(), countingInterceptor);
        }
    });
    D d = (D) injector.getInstance(Object.class);
    d.hashCode();
    d.hashCode();
    assertEquals(2, count.get());
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MethodInvocation(org.aopalliance.intercept.MethodInvocation)

Aggregations

MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)57 Test (org.junit.Test)30 MethodInvocation (org.aopalliance.intercept.MethodInvocation)28 List (java.util.List)20 Method (java.lang.reflect.Method)19 ArrayList (java.util.ArrayList)18 Test (org.junit.jupiter.api.Test)15 Advice (org.aopalliance.aop.Advice)14 Map (java.util.Map)13 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)13 AopUtils (org.springframework.aop.support.AopUtils)12 TestBean (org.springframework.beans.testfixture.beans.TestBean)12 ProxyFactory (org.springframework.aop.framework.ProxyFactory)11 HashMap (java.util.HashMap)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)9 Advisor (org.springframework.aop.Advisor)9 Message (org.springframework.messaging.Message)8