diff --git a/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java b/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java index 1619ea8458e..3e499a7cc80 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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,7 +27,6 @@ import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; @@ -174,7 +173,7 @@ public abstract class TestPropertySourceUtils { * @throws IllegalStateException if an error occurs while processing a properties file */ public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContext context, - String[] locations) { + String... locations) { Assert.notNull(context, "context must not be null"); Assert.notNull(locations, "locations must not be null"); try { @@ -204,7 +203,7 @@ public abstract class TestPropertySourceUtils { * @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[]) */ public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationContext context, - String[] inlinedProperties) { + String... inlinedProperties) { Assert.notNull(context, "context must not be null"); Assert.notNull(inlinedProperties, "inlinedProperties must not be null"); addInlinedPropertiesToEnvironment(context.getEnvironment(), inlinedProperties); @@ -226,7 +225,7 @@ public abstract class TestPropertySourceUtils { * @see TestPropertySource#properties * @see #convertInlinedPropertiesToMap */ - public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment environment, String[] inlinedProperties) { + public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment environment, String... inlinedProperties) { Assert.notNull(environment, "environment must not be null"); Assert.notNull(inlinedProperties, "inlinedProperties must not be null"); if (!ObjectUtils.isEmpty(inlinedProperties)) { @@ -234,9 +233,13 @@ public abstract class TestPropertySourceUtils { logger.debug("Adding inlined properties to environment: " + ObjectUtils.nullSafeToString(inlinedProperties)); } - MapPropertySource ps = new MapPropertySource(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME, - convertInlinedPropertiesToMap(inlinedProperties)); - environment.getPropertySources().addFirst(ps); + MapPropertySource ps = (MapPropertySource) environment.getPropertySources().get(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME); + if (ps == null) { + ps = new MapPropertySource(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME, + new LinkedHashMap()); + environment.getPropertySources().addFirst(ps); + } + ps.getSource().putAll(convertInlinedPropertiesToMap(inlinedProperties)); } } @@ -257,7 +260,7 @@ public abstract class TestPropertySourceUtils { * a given inlined property contains multiple key-value pairs * @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[]) */ - public static Map convertInlinedPropertiesToMap(String[] inlinedProperties) { + public static Map convertInlinedPropertiesToMap(String... inlinedProperties) { Assert.notNull(inlinedProperties, "inlinedProperties must not be null"); Map map = new LinkedHashMap(); diff --git a/spring-test/src/test/java/org/springframework/test/context/support/TestPropertySourceUtilsTests.java b/spring-test/src/test/java/org/springframework/test/context/support/TestPropertySourceUtilsTests.java index 8f6c8d7efb0..e6c41b5f8df 100644 --- a/spring-test/src/test/java/org/springframework/test/context/support/TestPropertySourceUtilsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/support/TestPropertySourceUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -140,7 +140,7 @@ public class TestPropertySourceUtilsTests { public void addInlinedPropertiesToEnvironmentWithContextAndNullInlinedProperties() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("inlined"); - addInlinedPropertiesToEnvironment(mock(ConfigurableApplicationContext.class), null); + addInlinedPropertiesToEnvironment(mock(ConfigurableApplicationContext.class), (String[]) null); } /** @@ -160,7 +160,7 @@ public class TestPropertySourceUtilsTests { public void addInlinedPropertiesToEnvironmentWithEnvironmentAndNullInlinedProperties() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("inlined"); - addInlinedPropertiesToEnvironment(new MockEnvironment(), null); + addInlinedPropertiesToEnvironment(new MockEnvironment(), (String[]) null); } /** @@ -202,7 +202,7 @@ public class TestPropertySourceUtilsTests { public void convertInlinedPropertiesToMapWithNullInlinedProperties() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("inlined"); - convertInlinedPropertiesToMap(null); + convertInlinedPropertiesToMap((String[]) null); } // -------------------------------------------------------------------