Skip to content

Commit f9afc79

Browse files
committed
Polish "Add support for configuring non-standard JMS acknowledge modes"
See spring-projectsgh-37576
1 parent e472df0 commit f9afc79

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) {
9090
private void mapTemplateProperties(Template properties, JmsTemplate template) {
9191
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
9292
map.from(properties.getSession()::getAcknowledgeMode)
93-
.to((acknowledgeMode) -> template
94-
.setSessionAcknowledgeMode(JmsAcknowledgeModeMapper.map(acknowledgeMode)));
93+
.as(JmsAcknowledgeModeMapper::map)
94+
.to(template::setSessionAcknowledgeMode);
9595
map.from(properties.getSession()::isTransacted).to(template::setSessionTransacted);
9696
map.from(properties::getDefaultDestination).whenNonNull().to(template::setDefaultDestinationName);
9797
map.from(properties::getDeliveryDelay).whenNonNull().as(Duration::toMillis).to(template::setDeliveryDelay);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.jms;
18+
19+
import jakarta.jms.Session;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.EnumSource;
23+
24+
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
28+
29+
/**
30+
* Tests for {@link JmsAcknowledgeModeMapper}.
31+
*
32+
* @author Andy Wilkinson
33+
*/
34+
class JmsAcknowledgeModeMapperTests {
35+
36+
@ParameterizedTest
37+
@EnumSource(Mapping.class)
38+
void stringIsMappedToInt(Mapping mapping) {
39+
assertThat(JmsAcknowledgeModeMapper.map(mapping.actual)).isEqualTo(mapping.expected);
40+
}
41+
42+
@Test
43+
void mapShouldThrowWhenMapIsCalledWithUnknownNonIntegerString() {
44+
assertThatExceptionOfType(InvalidConfigurationPropertyValueException.class)
45+
.isThrownBy(() -> JmsAcknowledgeModeMapper.map("some-string"));
46+
}
47+
48+
private enum Mapping {
49+
50+
AUTO_LOWER_CASE("auto", Session.AUTO_ACKNOWLEDGE),
51+
52+
CLIENT_LOWER_CASE("client", Session.CLIENT_ACKNOWLEDGE),
53+
54+
DUPS_OK_LOWER_CASE("dups_ok", Session.DUPS_OK_ACKNOWLEDGE),
55+
56+
AUTO_UPPER_CASE("AUTO", Session.AUTO_ACKNOWLEDGE),
57+
58+
CLIENT_UPPER_CASE("CLIENT", Session.CLIENT_ACKNOWLEDGE),
59+
60+
DUPS_OK_UPPER_CASE("DUPS_OK", Session.DUPS_OK_ACKNOWLEDGE),
61+
62+
AUTO_MIXED_CASE("AuTo", Session.AUTO_ACKNOWLEDGE),
63+
64+
CLIENT_MIXED_CASE("CliEnT", Session.CLIENT_ACKNOWLEDGE),
65+
66+
DUPS_OK_MIXED_CASE("dUPs_Ok", Session.DUPS_OK_ACKNOWLEDGE),
67+
68+
DUPS_OK_KEBAB_CASE("DUPS-OK", Session.DUPS_OK_ACKNOWLEDGE),
69+
70+
DUPS_OK_NO_SEPARATOR_UPPER_CASE("DUPSOK", Session.DUPS_OK_ACKNOWLEDGE),
71+
72+
DUPS_OK_NO_SEPARATOR_LOWER_CASE("dupsok", Session.DUPS_OK_ACKNOWLEDGE),
73+
74+
DUPS_OK_NO_SEPARATOR_MIXED_CASE("duPSok", Session.DUPS_OK_ACKNOWLEDGE),
75+
76+
INTEGER("36", 36);
77+
78+
private final String actual;
79+
80+
private final int expected;
81+
82+
Mapping(String actual, int expected) {
83+
this.actual = actual;
84+
this.expected = expected;
85+
}
86+
87+
}
88+
89+
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ private void testJmsListenerContainerFactoryWithCustomSettings(AssertableApplica
160160
assertThat(container).hasFieldOrPropertyWithValue("receiveTimeout", 2000L);
161161
}
162162

163+
@Test
164+
void testJmsListenerContainerFactoryWithNonStandardAcknowledgeMode() {
165+
this.contextRunner.withUserConfiguration(EnableJmsConfiguration.class)
166+
.withPropertyValues("spring.jms.listener.session.acknowledge-mode=9")
167+
.run((context) -> {
168+
DefaultMessageListenerContainer container = getContainer(context, "jmsListenerContainerFactory");
169+
assertThat(container.getSessionAcknowledgeMode()).isEqualTo(9);
170+
});
171+
}
172+
163173
@Test
164174
void testJmsListenerContainerFactoryWithDefaultSettings() {
165175
this.contextRunner.withUserConfiguration(EnableJmsConfiguration.class)
@@ -300,6 +310,16 @@ void testJmsTemplateFullCustomization() {
300310
});
301311
}
302312

313+
@Test
314+
void testJmsTemplateWithNonStandardAcknowledgeMode() {
315+
this.contextRunner.withUserConfiguration(EnableJmsConfiguration.class)
316+
.withPropertyValues("spring.jms.template.session.acknowledge-mode=7")
317+
.run((context) -> {
318+
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
319+
assertThat(jmsTemplate.getSessionAcknowledgeMode()).isEqualTo(7);
320+
});
321+
}
322+
303323
@Test
304324
void testJmsMessagingTemplateUseConfiguredDefaultDestination() {
305325
this.contextRunner.withPropertyValues("spring.jms.template.default-destination=testQueue").run((context) -> {

0 commit comments

Comments
 (0)