Polishing

This commit is contained in:
Juergen Hoeller 2016-07-06 18:11:33 +02:00
parent 102dc8a4dd
commit da9c24c41e
6 changed files with 53 additions and 61 deletions

View File

@ -992,7 +992,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException { Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException {
descriptor.initParameterNameDiscovery(getParameterNameDiscoverer()); descriptor.initParameterNameDiscovery(getParameterNameDiscoverer());
if (descriptor.getDependencyType() == Optional.class) { if (Optional.class == descriptor.getDependencyType()) {
return createOptionalDependency(descriptor, beanName); return createOptionalDependency(descriptor, beanName);
} }
else if (ObjectFactory.class == descriptor.getDependencyType() || else if (ObjectFactory.class == descriptor.getDependencyType() ||

View File

@ -75,7 +75,6 @@ public abstract class TestPropertySourceUtils {
return new MergedTestPropertySources(); return new MergedTestPropertySources();
} }
// else...
List<TestPropertySourceAttributes> attributesList = resolveTestPropertySourceAttributes(testClass); List<TestPropertySourceAttributes> attributesList = resolveTestPropertySourceAttributes(testClass);
String[] locations = mergeLocations(attributesList); String[] locations = mergeLocations(attributesList);
String[] properties = mergeProperties(attributesList); String[] properties = mergeProperties(attributesList);
@ -84,30 +83,27 @@ public abstract class TestPropertySourceUtils {
private static List<TestPropertySourceAttributes> resolveTestPropertySourceAttributes(Class<?> testClass) { private static List<TestPropertySourceAttributes> resolveTestPropertySourceAttributes(Class<?> testClass) {
Assert.notNull(testClass, "Class must not be null"); Assert.notNull(testClass, "Class must not be null");
List<TestPropertySourceAttributes> attributesList = new ArrayList<>();
Class<TestPropertySource> annotationType = TestPropertySource.class;
final List<TestPropertySourceAttributes> attributesList = new ArrayList<>();
final Class<TestPropertySource> annotationType = TestPropertySource.class;
AnnotationDescriptor<TestPropertySource> descriptor = findAnnotationDescriptor(testClass, annotationType); AnnotationDescriptor<TestPropertySource> descriptor = findAnnotationDescriptor(testClass, annotationType);
Assert.notNull(descriptor, String.format( Assert.notNull(descriptor, String.format(
"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]", "Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
annotationType.getName(), testClass.getName())); annotationType.getName(), testClass.getName()));
while (descriptor != null) { while (descriptor != null) {
TestPropertySource testPropertySource = descriptor.synthesizeAnnotation(); TestPropertySource testPropertySource = descriptor.synthesizeAnnotation();
Class<?> rootDeclaringClass = descriptor.getRootDeclaringClass(); Class<?> rootDeclaringClass = descriptor.getRootDeclaringClass();
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace(String.format("Retrieved @TestPropertySource [%s] for declaring class [%s].", logger.trace(String.format("Retrieved @TestPropertySource [%s] for declaring class [%s].",
testPropertySource, rootDeclaringClass.getName())); testPropertySource, rootDeclaringClass.getName()));
} }
TestPropertySourceAttributes attributes =
TestPropertySourceAttributes attributes = new TestPropertySourceAttributes(rootDeclaringClass, new TestPropertySourceAttributes(rootDeclaringClass, testPropertySource);
testPropertySource);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Resolved TestPropertySource attributes: " + attributes); logger.trace("Resolved TestPropertySource attributes: " + attributes);
} }
attributesList.add(attributes); attributesList.add(attributes);
descriptor = findAnnotationDescriptor(rootDeclaringClass.getSuperclass(), annotationType); descriptor = findAnnotationDescriptor(rootDeclaringClass.getSuperclass(), annotationType);
} }
@ -115,40 +111,32 @@ public abstract class TestPropertySourceUtils {
} }
private static String[] mergeLocations(List<TestPropertySourceAttributes> attributesList) { private static String[] mergeLocations(List<TestPropertySourceAttributes> attributesList) {
final List<String> locations = new ArrayList<>(); List<String> locations = new ArrayList<>();
for (TestPropertySourceAttributes attrs : attributesList) { for (TestPropertySourceAttributes attrs : attributesList) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace(String.format("Processing locations for TestPropertySource attributes %s", attrs)); logger.trace(String.format("Processing locations for TestPropertySource attributes %s", attrs));
} }
String[] locationsArray = TestContextResourceUtils.convertToClasspathResourcePaths( String[] locationsArray = TestContextResourceUtils.convertToClasspathResourcePaths(
attrs.getDeclaringClass(), attrs.getLocations()); attrs.getDeclaringClass(), attrs.getLocations());
locations.addAll(0, Arrays.<String> asList(locationsArray)); locations.addAll(0, Arrays.<String> asList(locationsArray));
if (!attrs.isInheritLocations()) { if (!attrs.isInheritLocations()) {
break; break;
} }
} }
return StringUtils.toStringArray(locations); return StringUtils.toStringArray(locations);
} }
private static String[] mergeProperties(List<TestPropertySourceAttributes> attributesList) { private static String[] mergeProperties(List<TestPropertySourceAttributes> attributesList) {
final List<String> properties = new ArrayList<>(); List<String> properties = new ArrayList<>();
for (TestPropertySourceAttributes attrs : attributesList) { for (TestPropertySourceAttributes attrs : attributesList) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace(String.format("Processing inlined properties for TestPropertySource attributes %s", attrs)); logger.trace(String.format("Processing inlined properties for TestPropertySource attributes %s", attrs));
} }
properties.addAll(0, Arrays.<String>asList(attrs.getProperties()));
properties.addAll(0, Arrays.<String> asList(attrs.getProperties()));
if (!attrs.isInheritProperties()) { if (!attrs.isInheritProperties()) {
break; break;
} }
} }
return StringUtils.toStringArray(properties); return StringUtils.toStringArray(properties);
} }
@ -168,8 +156,8 @@ public abstract class TestPropertySourceUtils {
* @throws IllegalStateException if an error occurs while processing a properties file * @throws IllegalStateException if an error occurs while processing a properties file
*/ */
public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContext context, String... locations) { public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContext context, String... locations) {
Assert.notNull(context, "context must not be null"); Assert.notNull(context, "'context' must not be null");
Assert.notNull(locations, "locations must not be null"); Assert.notNull(locations, "'locations' must not be null");
addPropertiesFilesToEnvironment(context.getEnvironment(), context, locations); addPropertiesFilesToEnvironment(context.getEnvironment(), context, locations);
} }
@ -196,9 +184,9 @@ public abstract class TestPropertySourceUtils {
public static void addPropertiesFilesToEnvironment(ConfigurableEnvironment environment, public static void addPropertiesFilesToEnvironment(ConfigurableEnvironment environment,
ResourceLoader resourceLoader, String... locations) { ResourceLoader resourceLoader, String... locations) {
Assert.notNull(environment, "environment must not be null"); Assert.notNull(environment, "'environment' must not be null");
Assert.notNull(resourceLoader, "resourceLoader must not be null"); Assert.notNull(resourceLoader, "'resourceLoader' must not be null");
Assert.notNull(locations, "locations must not be null"); Assert.notNull(locations, "'locations' must not be null");
try { try {
for (String location : locations) { for (String location : locations) {
String resolvedLocation = environment.resolveRequiredPlaceholders(location); String resolvedLocation = environment.resolveRequiredPlaceholders(location);
@ -225,8 +213,8 @@ public abstract class TestPropertySourceUtils {
* @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[]) * @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])
*/ */
public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationContext context, String... inlinedProperties) { public static void addInlinedPropertiesToEnvironment(ConfigurableApplicationContext context, String... inlinedProperties) {
Assert.notNull(context, "context must not be null"); Assert.notNull(context, "'context' must not be null");
Assert.notNull(inlinedProperties, "inlinedProperties must not be null"); Assert.notNull(inlinedProperties, "'inlinedProperties' must not be null");
addInlinedPropertiesToEnvironment(context.getEnvironment(), inlinedProperties); addInlinedPropertiesToEnvironment(context.getEnvironment(), inlinedProperties);
} }
@ -247,13 +235,15 @@ public abstract class TestPropertySourceUtils {
* @see #convertInlinedPropertiesToMap * @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(environment, "'environment' must not be null");
Assert.notNull(inlinedProperties, "inlinedProperties must not be null"); Assert.notNull(inlinedProperties, "'inlinedProperties' must not be null");
if (!ObjectUtils.isEmpty(inlinedProperties)) { if (!ObjectUtils.isEmpty(inlinedProperties)) {
if (logger.isDebugEnabled()) { 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 = (MapPropertySource) environment.getPropertySources().get(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME); MapPropertySource ps = (MapPropertySource)
environment.getPropertySources().get(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
if (ps == null) { if (ps == null) {
ps = new MapPropertySource(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME, new LinkedHashMap<>()); ps = new MapPropertySource(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME, new LinkedHashMap<>());
environment.getPropertySources().addFirst(ps); environment.getPropertySources().addFirst(ps);
@ -280,7 +270,7 @@ public abstract class TestPropertySourceUtils {
* @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[]) * @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"); Assert.notNull(inlinedProperties, "'inlinedProperties' must not be null");
Map<String, Object> map = new LinkedHashMap<>(); Map<String, Object> map = new LinkedHashMap<>();
Properties props = new Properties(); Properties props = new Properties();

View File

@ -32,10 +32,10 @@ import org.springframework.mock.env.MockEnvironment;
import org.springframework.mock.env.MockPropertySource; import org.springframework.mock.env.MockPropertySource;
import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.TestPropertySource;
import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.startsWith; import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.Matchers.*; import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import static org.springframework.test.context.support.TestPropertySourceUtils.*; import static org.springframework.test.context.support.TestPropertySourceUtils.*;
@ -48,8 +48,11 @@ import static org.springframework.test.context.support.TestPropertySourceUtils.*
public class TestPropertySourceUtilsTests { public class TestPropertySourceUtilsTests {
private static final String[] EMPTY_STRING_ARRAY = new String[0]; private static final String[] EMPTY_STRING_ARRAY = new String[0];
private static final String[] KEY_VALUE_PAIR = new String[] { "key = value" };
private static final String[] FOO_LOCATIONS = new String[] { "classpath:/foo.properties" }; private static final String[] KEY_VALUE_PAIR = new String[] {"key = value"};
private static final String[] FOO_LOCATIONS = new String[] {"classpath:/foo.properties"};
@Rule @Rule
public ExpectedException expectedException = ExpectedException.none(); public ExpectedException expectedException = ExpectedException.none();
@ -74,7 +77,7 @@ public class TestPropertySourceUtilsTests {
@Test @Test
public void value() { public void value() {
assertMergedTestPropertySources(ValuePropertySources.class, asArray("classpath:/value.xml"), assertMergedTestPropertySources(ValuePropertySources.class, asArray("classpath:/value.xml"),
EMPTY_STRING_ARRAY); EMPTY_STRING_ARRAY);
} }
@Test @Test
@ -86,67 +89,66 @@ public class TestPropertySourceUtilsTests {
@Test @Test
public void locationsAndProperties() { public void locationsAndProperties() {
assertMergedTestPropertySources(LocationsAndPropertiesPropertySources.class, assertMergedTestPropertySources(LocationsAndPropertiesPropertySources.class,
asArray("classpath:/foo1.xml", "classpath:/foo2.xml"), asArray("k1a=v1a", "k1b: v1b")); asArray("classpath:/foo1.xml", "classpath:/foo2.xml"), asArray("k1a=v1a", "k1b: v1b"));
} }
@Test @Test
public void inheritedLocationsAndProperties() { public void inheritedLocationsAndProperties() {
assertMergedTestPropertySources(InheritedPropertySources.class, assertMergedTestPropertySources(InheritedPropertySources.class,
asArray("classpath:/foo1.xml", "classpath:/foo2.xml"), asArray("k1a=v1a", "k1b: v1b")); asArray("classpath:/foo1.xml", "classpath:/foo2.xml"), asArray("k1a=v1a", "k1b: v1b"));
} }
@Test @Test
public void extendedLocationsAndProperties() { public void extendedLocationsAndProperties() {
assertMergedTestPropertySources(ExtendedPropertySources.class, assertMergedTestPropertySources(ExtendedPropertySources.class,
asArray("classpath:/foo1.xml", "classpath:/foo2.xml", "classpath:/bar1.xml", "classpath:/bar2.xml"), asArray("classpath:/foo1.xml", "classpath:/foo2.xml", "classpath:/bar1.xml", "classpath:/bar2.xml"),
asArray("k1a=v1a", "k1b: v1b", "k2a v2a", "k2b: v2b")); asArray("k1a=v1a", "k1b: v1b", "k2a v2a", "k2b: v2b"));
} }
@Test @Test
public void overriddenLocations() { public void overriddenLocations() {
assertMergedTestPropertySources(OverriddenLocationsPropertySources.class, assertMergedTestPropertySources(OverriddenLocationsPropertySources.class,
asArray("classpath:/baz.properties"), asArray("k1a=v1a", "k1b: v1b", "key = value")); asArray("classpath:/baz.properties"), asArray("k1a=v1a", "k1b: v1b", "key = value"));
} }
@Test @Test
public void overriddenProperties() { public void overriddenProperties() {
assertMergedTestPropertySources(OverriddenPropertiesPropertySources.class, assertMergedTestPropertySources(OverriddenPropertiesPropertySources.class,
asArray("classpath:/foo1.xml", "classpath:/foo2.xml", "classpath:/baz.properties"), KEY_VALUE_PAIR); asArray("classpath:/foo1.xml", "classpath:/foo2.xml", "classpath:/baz.properties"), KEY_VALUE_PAIR);
} }
@Test @Test
public void overriddenLocationsAndProperties() { public void overriddenLocationsAndProperties() {
assertMergedTestPropertySources(OverriddenLocationsAndPropertiesPropertySources.class, assertMergedTestPropertySources(OverriddenLocationsAndPropertiesPropertySources.class,
asArray("classpath:/baz.properties"), KEY_VALUE_PAIR); asArray("classpath:/baz.properties"), KEY_VALUE_PAIR);
} }
// -------------------------------------------------------------------------
@Test @Test
public void addPropertiesFilesToEnvironmentWithNullContext() { public void addPropertiesFilesToEnvironmentWithNullContext() {
expectedException.expect(IllegalArgumentException.class); expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("context must not be null"); expectedException.expectMessage("must not be null");
addPropertiesFilesToEnvironment((ConfigurableApplicationContext) null, FOO_LOCATIONS); addPropertiesFilesToEnvironment((ConfigurableApplicationContext) null, FOO_LOCATIONS);
} }
@Test @Test
public void addPropertiesFilesToEnvironmentWithContextAndNullLocations() { public void addPropertiesFilesToEnvironmentWithContextAndNullLocations() {
expectedException.expect(IllegalArgumentException.class); expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("locations must not be null"); expectedException.expectMessage("must not be null");
addPropertiesFilesToEnvironment(mock(ConfigurableApplicationContext.class), (String[]) null); addPropertiesFilesToEnvironment(mock(ConfigurableApplicationContext.class), (String[]) null);
} }
@Test @Test
public void addPropertiesFilesToEnvironmentWithNullEnvironment() { public void addPropertiesFilesToEnvironmentWithNullEnvironment() {
expectedException.expect(IllegalArgumentException.class); expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("environment must not be null"); expectedException.expectMessage("must not be null");
addPropertiesFilesToEnvironment((ConfigurableEnvironment) null, mock(ResourceLoader.class), FOO_LOCATIONS); addPropertiesFilesToEnvironment((ConfigurableEnvironment) null, mock(ResourceLoader.class), FOO_LOCATIONS);
} }
@Test @Test
public void addPropertiesFilesToEnvironmentWithEnvironmentAndNullLocations() { public void addPropertiesFilesToEnvironmentWithEnvironmentAndNullLocations() {
expectedException.expect(IllegalArgumentException.class); expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("locations must not be null"); expectedException.expectMessage("must not be null");
addPropertiesFilesToEnvironment(new MockEnvironment(), mock(ResourceLoader.class), (String[]) null); addPropertiesFilesToEnvironment(new MockEnvironment(), mock(ResourceLoader.class), (String[]) null);
} }
@ -168,8 +170,6 @@ public class TestPropertySourceUtilsTests {
assertEquals("value", environment.getProperty("key")); assertEquals("value", environment.getProperty("key"));
} }
// -------------------------------------------------------------------------
@Test @Test
public void addInlinedPropertiesToEnvironmentWithNullContext() { public void addInlinedPropertiesToEnvironmentWithNullContext() {
expectedException.expect(IllegalArgumentException.class); expectedException.expect(IllegalArgumentException.class);
@ -231,16 +231,17 @@ public class TestPropertySourceUtilsTests {
convertInlinedPropertiesToMap((String[]) null); convertInlinedPropertiesToMap((String[]) null);
} }
// -------------------------------------------------------------------
private static void assertMergedTestPropertySources(Class<?> testClass, String[] expectedLocations, private static void assertMergedTestPropertySources(Class<?> testClass, String[] expectedLocations,
String[] expectedProperties) { String[] expectedProperties) {
MergedTestPropertySources mergedPropertySources = buildMergedTestPropertySources(testClass); MergedTestPropertySources mergedPropertySources = buildMergedTestPropertySources(testClass);
assertNotNull(mergedPropertySources); assertNotNull(mergedPropertySources);
assertArrayEquals(expectedLocations, mergedPropertySources.getLocations()); assertArrayEquals(expectedLocations, mergedPropertySources.getLocations());
assertArrayEquals(expectedProperties, mergedPropertySources.getProperties()); assertArrayEquals(expectedProperties, mergedPropertySources.getProperties());
} }
@SafeVarargs @SafeVarargs
private static <T> T[] asArray(T... arr) { private static <T> T[] asArray(T... arr) {
return arr; return arr;

View File

@ -580,8 +580,9 @@ public class Jackson2ObjectMapperBuilder {
public <T extends ObjectMapper> T build() { public <T extends ObjectMapper> T build() {
ObjectMapper mapper; ObjectMapper mapper;
if (this.createXmlMapper) { if (this.createXmlMapper) {
mapper = (this.defaultUseWrapper == null ? new XmlObjectMapperInitializer().create() mapper = (this.defaultUseWrapper != null ?
: new XmlObjectMapperInitializer().create(this.defaultUseWrapper)); new XmlObjectMapperInitializer().create(this.defaultUseWrapper) :
new XmlObjectMapperInitializer().create());
} }
else { else {
mapper = new ObjectMapper(); mapper = new ObjectMapper();

View File

@ -49,12 +49,11 @@ import org.springframework.util.StringUtils;
*/ */
public class CssLinkResourceTransformer extends ResourceTransformerSupport { public class CssLinkResourceTransformer extends ResourceTransformerSupport {
private static final Log logger = LogFactory.getLog(CssLinkResourceTransformer.class);
private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private static final Log logger = LogFactory.getLog(CssLinkResourceTransformer.class);
private final List<CssLinkParser> linkParsers = new ArrayList<>(); private final List<CssLinkParser> linkParsers = new ArrayList<>(2);
public CssLinkResourceTransformer() { public CssLinkResourceTransformer() {
@ -81,7 +80,7 @@ public class CssLinkResourceTransformer extends ResourceTransformerSupport {
byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream()); byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream());
String content = new String(bytes, DEFAULT_CHARSET); String content = new String(bytes, DEFAULT_CHARSET);
Set<CssLinkInfo> infos = new HashSet<>(5); Set<CssLinkInfo> infos = new HashSet<>(8);
for (CssLinkParser parser : this.linkParsers) { for (CssLinkParser parser : this.linkParsers) {
parser.parseLink(content, infos); parser.parseLink(content, infos);
} }

View File

@ -143,6 +143,7 @@ public abstract class AbstractXhrTransport implements XhrTransport {
protected abstract ResponseEntity<String> executeInfoRequestInternal(URI infoUrl, HttpHeaders headers); protected abstract ResponseEntity<String> executeInfoRequestInternal(URI infoUrl, HttpHeaders headers);
// XhrTransport methods // XhrTransport methods
@Override @Override