diff --git a/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java b/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java index 7de77d5e10d..9b47e794439 100644 --- a/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java +++ b/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java @@ -215,7 +215,7 @@ class TypeConverterDelegate { return (T) convertedValue.toString(); } else if (convertedValue instanceof String && !requiredType.isInstance(convertedValue)) { - if (!requiredType.isInterface() && !requiredType.isEnum()) { + if (firstAttemptEx == null && !requiredType.isInterface() && !requiredType.isEnum()) { try { Constructor strCtor = requiredType.getConstructor(String.class); return (T) BeanUtils.instantiateClass(strCtor, convertedValue); @@ -237,7 +237,6 @@ class TypeConverterDelegate { // It's an empty enum identifier: reset the enum value to null. return null; } - convertedValue = attemptToConvertStringToEnum(requiredType, trimmedValue, convertedValue); standardConversion = true; } diff --git a/spring-context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java b/spring-context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java index 9c4f6837aae..ca7a926f9af 100644 --- a/spring-context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java +++ b/spring-context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -16,8 +16,6 @@ package org.springframework.format.datetime.joda; -import static org.junit.Assert.assertEquals; - import java.util.ArrayList; import java.util.Calendar; import java.util.Date; @@ -34,6 +32,7 @@ import org.joda.time.MutableDateTime; import org.junit.After; import org.junit.Before; import org.junit.Test; + import org.springframework.beans.MutablePropertyValues; import org.springframework.context.i18n.LocaleContextHolder; 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.validation.DataBinder; +import static org.junit.Assert.*; + /** * @author Keith Donald * @author Juergen Hoeller @@ -96,7 +97,7 @@ public class JodaTimeFormattingTests { @Test public void testBindLocalDateArray() { MutablePropertyValues propertyValues = new MutablePropertyValues(); - propertyValues.add("localDate", new String[] {"10/31/09"}); + propertyValues.add("localDate", new String[]{"10/31/09"}); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); } @@ -240,6 +241,24 @@ public class JodaTimeFormattingTests { 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 public void testBindDateAnnotated() { MutablePropertyValues propertyValues = new MutablePropertyValues();