From da9c24c41e258d6261b2b6582fd27b8910703513 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 6 Jul 2016 18:11:33 +0200 Subject: [PATCH] Polishing --- .../support/DefaultListableBeanFactory.java | 2 +- .../support/TestPropertySourceUtils.java | 58 ++++++++----------- .../support/TestPropertySourceUtilsTests.java | 41 ++++++------- .../json/Jackson2ObjectMapperBuilder.java | 5 +- .../resource/CssLinkResourceTransformer.java | 7 +-- .../sockjs/client/AbstractXhrTransport.java | 1 + 6 files changed, 53 insertions(+), 61 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index 7b8b9a8df6..4f861f1d11 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -992,7 +992,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto Set 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() || 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 6cc0c8b7f6..c51de9fa18 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 @@ -75,7 +75,6 @@ public abstract class TestPropertySourceUtils { return new MergedTestPropertySources(); } - // else... List attributesList = resolveTestPropertySourceAttributes(testClass); String[] locations = mergeLocations(attributesList); String[] properties = mergeProperties(attributesList); @@ -84,30 +83,27 @@ public abstract class TestPropertySourceUtils { private static List resolveTestPropertySourceAttributes(Class testClass) { Assert.notNull(testClass, "Class must not be null"); + List attributesList = new ArrayList<>(); + Class annotationType = TestPropertySource.class; - final List attributesList = new ArrayList<>(); - final Class annotationType = TestPropertySource.class; AnnotationDescriptor 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 attributesList) { - final List locations = new ArrayList<>(); - + List 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. asList(locationsArray)); - if (!attrs.isInheritLocations()) { break; } } - return StringUtils.toStringArray(locations); } private static String[] mergeProperties(List attributesList) { - final List properties = new ArrayList<>(); - + List 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. asList(attrs.getProperties())); - + properties.addAll(0, Arrays.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 convertInlinedPropertiesToMap(String... inlinedProperties) { - Assert.notNull(inlinedProperties, "inlinedProperties must not be null"); + Assert.notNull(inlinedProperties, "'inlinedProperties' must not be null"); Map map = new LinkedHashMap<>(); Properties props = new Properties(); 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 1324c21902..b1d7ddfdbb 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 @@ -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[] asArray(T... arr) { return arr; diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java index d7ea7cf057..77ed24fcdb 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java @@ -580,8 +580,9 @@ public class Jackson2ObjectMapperBuilder { public 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(); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/CssLinkResourceTransformer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/CssLinkResourceTransformer.java index dd19b1f95a..5889c3b0b9 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/CssLinkResourceTransformer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/CssLinkResourceTransformer.java @@ -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 linkParsers = new ArrayList<>(); + private final List 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 infos = new HashSet<>(5); + Set infos = new HashSet<>(8); for (CssLinkParser parser : this.linkParsers) { parser.parseLink(content, infos); } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/AbstractXhrTransport.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/AbstractXhrTransport.java index 53828b06f8..e147ca113c 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/AbstractXhrTransport.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/AbstractXhrTransport.java @@ -143,6 +143,7 @@ public abstract class AbstractXhrTransport implements XhrTransport { protected abstract ResponseEntity executeInfoRequestInternal(URI infoUrl, HttpHeaders headers); + // XhrTransport methods @Override