Polishing

(cherry picked from commit caa1218)
This commit is contained in:
Juergen Hoeller 2013-08-29 19:17:59 +02:00
parent 692908b722
commit 9317d8f43e
4 changed files with 62 additions and 56 deletions

View File

@ -38,31 +38,31 @@ public class JodaTimeContext {
/** /**
* Set the user's chronology. * Set the user's chronology (calendar system).
*/ */
public void setChronology(Chronology chronology) { public void setChronology(Chronology chronology) {
this.chronology = chronology; this.chronology = chronology;
} }
/** /**
* The user's chronology (calendar system), if any. * Return the user's chronology (calendar system), if any.
*/ */
public Chronology getChronology() { public Chronology getChronology() {
return this.chronology; return this.chronology;
} }
/** /**
* Set the user's timezone. * Set the user's time zone.
*/ */
public void setTimeZone(DateTimeZone timeZone) { public void setTimeZone(DateTimeZone timeZone) {
this.timeZone = timeZone; this.timeZone = timeZone;
} }
/** /**
* The user's timezone, if any. * Return the user's time zone, if any.
*/ */
public DateTimeZone getTimeZone() { public DateTimeZone getTimeZone() {
return timeZone; return this.timeZone;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -49,7 +49,7 @@ import org.springframework.tests.TestGroup;
import org.springframework.util.StopWatch; import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
@ -81,14 +81,14 @@ public class GenericConversionServiceTests {
try { try {
assertFalse(conversionService.canConvert(String.class, null)); assertFalse(conversionService.canConvert(String.class, null));
fail("Should have failed"); fail("Should have failed");
} catch (IllegalArgumentException e) { }
catch (IllegalArgumentException ex) {
} }
try { try {
assertFalse(conversionService.canConvert(TypeDescriptor.valueOf(String.class), null)); assertFalse(conversionService.canConvert(TypeDescriptor.valueOf(String.class), null));
fail("Should have failed"); fail("Should have failed");
} catch (IllegalArgumentException e) { }
catch (IllegalArgumentException ex) {
} }
} }
@ -151,8 +151,8 @@ public class GenericConversionServiceTests {
} }
}); });
fail("Should have failed"); fail("Should have failed");
} catch (IllegalArgumentException e) { }
catch (IllegalArgumentException ex) {
} }
} }
@ -254,7 +254,8 @@ public class GenericConversionServiceTests {
try { try {
conversionService.convert("3,4,5", Integer[].class); conversionService.convert("3,4,5", Integer[].class);
fail("should have failed"); fail("should have failed");
} catch (ConverterNotFoundException e) { }
catch (ConverterNotFoundException ex) {
} }
} }
@ -389,7 +390,7 @@ public class GenericConversionServiceTests {
} }
@Test @Test
public void testConvertUUID() throws Exception { public void testConvertUUID() {
GenericConversionService service = new DefaultConversionService(); GenericConversionService service = new DefaultConversionService();
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
String convertToString = service.convert(uuid, String.class); String convertToString = service.convert(uuid, String.class);
@ -472,7 +473,7 @@ public class GenericConversionServiceTests {
public static Map<String, Integer> map; public static Map<String, Integer> map;
@Test @Test
public void emptyListToArray() throws Exception { public void emptyListToArray() {
conversionService.addConverter(new CollectionToArrayConverter(conversionService)); conversionService.addConverter(new CollectionToArrayConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
@ -483,7 +484,7 @@ public class GenericConversionServiceTests {
} }
@Test @Test
public void emptyListToObject() throws Exception { public void emptyListToObject() {
conversionService.addConverter(new CollectionToObjectConverter(conversionService)); conversionService.addConverter(new CollectionToObjectConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.addConverterFactory(new StringToNumberConverterFactory());
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
@ -590,14 +591,14 @@ public class GenericConversionServiceTests {
public Collection<Integer> stringToCollection; public Collection<Integer> stringToCollection;
@Test @Test
public void testConvertiblePairsInSet() throws Exception { public void testConvertiblePairsInSet() {
Set<GenericConverter.ConvertiblePair> set = new HashSet<GenericConverter.ConvertiblePair>(); Set<GenericConverter.ConvertiblePair> set = new HashSet<GenericConverter.ConvertiblePair>();
set.add(new GenericConverter.ConvertiblePair(Number.class, String.class)); set.add(new GenericConverter.ConvertiblePair(Number.class, String.class));
assert set.contains(new GenericConverter.ConvertiblePair(Number.class, String.class)); assert set.contains(new GenericConverter.ConvertiblePair(Number.class, String.class));
} }
@Test @Test
public void testConvertiblePairEqualsAndHash() throws Exception { public void testConvertiblePairEqualsAndHash() {
GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class); GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class);
GenericConverter.ConvertiblePair pairEqual = new GenericConverter.ConvertiblePair(Number.class, String.class); GenericConverter.ConvertiblePair pairEqual = new GenericConverter.ConvertiblePair(Number.class, String.class);
assertEquals(pair, pairEqual); assertEquals(pair, pairEqual);
@ -605,7 +606,7 @@ public class GenericConversionServiceTests {
} }
@Test @Test
public void testConvertiblePairDifferentEqualsAndHash() throws Exception { public void testConvertiblePairDifferentEqualsAndHash() {
GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class); GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class);
GenericConverter.ConvertiblePair pairOpposite = new GenericConverter.ConvertiblePair(String.class, Number.class); GenericConverter.ConvertiblePair pairOpposite = new GenericConverter.ConvertiblePair(String.class, Number.class);
assertFalse(pair.equals(pairOpposite)); assertFalse(pair.equals(pairOpposite));
@ -613,7 +614,7 @@ public class GenericConversionServiceTests {
} }
@Test @Test
public void convertPrimitiveArray() throws Exception { public void convertPrimitiveArray() {
GenericConversionService conversionService = new DefaultConversionService(); GenericConversionService conversionService = new DefaultConversionService();
byte[] byteArray = new byte[] { 1, 2, 3 }; byte[] byteArray = new byte[] { 1, 2, 3 };
Byte[] converted = conversionService.convert(byteArray, Byte[].class); Byte[] converted = conversionService.convert(byteArray, Byte[].class);
@ -625,7 +626,8 @@ public class GenericConversionServiceTests {
try { try {
conversionService.canConvert(String.class, null); conversionService.canConvert(String.class, null);
fail("Did not thow IllegalArgumentException"); fail("Did not thow IllegalArgumentException");
} catch(IllegalArgumentException e) { }
catch (IllegalArgumentException ex) {
} }
} }
@ -634,13 +636,14 @@ public class GenericConversionServiceTests {
try { try {
conversionService.canConvert(TypeDescriptor.valueOf(String.class), null); conversionService.canConvert(TypeDescriptor.valueOf(String.class), null);
fail("Did not thow IllegalArgumentException"); fail("Did not thow IllegalArgumentException");
} catch(IllegalArgumentException e) { }
catch(IllegalArgumentException ex) {
} }
} }
@Test @Test
@SuppressWarnings({ "rawtypes" }) @SuppressWarnings({ "rawtypes" })
public void convertHashMapValuesToList() throws Exception { public void convertHashMapValuesToList() {
GenericConversionService conversionService = new DefaultConversionService(); GenericConversionService conversionService = new DefaultConversionService();
Map<String, Integer> hashMap = new LinkedHashMap<String, Integer>(); Map<String, Integer> hashMap = new LinkedHashMap<String, Integer>();
hashMap.put("1", 1); hashMap.put("1", 1);
@ -650,7 +653,7 @@ public class GenericConversionServiceTests {
} }
@Test @Test
public void removeConvertible() throws Exception { public void removeConvertible() {
conversionService.addConverter(new ColorConverter()); conversionService.addConverter(new ColorConverter());
assertTrue(conversionService.canConvert(String.class, Color.class)); assertTrue(conversionService.canConvert(String.class, Color.class));
conversionService.removeConvertible(String.class, Color.class); conversionService.removeConvertible(String.class, Color.class);
@ -658,7 +661,7 @@ public class GenericConversionServiceTests {
} }
@Test @Test
public void conditionalConverter() throws Exception { public void conditionalConverter() {
GenericConversionService conversionService = new GenericConversionService(); GenericConversionService conversionService = new GenericConversionService();
MyConditionalConverter converter = new MyConditionalConverter(); MyConditionalConverter converter = new MyConditionalConverter();
conversionService.addConverter(new ColorConverter()); conversionService.addConverter(new ColorConverter());
@ -668,7 +671,7 @@ public class GenericConversionServiceTests {
} }
@Test @Test
public void conditionalConverterFactory() throws Exception { public void conditionalConverterFactory() {
GenericConversionService conversionService = new GenericConversionService(); GenericConversionService conversionService = new GenericConversionService();
MyConditionalConverterFactory converter = new MyConditionalConverterFactory(); MyConditionalConverterFactory converter = new MyConditionalConverterFactory();
conversionService.addConverter(new ColorConverter()); conversionService.addConverter(new ColorConverter());
@ -679,32 +682,29 @@ public class GenericConversionServiceTests {
} }
@Test @Test
public void shouldNotSuportNullConvertibleTypesFromNonConditionalGenericConverter() public void shouldNotSuportNullConvertibleTypesFromNonConditionalGenericConverter() {
throws Exception {
GenericConversionService conversionService = new GenericConversionService(); GenericConversionService conversionService = new GenericConversionService();
GenericConverter converter = new GenericConverter() { GenericConverter converter = new GenericConverter() {
@Override @Override
public Set<ConvertiblePair> getConvertibleTypes() { public Set<ConvertiblePair> getConvertibleTypes() {
return null; return null;
} }
@Override @Override
public Object convert(Object source, TypeDescriptor sourceType, public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
TypeDescriptor targetType) {
return null; return null;
} }
}; };
try { try {
conversionService.addConverter(converter); conversionService.addConverter(converter);
fail("Did not throw"); fail("Did not throw");
} catch (IllegalStateException e) { }
assertEquals("Only conditional converters may return null convertible types", e.getMessage()); catch (IllegalStateException ex) {
assertEquals("Only conditional converters may return null convertible types", ex.getMessage());
} }
} }
@Test @Test
public void conditionalConversionForAllTypes() throws Exception { public void conditionalConversionForAllTypes() {
GenericConversionService conversionService = new GenericConversionService(); GenericConversionService conversionService = new GenericConversionService();
MyConditionalGenericConverter converter = new MyConditionalGenericConverter(); MyConditionalGenericConverter converter = new MyConditionalGenericConverter();
conversionService.addConverter(converter); conversionService.addConverter(converter);
@ -717,7 +717,7 @@ public class GenericConversionServiceTests {
} }
@Test @Test
public void convertOptimizeArray() throws Exception { public void convertOptimizeArray() {
// SPR-9566 // SPR-9566
GenericConversionService conversionService = new DefaultConversionService(); GenericConversionService conversionService = new DefaultConversionService();
byte[] byteArray = new byte[] { 1, 2, 3 }; byte[] byteArray = new byte[] { 1, 2, 3 };
@ -726,7 +726,7 @@ public class GenericConversionServiceTests {
} }
@Test @Test
public void convertCannotOptimizeArray() throws Exception { public void convertCannotOptimizeArray() {
GenericConversionService conversionService = new GenericConversionService(); GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(new Converter<Byte, Byte>() { conversionService.addConverter(new Converter<Byte, Byte>() {
@Override @Override
@ -766,6 +766,7 @@ public class GenericConversionServiceTests {
conversionService.convert(source, sourceType, targetType); conversionService.convert(source, sourceType, targetType);
} }
@ExampleAnnotation @ExampleAnnotation
public String annotatedString; public String annotatedString;
@ -773,8 +774,7 @@ public class GenericConversionServiceTests {
public static @interface ExampleAnnotation { public static @interface ExampleAnnotation {
} }
private static class MyConditionalConverter implements Converter<String, Color>, private static class MyConditionalConverter implements Converter<String, Color>, ConditionalConverter {
ConditionalConverter {
private int matchAttempts = 0; private int matchAttempts = 0;
@ -850,23 +850,26 @@ public class GenericConversionServiceTests {
} }
interface MyEnumInterface { interface MyEnumInterface {
String getCode(); String getCode();
} }
public static enum MyEnum implements MyEnumInterface { public static enum MyEnum implements MyEnumInterface {
A { A {
@Override @Override
public String getCode() { public String getCode() {
return "1"; return "1";
} }
}; }
} }
private static class MyEnumInterfaceToStringConverter<T extends MyEnumInterface> private static class MyEnumInterfaceToStringConverter<T extends MyEnumInterface> implements Converter<T, String> {
implements Converter<T, String> {
@Override @Override
public String convert(T source) { public String convert(T source) {
return source.getCode(); return source.getCode();
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -141,6 +141,6 @@ public abstract class JstlUtils {
} }
return RequestContextUtils.getLocale(this.request); return RequestContextUtils.getLocale(this.request);
} }
}; }
} }

View File

@ -20,14 +20,12 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import javax.servlet.jsp.jstl.core.Config; import javax.servlet.jsp.jstl.core.Config;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceResolvable; import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException; import org.springframework.context.NoSuchMessageException;
@ -90,6 +88,7 @@ public class RequestContext {
*/ */
private static final String REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME = "requestDataValueProcessor"; private static final String REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME = "requestDataValueProcessor";
protected static final boolean jstlPresent = ClassUtils.isPresent("javax.servlet.jsp.jstl.core.Config", protected static final boolean jstlPresent = ClassUtils.isPresent("javax.servlet.jsp.jstl.core.Config",
RequestContext.class.getClassLoader()); RequestContext.class.getClassLoader());
@ -113,6 +112,7 @@ public class RequestContext {
private Map<String, Errors> errorsMap; private Map<String, Errors> errorsMap;
/** /**
* Create a new RequestContext for the given request, using the request attributes for Errors retrieval. <p>This * Create a new RequestContext for the given request, using the request attributes for Errors retrieval. <p>This
* only works with InternalResourceViews, as Errors instances are part of the model and not normally exposed as * only works with InternalResourceViews, as Errors instances are part of the model and not normally exposed as
@ -181,6 +181,7 @@ public class RequestContext {
protected RequestContext() { protected RequestContext() {
} }
/** /**
* Initialize this context with the given request, using the given model attributes for Errors retrieval. * Initialize this context with the given request, using the given model attributes for Errors retrieval.
* <p>Delegates to {@code getFallbackLocale} and {@code getFallbackTheme} for determining the fallback * <p>Delegates to {@code getFallbackLocale} and {@code getFallbackTheme} for determining the fallback
@ -214,7 +215,8 @@ public class RequestContext {
if (localeResolver != null) { if (localeResolver != null) {
// Try LocaleResolver (we're within a DispatcherServlet request). // Try LocaleResolver (we're within a DispatcherServlet request).
this.locale = localeResolver.resolveLocale(request); this.locale = localeResolver.resolveLocale(request);
} else { }
else {
// No LocaleResolver available -> try fallback. // No LocaleResolver available -> try fallback.
this.locale = getFallbackLocale(); this.locale = getFallbackLocale();
} }
@ -225,13 +227,10 @@ public class RequestContext {
this.urlPathHelper = new UrlPathHelper(); this.urlPathHelper = new UrlPathHelper();
try { if (this.webApplicationContext.containsBean(REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
this.requestDataValueProcessor = this.webApplicationContext.getBean( this.requestDataValueProcessor = this.webApplicationContext.getBean(
REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class); REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
} }
catch (NoSuchBeanDefinitionException ex) {
// Ignored
}
} }
/** /**
@ -269,6 +268,7 @@ public class RequestContext {
return theme; return theme;
} }
/** /**
* Return the underlying HttpServletRequest. Only intended for cooperating classes in this package. * Return the underlying HttpServletRequest. Only intended for cooperating classes in this package.
*/ */
@ -702,7 +702,8 @@ public class RequestContext {
if (htmlEscape && !(errors instanceof EscapedErrors)) { if (htmlEscape && !(errors instanceof EscapedErrors)) {
errors = new EscapedErrors(errors); errors = new EscapedErrors(errors);
put = true; put = true;
} else if (!htmlEscape && errors instanceof EscapedErrors) { }
else if (!htmlEscape && errors instanceof EscapedErrors) {
errors = ((EscapedErrors) errors).getSource(); errors = ((EscapedErrors) errors).getSource();
put = true; put = true;
} }
@ -720,7 +721,8 @@ public class RequestContext {
protected Object getModelObject(String modelName) { protected Object getModelObject(String modelName) {
if (this.model != null) { if (this.model != null) {
return this.model.get(modelName); return this.model.get(modelName);
} else { }
else {
return this.request.getAttribute(modelName); return this.request.getAttribute(modelName);
} }
} }
@ -746,9 +748,10 @@ public class RequestContext {
return new BindStatus(this, path, htmlEscape); return new BindStatus(this, path, htmlEscape);
} }
/** /**
* Inner class that isolates the JSTL dependency. Just called to resolve the fallback locale if the JSTL API is * Inner class that isolates the JSTL dependency.
* present. * Just called to resolve the fallback locale if the JSTL API is present.
*/ */
private static class JstlLocaleResolver { private static class JstlLocaleResolver {