From f09e252417edf0dd1d3d82d4c1a9670a18b2b5ce Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 21 Nov 2017 15:25:33 +0100 Subject: [PATCH] Polishing --- .../scripting/bsh/BshScriptFactoryTests.java | 43 ++++++++--------- .../scripting/bsh/bsh-with-xsd.xml | 23 +++++----- .../support/DefaultConversionService.java | 46 ++++++++----------- .../request/MockMvcRequestBuilders.java | 3 +- .../converter/FormHttpMessageConverter.java | 5 +- ...lEncompassingFormHttpMessageConverter.java | 17 ++++--- .../web/client/RestTemplate.java | 21 ++++++--- .../AnnotationDrivenBeanDefinitionParser.java | 21 ++++++--- .../WebMvcConfigurationSupport.java | 18 +++++--- 9 files changed, 109 insertions(+), 88 deletions(-) diff --git a/spring-context/src/test/java/org/springframework/scripting/bsh/BshScriptFactoryTests.java b/spring-context/src/test/java/org/springframework/scripting/bsh/BshScriptFactoryTests.java index 1d3ba17d966..8b8c113ae70 100644 --- a/spring-context/src/test/java/org/springframework/scripting/bsh/BshScriptFactoryTests.java +++ b/spring-context/src/test/java/org/springframework/scripting/bsh/BshScriptFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2017 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. @@ -16,6 +16,7 @@ package org.springframework.scripting.bsh; +import java.io.IOException; import java.util.Arrays; import java.util.Collection; @@ -47,7 +48,7 @@ import static org.mockito.BDDMockito.*; public class BshScriptFactoryTests { @Test - public void staticScript() throws Exception { + public void staticScript() { ApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass()); assertTrue(Arrays.asList(ctx.getBeanNamesForType(Calculator.class)).contains("calculator")); @@ -75,7 +76,7 @@ public class BshScriptFactoryTests { } @Test - public void staticScriptWithNullReturnValue() throws Exception { + public void staticScriptWithNullReturnValue() { ApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass()); assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerWithConfig")); @@ -86,7 +87,7 @@ public class BshScriptFactoryTests { } @Test - public void staticScriptWithTwoInterfacesSpecified() throws Exception { + public void staticScriptWithTwoInterfacesSpecified() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass()); assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerWithConfigExtra")); @@ -100,7 +101,7 @@ public class BshScriptFactoryTests { } @Test - public void staticWithScriptReturningInstance() throws Exception { + public void staticWithScriptReturningInstance() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass()); assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstance")); @@ -114,7 +115,7 @@ public class BshScriptFactoryTests { } @Test - public void staticScriptImplementingInterface() throws Exception { + public void staticScriptImplementingInterface() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass()); assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerImpl")); @@ -128,7 +129,7 @@ public class BshScriptFactoryTests { } @Test - public void staticPrototypeScript() throws Exception { + public void staticPrototypeScript() { ApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass()); ConfigurableMessenger messenger = (ConfigurableMessenger) ctx.getBean("messengerPrototype"); ConfigurableMessenger messenger2 = (ConfigurableMessenger) ctx.getBean("messengerPrototype"); @@ -147,7 +148,7 @@ public class BshScriptFactoryTests { } @Test - public void nonStaticScript() throws Exception { + public void nonStaticScript() { ApplicationContext ctx = new ClassPathXmlApplicationContext("bshRefreshableContext.xml", getClass()); Messenger messenger = (Messenger) ctx.getBean("messenger"); @@ -165,7 +166,7 @@ public class BshScriptFactoryTests { } @Test - public void nonStaticPrototypeScript() throws Exception { + public void nonStaticPrototypeScript() { ApplicationContext ctx = new ClassPathXmlApplicationContext("bshRefreshableContext.xml", getClass()); ConfigurableMessenger messenger = (ConfigurableMessenger) ctx.getBean("messengerPrototype"); ConfigurableMessenger messenger2 = (ConfigurableMessenger) ctx.getBean("messengerPrototype"); @@ -189,7 +190,7 @@ public class BshScriptFactoryTests { } @Test - public void scriptCompilationException() throws Exception { + public void scriptCompilationException() { try { new ClassPathXmlApplicationContext("org/springframework/scripting/bsh/bshBrokenContext.xml"); fail("Must throw exception for broken script file"); @@ -200,7 +201,7 @@ public class BshScriptFactoryTests { } @Test - public void scriptThatCompilesButIsJustPlainBad() throws Exception { + public void scriptThatCompilesButIsJustPlainBad() throws IOException { ScriptSource script = mock(ScriptSource.class); final String badScript = "String getMessage() { throw new IllegalArgumentException(); }"; given(script.getScriptAsString()).willReturn(badScript); @@ -217,7 +218,7 @@ public class BshScriptFactoryTests { } @Test - public void ctorWithNullScriptSourceLocator() throws Exception { + public void ctorWithNullScriptSourceLocator() { try { new BshScriptFactory(null, Messenger.class); fail("Must have thrown exception by this point."); @@ -227,9 +228,9 @@ public class BshScriptFactoryTests { } @Test - public void ctorWithEmptyScriptSourceLocator() throws Exception { + public void ctorWithEmptyScriptSourceLocator() { try { - new BshScriptFactory("", new Class[] {Messenger.class}); + new BshScriptFactory("", Messenger.class); fail("Must have thrown exception by this point."); } catch (IllegalArgumentException expected) { @@ -237,9 +238,9 @@ public class BshScriptFactoryTests { } @Test - public void ctorWithWhitespacedScriptSourceLocator() throws Exception { + public void ctorWithWhitespacedScriptSourceLocator() { try { - new BshScriptFactory("\n ", new Class[] {Messenger.class}); + new BshScriptFactory("\n ", Messenger.class); fail("Must have thrown exception by this point."); } catch (IllegalArgumentException expected) { @@ -247,7 +248,7 @@ public class BshScriptFactoryTests { } @Test - public void resourceScriptFromTag() throws Exception { + public void resourceScriptFromTag() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass()); TestBean testBean = (TestBean) ctx.getBean("testBean"); @@ -286,7 +287,7 @@ public class BshScriptFactoryTests { } @Test - public void prototypeScriptFromTag() throws Exception { + public void prototypeScriptFromTag() { ApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass()); ConfigurableMessenger messenger = (ConfigurableMessenger) ctx.getBean("messengerPrototype"); ConfigurableMessenger messenger2 = (ConfigurableMessenger) ctx.getBean("messengerPrototype"); @@ -302,7 +303,7 @@ public class BshScriptFactoryTests { } @Test - public void inlineScriptFromTag() throws Exception { + public void inlineScriptFromTag() { ApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass()); Calculator calculator = (Calculator) ctx.getBean("calculator"); assertNotNull(calculator); @@ -310,7 +311,7 @@ public class BshScriptFactoryTests { } @Test - public void refreshableFromTag() throws Exception { + public void refreshableFromTag() { ApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass()); Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger"); assertEquals("Hello World!", messenger.getMessage()); @@ -318,7 +319,7 @@ public class BshScriptFactoryTests { } @Test - public void applicationEventListener() throws Exception { + public void applicationEventListener() { ApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass()); Messenger eventListener = (Messenger) ctx.getBean("eventListener"); ctx.publishEvent(new MyEvent(ctx)); diff --git a/spring-context/src/test/resources/org/springframework/scripting/bsh/bsh-with-xsd.xml b/spring-context/src/test/resources/org/springframework/scripting/bsh/bsh-with-xsd.xml index 60ea74676e9..e1ce67e164b 100644 --- a/spring-context/src/test/resources/org/springframework/scripting/bsh/bsh-with-xsd.xml +++ b/spring-context/src/test/resources/org/springframework/scripting/bsh/bsh-with-xsd.xml @@ -1,9 +1,8 @@ + autowire="byType" init-method="init" destroy-method="destroy"> - - - + + + diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java b/spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java index af4e8390936..9cb291e0c9e 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -26,12 +26,12 @@ import org.springframework.core.convert.converter.ConverterRegistry; import org.springframework.util.ClassUtils; /** - * A specialization of {@link GenericConversionService} configured by default with - * converters appropriate for most environments. + * A specialization of {@link GenericConversionService} configured by default + * with converters appropriate for most environments. * *

Designed for direct instantiation but also exposes the static - * {@link #addDefaultConverters(ConverterRegistry)} utility method for ad hoc use against any - * {@code ConverterRegistry} instance. + * {@link #addDefaultConverters(ConverterRegistry)} utility method for ad-hoc + * use against any {@code ConverterRegistry} instance. * * @author Chris Beams * @author Juergen Hoeller @@ -55,6 +55,15 @@ public class DefaultConversionService extends GenericConversionService { private static volatile DefaultConversionService sharedInstance; + /** + * Create a new {@code DefaultConversionService} with the set of + * {@linkplain DefaultConversionService#addDefaultConverters(ConverterRegistry) default converters}. + */ + public DefaultConversionService() { + addDefaultConverters(this); + } + + /** * Return a shared default {@code ConversionService} instance, * lazily building it once needed. @@ -77,22 +86,10 @@ public class DefaultConversionService extends GenericConversionService { return sharedInstance; } - - /** - * Create a new {@code DefaultConversionService} with the set of - * {@linkplain DefaultConversionService#addDefaultConverters(ConverterRegistry) default converters}. - */ - public DefaultConversionService() { - addDefaultConverters(this); - } - - - // static utility methods - /** * Add converters appropriate for most environments. - * @param converterRegistry the registry of converters to add to (must also be castable to ConversionService, - * e.g. being a {@link ConfigurableConversionService}) + * @param converterRegistry the registry of converters to add to + * (must also be castable to ConversionService, e.g. being a {@link ConfigurableConversionService}) * @throws ClassCastException if the given ConverterRegistry could not be cast to a ConversionService */ public static void addDefaultConverters(ConverterRegistry converterRegistry) { @@ -113,9 +110,9 @@ public class DefaultConversionService extends GenericConversionService { } /** - * Add collection converters. - * @param converterRegistry the registry of converters to add to (must also be castable to ConversionService, - * e.g. being a {@link ConfigurableConversionService}) + * Add common collection converters. + * @param converterRegistry the registry of converters to add to + * (must also be castable to ConversionService, e.g. being a {@link ConfigurableConversionService}) * @throws ClassCastException if the given ConverterRegistry could not be cast to a ConversionService * @since 4.2.3 */ @@ -146,9 +143,6 @@ public class DefaultConversionService extends GenericConversionService { } } - - // internal helpers - private static void addScalarConverters(ConverterRegistry converterRegistry) { converterRegistry.addConverterFactory(new NumberToNumberConverterFactory()); @@ -166,7 +160,7 @@ public class DefaultConversionService extends GenericConversionService { converterRegistry.addConverterFactory(new StringToEnumConverterFactory()); converterRegistry.addConverter(new EnumToStringConverter((ConversionService) converterRegistry)); - + converterRegistry.addConverterFactory(new IntegerToEnumConverterFactory()); converterRegistry.addConverter(new EnumToIntegerConverter((ConversionService) converterRegistry)); diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockMvcRequestBuilders.java b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockMvcRequestBuilders.java index 17322025a53..0f6e768bf10 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockMvcRequestBuilders.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockMvcRequestBuilders.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -222,6 +222,7 @@ public abstract class MockMvcRequestBuilders { return new MockMultipartHttpServletRequestBuilder(uri); } + /** * Create a {@link RequestBuilder} for an async dispatch from the * {@link MvcResult} of the request that started async processing. diff --git a/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java index 51a3538af4f..7849a6e891e 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java @@ -104,9 +104,10 @@ public class FormHttpMessageConverter implements HttpMessageConverter> messageConverters = new ArrayList>(); @@ -215,7 +221,8 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat } /** - * Return the message body converters. + * Return the list of message body converters. + *

The returned {@link List} is active and may get appended to. */ public List> getMessageConverters() { return this.messageConverters; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParser.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParser.java index a9efe294987..8289e043aab 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParser.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParser.java @@ -156,23 +156,30 @@ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser { public static final String CONTENT_NEGOTIATION_MANAGER_BEAN_NAME = "mvcContentNegotiationManager"; private static final boolean javaxValidationPresent = - ClassUtils.isPresent("javax.validation.Validator", AnnotationDrivenBeanDefinitionParser.class.getClassLoader()); + ClassUtils.isPresent("javax.validation.Validator", + AnnotationDrivenBeanDefinitionParser.class.getClassLoader()); private static boolean romePresent = - ClassUtils.isPresent("com.rometools.rome.feed.WireFeed", AnnotationDrivenBeanDefinitionParser.class.getClassLoader()); + ClassUtils.isPresent("com.rometools.rome.feed.WireFeed", + AnnotationDrivenBeanDefinitionParser.class.getClassLoader()); private static final boolean jaxb2Present = - ClassUtils.isPresent("javax.xml.bind.Binder", AnnotationDrivenBeanDefinitionParser.class.getClassLoader()); + ClassUtils.isPresent("javax.xml.bind.Binder", + AnnotationDrivenBeanDefinitionParser.class.getClassLoader()); private static final boolean jackson2Present = - ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", AnnotationDrivenBeanDefinitionParser.class.getClassLoader()) && - ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", AnnotationDrivenBeanDefinitionParser.class.getClassLoader()); + ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", + AnnotationDrivenBeanDefinitionParser.class.getClassLoader()) && + ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", + AnnotationDrivenBeanDefinitionParser.class.getClassLoader()); private static final boolean jackson2XmlPresent = - ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", AnnotationDrivenBeanDefinitionParser.class.getClassLoader()); + ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", + AnnotationDrivenBeanDefinitionParser.class.getClassLoader()); private static final boolean gsonPresent = - ClassUtils.isPresent("com.google.gson.Gson", AnnotationDrivenBeanDefinitionParser.class.getClassLoader()); + ClassUtils.isPresent("com.google.gson.Gson", + AnnotationDrivenBeanDefinitionParser.class.getClassLoader()); @Override diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java index 85dde2d87c6..78d46911ce9 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java @@ -174,20 +174,26 @@ import org.springframework.web.util.UrlPathHelper; public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware { private static boolean romePresent = - ClassUtils.isPresent("com.rometools.rome.feed.WireFeed", WebMvcConfigurationSupport.class.getClassLoader()); + ClassUtils.isPresent("com.rometools.rome.feed.WireFeed", + WebMvcConfigurationSupport.class.getClassLoader()); private static final boolean jaxb2Present = - ClassUtils.isPresent("javax.xml.bind.Binder", WebMvcConfigurationSupport.class.getClassLoader()); + ClassUtils.isPresent("javax.xml.bind.Binder", + WebMvcConfigurationSupport.class.getClassLoader()); private static final boolean jackson2Present = - ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", WebMvcConfigurationSupport.class.getClassLoader()) && - ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", WebMvcConfigurationSupport.class.getClassLoader()); + ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", + WebMvcConfigurationSupport.class.getClassLoader()) && + ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", + WebMvcConfigurationSupport.class.getClassLoader()); private static final boolean jackson2XmlPresent = - ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", WebMvcConfigurationSupport.class.getClassLoader()); + ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", + WebMvcConfigurationSupport.class.getClassLoader()); private static final boolean gsonPresent = - ClassUtils.isPresent("com.google.gson.Gson", WebMvcConfigurationSupport.class.getClassLoader()); + ClassUtils.isPresent("com.google.gson.Gson", + WebMvcConfigurationSupport.class.getClassLoader()); private ApplicationContext applicationContext;