Polishing (backported from master)

(cherry picked from commit 92d78c1)
This commit is contained in:
Juergen Hoeller
2016-07-06 15:29:15 +02:00
parent 2e075baede
commit 0445ce6c53
54 changed files with 283 additions and 426 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.
@@ -38,7 +38,7 @@ import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePropertySource;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.util.TestContextResourceUtils;
import org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor;
import org.springframework.test.util.MetaAnnotationUtils.*;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@@ -57,19 +57,15 @@ import static org.springframework.test.util.MetaAnnotationUtils.*;
*/
public abstract class TestPropertySourceUtils {
private static final Log logger = LogFactory.getLog(TestPropertySourceUtils.class);
/**
* The name of the {@link MapPropertySource} created from <em>inlined properties</em>.
* @since 4.1.5
* @see {@link #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])}
* @see #addInlinedPropertiesToEnvironment
*/
public static final String INLINED_PROPERTIES_PROPERTY_SOURCE_NAME = "Inlined Test Properties";
private static final Log logger = LogFactory.getLog(TestPropertySourceUtils.class);
private TestPropertySourceUtils() {
/* no-op */
}
static MergedTestPropertySources buildMergedTestPropertySources(Class<?> testClass) {
Class<TestPropertySource> annotationType = TestPropertySource.class;
@@ -168,15 +164,14 @@ public abstract class TestPropertySourceUtils {
* never {@code null}
* @param locations the resource locations of {@code Properties} files to add
* to the environment; potentially empty but never {@code null}
* @throws IllegalStateException if an error occurs while processing a properties file
* @since 4.1.5
* @see ResourcePropertySource
* @see TestPropertySource#locations
* @throws IllegalStateException if an error occurs while processing a properties file
*/
public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContext context,
String[] locations) {
Assert.notNull(context, "context must not be null");
Assert.notNull(locations, "locations must not be null");
public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContext context, String... locations) {
Assert.notNull(context, "'context' must not be null");
Assert.notNull(locations, "'locations' must not be null");
try {
ConfigurableEnvironment environment = context.getEnvironment();
for (String location : locations) {
@@ -185,8 +180,8 @@ public abstract class TestPropertySourceUtils {
environment.getPropertySources().addFirst(new ResourcePropertySource(resource));
}
}
catch (IOException e) {
throw new IllegalStateException("Failed to add PropertySource to Environment", e);
catch (IOException ex) {
throw new IllegalStateException("Failed to add PropertySource to Environment", ex);
}
}
@@ -203,10 +198,9 @@ public abstract class TestPropertySourceUtils {
* @see TestPropertySource#properties
* @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])
*/
public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationContext context,
String[] inlinedProperties) {
Assert.notNull(context, "context must not be null");
Assert.notNull(inlinedProperties, "inlinedProperties must not be null");
public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationContext context, String... inlinedProperties) {
Assert.notNull(context, "'context' must not be null");
Assert.notNull(inlinedProperties, "'inlinedProperties' must not be null");
addInlinedPropertiesToEnvironment(context.getEnvironment(), inlinedProperties);
}
@@ -226,16 +220,16 @@ public abstract class TestPropertySourceUtils {
* @see TestPropertySource#properties
* @see #convertInlinedPropertiesToMap
*/
public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment environment, String[] inlinedProperties) {
Assert.notNull(environment, "environment must not be null");
Assert.notNull(inlinedProperties, "inlinedProperties must not be null");
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)) {
if (logger.isDebugEnabled()) {
logger.debug("Adding inlined properties to environment: "
+ ObjectUtils.nullSafeToString(inlinedProperties));
logger.debug("Adding inlined properties to environment: " +
ObjectUtils.nullSafeToString(inlinedProperties));
}
MapPropertySource ps = new MapPropertySource(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME,
convertInlinedPropertiesToMap(inlinedProperties));
convertInlinedPropertiesToMap(inlinedProperties));
environment.getPropertySources().addFirst(ps);
}
}
@@ -257,24 +251,22 @@ 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) {
Assert.notNull(inlinedProperties, "inlinedProperties must not be null");
public static Map<String, Object> convertInlinedPropertiesToMap(String... inlinedProperties) {
Assert.notNull(inlinedProperties, "'inlinedProperties' must not be null");
Map<String, Object> map = new LinkedHashMap<String, Object>();
Properties props = new Properties();
for (String pair : inlinedProperties) {
if (!StringUtils.hasText(pair)) {
continue;
}
try {
props.load(new StringReader(pair));
}
catch (Exception e) {
throw new IllegalStateException("Failed to load test environment property from [" + pair + "].", e);
catch (Exception ex) {
throw new IllegalStateException("Failed to load test environment property from [" + pair + "]", ex);
}
Assert.state(props.size() == 1, "Failed to load exactly one test environment property from [" + pair + "].");
Assert.state(props.size() == 1, "Failed to load exactly one test environment property from [" + pair + "]");
for (String name : props.stringPropertyNames()) {
map.put(name, props.getProperty(name));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 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.
@@ -124,6 +124,7 @@ public class JdbcTestUtils {
*/
public static int deleteFromTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause,
Object... args) {
String sql = "DELETE FROM " + tableName;
if (StringUtils.hasText(whereClause)) {
sql += " WHERE " + whereClause;
@@ -170,6 +171,7 @@ public class JdbcTestUtils {
@Deprecated
public static void executeSqlScript(JdbcTemplate jdbcTemplate, ResourceLoader resourceLoader,
String sqlResourcePath, boolean continueOnError) throws DataAccessException {
Resource resource = resourceLoader.getResource(sqlResourcePath);
executeSqlScript(jdbcTemplate, resource, continueOnError);
}
@@ -197,6 +199,7 @@ public class JdbcTestUtils {
@Deprecated
public static void executeSqlScript(JdbcTemplate jdbcTemplate, Resource resource, boolean continueOnError)
throws DataAccessException {
executeSqlScript(jdbcTemplate, new EncodedResource(resource), continueOnError);
}
@@ -220,6 +223,7 @@ public class JdbcTestUtils {
@Deprecated
public static void executeSqlScript(JdbcTemplate jdbcTemplate, EncodedResource resource, boolean continueOnError)
throws DataAccessException {
new ResourceDatabasePopulator(continueOnError, false, resource.getEncoding(), resource.getResource()).execute(jdbcTemplate.getDataSource());
}
@@ -288,4 +292,5 @@ public class JdbcTestUtils {
public static void splitSqlScript(String script, char delim, List<String> statements) {
ScriptUtils.splitSqlScript(script, delim, statements);
}
}