From 8681a8ad2a5534a74829e6433e53be047142a161 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 22 Jun 2015 15:20:41 +0100 Subject: [PATCH] Map empty virtual host to "/" when parsed from an address Previously, an address that ended in a "/" would result in the virtual host being an empty string. This was inconsistent with setVirtualHost which would map an empty string to "/". This commit updates the address parsing logic to call setVirtualHost rather than assigning the value directly to this.virtualHost. This ensures that the special handling for an empty string is applied consistently. Closes gh-3304 --- .../boot/autoconfigure/amqp/RabbitProperties.java | 5 +++-- .../autoconfigure/amqp/RabbitPropertiesTests.java | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) 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 72b2e58552..773253d831 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ import org.springframework.util.StringUtils; * * @author Greg Turnquist * @author Dave Syer + * @author Andy Wilkinson */ @ConfigurationProperties(prefix = "spring.rabbitmq") public class RabbitProperties { @@ -115,7 +116,7 @@ public class RabbitProperties { } int index = address.indexOf("/"); if (index >= 0 && index < address.length()) { - this.virtualHost = address.substring(index + 1); + setVirtualHost(address.substring(index + 1)); address = address.substring(0, index); } if (!address.contains(":")) { 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 d62bad95aa..f5c0df981a 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import static org.junit.Assert.assertNull; * Tests for {@link RabbitProperties}. * * @author Dave Syer + * @author Andy Wilkinson */ public class RabbitPropertiesTests { @@ -85,6 +86,15 @@ public class RabbitPropertiesTests { assertEquals("lemur.cloudamqp.com:5672", this.properties.getAddresses()); } + @Test + public void addressWithTrailingSlash() { + this.properties.setAddresses("amqp://root:password@otherhost:1111/"); + assertEquals("otherhost", this.properties.getHost()); + assertEquals(1111, this.properties.getPort()); + assertEquals("root", this.properties.getUsername()); + assertEquals("/", this.properties.getVirtualHost()); + } + @Test public void testDefaultVirtualHost() { this.properties.setVirtualHost("/"); @@ -92,7 +102,7 @@ public class RabbitPropertiesTests { } @Test - public void testemptyVirtualHost() { + public void testEmptyVirtualHost() { this.properties.setVirtualHost(""); assertEquals("/", this.properties.getVirtualHost()); }