|
3 | 3 |
|
4 | 4 | package com.azure.spring.cloud.autoconfigure.implementation.jms; |
5 | 5 |
|
| 6 | +import com.azure.core.credential.TokenCredential; |
6 | 7 | import com.azure.servicebus.jms.ServiceBusJmsConnectionFactory; |
| 8 | +import com.azure.servicebus.jms.ServiceBusJmsConnectionFactorySettings; |
7 | 9 | import com.azure.spring.cloud.autoconfigure.implementation.context.properties.AzureGlobalProperties; |
| 10 | +import com.azure.spring.cloud.autoconfigure.jms.AzureServiceBusJmsConnectionFactoryFactory; |
8 | 11 | import jakarta.jms.Connection; |
9 | 12 | import jakarta.jms.ConnectionFactory; |
10 | 13 | import jakarta.jms.Destination; |
|
19 | 22 | import org.springframework.boot.jms.autoconfigure.JmsAutoConfiguration; |
20 | 23 | import org.springframework.boot.test.context.FilteredClassLoader; |
21 | 24 | import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 25 | +import org.springframework.context.annotation.Bean; |
22 | 26 | import org.springframework.context.annotation.Configuration; |
23 | 27 | import org.springframework.context.annotation.PropertySource; |
24 | 28 | import org.springframework.jms.connection.CachingConnectionFactory; |
@@ -180,6 +184,72 @@ void fallbackToServiceBusConnectionFactoryWhenNoCachingOrPoolClassesPresent(Stri |
180 | 184 | }); |
181 | 185 | } |
182 | 186 |
|
| 187 | + @Test |
| 188 | + void useCustomServiceBusJmsConnectionFactoryClassForServiceBusFactory() { |
| 189 | + this.contextRunner |
| 190 | + .withUserConfiguration(CustomConnectionFactoryClassConfiguration.class) |
| 191 | + .withPropertyValues( |
| 192 | + "spring.jms.servicebus.pricing-tier=premium", |
| 193 | + "spring.jms.servicebus.pool.enabled=false", |
| 194 | + "spring.jms.cache.enabled=false" |
| 195 | + ) |
| 196 | + .run(context -> { |
| 197 | + assertThat(context).hasSingleBean(ServiceBusJmsConnectionFactory.class); |
| 198 | + assertThat(context.getBean(ServiceBusJmsConnectionFactory.class)) |
| 199 | + .isInstanceOf(CustomServiceBusJmsConnectionFactory.class); |
| 200 | + }); |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + void useCustomServiceBusJmsConnectionFactoryClassForPasswordlessServiceBusFactory() { |
| 205 | + this.contextRunner |
| 206 | + .withUserConfiguration(CustomConnectionFactoryClassConfiguration.class) |
| 207 | + .withPropertyValues( |
| 208 | + "spring.jms.servicebus.pricing-tier=premium", |
| 209 | + "spring.jms.servicebus.passwordless-enabled=true", |
| 210 | + "spring.jms.servicebus.namespace=test-namespace", |
| 211 | + "spring.jms.servicebus.pool.enabled=false", |
| 212 | + "spring.jms.cache.enabled=false" |
| 213 | + ) |
| 214 | + .run(context -> { |
| 215 | + assertThat(context).hasSingleBean(ServiceBusJmsConnectionFactory.class); |
| 216 | + assertThat(context.getBean(ServiceBusJmsConnectionFactory.class)) |
| 217 | + .isInstanceOf(CustomServiceBusJmsConnectionFactory.class); |
| 218 | + }); |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + void useCustomServiceBusJmsConnectionFactoryClassForCachingFactory() { |
| 223 | + this.contextRunner |
| 224 | + .withUserConfiguration(CustomConnectionFactoryClassConfiguration.class) |
| 225 | + .withPropertyValues( |
| 226 | + "spring.jms.servicebus.pricing-tier=premium", |
| 227 | + "spring.jms.cache.enabled=true" |
| 228 | + ) |
| 229 | + .run(context -> { |
| 230 | + assertThat(context).hasSingleBean(CachingConnectionFactory.class); |
| 231 | + CachingConnectionFactory cachingConnectionFactory = context.getBean(CachingConnectionFactory.class); |
| 232 | + assertThat(cachingConnectionFactory.getTargetConnectionFactory()) |
| 233 | + .isInstanceOf(CustomServiceBusJmsConnectionFactory.class); |
| 234 | + }); |
| 235 | + } |
| 236 | + |
| 237 | + @Test |
| 238 | + void useCustomServiceBusJmsConnectionFactoryClassForPoolingFactory() { |
| 239 | + this.contextRunner |
| 240 | + .withUserConfiguration(CustomConnectionFactoryClassConfiguration.class) |
| 241 | + .withPropertyValues( |
| 242 | + "spring.jms.servicebus.pricing-tier=premium", |
| 243 | + "spring.jms.servicebus.pool.enabled=true" |
| 244 | + ) |
| 245 | + .run(context -> { |
| 246 | + assertThat(context).hasSingleBean(JmsPoolConnectionFactory.class); |
| 247 | + JmsPoolConnectionFactory poolConnectionFactory = context.getBean(JmsPoolConnectionFactory.class); |
| 248 | + assertThat(poolConnectionFactory.getConnectionFactory()) |
| 249 | + .isInstanceOf(CustomServiceBusJmsConnectionFactory.class); |
| 250 | + }); |
| 251 | + } |
| 252 | + |
183 | 253 | @Test |
184 | 254 | void cachingConnectionFactoryReusesSameProducerForSameDestination() throws Exception { |
185 | 255 | // Create mock objects for JMS components |
@@ -276,4 +346,25 @@ private Session createServiceBusJmsSession(Session innerSession) throws Exceptio |
276 | 346 | static class AdditionalPropertySourceConfiguration { |
277 | 347 |
|
278 | 348 | } |
| 349 | + |
| 350 | + @Configuration |
| 351 | + static class CustomConnectionFactoryClassConfiguration { |
| 352 | + @Bean |
| 353 | + AzureServiceBusJmsConnectionFactoryFactory connectionFactoryFactory() { |
| 354 | + return () -> new CustomServiceBusJmsConnectionFactory( |
| 355 | + String.format(CONNECTION_STRING_FORMAT, "test-namespace"), |
| 356 | + new ServiceBusJmsConnectionFactorySettings()); |
| 357 | + } |
| 358 | + } |
| 359 | + |
| 360 | + static class CustomServiceBusJmsConnectionFactory extends ServiceBusJmsConnectionFactory { |
| 361 | + public CustomServiceBusJmsConnectionFactory(String connectionString, ServiceBusJmsConnectionFactorySettings settings) { |
| 362 | + super(connectionString, settings); |
| 363 | + } |
| 364 | + |
| 365 | + public CustomServiceBusJmsConnectionFactory(TokenCredential tokenCredential, String host, |
| 366 | + ServiceBusJmsConnectionFactorySettings settings) { |
| 367 | + super(tokenCredential, host, settings); |
| 368 | + } |
| 369 | + } |
279 | 370 | } |
0 commit comments