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