@ -17,10 +17,12 @@
package org.springframework.boot.autoconfigure.amqp ;
import java.security.NoSuchAlgorithmException ;
import java.util.concurrent.atomic.AtomicInteger ;
import javax.net.ssl.SSLSocketFactory ;
import com.rabbitmq.client.Address ;
import com.rabbitmq.client.Connection ;
import org.aopalliance.aop.Advice ;
import org.junit.Rule ;
import org.junit.Test ;
@ -36,6 +38,7 @@ import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFacto
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory ;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode ;
import org.springframework.amqp.rabbit.connection.ConnectionFactory ;
import org.springframework.amqp.rabbit.connection.ConnectionNameStrategy ;
import org.springframework.amqp.rabbit.core.RabbitAdmin ;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate ;
import org.springframework.amqp.rabbit.core.RabbitTemplate ;
@ -57,6 +60,10 @@ import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.mockito.ArgumentMatchers.anyString ;
import static org.mockito.ArgumentMatchers.eq ;
import static org.mockito.ArgumentMatchers.isNull ;
import static org.mockito.BDDMockito.given ;
import static org.mockito.Mockito.mock ;
import static org.mockito.Mockito.verify ;
@ -151,6 +158,28 @@ public class RabbitAutoConfigurationTests {
} ) ;
}
@Test
public void testConnectionFactoryWithCustomConnectionNameStrategy ( ) {
this . contextRunner
. withUserConfiguration ( ConnectionNameStrategyConfiguration . class )
. run ( ( context ) - > {
CachingConnectionFactory connectionFactory = context
. getBean ( CachingConnectionFactory . class ) ;
DirectFieldAccessor dfa = new DirectFieldAccessor ( connectionFactory ) ;
Address [ ] addresses = ( Address [ ] ) dfa . getPropertyValue ( "addresses" ) ;
assertThat ( addresses ) . hasSize ( 1 ) ;
com . rabbitmq . client . ConnectionFactory rcf = mock ( com . rabbitmq . client . ConnectionFactory . class ) ;
given ( rcf . newConnection ( isNull ( ) , eq ( addresses ) , anyString ( ) ) )
. willReturn ( mock ( Connection . class ) ) ;
dfa . setPropertyValue ( "rabbitConnectionFactory" , rcf ) ;
connectionFactory . createConnection ( ) ;
verify ( rcf ) . newConnection ( isNull ( ) , eq ( addresses ) , eq ( "test#0" ) ) ;
connectionFactory . resetConnection ( ) ;
connectionFactory . createConnection ( ) ;
verify ( rcf ) . newConnection ( isNull ( ) , eq ( addresses ) , eq ( "test#1" ) ) ;
} ) ;
}
@Test
public void testConnectionFactoryEmptyVirtualHost ( ) {
this . contextRunner . withUserConfiguration ( TestConfiguration . class )
@ -751,6 +780,18 @@ public class RabbitAutoConfigurationTests {
}
@Configuration
protected static class ConnectionNameStrategyConfiguration {
private final AtomicInteger counter = new AtomicInteger ( ) ;
@Bean
public ConnectionNameStrategy myConnectionNameStrategy ( ) {
return c - > "test#" + this . counter . getAndIncrement ( ) ;
}
}
@Configuration
@EnableRabbit
protected static class EnableRabbitConfiguration {