Search in sources :

Example 21 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project ff4j by ff4j.

the class InvalidParameter method testInvalidParameter.

@Test(expected = IllegalArgumentException.class)
public void testInvalidParameter() throws Throwable {
    final IDoIt service = new IDoItImpl();
    service.doIt("");
    FeatureAdvisor fa = new FeatureAdvisor();
    fa.setFf4j(new FF4j("test-ff4j-features.xml"));
    MethodInvocation mi = new MethodInvocation() {

        public Object proceed() throws Throwable {
            return null;
        }

        public Object getThis() {
            return service;
        }

        public AccessibleObject getStaticPart() {
            return null;
        }

        public Object[] getArguments() {
            return null;
        }

        public Method getMethod() {
            try {
                Method m = IDoIt.class.getMethod("doIt", String.class);
                return m;
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    fa.invoke(mi);
}
Also used : FF4j(org.ff4j.FF4j) FeatureAdvisor(org.ff4j.aop.FeatureAdvisor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AccessibleObject(java.lang.reflect.AccessibleObject) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 22 with MethodInvocation

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

the class AdvisedMessageHandlerTests method throwableProperlyPropagated.

/**
 * Verify that Errors such as OOM are properly propagated.
 */
@Test
public void throwableProperlyPropagated() throws Exception {
    AbstractRequestHandlerAdvice advice = new AbstractRequestHandlerAdvice() {

        @Override
        protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception {
            Object result;
            try {
                result = callback.execute();
            } catch (Exception e) {
                // should not be unwrapped because the cause is a Throwable
                throw this.unwrapExceptionIfNecessary(e);
            }
            return result;
        }
    };
    final Throwable theThrowable = new Throwable("foo");
    MethodInvocation methodInvocation = mock(MethodInvocation.class);
    Method method = AbstractReplyProducingMessageHandler.class.getDeclaredMethod("handleRequestMessage", Message.class);
    when(methodInvocation.getMethod()).thenReturn(method);
    when(methodInvocation.getArguments()).thenReturn(new Object[] { new GenericMessage<String>("foo") });
    try {
        doAnswer(invocation -> {
            throw theThrowable;
        }).when(methodInvocation).proceed();
        advice.invoke(methodInvocation);
        fail("Expected throwable");
    } catch (Throwable t) {
        assertSame(theThrowable, t);
    }
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) Matchers.containsString(org.hamcrest.Matchers.containsString) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) Test(org.junit.Test)

Example 23 with MethodInvocation

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

the class SourcePollingChannelAdapterFactoryBeanTests method testTransactionalAdviceChain.

@Test
public void testTransactionalAdviceChain() throws Throwable {
    SourcePollingChannelAdapterFactoryBean factoryBean = new SourcePollingChannelAdapterFactoryBean();
    QueueChannel outputChannel = new QueueChannel();
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    factoryBean.setBeanFactory(context.getBeanFactory());
    factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
    factoryBean.setOutputChannel(outputChannel);
    factoryBean.setSource(() -> new GenericMessage<>("test"));
    PollerMetadata pollerMetadata = new PollerMetadata();
    List<Advice> adviceChain = new ArrayList<Advice>();
    final AtomicBoolean adviceApplied = new AtomicBoolean(false);
    adviceChain.add((MethodInterceptor) invocation -> {
        adviceApplied.set(true);
        return invocation.proceed();
    });
    pollerMetadata.setTrigger(new PeriodicTrigger(5000));
    pollerMetadata.setMaxMessagesPerPoll(1);
    final AtomicInteger count = new AtomicInteger();
    final MethodInterceptor txAdvice = mock(MethodInterceptor.class);
    adviceChain.add((MethodInterceptor) invocation -> {
        count.incrementAndGet();
        return invocation.proceed();
    });
    when(txAdvice.invoke(any(MethodInvocation.class))).thenAnswer(invocation -> {
        count.incrementAndGet();
        return ((MethodInvocation) invocation.getArgument(0)).proceed();
    });
    pollerMetadata.setAdviceChain(adviceChain);
    factoryBean.setPollerMetadata(pollerMetadata);
    factoryBean.setAutoStartup(true);
    factoryBean.afterPropertiesSet();
    context.registerEndpoint("testPollingEndpoint", factoryBean.getObject());
    context.refresh();
    Message<?> message = outputChannel.receive(5000);
    assertEquals("test", message.getPayload());
    assertEquals(1, count.get());
    assertTrue("adviceChain was not applied", adviceApplied.get());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ArgumentMatchers.contains(org.mockito.ArgumentMatchers.contains) Mockito.spy(org.mockito.Mockito.spy) TestUtils(org.springframework.integration.test.util.TestUtils) MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) MessageSource(org.springframework.integration.core.MessageSource) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) NullChannel(org.springframework.integration.channel.NullChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Advice(org.aopalliance.aop.Advice) Message(org.springframework.messaging.Message) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) ClassUtils(org.springframework.util.ClassUtils) Trigger(org.springframework.scheduling.Trigger) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) TaskScheduler(org.springframework.scheduling.TaskScheduler) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Mockito.verify(org.mockito.Mockito.verify) Lifecycle(org.springframework.context.Lifecycle) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) List(java.util.List) BeanFactory(org.springframework.beans.factory.BeanFactory) Log(org.apache.commons.logging.Log) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) QueueChannel(org.springframework.integration.channel.QueueChannel) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Advice(org.aopalliance.aop.Advice) PollerMetadata(org.springframework.integration.scheduling.PollerMetadata) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) Test(org.junit.Test)

Example 24 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project shiro by apache.

the class AopAllianceMethodInterceptorAdapterTest method testInvoke.

@Test
public void testInvoke() throws Throwable {
    MethodInvocation allianceInvocation = createMock(MethodInvocation.class);
    MethodInterceptor mockShiroInterceptor = createMock(MethodInterceptor.class);
    expect(mockShiroInterceptor.invoke(anyObject(AopAllianceMethodInvocationAdapter.class))).andAnswer(new IAnswer<Object>() {

        public Object answer() throws Throwable {
            return getCurrentArguments()[0];
        }
    });
    final Object expectedValue = new Object();
    expect(allianceInvocation.proceed()).andReturn(expectedValue);
    replay(mockShiroInterceptor, allianceInvocation);
    AopAllianceMethodInterceptorAdapter underTest = new AopAllianceMethodInterceptorAdapter(mockShiroInterceptor);
    Object invocation = underTest.invoke(allianceInvocation);
    Object value = ((AopAllianceMethodInvocationAdapter) invocation).proceed();
    assertSame("Adapter invocation returned a different value.", expectedValue, value);
    verify(mockShiroInterceptor, allianceInvocation);
}
Also used : MethodInterceptor(org.apache.shiro.aop.MethodInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.Test)

Example 25 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project shiro by apache.

the class AopAllianceMethodInvocationAdapterTest method testGetArguments.

@Test
public void testGetArguments() throws Exception {
    MethodInvocation mock = createMock(MethodInvocation.class);
    Object[] args = new Object[0];
    expect(mock.getArguments()).andReturn(args);
    AopAllianceMethodInvocationAdapter underTest = new AopAllianceMethodInvocationAdapter(mock);
    replay(mock);
    assertSame(args, underTest.getArguments());
    verify(mock);
}
Also used : MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.Test)

Aggregations

MethodInvocation (org.aopalliance.intercept.MethodInvocation)117 Test (org.junit.jupiter.api.Test)50 Test (org.junit.Test)35 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)25 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)22 Method (java.lang.reflect.Method)21 ArrayList (java.util.ArrayList)11 Log (org.apache.commons.logging.Log)11 Authentication (org.springframework.security.core.Authentication)10 EvaluationContext (org.springframework.expression.EvaluationContext)9 Expression (org.springframework.expression.Expression)9 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)9 List (java.util.List)7 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 MyThrowsHandler (org.springframework.aop.testfixture.advice.MyThrowsHandler)7 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)7 RemoteInvocation (org.springframework.remoting.support.RemoteInvocation)6 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)5 Promise (ratpack.exec.Promise)5