From ce0cbe05bf28dabc4a71ce9a4c46366824617273 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Thu, 3 Jul 2014 17:56:16 -0400 Subject: [PATCH] Restore AMQP vHost leading `/` support Update RabbitProperties since vHosts have no restrictions requiring a leading slash and can have any number of leading slashes. Fixes gh-1206 --- .../autoconfigure/amqp/RabbitProperties.java | 4 --- ...java => RabbitAutoConfigurationTests.java} | 28 +++++++++++++++++-- .../amqp/RabbitPropertiesTests.java | 2 +- 3 files changed, 27 insertions(+), 7 deletions(-) rename spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/{RabbitAutoconfigurationTests.java => RabbitAutoConfigurationTests.java} (83%) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java index 63d11b1752..ec5af23e74 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java @@ -143,11 +143,7 @@ public class RabbitProperties { } public void setVirtualHost(String virtualHost) { - while (virtualHost.startsWith("/") && virtualHost.length() > 0) { - virtualHost = virtualHost.substring(1); - } this.virtualHost = ("".equals(virtualHost) ? "/" : virtualHost); - } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoconfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java similarity index 83% rename from spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoconfigurationTests.java rename to spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java index d5cedd016f..32418a3260 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoconfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java @@ -38,7 +38,7 @@ import static org.junit.Assert.assertNotNull; * * @author Greg Turnquist */ -public class RabbitAutoconfigurationTests { +public class RabbitAutoConfigurationTests { private AnnotationConfigApplicationContext context; @@ -74,7 +74,7 @@ public class RabbitAutoconfigurationTests { .getBean(CachingConnectionFactory.class); assertEquals("remote-server", connectionFactory.getHost()); assertEquals(9000, connectionFactory.getPort()); - assertEquals("vhost", connectionFactory.getVirtualHost()); + assertEquals("/vhost", connectionFactory.getVirtualHost()); } @Test @@ -89,6 +89,30 @@ public class RabbitAutoconfigurationTests { assertEquals("/", connectionFactory.getVirtualHost()); } + @Test + public void testRabbitTemplateVirtualHostNoLeadingSlash() { + this.context = new AnnotationConfigApplicationContext(); + this.context.register(TestConfiguration.class, RabbitAutoConfiguration.class); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.rabbitmq.virtual_host:foo"); + this.context.refresh(); + CachingConnectionFactory connectionFactory = this.context + .getBean(CachingConnectionFactory.class); + assertEquals("foo", connectionFactory.getVirtualHost()); + } + + @Test + public void testRabbitTemplateVirtualHostMultiLeadingSlashes() { + this.context = new AnnotationConfigApplicationContext(); + this.context.register(TestConfiguration.class, RabbitAutoConfiguration.class); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.rabbitmq.virtual_host:///foo"); + this.context.refresh(); + CachingConnectionFactory connectionFactory = this.context + .getBean(CachingConnectionFactory.class); + assertEquals("///foo", connectionFactory.getVirtualHost()); + } + @Test public void testRabbitTemplateDefaultVirtualHost() { this.context = new AnnotationConfigApplicationContext(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java index 0a9c0058ff..b84bbc2345 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java @@ -99,7 +99,7 @@ public class RabbitPropertiesTests { @Test public void testCustomFalsyVirtualHost() { this.properties.setVirtualHost("/myvHost"); - assertEquals("myvHost", this.properties.getVirtualHost()); + assertEquals("/myvHost", this.properties.getVirtualHost()); } }