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 {
descriptor.initParameterNameDiscovery(getParameterNameDiscoverer());
if (descriptor.getDependencyType() == Optional.class) {
if (Optional.class == descriptor.getDependencyType()) {
return createOptionalDependency(descriptor, beanName);
}
else if (ObjectFactory.class == descriptor.getDependencyType() ||

View File

@ -75,7 +75,6 @@ public abstract class TestPropertySourceUtils {
return new MergedTestPropertySources();
}
// else...
List<TestPropertySourceAttributes> attributesList = resolveTestPropertySourceAttributes(testClass);
String[] locations = mergeLocations(attributesList);
String[] properties = mergeProperties(attributesList);
@ -84,30 +83,27 @@ public abstract class TestPropertySourceUtils {
private static List<TestPropertySourceAttributes> resolveTestPropertySourceAttributes(Class<?> testClass) {
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);
Assert.notNull(descriptor, String.format(
"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
annotationType.getName(), testClass.getName()));
"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
annotationType.getName(), testClass.getName()));
while (descriptor != null) {
TestPropertySource testPropertySource = descriptor.synthesizeAnnotation();
Class<?> rootDeclaringClass = descriptor.getRootDeclaringClass();
if (logger.isTraceEnabled()) {
logger.trace(String.format("Retrieved @TestPropertySource [%s] for declaring class [%s].",
testPropertySource, rootDeclaringClass.getName()));
}
TestPropertySourceAttributes attributes = new TestPropertySourceAttributes(rootDeclaringClass,
testPropertySource);
TestPropertySourceAttributes attributes =
new TestPropertySourceAttributes(rootDeclaringClass, testPropertySource);
if (logger.isTraceEnabled()) {
logger.trace("Resolved TestPropertySource attributes: " + attributes);
}
attributesList.add(attributes);
descriptor = findAnnotationDescriptor(rootDeclaringClass.getSuperclass(), annotationType);
}
@ -115,40 +111,32 @@ public abstract class TestPropertySourceUtils {
}
private static String[] mergeLocations(List<TestPropertySourceAttributes> attributesList) {
final List<String> locations = new ArrayList<>();
List<String> locations = new ArrayList<>();
for (TestPropertySourceAttributes attrs : attributesList) {
if (logger.isTraceEnabled()) {
logger.trace(String.format("Processing locations for TestPropertySource attributes %s", attrs));
}
String[] locationsArray = TestContextResourceUtils.convertToClasspathResourcePaths(
attrs.getDeclaringClass(), attrs.getLocations());
attrs.getDeclaringClass(), attrs.getLocations());
locations.addAll(0, Arrays.<String> asList(locationsArray));
if (!attrs.isInheritLocations()) {
break;
}
}
return StringUtils.toStringArray(locations);
}
private static String[] mergeProperties(List<TestPropertySourceAttributes> attributesList) {
final List<String> properties = new ArrayList<>();
List<String> properties = new ArrayList<>();
for (TestPropertySourceAttributes attrs : attributesList) {
if (logger.isTraceEnabled()) {
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()) {
break;
}
}
return StringUtils.toStringArray(properties);
}
@ -168,8 +156,8 @@ public abstract class TestPropertySourceUtils {
* @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");
Assert.notNull(context, "'context' must not be null");
Assert.notNull(locations, "'locations' must not be null");
addPropertiesFilesToEnvironment(context.getEnvironment(), context, locations);
}
@ -196,9 +184,9 @@ public abstract class TestPropertySourceUtils {
public static void addPropertiesFilesToEnvironment(ConfigurableEnvironment environment,
ResourceLoader resourceLoader, String... locations) {
Assert.notNull(environment, "environment must not be null");
Assert.notNull(resourceLoader, "resourceLoader must not be null");
Assert.notNull(locations, "locations must not be null");
Assert.notNull(environment, "'environment' must not be null");
Assert.notNull(resourceLoader, "'resourceLoader' must not be null");
Assert.notNull(locations, "'locations' must not be null");
try {
for (String location : locations) {
String resolvedLocation = environment.resolveRequiredPlaceholders(location);
@ -225,8 +213,8 @@ public abstract class TestPropertySourceUtils {
* @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");
Assert.notNull(context, "'context' must not be null");
Assert.notNull(inlinedProperties, "'inlinedProperties' must not be null");
addInlinedPropertiesToEnvironment(context.getEnvironment(), inlinedProperties);
}
@ -247,13 +235,15 @@ public abstract class TestPropertySourceUtils {
* @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");
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 = (MapPropertySource) environment.getPropertySources().get(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
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);
@ -280,7 +270,7 @@ public abstract class TestPropertySourceUtils {
* @see #addInlinedPropertiesToEnvironment(ConfigurableEnvironment, String[])
*/
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<>();
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.test.context.TestPropertySource;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;
import static org.springframework.test.context.support.TestPropertySourceUtils.*;
@ -48,8 +48,11 @@ import static org.springframework.test.context.support.TestPropertySourceUtils.*
public class TestPropertySourceUtilsTests {
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
public ExpectedException expectedException = ExpectedException.none();
@ -74,7 +77,7 @@ public class TestPropertySourceUtilsTests {
@Test
public void value() {
assertMergedTestPropertySources(ValuePropertySources.class, asArray("classpath:/value.xml"),
EMPTY_STRING_ARRAY);
EMPTY_STRING_ARRAY);
}
@Test
@ -86,67 +89,66 @@ public class TestPropertySourceUtilsTests {
@Test
public void locationsAndProperties() {
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
public void inheritedLocationsAndProperties() {
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
public void extendedLocationsAndProperties() {
assertMergedTestPropertySources(ExtendedPropertySources.class,
asArray("classpath:/foo1.xml", "classpath:/foo2.xml", "classpath:/bar1.xml", "classpath:/bar2.xml"),
asArray("k1a=v1a", "k1b: v1b", "k2a v2a", "k2b: v2b"));
asArray("classpath:/foo1.xml", "classpath:/foo2.xml", "classpath:/bar1.xml", "classpath:/bar2.xml"),
asArray("k1a=v1a", "k1b: v1b", "k2a v2a", "k2b: v2b"));
}
@Test
public void overriddenLocations() {
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
public void overriddenProperties() {
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
public void overriddenLocationsAndProperties() {
assertMergedTestPropertySources(OverriddenLocationsAndPropertiesPropertySources.class,
asArray("classpath:/baz.properties"), KEY_VALUE_PAIR);
asArray("classpath:/baz.properties"), KEY_VALUE_PAIR);
}
// -------------------------------------------------------------------------
@Test
public void addPropertiesFilesToEnvironmentWithNullContext() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("context must not be null");
expectedException.expectMessage("must not be null");
addPropertiesFilesToEnvironment((ConfigurableApplicationContext) null, FOO_LOCATIONS);
}
@Test
public void addPropertiesFilesToEnvironmentWithContextAndNullLocations() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("locations must not be null");
expectedException.expectMessage("must not be null");
addPropertiesFilesToEnvironment(mock(ConfigurableApplicationContext.class), (String[]) null);
}
@Test
public void addPropertiesFilesToEnvironmentWithNullEnvironment() {
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);
}
@Test
public void addPropertiesFilesToEnvironmentWithEnvironmentAndNullLocations() {
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);
}
@ -168,8 +170,6 @@ public class TestPropertySourceUtilsTests {
assertEquals("value", environment.getProperty("key"));
}
// -------------------------------------------------------------------------
@Test
public void addInlinedPropertiesToEnvironmentWithNullContext() {
expectedException.expect(IllegalArgumentException.class);
@ -231,16 +231,17 @@ public class TestPropertySourceUtilsTests {
convertInlinedPropertiesToMap((String[]) null);
}
// -------------------------------------------------------------------
private static void assertMergedTestPropertySources(Class<?> testClass, String[] expectedLocations,
String[] expectedProperties) {
MergedTestPropertySources mergedPropertySources = buildMergedTestPropertySources(testClass);
assertNotNull(mergedPropertySources);
assertArrayEquals(expectedLocations, mergedPropertySources.getLocations());
assertArrayEquals(expectedProperties, mergedPropertySources.getProperties());
}
@SafeVarargs
private static <T> T[] asArray(T... arr) {
return arr;

View File

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

View File

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