BeanWrapper does not fall back to String constructor if ConversionService attempt failed before

Issue: SPR-9865
This commit is contained in:
Juergen Hoeller 2012-10-11 19:43:41 +02:00 committed by unknown
parent 65743936ff
commit ff7dcec5f7
2 changed files with 24 additions and 6 deletions

View File

@ -215,7 +215,7 @@ class TypeConverterDelegate {
return (T) convertedValue.toString(); return (T) convertedValue.toString();
} }
else if (convertedValue instanceof String && !requiredType.isInstance(convertedValue)) { else if (convertedValue instanceof String && !requiredType.isInstance(convertedValue)) {
if (!requiredType.isInterface() && !requiredType.isEnum()) { if (firstAttemptEx == null && !requiredType.isInterface() && !requiredType.isEnum()) {
try { try {
Constructor strCtor = requiredType.getConstructor(String.class); Constructor strCtor = requiredType.getConstructor(String.class);
return (T) BeanUtils.instantiateClass(strCtor, convertedValue); return (T) BeanUtils.instantiateClass(strCtor, convertedValue);
@ -237,7 +237,6 @@ class TypeConverterDelegate {
// It's an empty enum identifier: reset the enum value to null. // It's an empty enum identifier: reset the enum value to null.
return null; return null;
} }
convertedValue = attemptToConvertStringToEnum(requiredType, trimmedValue, convertedValue); convertedValue = attemptToConvertStringToEnum(requiredType, trimmedValue, convertedValue);
standardConversion = true; standardConversion = true;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2012 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.
@ -16,8 +16,6 @@
package org.springframework.format.datetime.joda; package org.springframework.format.datetime.joda;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
@ -34,6 +32,7 @@ import org.joda.time.MutableDateTime;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.MutablePropertyValues;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultConversionService;
@ -42,6 +41,8 @@ import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.format.support.FormattingConversionService; import org.springframework.format.support.FormattingConversionService;
import org.springframework.validation.DataBinder; import org.springframework.validation.DataBinder;
import static org.junit.Assert.*;
/** /**
* @author Keith Donald * @author Keith Donald
* @author Juergen Hoeller * @author Juergen Hoeller
@ -96,7 +97,7 @@ public class JodaTimeFormattingTests {
@Test @Test
public void testBindLocalDateArray() { public void testBindLocalDateArray() {
MutablePropertyValues propertyValues = new MutablePropertyValues(); MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", new String[] {"10/31/09"}); propertyValues.add("localDate", new String[]{"10/31/09"});
binder.bind(propertyValues); binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals(0, binder.getBindingResult().getErrorCount());
} }
@ -240,6 +241,24 @@ public class JodaTimeFormattingTests {
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("date")); assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("date"));
} }
@Test
public void testBindDateWithErrorAvoidingDateConstructor() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("date", "Sat, 12 Aug 1995 13:30:00 GMT");
binder.bind(propertyValues);
assertEquals(1, binder.getBindingResult().getErrorCount());
assertEquals("Sat, 12 Aug 1995 13:30:00 GMT", binder.getBindingResult().getFieldValue("date"));
}
@Test
public void testBindDateWithoutErrorFallingBackToDateConstructor() {
DataBinder binder = new DataBinder(new JodaTimeBean());
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("date", "Sat, 12 Aug 1995 13:30:00 GMT");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
}
@Test @Test
public void testBindDateAnnotated() { public void testBindDateAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues(); MutablePropertyValues propertyValues = new MutablePropertyValues();