Refine TestPropertySourceUtils for direct use

Update methods in TestPropertySourceUtils to accept var-args and allow
addInlinedProperties to be called more than once.

Issue: SPR-14088
This commit is contained in:
Phillip Webb 2016-03-25 11:10:47 -07:00
parent fee056a1b1
commit fc839331a7
2 changed files with 16 additions and 13 deletions

View File

@ -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<String, Object>());
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<String, Object> convertInlinedPropertiesToMap(String[] inlinedProperties) {
public static Map<String, Object> convertInlinedPropertiesToMap(String... inlinedProperties) {
Assert.notNull(inlinedProperties, "inlinedProperties must not be null");
Map<String, Object> map = new LinkedHashMap<String, Object>();

View File

@ -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);
}
// -------------------------------------------------------------------