Search in sources :

Example 41 with MethodInterceptor

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

the class AdvisedMessageHandlerTests method testINT2858ExpressionAdviceWithSendFailureOnEachRetry.

@Test
public void testINT2858ExpressionAdviceWithSendFailureOnEachRetry() {
    final AtomicInteger counter = new AtomicInteger(0);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return "foo";
        }
    };
    QueueChannel errors = new QueueChannel();
    List<Advice> adviceChain = new ArrayList<Advice>();
    ExpressionEvaluatingRequestHandlerAdvice expressionAdvice = new ExpressionEvaluatingRequestHandlerAdvice();
    expressionAdvice.setBeanFactory(mock(BeanFactory.class));
    expressionAdvice.setOnFailureExpressionString("#exception.message");
    expressionAdvice.setFailureChannel(errors);
    adviceChain.add(new RequestHandlerRetryAdvice());
    adviceChain.add(expressionAdvice);
    adviceChain.add((MethodInterceptor) invocation -> {
        throw new RuntimeException("intentional: " + counter.incrementAndGet());
    });
    handler.setAdviceChain(adviceChain);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    try {
        handler.handleMessage(new GenericMessage<String>("test"));
    } catch (Exception e) {
        assertEquals("intentional: 3", e.getCause().getMessage());
    }
    for (int i = 1; i <= 3; i++) {
        Message<?> receive = errors.receive(10000);
        assertNotNull(receive);
        assertEquals("intentional: " + i, ((MessageHandlingExpressionEvaluatingAdviceException) receive.getPayload()).getEvaluationResult());
    }
    assertNull(errors.receive(1));
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) DefaultRetryState(org.springframework.retry.support.DefaultRetryState) AopUtils(org.springframework.aop.support.AopUtils) Autowired(org.springframework.beans.factory.annotation.Autowired) ErrorMessage(org.springframework.messaging.support.ErrorMessage) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) MethodInvocation(org.aopalliance.intercept.MethodInvocation) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) PollableChannel(org.springframework.messaging.PollableChannel) Method(java.lang.reflect.Method) AdviceMessage(org.springframework.integration.message.AdviceMessage) TaskScheduler(org.springframework.scheduling.TaskScheduler) MessageChannel(org.springframework.messaging.MessageChannel) Executors(java.util.concurrent.Executors) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) RunWith(org.junit.runner.RunWith) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Mockito.spy(org.mockito.Mockito.spy) TestUtils(org.springframework.integration.test.util.TestUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) MessageFilter(org.springframework.integration.filter.MessageFilter) Advice(org.aopalliance.aop.Advice) Message(org.springframework.messaging.Message) ExecutorService(java.util.concurrent.ExecutorService) ErrorHandlingTaskExecutor(org.springframework.integration.util.ErrorHandlingTaskExecutor) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Mockito(org.mockito.Mockito) Assert.assertNull(org.junit.Assert.assertNull) RetryContext(org.springframework.retry.RetryContext) BeanFactory(org.springframework.beans.factory.BeanFactory) ContextConfiguration(org.springframework.test.context.ContextConfiguration) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Log(org.apache.commons.logging.Log) GenericMessage(org.springframework.messaging.support.GenericMessage) RetryTemplate(org.springframework.retry.support.RetryTemplate) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Advice(org.aopalliance.aop.Advice) Test(org.junit.Test)

Example 42 with MethodInterceptor

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

the class TypeListenerTest method testEncounterCannotBeUsedAfterHearReturns.

@Test
public void testEncounterCannotBeUsedAfterHearReturns() {
    final AtomicReference<TypeEncounter<?>> encounterReference = new AtomicReference<TypeEncounter<?>>();
    Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bindListener(any(), new TypeListener() {

                @Override
                public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
                    encounterReference.set(encounter);
                }
            });
            bind(C.class);
        }
    });
    TypeEncounter<?> encounter = encounterReference.get();
    try {
        encounter.register(new InjectionListener<Object>() {

            @Override
            public void afterInjection(Object injectee) {
            }
        });
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        encounter.bindInterceptor(any(), new MethodInterceptor() {

            @Override
            public Object invoke(MethodInvocation methodInvocation) throws Throwable {
                return methodInvocation.proceed();
            }
        });
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        encounter.addError(new Exception());
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        encounter.getMembersInjector(A.class);
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        encounter.getProvider(B.class);
        fail();
    } catch (IllegalStateException expected) {
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) MethodInvocation(org.aopalliance.intercept.MethodInvocation) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) TypeListener(com.google.inject.spi.TypeListener) TypeEncounter(com.google.inject.spi.TypeEncounter) Test(org.junit.Test)

Example 43 with MethodInterceptor

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

the class FactoryProvider2Test method testMethodInterceptorsOnAssistedTypes.

@Test
public void testMethodInterceptorsOnAssistedTypes() {
    assumeTrue(InternalFlags.isBytecodeGenEnabled());
    final AtomicInteger invocationCount = new AtomicInteger();
    final MethodInterceptor interceptor = new MethodInterceptor() {

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

        @Override
        protected void configure() {
            bindInterceptor(Matchers.any(), Matchers.any(), interceptor);
            bind(Double.class).toInstance(5.0d);
            bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
        }
    });
    ColoredCarFactory factory = injector.getInstance(ColoredCarFactory.class);
    Mustang mustang = (Mustang) factory.create(Color.GREEN);
    assertEquals(0, invocationCount.get());
    mustang.drive();
    assertEquals(1, invocationCount.get());
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Injector(com.google.inject.Injector) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AbstractModule(com.google.inject.AbstractModule) Test(org.junit.Test)

Example 44 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor 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)

Example 45 with MethodInterceptor

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

the class BindingTest method testToConstructorAndMethodInterceptors.

@Test
public void testToConstructorAndMethodInterceptors() throws NoSuchMethodException {
    assumeTrue(InternalFlags.isBytecodeGenEnabled());
    final Constructor<D> constructor = D.class.getConstructor(Stage.class);
    final AtomicInteger count = new AtomicInteger();
    final MethodInterceptor countingInterceptor = new MethodInterceptor() {

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

        @Override
        protected void configure() {
            bind(Object.class).toConstructor(constructor);
            bindInterceptor(Matchers.any(), Matchers.any(), countingInterceptor);
        }
    });
    D d = (D) injector.getInstance(Object.class);
    int unused = d.hashCode();
    int unused2 = d.hashCode();
    assertEquals(2, count.get());
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MethodInvocation(org.aopalliance.intercept.MethodInvocation) InjectionPoint(com.google.inject.spi.InjectionPoint) Test(org.junit.Test)

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