Skip to content

Commit

Permalink
GH-973: Higher order for RabbitListenerTestHarness
Browse files Browse the repository at this point in the history
Fixes #973

When we use `@EnableRabbit` and `@RabbitListenerTest` in the same
configuration set, e.g. mixing real `@Configuration` and test one for
`@RabbitListenerTest`, we may end up with the case when `@EnableRabbit`
is processed before `@RabbitListenerTest`, so, `RabbitListenerTestHarness`
bean is not going to appear in the application context.

* Override `getOrder()` for the `RabbitListenerTestHarness` to give it
higher priority than regular `RabbitListenerAnnotationBeanPostProcessor`,
so it is registered first and then the last one won't override existing
bean

**Cherry-pick to 2.1.x**
  • Loading branch information
artembilan authored and garyrussell committed Apr 10, 2019
1 parent 45ec57f commit ff49223
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,20 @@
* by autowiring the test harness into test cases.
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 1.6
*
*/
public class RabbitListenerTestHarness extends RabbitListenerAnnotationBeanPostProcessor {

private final Log logger = LogFactory.getLog(this.getClass());

private final Map<String, CaptureAdvice> listenerCapture = new HashMap<String, CaptureAdvice>();
private final Map<String, CaptureAdvice> listenerCapture = new HashMap<>();

private final AnnotationAttributes attributes;
private final Map<String, Object> listeners = new HashMap<>();

private final Map<String, Object> listeners = new HashMap<String, Object>();
private final AnnotationAttributes attributes;

public RabbitListenerTestHarness(AnnotationMetadata importMetadata) {
Map<String, Object> map = importMetadata.getAnnotationAttributes(RabbitListenerTest.class.getName());
Expand Down Expand Up @@ -112,33 +114,37 @@ public <T> T getSpy(String id) {
return (T) this.listeners.get(id);
}

@Override
public int getOrder() {
return super.getOrder() - 100;
}

private static final class CaptureAdvice implements MethodInterceptor {

private final BlockingQueue<InvocationData> invocationData = new LinkedBlockingQueue<InvocationData>();
private final BlockingQueue<InvocationData> invocationData = new LinkedBlockingQueue<>();

CaptureAdvice() {
super();
}

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object result = null;
boolean isListenerMethod =
AnnotationUtils.findAnnotation(invocation.getMethod(), RabbitListener.class) != null
|| AnnotationUtils.findAnnotation(invocation.getMethod(), RabbitHandler.class) != null;
try {
result = invocation.proceed();
Object result = invocation.proceed();
if (isListenerMethod) {
this.invocationData.put(new InvocationData(invocation, result));
}
return result;
}
catch (Throwable t) { // NOSONAR - rethrown below
if (isListenerMethod) {
this.invocationData.put(new InvocationData(invocation, t));
}
throw t;
}
return result;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import org.springframework.amqp.core.AnonymousQueue;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
Expand All @@ -50,6 +51,8 @@

/**
* @author Gary Russell
* @author Artem Bilan
*
* @since 1.6
*
*/
Expand All @@ -74,7 +77,7 @@ public class ExampleRabbitListenerSpyTest {
private RabbitListenerTestHarness harness;

@Test
public void testTwoWay() throws Exception {
public void testTwoWay() {
assertEquals("FOO", this.rabbitTemplate.convertSendAndReceive(this.queue1.getName(), "foo"));

Listener listener = this.harness.getSpy("foo");
Expand All @@ -99,6 +102,7 @@ public void testOneWay() throws Exception {
}

@Configuration
@EnableRabbit
@RabbitListenerTest
public static class Config {

Expand Down

0 comments on commit ff49223

Please sign in to comment.