From aca564618166a35e4a7ba3c461c9e72643fde2a5 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 15 Dec 2015 15:39:04 -0500 Subject: [PATCH] Retry Advice Test Polishing Clean up and add test with recovery. --- ...boundChannelAdapterParserTests-context.xml | 6 ++- ...aultOutboundChannelAdapterParserTests.java | 27 +++++++++-- .../xml/RetryAdviceParserTests-context.xml | 6 +++ .../config/xml/RetryAdviceParserTests.java | 47 +++++++++++-------- 4 files changed, 62 insertions(+), 24 deletions(-) diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/DefaultOutboundChannelAdapterParserTests-context.xml b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/DefaultOutboundChannelAdapterParserTests-context.xml index 51233fde6c..65862c08fb 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/DefaultOutboundChannelAdapterParserTests-context.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/DefaultOutboundChannelAdapterParserTests-context.xml @@ -24,10 +24,14 @@ - + + + + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/DefaultOutboundChannelAdapterParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/DefaultOutboundChannelAdapterParserTests.java index 445f01187a..f87e4a24ee 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/DefaultOutboundChannelAdapterParserTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/DefaultOutboundChannelAdapterParserTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,11 @@ package org.springframework.integration.config.xml; -import static org.junit.Assert.*; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import org.hamcrest.Matchers; import org.junit.Test; @@ -26,10 +30,17 @@ import org.springframework.aop.support.AopUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationContext; +import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.config.TestConsumer; import org.springframework.integration.handler.MethodInvokingMessageHandler; import org.springframework.integration.handler.advice.RequestHandlerRetryAdvice; import org.springframework.integration.test.util.TestUtils; +import org.springframework.messaging.Message; +import org.springframework.messaging.MessageHandler; +import org.springframework.messaging.MessagingException; +import org.springframework.messaging.support.ErrorMessage; +import org.springframework.messaging.support.GenericMessage; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -40,6 +51,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration +@DirtiesContext public class DefaultOutboundChannelAdapterParserTests { @Autowired @@ -68,10 +80,18 @@ public class DefaultOutboundChannelAdapterParserTests { public void checkConfigWithInnerBeanAndPoller() { Object adapter = context.getBean("adapterB"); assertEquals(Boolean.FALSE, TestUtils.getPropertyValue(adapter, "autoStartup")); - Object handler = TestUtils.getPropertyValue(adapter, "handler"); + MessageHandler handler = TestUtils.getPropertyValue(adapter, "handler", MessageHandler.class); assertTrue(AopUtils.isAopProxy(handler)); assertThat(TestUtils.getPropertyValue(handler, "h.advised.advisors.first.item.advice"), Matchers.instanceOf(RequestHandlerRetryAdvice.class)); + + handler.handleMessage(new GenericMessage<>("foo")); + QueueChannel recovery = context.getBean("recovery", QueueChannel.class); + Message received = recovery.receive(10000); + assertNotNull(received); + assertThat(received, instanceOf(ErrorMessage.class)); + assertThat(received.getPayload(), instanceOf(MessagingException.class)); + assertEquals("foo", ((MessagingException) received.getPayload()).getFailedMessage().getPayload()); } @Test @@ -88,6 +108,7 @@ public class DefaultOutboundChannelAdapterParserTests { static class TestBean { public void out(Object o) { + throw new RuntimeException("ex"); } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/RetryAdviceParserTests-context.xml b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/RetryAdviceParserTests-context.xml index 3a334835cb..a09a24dbee 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/RetryAdviceParserTests-context.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/RetryAdviceParserTests-context.xml @@ -41,4 +41,10 @@ + + + + + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/RetryAdviceParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/RetryAdviceParserTests.java index c6ad2de81c..0bc3b2e4bb 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/RetryAdviceParserTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/RetryAdviceParserTests.java @@ -70,37 +70,44 @@ public class RetryAdviceParserTests { @Autowired @Qualifier("sa2.handler") private MessageHandler handler2; + @Autowired @Qualifier("saDefaultRetry.handler") + private MessageHandler defaultRetryHandler; + @Autowired private MessageChannel foo; @Test public void testAll() { - assertEquals(Integer.valueOf(3), TestUtils.getPropertyValue(a1, "retryTemplate.retryPolicy.maxAttempts", Integer.class)); - assertEquals(Integer.valueOf(4), TestUtils.getPropertyValue(a2, "retryTemplate.retryPolicy.maxAttempts", Integer.class)); - assertEquals(Integer.valueOf(5), TestUtils.getPropertyValue(a3, "retryTemplate.retryPolicy.maxAttempts", Integer.class)); - assertEquals(Integer.valueOf(6), TestUtils.getPropertyValue(a4, "retryTemplate.retryPolicy.maxAttempts", Integer.class)); - assertEquals(Integer.valueOf(7), TestUtils.getPropertyValue(a5, "retryTemplate.retryPolicy.maxAttempts", Integer.class)); - assertEquals(Integer.valueOf(8), TestUtils.getPropertyValue(a6, "retryTemplate.retryPolicy.maxAttempts", Integer.class)); - assertEquals(Integer.valueOf(3), TestUtils.getPropertyValue(a7, "retryTemplate.retryPolicy.maxAttempts", Integer.class)); + assertEquals(3, TestUtils.getPropertyValue(a1, "retryTemplate.retryPolicy.maxAttempts")); + assertEquals(4, TestUtils.getPropertyValue(a2, "retryTemplate.retryPolicy.maxAttempts")); + assertEquals(5, TestUtils.getPropertyValue(a3, "retryTemplate.retryPolicy.maxAttempts")); + assertEquals(6, TestUtils.getPropertyValue(a4, "retryTemplate.retryPolicy.maxAttempts")); + assertEquals(7, TestUtils.getPropertyValue(a5, "retryTemplate.retryPolicy.maxAttempts")); + assertEquals(8, TestUtils.getPropertyValue(a6, "retryTemplate.retryPolicy.maxAttempts")); + assertEquals(3, TestUtils.getPropertyValue(a7, "retryTemplate.retryPolicy.maxAttempts")); - assertEquals(Long.valueOf(1000), TestUtils.getPropertyValue(a3, "retryTemplate.backOffPolicy.backOffPeriod", Long.class)); - assertEquals(Long.valueOf(1234), TestUtils.getPropertyValue(a4, "retryTemplate.backOffPolicy.backOffPeriod", Long.class)); + assertEquals(1000L, TestUtils.getPropertyValue(a3, "retryTemplate.backOffPolicy.backOffPeriod")); + assertEquals(1234L, TestUtils.getPropertyValue(a4, "retryTemplate.backOffPolicy.backOffPeriod")); - assertEquals(Long.valueOf(100), TestUtils.getPropertyValue(a5, "retryTemplate.backOffPolicy.initialInterval", Long.class)); - assertEquals(Double.valueOf(2.0), TestUtils.getPropertyValue(a5, "retryTemplate.backOffPolicy.multiplier", Double.class)); - assertEquals(Long.valueOf(30000), TestUtils.getPropertyValue(a5, "retryTemplate.backOffPolicy.maxInterval", Long.class)); - assertEquals(Long.valueOf(1000), TestUtils.getPropertyValue(a6, "retryTemplate.backOffPolicy.initialInterval", Long.class)); - assertEquals(Double.valueOf(3.0), TestUtils.getPropertyValue(a6, "retryTemplate.backOffPolicy.multiplier", Double.class)); - assertEquals(Long.valueOf(10000), TestUtils.getPropertyValue(a6, "retryTemplate.backOffPolicy.maxInterval", Long.class)); + assertEquals(100L, TestUtils.getPropertyValue(a5, "retryTemplate.backOffPolicy.initialInterval")); + assertEquals(2.0, TestUtils.getPropertyValue(a5, "retryTemplate.backOffPolicy.multiplier")); + assertEquals(30000L, TestUtils.getPropertyValue(a5, "retryTemplate.backOffPolicy.maxInterval")); + assertEquals(1000L, TestUtils.getPropertyValue(a6, "retryTemplate.backOffPolicy.initialInterval")); + assertEquals(3.0, TestUtils.getPropertyValue(a6, "retryTemplate.backOffPolicy.multiplier")); + assertEquals(10000L, TestUtils.getPropertyValue(a6, "retryTemplate.backOffPolicy.maxInterval")); assertNull(TestUtils.getPropertyValue(a1, "recoveryCallback")); assertNotNull(TestUtils.getPropertyValue(a7, "recoveryCallback")); - assertSame(foo, TestUtils.getPropertyValue(a7, "recoveryCallback.messagingTemplate.defaultDestination")); - assertEquals(Long.valueOf(4567), TestUtils.getPropertyValue(a7, "recoveryCallback.messagingTemplate.sendTimeout")); + assertSame(this.foo, TestUtils.getPropertyValue(a7, "recoveryCallback.messagingTemplate.defaultDestination")); + assertEquals(4567L, TestUtils.getPropertyValue(a7, "recoveryCallback.messagingTemplate.sendTimeout")); - assertSame(a1, TestUtils.getPropertyValue(handler1, "adviceChain", List.class).get(0)); - assertEquals(Integer.valueOf(9), TestUtils.getPropertyValue(TestUtils.getPropertyValue(handler2, "adviceChain", List.class).get(0), - "retryTemplate.retryPolicy.maxAttempts", Integer.class)); + assertSame(this.a1, TestUtils.getPropertyValue(this.handler1, "adviceChain", List.class).get(0)); + assertEquals(9, TestUtils.getPropertyValue( + TestUtils.getPropertyValue(this.handler2, "adviceChain", List.class).get(0), + "retryTemplate.retryPolicy.maxAttempts")); + assertEquals(3, TestUtils.getPropertyValue( + TestUtils.getPropertyValue(this.defaultRetryHandler, "adviceChain", List.class).get(0), + "retryTemplate.retryPolicy.maxAttempts")); } }