|
6 | 6 | import org.apache.qpid.jms.JmsConnectionFactory; |
7 | 7 | import org.junit.Test; |
8 | 8 | import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 9 | +import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration; |
9 | 10 | import org.springframework.boot.test.context.FilteredClassLoader; |
10 | 11 | import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 12 | +import org.springframework.jms.core.JmsTemplate; |
| 13 | + |
| 14 | +import javax.jms.ConnectionFactory; |
11 | 15 |
|
12 | 16 | import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
13 | 18 |
|
14 | 19 | public class ServiceBusJMSAutoConfigurationTest { |
15 | | - private ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
16 | | - .withConfiguration(AutoConfigurations.of(ServiceBusJMSAutoConfiguration.class)); |
| 20 | + |
| 21 | + private static final String CONNECTION_STRING = "Endpoint=sb://host/;SharedAccessKeyName=sasKeyName;" |
| 22 | + + "SharedAccessKey=sasKey"; |
17 | 23 |
|
18 | 24 | @Test |
19 | 25 | public void testAzureServiceBusDisabled() { |
20 | | - this.contextRunner.withPropertyValues("spring.jms.servicebus.enabled=false") |
21 | | - .run(context -> assertThat(context).doesNotHaveBean(AzureServiceBusJMSProperties.class)); |
| 26 | + ApplicationContextRunner contextRunner = getEmptyContextRunner(); |
| 27 | + contextRunner.withPropertyValues("spring.jms.servicebus.enabled=false") |
| 28 | + .run(context -> assertThat(context).doesNotHaveBean(AzureServiceBusJMSProperties.class)); |
22 | 29 | } |
23 | 30 |
|
24 | 31 | @Test |
25 | 32 | public void testWithoutServiceBusJMSNamespace() { |
26 | | - this.contextRunner.withClassLoader(new FilteredClassLoader(JmsConnectionFactory.class)) |
27 | | - .run(context -> assertThat(context).doesNotHaveBean(AzureServiceBusJMSProperties.class)); |
| 33 | + ApplicationContextRunner contextRunner = getEmptyContextRunner(); |
| 34 | + contextRunner.withClassLoader(new FilteredClassLoader(JmsConnectionFactory.class)) |
| 35 | + .run(context -> assertThat(context).doesNotHaveBean(AzureServiceBusJMSProperties.class)); |
28 | 36 | } |
29 | 37 |
|
30 | 38 | @Test(expected = IllegalStateException.class) |
31 | 39 | public void testAzureServiceBusJMSPropertiesValidation() { |
32 | | - this.contextRunner.run(context -> context.getBean(AzureServiceBusJMSProperties.class)); |
| 40 | + ApplicationContextRunner contextRunner = getEmptyContextRunner(); |
| 41 | + contextRunner.run(context -> context.getBean(AzureServiceBusJMSProperties.class)); |
33 | 42 | } |
34 | 43 |
|
35 | 44 | @Test |
36 | | - public void testAzureServiceBusJMSPropertiesConfigured() { |
| 45 | + public void testCachingConnectionFactoryIsAutowired() { |
37 | 46 |
|
38 | | - final String connectionString = "Endpoint=sb://host/;SharedAccessKeyName=sasKeyName;SharedAccessKey=sasKey"; |
| 47 | + ApplicationContextRunner contextRunner = getContextRunnerWithProperties(); |
39 | 48 |
|
40 | | - this.contextRunner = this.contextRunner.withPropertyValues( |
41 | | - "spring.jms.servicebus.connection-string=" + connectionString |
| 49 | + contextRunner.run( |
| 50 | + context -> { |
| 51 | + assertThat(context).hasSingleBean(ConnectionFactory.class); |
| 52 | + assertThat(context).hasSingleBean(JmsTemplate.class); |
| 53 | + ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class); |
| 54 | + assertTrue(connectionFactory == context.getBean(JmsTemplate.class).getConnectionFactory()); |
| 55 | + } |
42 | 56 | ); |
| 57 | + } |
43 | 58 |
|
44 | | - this.contextRunner = this.contextRunner.withPropertyValues( |
45 | | - "spring.jms.servicebus.topic-client-id=cid" |
46 | | - ); |
| 59 | + @Test |
| 60 | + public void testAzureServiceBusJMSPropertiesConfigured() { |
47 | 61 |
|
48 | | - this.contextRunner = this.contextRunner.withPropertyValues( |
49 | | - "spring.jms.servicebus.idle-timeout=123" |
50 | | - ); |
| 62 | + ApplicationContextRunner contextRunner = getContextRunnerWithProperties(); |
51 | 63 |
|
52 | | - this.contextRunner.run( |
| 64 | + contextRunner.run( |
53 | 65 | context -> { |
54 | 66 | assertThat(context).hasSingleBean(AzureServiceBusJMSProperties.class); |
55 | 67 | assertThat(context.getBean(AzureServiceBusJMSProperties.class).getConnectionString()).isEqualTo( |
56 | | - connectionString); |
| 68 | + CONNECTION_STRING); |
57 | 69 | assertThat(context.getBean(AzureServiceBusJMSProperties.class).getTopicClientId()).isEqualTo("cid"); |
58 | 70 | assertThat(context.getBean(AzureServiceBusJMSProperties.class).getIdleTimeout()).isEqualTo(123); |
59 | 71 | } |
60 | 72 | ); |
61 | 73 | } |
62 | 74 |
|
| 75 | + private ApplicationContextRunner getEmptyContextRunner() { |
| 76 | + |
| 77 | + return new ApplicationContextRunner() |
| 78 | + .withConfiguration(AutoConfigurations.of(ServiceBusJMSAutoConfiguration.class, JmsAutoConfiguration.class)); |
| 79 | + } |
| 80 | + |
| 81 | + private ApplicationContextRunner getContextRunnerWithProperties() { |
| 82 | + |
| 83 | + return new ApplicationContextRunner() |
| 84 | + .withConfiguration(AutoConfigurations.of(ServiceBusJMSAutoConfiguration.class, JmsAutoConfiguration.class)) |
| 85 | + .withPropertyValues( |
| 86 | + "spring.jms.servicebus.connection-string=" + CONNECTION_STRING, |
| 87 | + "spring.jms.servicebus.topic-client-id=cid", |
| 88 | + "spring.jms.servicebus.idle-timeout=123" |
| 89 | + ); |
| 90 | + } |
63 | 91 | } |
0 commit comments