Browse Source

Assert DefaultMessageListenerContainer.setCacheLevel() values

See gh-30851
pull/28156/merge
Sam Brannen 1 year ago
parent
commit
b45bfcafc2
  1. 3
      spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java
  2. 22
      spring-jms/src/test/java/org/springframework/jms/listener/DefaultMessageListenerContainerTests.java

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

@ -280,7 +280,7 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe @@ -280,7 +280,7 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe
Assert.hasText(constantName, "'constantName' must not be null or blank");
Integer cacheLevel = constants.get(constantName);
Assert.notNull(cacheLevel, "Only cache constants allowed");
setCacheLevel(cacheLevel);
this.cacheLevel = cacheLevel;
}
/**
@ -304,6 +304,7 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe @@ -304,6 +304,7 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe
* @see #setTransactionManager
*/
public void setCacheLevel(int cacheLevel) {
Assert.isTrue(constants.containsValue(cacheLevel), "Only values of cache constants allowed");
this.cacheLevel = cacheLevel;
}

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

@ -172,6 +172,28 @@ class DefaultMessageListenerContainerTests { @@ -172,6 +172,28 @@ class DefaultMessageListenerContainerTests {
assertThat(uniqueValues).hasSize(5);
}
@Test
void setCacheLevel() {
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
assertThatIllegalArgumentException().isThrownBy(() -> container.setCacheLevel(999));
container.setCacheLevel(DefaultMessageListenerContainer.CACHE_NONE);
assertThat(container.getCacheLevel()).isEqualTo(DefaultMessageListenerContainer.CACHE_NONE);
container.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONNECTION);
assertThat(container.getCacheLevel()).isEqualTo(DefaultMessageListenerContainer.CACHE_CONNECTION);
container.setCacheLevel(DefaultMessageListenerContainer.CACHE_SESSION);
assertThat(container.getCacheLevel()).isEqualTo(DefaultMessageListenerContainer.CACHE_SESSION);
container.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONSUMER);
assertThat(container.getCacheLevel()).isEqualTo(DefaultMessageListenerContainer.CACHE_CONSUMER);
container.setCacheLevel(DefaultMessageListenerContainer.CACHE_AUTO);
assertThat(container.getCacheLevel()).isEqualTo(DefaultMessageListenerContainer.CACHE_AUTO);
}
private static Stream<String> streamCacheConstants() {
return Arrays.stream(DefaultMessageListenerContainer.class.getFields())

Loading…
Cancel
Save