Browse Source

Polish DefaultMessageListenerContainer[Tests]

pull/30904/head
Sam Brannen 3 years ago
parent
commit
b375a061df
  1. 9
      spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java
  2. 28
      spring-jms/src/test/java/org/springframework/jms/listener/DefaultMessageListenerContainerTests.java

9
spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java

@ -256,8 +256,14 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe
/** /**
* Specify the level of caching that this listener container is allowed to apply, * Specify the level of caching that this listener container is allowed to apply,
* in the form of the name of the corresponding constant: e.g. "CACHE_CONNECTION". * in the form of the name of the corresponding constant — for example,
* {@code "CACHE_CONNECTION"}.
* @see #setCacheLevel * @see #setCacheLevel
* @see #CACHE_NONE
* @see #CACHE_CONNECTION
* @see #CACHE_SESSION
* @see #CACHE_CONSUMER
* @see #CACHE_AUTO
*/ */
public void setCacheLevelName(String constantName) throws IllegalArgumentException { public void setCacheLevelName(String constantName) throws IllegalArgumentException {
if (!constantName.startsWith("CACHE_")) { if (!constantName.startsWith("CACHE_")) {
@ -282,6 +288,7 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe
* @see #CACHE_CONNECTION * @see #CACHE_CONNECTION
* @see #CACHE_SESSION * @see #CACHE_SESSION
* @see #CACHE_CONSUMER * @see #CACHE_CONSUMER
* @see #CACHE_AUTO
* @see #setCacheLevelName * @see #setCacheLevelName
* @see #setTransactionManager * @see #setTransactionManager
*/ */

28
spring-jms/src/test/java/org/springframework/jms/listener/DefaultMessageListenerContainerTests.java

@ -38,13 +38,15 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link DefaultMessageListenerContainer}.
*
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Juergen Hoeller * @author Juergen Hoeller
*/ */
public class DefaultMessageListenerContainerTests { class DefaultMessageListenerContainerTests {
@Test @Test
public void applyBackOff() { void applyBackOff() {
BackOff backOff = mock(); BackOff backOff = mock();
BackOffExecution execution = mock(); BackOffExecution execution = mock();
given(execution.nextBackOff()).willReturn(BackOffExecution.STOP); given(execution.nextBackOff()).willReturn(BackOffExecution.STOP);
@ -65,7 +67,7 @@ public class DefaultMessageListenerContainerTests {
} }
@Test @Test
public void applyBackOffRetry() { void applyBackOffRetry() {
BackOff backOff = mock(); BackOff backOff = mock();
BackOffExecution execution = mock(); BackOffExecution execution = mock();
given(execution.nextBackOff()).willReturn(50L, BackOffExecution.STOP); given(execution.nextBackOff()).willReturn(50L, BackOffExecution.STOP);
@ -84,7 +86,7 @@ public class DefaultMessageListenerContainerTests {
} }
@Test @Test
public void recoverResetBackOff() { void recoverResetBackOff() {
BackOff backOff = mock(); BackOff backOff = mock();
BackOffExecution execution = mock(); BackOffExecution execution = mock();
given(execution.nextBackOff()).willReturn(50L, 50L, 50L); // 3 attempts max given(execution.nextBackOff()).willReturn(50L, 50L, 50L); // 3 attempts max
@ -103,7 +105,7 @@ public class DefaultMessageListenerContainerTests {
} }
@Test @Test
public void stopAndRestart() { void stopAndRestart() {
DefaultMessageListenerContainer container = createRunningContainer(); DefaultMessageListenerContainer container = createRunningContainer();
container.stop(); container.stop();
@ -112,7 +114,7 @@ public class DefaultMessageListenerContainerTests {
} }
@Test @Test
public void stopWithCallbackAndRestart() throws InterruptedException { void stopWithCallbackAndRestart() throws InterruptedException {
DefaultMessageListenerContainer container = createRunningContainer(); DefaultMessageListenerContainer container = createRunningContainer();
TestRunnable stopCallback = new TestRunnable(); TestRunnable stopCallback = new TestRunnable();
@ -124,7 +126,7 @@ public class DefaultMessageListenerContainerTests {
} }
@Test @Test
public void stopCallbackIsInvokedEvenIfContainerIsNotRunning() throws InterruptedException { void stopCallbackIsInvokedEvenIfContainerIsNotRunning() throws InterruptedException {
DefaultMessageListenerContainer container = createRunningContainer(); DefaultMessageListenerContainer container = createRunningContainer();
container.stop(); container.stop();
@ -137,7 +139,7 @@ public class DefaultMessageListenerContainerTests {
} }
private DefaultMessageListenerContainer createRunningContainer() { private static DefaultMessageListenerContainer createRunningContainer() {
DefaultMessageListenerContainer container = createContainer(createSuccessfulConnectionFactory()); DefaultMessageListenerContainer container = createContainer(createSuccessfulConnectionFactory());
container.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONNECTION); container.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONNECTION);
container.setBackOff(new FixedBackOff(100, 1)); container.setBackOff(new FixedBackOff(100, 1));
@ -146,7 +148,7 @@ public class DefaultMessageListenerContainerTests {
return container; return container;
} }
private ConnectionFactory createSuccessfulConnectionFactory() { private static ConnectionFactory createSuccessfulConnectionFactory() {
try { try {
ConnectionFactory connectionFactory = mock(); ConnectionFactory connectionFactory = mock();
given(connectionFactory.createConnection()).willReturn(mock()); given(connectionFactory.createConnection()).willReturn(mock());
@ -157,7 +159,7 @@ public class DefaultMessageListenerContainerTests {
} }
} }
private DefaultMessageListenerContainer createContainer(ConnectionFactory connectionFactory) { private static DefaultMessageListenerContainer createContainer(ConnectionFactory connectionFactory) {
Destination destination = new Destination() {}; Destination destination = new Destination() {};
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer(); DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
@ -167,7 +169,7 @@ public class DefaultMessageListenerContainerTests {
return container; return container;
} }
private ConnectionFactory createFailingContainerFactory() { private static ConnectionFactory createFailingContainerFactory() {
try { try {
ConnectionFactory connectionFactory = mock(); ConnectionFactory connectionFactory = mock();
given(connectionFactory.createConnection()).will(invocation -> { given(connectionFactory.createConnection()).will(invocation -> {
@ -180,7 +182,7 @@ public class DefaultMessageListenerContainerTests {
} }
} }
private ConnectionFactory createRecoverableContainerFactory(final int failingAttempts) { private static ConnectionFactory createRecoverableContainerFactory(final int failingAttempts) {
try { try {
ConnectionFactory connectionFactory = mock(); ConnectionFactory connectionFactory = mock();
given(connectionFactory.createConnection()).will(new Answer<Object>() { given(connectionFactory.createConnection()).will(new Answer<Object>() {
@ -213,7 +215,7 @@ public class DefaultMessageListenerContainerTests {
this.countDownLatch.countDown(); this.countDownLatch.countDown();
} }
public void waitForCompletion() throws InterruptedException { void waitForCompletion() throws InterruptedException {
this.countDownLatch.await(1, TimeUnit.SECONDS); this.countDownLatch.await(1, TimeUnit.SECONDS);
assertThat(this.countDownLatch.getCount()).as("callback was not invoked").isEqualTo(0); assertThat(this.countDownLatch.getCount()).as("callback was not invoked").isEqualTo(0);
} }

Loading…
Cancel
Save