|
17 | 17 | package org.springframework.boot.autoconfigure.amqp;
|
18 | 18 |
|
19 | 19 | import java.security.NoSuchAlgorithmException;
|
| 20 | +import java.util.Collection; |
20 | 21 | import java.util.List;
|
21 | 22 | import java.util.concurrent.ThreadFactory;
|
22 | 23 | import java.util.concurrent.atomic.AtomicInteger;
|
|
35 | 36 | import org.junit.jupiter.api.condition.EnabledForJreRange;
|
36 | 37 | import org.junit.jupiter.api.condition.JRE;
|
37 | 38 | import org.junit.jupiter.api.extension.ExtendWith;
|
| 39 | +import org.junit.jupiter.params.ParameterizedTest; |
| 40 | +import org.junit.jupiter.params.provider.ValueSource; |
38 | 41 | import org.mockito.InOrder;
|
39 | 42 |
|
40 | 43 | import org.springframework.amqp.core.AcknowledgeMode;
|
41 | 44 | import org.springframework.amqp.core.AmqpAdmin;
|
42 | 45 | import org.springframework.amqp.core.Message;
|
| 46 | +import org.springframework.amqp.core.MessageProperties; |
43 | 47 | import org.springframework.amqp.rabbit.annotation.EnableRabbit;
|
44 | 48 | import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
45 | 49 | import org.springframework.amqp.rabbit.config.AbstractRabbitListenerContainerFactory;
|
|
59 | 63 | import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
|
60 | 64 | import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
|
61 | 65 | import org.springframework.amqp.rabbit.retry.MessageRecoverer;
|
| 66 | +import org.springframework.amqp.support.converter.MessageConversionException; |
62 | 67 | import org.springframework.amqp.support.converter.MessageConverter;
|
| 68 | +import org.springframework.amqp.support.converter.SerializerMessageConverter; |
63 | 69 | import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
64 | 70 | import org.springframework.boot.autoconfigure.AutoConfigurations;
|
65 | 71 | import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration;
|
| 72 | +import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException; |
66 | 73 | import org.springframework.boot.test.context.FilteredClassLoader;
|
67 | 74 | import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
68 | 75 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
|
107 | 114 | * @author Andy Wilkinson
|
108 | 115 | * @author Phillip Webb
|
109 | 116 | * @author Scott Frederick
|
| 117 | + * @author Yanming Zhou |
110 | 118 | */
|
111 | 119 | @ExtendWith(OutputCaptureExtension.class)
|
112 | 120 | class RabbitAutoConfigurationTests {
|
@@ -796,6 +804,27 @@ void customizeRequestedChannelMax() {
|
796 | 804 | });
|
797 | 805 | }
|
798 | 806 |
|
| 807 | + @ParameterizedTest |
| 808 | + @ValueSource(classes = { TestConfiguration.class, TestConfiguration6.class }) |
| 809 | + @SuppressWarnings("unchecked") |
| 810 | + void customizeAllowedListPatterns(Class<?> configuration) { |
| 811 | + this.contextRunner.withUserConfiguration(configuration) |
| 812 | + .withPropertyValues("spring.rabbitmq.template.allowed-list-patterns:*") |
| 813 | + .run((context) -> { |
| 814 | + MessageConverter messageConverter = context.getBean(RabbitTemplate.class).getMessageConverter(); |
| 815 | + assertThat(messageConverter).extracting("allowedListPatterns") |
| 816 | + .isInstanceOfSatisfying(Collection.class, (set) -> assertThat(set).contains("*")); |
| 817 | + }); |
| 818 | + } |
| 819 | + |
| 820 | + @Test |
| 821 | + void customizeAllowedListPatternsWhenHasNoAllowedListDeserializingMessageConverter() { |
| 822 | + this.contextRunner.withUserConfiguration(CustomMessageConverterConfiguration.class) |
| 823 | + .withPropertyValues("spring.rabbitmq.template.allowed-list-patterns:*") |
| 824 | + .run((context) -> assertThat(context).getFailure() |
| 825 | + .hasRootCauseInstanceOf(InvalidConfigurationPropertyValueException.class)); |
| 826 | + } |
| 827 | + |
799 | 828 | @Test
|
800 | 829 | void noSslByDefault() {
|
801 | 830 | this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> {
|
@@ -1113,6 +1142,16 @@ RabbitListenerContainerFactory<?> rabbitListenerContainerFactory() {
|
1113 | 1142 |
|
1114 | 1143 | }
|
1115 | 1144 |
|
| 1145 | + @Configuration(proxyBeanMethods = false) |
| 1146 | + static class TestConfiguration6 { |
| 1147 | + |
| 1148 | + @Bean |
| 1149 | + MessageConverter messageConverter() { |
| 1150 | + return new SerializerMessageConverter(); |
| 1151 | + } |
| 1152 | + |
| 1153 | + } |
| 1154 | + |
1116 | 1155 | @Configuration(proxyBeanMethods = false)
|
1117 | 1156 | static class MessageConvertersConfiguration {
|
1118 | 1157 |
|
@@ -1387,6 +1426,29 @@ public List<Address> getAddresses() {
|
1387 | 1426 |
|
1388 | 1427 | }
|
1389 | 1428 |
|
| 1429 | + @Configuration |
| 1430 | + static class CustomMessageConverterConfiguration { |
| 1431 | + |
| 1432 | + @Bean |
| 1433 | + MessageConverter messageConverter() { |
| 1434 | + return new MessageConverter() { |
| 1435 | + |
| 1436 | + @Override |
| 1437 | + public Message toMessage(Object object, MessageProperties messageProperties) |
| 1438 | + throws MessageConversionException { |
| 1439 | + return new Message(object.toString().getBytes()); |
| 1440 | + } |
| 1441 | + |
| 1442 | + @Override |
| 1443 | + public Object fromMessage(Message message) throws MessageConversionException { |
| 1444 | + return new String(message.getBody()); |
| 1445 | + } |
| 1446 | + |
| 1447 | + }; |
| 1448 | + } |
| 1449 | + |
| 1450 | + } |
| 1451 | + |
1390 | 1452 | static class TestListener {
|
1391 | 1453 |
|
1392 | 1454 | @RabbitListener(queues = "test", autoStartup = "false")
|
|
0 commit comments