commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r555641 - in /jakarta/commons/proper/beanutils/trunk/src: java/org/apache/commons/beanutils/locale/converters/DateLocaleConverter.java test/org/apache/commons/beanutils/locale/converters/DateLocaleConverterTestCase.java
Date Thu, 12 Jul 2007 14:25:24 GMT
Author: niallp
Date: Thu Jul 12 07:25:21 2007
New Revision: 555641

URL: http://svn.apache.org/viewvc?view=rev&rev=555641
Log:
BEANUTILS-271 - DateLocaleConverter does not always throw an Exception for invalid dates -
thanks to Nico Hoogervorst

Modified:
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/locale/converters/DateLocaleConverter.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/locale/converters/DateLocaleConverterTestCase.java

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/locale/converters/DateLocaleConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/locale/converters/DateLocaleConverter.java?view=diff&rev=555641&r1=555640&r2=555641
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/locale/converters/DateLocaleConverter.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/locale/converters/DateLocaleConverter.java
Thu Jul 12 07:25:21 2007
@@ -17,11 +17,13 @@
 
 package org.apache.commons.beanutils.locale.converters;
 
+import org.apache.commons.beanutils.ConversionException;
 import org.apache.commons.beanutils.locale.BaseLocaleConverter;
 import org.apache.commons.logging.LogFactory;
 import org.apache.commons.logging.Log;
 
 import java.text.ParseException;
+import java.text.ParsePosition;
 import java.text.SimpleDateFormat;
 import java.text.DateFormat;
 import java.text.DateFormatSymbols;
@@ -268,7 +270,17 @@
  
 
          // Parse the Date
-         return formatter.parse((String) value);
+        ParsePosition pos = new ParsePosition(0);
+        String strValue = value.toString();
+        Object parsedValue = formatter.parseObject(strValue, pos);
+        if (pos.getErrorIndex() > -1) {
+            throw new ConversionException("Error parsing date '" + value + "' at position="+
pos.getErrorIndex());
+        }
+        if (pos.getIndex() < strValue.length()) {
+            throw new ConversionException("Date '" + value + "' contains unparsed characters
from position=" + pos.getIndex());
+        }
+
+        return parsedValue;
      }
    
      /**

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/locale/converters/DateLocaleConverterTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/locale/converters/DateLocaleConverterTestCase.java?view=diff&rev=555641&r1=555640&r2=555641
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/locale/converters/DateLocaleConverterTestCase.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/locale/converters/DateLocaleConverterTestCase.java
Thu Jul 12 07:25:21 2007
@@ -409,5 +409,26 @@
 
     }
 
+    /**
+     * Test invalid date
+     */
+    public void testInvalidDate() {
+
+        converter = new DateLocaleConverter(defaultLocale);
+
+        try {
+            converter.convert("01/10/2004", "dd-MM-yyyy");
+        } catch (ConversionException e) {
+            assertEquals("Parse Error", "Error parsing date '01/10/2004' at position=2",
e.getMessage());
+        }
+
+        try {
+            converter.convert("01-10-2004X", "dd-MM-yyyy");
+        } catch (ConversionException e) {
+            assertEquals("Parse Length", "Date '01-10-2004X' contains unparsed characters
from position=10", e.getMessage());
+        }
+
+    }
+
 }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message