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);
}
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);
}
}
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());
}
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);
}
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);
}
Aggregations