Skip to content

Commit 3a8b511

Browse files
committed
Stop using Constants utility in DefaultMessageListenerContainer
See gh-30851
1 parent b375a06 commit 3a8b511

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.jms.listener;
1818

1919
import java.util.HashSet;
20+
import java.util.Map;
2021
import java.util.Set;
2122
import java.util.concurrent.Executor;
2223

@@ -25,7 +26,6 @@
2526
import jakarta.jms.MessageConsumer;
2627
import jakarta.jms.Session;
2728

28-
import org.springframework.core.Constants;
2929
import org.springframework.core.task.SimpleAsyncTaskExecutor;
3030
import org.springframework.core.task.TaskExecutor;
3131
import org.springframework.jms.JmsException;
@@ -115,6 +115,7 @@
115115
* before listener execution, with no redelivery in case of an exception.
116116
*
117117
* @author Juergen Hoeller
118+
* @author Sam Brannen
118119
* @since 2.0
119120
* @see #setTransactionManager
120121
* @see #setCacheLevel
@@ -171,7 +172,17 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe
171172
public static final int CACHE_AUTO = 4;
172173

173174

174-
private static final Constants constants = new Constants(DefaultMessageListenerContainer.class);
175+
/**
176+
* Map of constant names to constant values for the cache constants defined
177+
* in this class.
178+
*/
179+
private static final Map<String, Integer> constants = Map.of(
180+
"CACHE_NONE", CACHE_NONE,
181+
"CACHE_CONNECTION", CACHE_CONNECTION,
182+
"CACHE_SESSION", CACHE_SESSION,
183+
"CACHE_CONSUMER", CACHE_CONSUMER,
184+
"CACHE_AUTO", CACHE_AUTO
185+
);
175186

176187

177188
@Nullable
@@ -266,10 +277,10 @@ public void setRecoveryInterval(long recoveryInterval) {
266277
* @see #CACHE_AUTO
267278
*/
268279
public void setCacheLevelName(String constantName) throws IllegalArgumentException {
269-
if (!constantName.startsWith("CACHE_")) {
270-
throw new IllegalArgumentException("Only cache constants allowed");
271-
}
272-
setCacheLevel(constants.asNumber(constantName).intValue());
280+
Assert.hasText(constantName, "'constantName' must not be null or blank");
281+
Integer cacheLevel = constants.get(constantName);
282+
Assert.notNull(cacheLevel, "Only cache constants allowed");
283+
setCacheLevel(cacheLevel);
273284
}
274285

275286
/**

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
package org.springframework.jms.listener;
1818

19+
import java.lang.reflect.Field;
20+
import java.util.Arrays;
1921
import java.util.concurrent.CountDownLatch;
2022
import java.util.concurrent.TimeUnit;
23+
import java.util.stream.Stream;
2124

2225
import jakarta.jms.Connection;
2326
import jakarta.jms.ConnectionFactory;
@@ -27,11 +30,14 @@
2730
import org.mockito.invocation.InvocationOnMock;
2831
import org.mockito.stubbing.Answer;
2932

33+
import org.springframework.util.ReflectionUtils;
3034
import org.springframework.util.backoff.BackOff;
3135
import org.springframework.util.backoff.BackOffExecution;
3236
import org.springframework.util.backoff.FixedBackOff;
3337

3438
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
40+
import static org.assertj.core.api.Assertions.assertThatNoException;
3541
import static org.mockito.BDDMockito.given;
3642
import static org.mockito.Mockito.mock;
3743
import static org.mockito.Mockito.times;
@@ -42,6 +48,7 @@
4248
*
4349
* @author Stephane Nicoll
4450
* @author Juergen Hoeller
51+
* @author Sam Brannen
4552
*/
4653
class DefaultMessageListenerContainerTests {
4754

@@ -138,6 +145,32 @@ void stopCallbackIsInvokedEvenIfContainerIsNotRunning() throws InterruptedExcept
138145
container.destroy();
139146
}
140147

148+
@Test
149+
void setCacheLevelNameToUnsupportedValues() {
150+
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
151+
assertThatIllegalArgumentException().isThrownBy(() -> container.setCacheLevelName(null));
152+
assertThatIllegalArgumentException().isThrownBy(() -> container.setCacheLevelName(" "));
153+
assertThatIllegalArgumentException().isThrownBy(() -> container.setCacheLevelName("bogus"));
154+
}
155+
156+
/**
157+
* This test effectively verifies that the internal 'constants' map is properly
158+
* configured for all cache constants defined in {@link DefaultMessageListenerContainer}.
159+
*/
160+
@Test
161+
void setCacheLevelNameToAllSupportedValues() {
162+
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
163+
streamCacheConstants()
164+
.map(Field::getName)
165+
.forEach(name -> assertThatNoException().isThrownBy(() -> container.setCacheLevelName(name)));
166+
}
167+
168+
169+
private static Stream<Field> streamCacheConstants() {
170+
return Arrays.stream(DefaultMessageListenerContainer.class.getFields())
171+
.filter(ReflectionUtils::isPublicStaticFinal)
172+
.filter(field -> field.getName().startsWith("CACHE_"));
173+
}
141174

142175
private static DefaultMessageListenerContainer createRunningContainer() {
143176
DefaultMessageListenerContainer container = createContainer(createSuccessfulConnectionFactory());

0 commit comments

Comments
 (0)