db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r454469 - in /db/ddlutils/trunk/src: java/org/apache/ddlutils/io/converters/ test/org/apache/ddlutils/ test/org/apache/ddlutils/io/converters/
Date Mon, 09 Oct 2006 19:31:50 GMT
Author: tomdz
Date: Mon Oct  9 12:31:47 2006
New Revision: 454469

URL: http://svn.apache.org/viewvc?view=rev&rev=454469
Log:
Fix for DDLUTILS-57

Added:
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/converters/
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/converters/TestDateConverter.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/converters/TestTimeConverter.java
Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/io/converters/DateConverter.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/io/converters/TimeConverter.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/RunAllTests.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/io/converters/DateConverter.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/io/converters/DateConverter.java?view=diff&rev=454469&r1=454468&r2=454469
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/io/converters/DateConverter.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/io/converters/DateConverter.java Mon Oct
 9 12:31:47 2006
@@ -18,17 +18,30 @@
 
 import java.sql.Date;
 import java.sql.Types;
+import java.util.Calendar;
 
 /**
  * Converts between {@link java.sql.Date} and {@link java.lang.String} using the standard
  * representation "yyyy", or "yyyy-mm", or "yyyy-mm-dd".
  * 
- * @author Thomas Dudziak
  * @version $Revision: 289996 $
  */
 public class DateConverter implements SqlTypeConverter 
 {
-    /**
+	/** The calendar object to convert to/from dates. */
+	private Calendar _calendar;
+
+	/**
+	 * Creates a new date converter object.
+	 */
+	public DateConverter()
+	{
+		_calendar = Calendar.getInstance();
+
+		_calendar.setLenient(false);
+	}
+
+	/**
      * {@inheritDoc}
      */
     public Object convertFromString(String textRep, int sqlTypeCode) throws ConversionException
@@ -69,7 +82,10 @@
                         day   = Integer.parseInt(dateAsText.substring(slashPos + 1));
                     }
                 }
-                return new Date(year - 1900, month - 1, day);
+
+                _calendar.clear();
+                _calendar.set(year, month - 1, day);
+                return new Date(_calendar.getTimeInMillis());
             }
             catch (NumberFormatException ex)
             {
@@ -91,6 +107,16 @@
      */
     public String convertToString(Object obj, int sqlTypeCode) throws ConversionException
     {
-        return obj == null ? null : obj.toString();
+        String result = null;
+
+        if (obj != null)
+        {
+            if (!(obj instanceof Date))
+            {
+                throw new ConversionException("Expected object of type java.sql.Date, but
instead received " + obj.getClass().getName());
+            }
+            result = obj.toString();
+        }
+        return result;
     }
 }

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/io/converters/TimeConverter.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/io/converters/TimeConverter.java?view=diff&rev=454469&r1=454468&r2=454469
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/io/converters/TimeConverter.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/io/converters/TimeConverter.java Mon Oct
 9 12:31:47 2006
@@ -18,6 +18,7 @@
 
 import java.sql.Time;
 import java.sql.Types;
+import java.util.Calendar;
 
 /**
  * Converts between {@link java.sql.Time} and {@link java.lang.String} using the standard
@@ -28,7 +29,20 @@
  */
 public class TimeConverter implements SqlTypeConverter 
 {
-    /**
+	/** The calendar object to convert to/from times. */
+	private Calendar _calendar;
+
+	/**
+	 * Creates a new time converter object.
+	 */
+	public TimeConverter()
+	{
+		_calendar = Calendar.getInstance();
+
+		_calendar.setLenient(false);
+	}
+
+	/**
      * {@inheritDoc}
      */
     public Object convertFromString(String textRep, int sqlTypeCode) throws ConversionException
@@ -69,7 +83,12 @@
                         seconds = Integer.parseInt(timeAsText.substring(slashPos + 1));
                     }
                 }
-                return new Time(hours, minutes, seconds);
+
+                _calendar.clear();
+                _calendar.set(Calendar.HOUR, hours);
+                _calendar.set(Calendar.MINUTE, minutes);
+                _calendar.set(Calendar.SECOND, seconds);
+                return new Time(_calendar.getTimeInMillis());
             }
             catch (NumberFormatException ex)
             {
@@ -91,6 +110,16 @@
      */
     public String convertToString(Object obj, int sqlTypeCode) throws ConversionException
     {
-        return obj == null ? null : obj.toString();
+        String result = null;
+
+        if (obj != null)
+        {
+            if (!(obj instanceof Time))
+            {
+                throw new ConversionException("Expected object of type java.sql.Time, but
instead received " + obj.getClass().getName());
+            }
+            result = obj.toString();
+        }
+        return result;
     }
 }

Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/RunAllTests.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/RunAllTests.java?view=diff&rev=454469&r1=454468&r2=454469
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/RunAllTests.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/RunAllTests.java Mon Oct  9 12:31:47 2006
@@ -24,6 +24,8 @@
 import org.apache.ddlutils.io.TestDataReader;
 import org.apache.ddlutils.io.TestDatabaseIO;
 import org.apache.ddlutils.io.TestDatatypes;
+import org.apache.ddlutils.io.converters.TestDateConverter;
+import org.apache.ddlutils.io.converters.TestTimeConverter;
 import org.apache.ddlutils.model.TestArrayAccessAtTable;
 import org.apache.ddlutils.platform.TestAxionPlatform;
 import org.apache.ddlutils.platform.TestCloudscapePlatform;
@@ -91,6 +93,8 @@
         suite.addTestSuite(TestPlatformUtils.class);
         suite.addTestSuite(TestDatabaseIO.class);
         suite.addTestSuite(TestDataReader.class);
+        suite.addTestSuite(TestDateConverter.class);
+        suite.addTestSuite(TestTimeConverter.class);
         suite.addTestSuite(TestAxionPlatform.class);
         suite.addTestSuite(TestCloudscapePlatform.class);
         suite.addTestSuite(TestDB2Platform.class);

Added: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/converters/TestDateConverter.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/converters/TestDateConverter.java?view=auto&rev=454469
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/converters/TestDateConverter.java (added)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/converters/TestDateConverter.java Mon
Oct  9 12:31:47 2006
@@ -0,0 +1,196 @@
+package org.apache.ddlutils.io.converters;
+
+import java.sql.Date;
+import java.sql.Types;
+import java.util.Calendar;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests the {@link DateConverter}.
+ *
+ * @version $Revision: 1.0 $
+ */
+public class TestDateConverter extends TestCase
+{
+	/** The tested date converter. */
+    private DateConverter _dateConverter;
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        _dateConverter = new DateConverter();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void tearDown() throws Exception
+    {
+        _dateConverter = null;
+        super.tearDown();
+    }
+
+    /**
+     * Tests a normal date string.
+     */
+    public void testNormalConvertFromYearMonthDateString()
+    {
+        String   textRep = "2005-12-19";
+        Calendar cal     = Calendar.getInstance();
+
+        cal.setLenient(false);
+        cal.clear();
+        cal.set(2005, 11, 19);
+        
+        Object result = _dateConverter.convertFromString(textRep, Types.DATE);
+        
+        assertTrue(result instanceof Date);
+        assertEquals(cal.getTimeInMillis(), ((Date)result).getTime());
+    }
+
+    /**
+     * Tests a date string that has no day.
+     */
+    public void testNormalConvertFromYearMonthString()
+    {
+        String   textRep = "2005-12";
+        Calendar cal     = Calendar.getInstance();
+
+        cal.setLenient(false);
+        cal.clear();
+        cal.set(2005, 11, 1);
+        
+        Object result = _dateConverter.convertFromString(textRep, Types.DATE);
+        
+        assertTrue(result instanceof Date);
+        assertEquals(cal.getTimeInMillis(), ((Date)result).getTime());
+    }
+
+    /**
+     * Tests a date string that has only a year.
+     */
+    public void testNormalConvertFromYearString()
+    {
+        String   textRep = "2005";
+        Calendar cal     = Calendar.getInstance();
+
+        cal.clear();
+        cal.set(2005, 0, 1);
+        
+        Object result = _dateConverter.convertFromString(textRep, Types.DATE);
+
+        assertTrue(result instanceof Date);
+        assertEquals(cal.getTimeInMillis(), ((Date)result).getTime());
+    }
+
+    /**
+     * Tests converting with an invalid SQL type.
+     */
+    public void testConvertFromStringWithInvalidSqlType()
+    {
+        String textRep = "2005-12-19";
+        Object result  = _dateConverter.convertFromString(textRep, Types.INTEGER);
+
+        // Make sure that the text representation is returned since SQL type was not a DATE
+        assertNotNull(result);
+        assertEquals(textRep, result);
+    }
+
+    /**
+     * Tests handling of null.
+     */
+    public void testConvertFromStringWithNullTextRep()
+    {
+        String textRep = null;
+        Object result  = _dateConverter.convertFromString(textRep, Types.DATE);
+
+        assertNull(result);
+    }
+
+    /**
+     * Tests an invalid date.
+     */
+    public void testConvertFromStringWithInvalidTextRep()
+    {
+        String textRep = "9999-99-99";
+
+        try
+        {
+            _dateConverter.convertFromString(textRep, Types.DATE);
+            fail("ConversionException expected");
+        }
+        catch (ConversionException ex)
+        {
+            // we expect the exception
+        }
+    }
+
+    /**
+     * Tests an invalid date that contains non-numbers.
+     */
+    public void testConvertFromStringWithAlphaTextRep()
+    {
+        String textRep = "aaaa-bb-cc";
+
+        try
+        {
+            _dateConverter.convertFromString(textRep, Types.DATE);
+            fail("ConversionException expected");
+        }
+        catch (ConversionException ex)
+        {
+            // we expect the exception
+        }
+    }
+
+    /**
+     * Tests converting a normal date to a string.
+     */
+    public void testNormalConvertToString()
+    {
+        Calendar cal = Calendar.getInstance();
+
+        cal.setLenient(false);
+        cal.clear();
+        cal.set(2005, 11, 19);
+
+        Date   date   = new Date(cal.getTimeInMillis());
+        String result = _dateConverter.convertToString(date, Types.DATE);
+
+        assertNotNull(result);
+        assertEquals("2005-12-19", result);
+    }
+
+    /**
+     * Tests converting a null.
+     */
+    public void testConvertToStringWithNullDate()
+    {
+        Date   date   = null;
+        String result = _dateConverter.convertToString(date, Types.DATE);
+
+        assertNull(result);
+    }
+
+    /**
+     * Tests converting a {@link java.util.Date}.
+     */
+    public void testConvertToStringWithWrongType()
+    {
+        java.util.Date date = new java.util.Date();
+
+        try
+        {
+            _dateConverter.convertToString(date, Types.DATE);
+            fail("ConversionException expected");
+        }
+        catch (ConversionException expected)
+        {
+            // we expect the exception
+        }
+    }
+}

Added: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/converters/TestTimeConverter.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/converters/TestTimeConverter.java?view=auto&rev=454469
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/converters/TestTimeConverter.java (added)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/converters/TestTimeConverter.java Mon
Oct  9 12:31:47 2006
@@ -0,0 +1,201 @@
+package org.apache.ddlutils.io.converters;
+
+import java.sql.Time;
+import java.sql.Types;
+import java.util.Calendar;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests the {@link TimeConverter}.
+ *
+ * @version $Revision: 1.0 $
+ */
+public class TestTimeConverter extends TestCase
+{
+	/** The tested time converter. */
+    private TimeConverter _timeConverter;
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        _timeConverter = new TimeConverter();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected void tearDown() throws Exception
+    {
+        _timeConverter = null;
+        super.tearDown();
+    }
+
+    /**
+     * Tests a normal time string.
+     */
+    public void testNormalConvertFromHoursMinutesSecondsTimeString()
+    {
+        String   textRep = "02:15:59";
+        Calendar cal     = Calendar.getInstance();
+
+        cal.setLenient(false);
+        cal.clear();
+        cal.set(Calendar.HOUR, 2);
+        cal.set(Calendar.MINUTE, 15);
+        cal.set(Calendar.SECOND, 59);
+        
+        Object result = _timeConverter.convertFromString(textRep, Types.TIME);
+        
+        assertTrue(result instanceof Time);
+        assertEquals(cal.getTimeInMillis(), ((Time)result).getTime());
+    }
+
+    /**
+     * Tests a time string without seconds.
+     */
+    public void testNormalConvertFromHoursMinutesTimeString()
+    {
+        String   textRep = "02:15";
+        Calendar cal     = Calendar.getInstance();
+
+        cal.setLenient(false);
+        cal.clear();
+        cal.set(Calendar.HOUR, 2);
+        cal.set(Calendar.MINUTE, 15);
+
+        Object result = _timeConverter.convertFromString(textRep, Types.TIME);
+
+        assertTrue(result instanceof Time);
+        assertEquals(cal.getTimeInMillis(), ((Time)result).getTime());
+    }
+
+    /**
+     * Tests a time string with only an hour value.
+     */
+    public void testNormalConvertFromHoursTimeString()
+    {
+        String   textRep = "02";
+        Calendar cal     = Calendar.getInstance();
+
+        cal.setLenient(false);
+        cal.clear();
+        cal.set(Calendar.HOUR, 2);
+
+        Object result = _timeConverter.convertFromString(textRep, Types.TIME);
+
+        assertTrue(result instanceof Time);
+        assertEquals(cal.getTimeInMillis(), ((Time)result).getTime());
+    }
+
+    /**
+     * Tests converting with an invalid SQL type.
+     */
+    public void testConvertFromStringWithInvalidSqlType()
+    {
+        String textRep = "02:15:59";
+        Object result  = _timeConverter.convertFromString(textRep, Types.INTEGER);
+
+        assertNotNull(result);
+        assertEquals(textRep, result);
+    }
+
+    /**
+     * Tests converting a null.
+     */
+    public void testConvertFromStringWithNullTextRep()
+    {
+        String textRep = null;
+        Object result  = _timeConverter.convertFromString(textRep, Types.TIME);
+
+        assertNull(result);
+    }
+
+    /**
+     * Tests converting an invalid time string.
+     */
+    public void testConvertFromStringWithInvalidTextRep()
+    {
+        String textRep = "99:99:99";
+
+        try
+        {
+            _timeConverter.convertFromString(textRep, Types.TIME);
+            fail("ConversionException expected");
+        }
+        catch (ConversionException ex)
+        {
+            // We expect the exception 
+        }
+    }
+
+    /**
+     * Tests converting an invalid time string containing not only numbers.
+     */
+    public void testConvertFromStringWithAlphaTextRep()
+    {
+        String textRep = "aa:bb:cc";
+
+        try
+        {
+            _timeConverter.convertFromString(textRep, Types.TIME);
+            fail("ConversionException expected");
+        }
+        catch (ConversionException expected)
+        {
+            // We expect the exception
+        }
+    }
+
+    /**
+     * Tests converting a normal time to a string.
+     */
+    public void testNormalConvertToString()
+    {
+        Calendar cal = Calendar.getInstance();
+
+        cal.setLenient(false);
+        cal.clear();
+        cal.set(Calendar.HOUR, 2);
+        cal.set(Calendar.MINUTE, 15);
+        cal.set(Calendar.SECOND, 59);
+
+        Time   time   = new Time(cal.getTimeInMillis());
+        String result = _timeConverter.convertToString(time, Types.TIME);
+
+        assertNotNull(result);
+        assertEquals("02:15:59", result);
+    }
+
+    /**
+     * Tests converting a null time.
+     */
+    public void testConvertToStringWithNullTime()
+    {
+        Time   time   = null;
+        String result = _timeConverter.convertToString(time, Types.TIME);
+
+        assertNull(result);
+    }
+
+    /**
+     * Tests converting a {@link java.util.Date}.
+     */
+    public void testConvertToStringWithWrongType()
+    {
+        java.util.Date date = new java.util.Date();
+
+        try
+        {
+            _timeConverter.convertToString(date, Types.TIME);
+            fail("ConversionException expected");
+        }
+        catch (ConversionException expected)
+        {
+            // We expect the exception
+        }
+    }
+}



Mime
View raw message