db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r963761 - in /db/derby/code/branches/10.5: ./ java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement.java java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/CallableTest.java
Date Tue, 13 Jul 2010 15:29:31 GMT
Author: mamta
Date: Tue Jul 13 15:29:31 2010
New Revision: 963761

URL: http://svn.apache.org/viewvc?rev=963761&view=rev
Log:
Backport DERBY-4615 (EmbedCallableStatement ignores Calendar in getDate, getTime and getTimestamp)
to 10.5 codeline with this checkin.

Had to change the test to not look for nanosec granularity. for that to work, we will need
the fix for DERBY-2602 but backporting 


Modified:
    db/derby/code/branches/10.5/   (props changed)
    db/derby/code/branches/10.5/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement.java
    db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/CallableTest.java

Propchange: db/derby/code/branches/10.5/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Tue Jul 13 15:29:31 2010
@@ -1,2 +1,2 @@
 /db/derby/code/branches/10.6:957000,962738
-/db/derby/code/trunk:757811,769596,769602,769606,769962,772090,772337,772449,772534,774281,777105,779681,782991,785131,785139,785163,785570,785662,788369,788670,788674,788968,789264,790218,791027,792434,793089,793588,794106,794303,794955,795166,795459,796020,796027,796316,796372,797147,798347,798742,800523,803548,803948,805696,808494,808850,809643,810860,812669,816531,816536,819006,822289,823659,824694,827505,829022,829410,831304,831319,832379,833430,835286,881074,881444,882732,884163,885421,885659,887246,888311,892912,897161,898635,901165,901648,901760,902857,903108,905224,908418,908586,909176,910481,910511,911315,911793,915733,916075,916897,918359,921028,927430,928065,936215,942286,942476,942480,942587,946794,948045,948069,951346,951366,952138,952581,954748,955001,955634,956075,956445,956659,958163,959550,962716
+/db/derby/code/trunk:757811,769596,769602,769606,769962,772090,772337,772449,772534,774281,777105,779681,782991,785131,785139,785163,785570,785662,788369,788670,788674,788968,789264,790218,791027,792434,793089,793588,794106,794303,794955,795166,795459,796020,796027,796316,796372,797147,798347,798742,800523,803548,803948,805696,808494,808850,809643,810860,812669,816531,816536,819006,822289,823659,824694,827505,829022,829410,831304,831319,832379,833430,835286,881074,881444,882732,884163,885421,885659,887246,888311,892912,897161,898635,901165,901648,901760,902857,903108,905224,908418,908586,909176,910481,910511,911315,911793,915733,916075,916897,918359,921028,927430,928065,934474,936215,942286,942476,942480,942587,946794,948045,948069,951346,951366,952138,952581,954748,955001,955634,956075,956445,956659,958163,959550,962716

Modified: db/derby/code/branches/10.5/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.5/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement.java?rev=963761&r1=963760&r2=963761&view=diff
==============================================================================
--- db/derby/code/branches/10.5/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement.java
(original)
+++ db/derby/code/branches/10.5/java/engine/org/apache/derby/impl/jdbc/EmbedCallableStatement.java
Tue Jul 13 15:29:31 2010
@@ -405,11 +405,12 @@ public abstract class EmbedCallableState
 	 * @see CallableStatement#getDate
      * @exception SQLException NoOutputParameters thrown.
      */
-    public Date getDate(int parameterIndex) throws SQLException
+    public Date getDate(int parameterIndex, Calendar cal) throws SQLException
 	{
 		checkStatus();
 		try {
-			Date v =  getParms().getParameterForGet(parameterIndex-1).getDate(getCal());
+            Date v = getParms().
+                    getParameterForGet(parameterIndex-1).getDate(cal);
 			wasNull = (v == null);
 			return v;
 		} catch (StandardException e)
@@ -423,11 +424,12 @@ public abstract class EmbedCallableState
 	 * @see CallableStatement#getTime
      * @exception SQLException NoOutputParameters thrown.
      */
-	public Time getTime(int parameterIndex) throws SQLException 
+    public Time getTime(int parameterIndex, Calendar cal) throws SQLException
 	{
 		checkStatus();
 		try {
-			Time v =  getParms().getParameterForGet(parameterIndex-1).getTime(getCal());
+            Time v = getParms().
+                    getParameterForGet(parameterIndex-1).getTime(cal);
 			wasNull = (v == null);
 			return v;
 		} catch (StandardException e)
@@ -441,12 +443,13 @@ public abstract class EmbedCallableState
 	 * @see CallableStatement#getTimestamp
      * @exception SQLException NoOutputParameters thrown.
      */
-    public Timestamp getTimestamp(int parameterIndex)
+    public Timestamp getTimestamp(int parameterIndex, Calendar cal)
 	    throws SQLException 
 	{
 		checkStatus();
 		try {
-			Timestamp v =  getParms().getParameterForGet(parameterIndex-1).getTimestamp(getCal());
+            Timestamp v = getParms().
+                    getParameterForGet(parameterIndex-1).getTimestamp(cal);
 			wasNull = (v == null);
 			return v;
 		} catch (StandardException e)
@@ -462,10 +465,10 @@ public abstract class EmbedCallableState
      * null
      * @exception SQLException if a database-access error occurs.
      */
-    public java.sql.Date getDate(int parameterIndex, Calendar cal) 
+    public java.sql.Date getDate(int parameterIndex)
       throws SQLException 
 	{
-		return getDate(parameterIndex);
+        return getDate(parameterIndex, getCal());
 	}
 
     /**
@@ -476,10 +479,10 @@ public abstract class EmbedCallableState
 	 * null
      * @exception SQLException if a database-access error occurs.
      */
-    public java.sql.Time getTime(int parameterIndex, Calendar cal) 
+    public java.sql.Time getTime(int parameterIndex)
       throws SQLException 
 	{
-		return getTime(parameterIndex);
+        return getTime(parameterIndex, getCal());
 	}
 
     /**
@@ -491,10 +494,10 @@ public abstract class EmbedCallableState
      * null
      * @exception SQLException if a database-access error occurs.
      */
-    public java.sql.Timestamp getTimestamp(int parameterIndex, Calendar cal) 
+    public java.sql.Timestamp getTimestamp(int parameterIndex)
       throws SQLException 
 	{
-		return getTimestamp(parameterIndex);
+        return getTimestamp(parameterIndex, getCal());
 	}
 
     /**

Modified: db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/CallableTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/CallableTest.java?rev=963761&r1=963760&r2=963761&view=diff
==============================================================================
--- db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/CallableTest.java
(original)
+++ db/derby/code/branches/10.5/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/CallableTest.java
Tue Jul 13 15:29:31 2010
@@ -26,6 +26,8 @@ import java.sql.Time;
 import java.sql.Timestamp;
 
 import java.util.Arrays;   // Used by testUpdateLongBinaryProc
+import java.util.Calendar;
+import java.util.TimeZone;
 
 import java.sql.BatchUpdateException;
 import java.sql.CallableStatement;
@@ -35,10 +37,10 @@ import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
+import java.sql.Types;
 
 
 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
-import org.apache.derbyTesting.junit.BaseJDBCTestSetup;
 import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
 import org.apache.derbyTesting.junit.JDBC;
 import org.apache.derbyTesting.junit.TestConfiguration;
@@ -384,6 +386,151 @@ public class CallableTest extends BaseJD
     }
 
     /**
+     * Test that the getters and setters for Date, Time and Timestamp work as
+     * expected when given a Calendar argument. Test case for DERBY-4615.
+     */
+    public void testTimeAndDateWithCalendar() throws SQLException {
+        // Create calendars for some time zones to use when testing the
+        // setter methods.
+        Calendar[] cal1 = {
+            Calendar.getInstance(), // local calendar
+            Calendar.getInstance(TimeZone.getTimeZone("GMT")),
+            Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo")),
+            Calendar.getInstance(TimeZone.getTimeZone("Asia/Hong_Kong")),
+        };
+
+        // Use calendars for the same time zones in the getters, but create
+        // clones so that we don't get interference between the calendars.
+        Calendar[] cal2 = (Calendar[]) cal1.clone();
+        for (int i = 0; i < cal2.length; i++) {
+            cal2[i] = (Calendar) cal2[i].clone();
+        }
+
+        // Now test all the combinations.
+        for (int i = 0; i < cal1.length; i++) {
+            for (int j = 0; j < cal2.length; j++) {
+                testTimeAndDateWithCalendar(cal1[i], cal2[j]);
+            }
+        }
+    }
+
+    /**
+     * Private helper for {@link #testTimeAndDateWithCalendar()}. This method
+     * calls a procedure that takes Date, Time and Timestamp arguments and
+     * returns the exact same values. Call the setters with one calendar and
+     * the getters with another calendar, and verify that the expected
+     * conversion between time zones has happened.
+     *
+     * @param cal1 the calendar to use for the setter methods
+     * @param cal2 the calendar to use for the getter methods
+     */
+    private void testTimeAndDateWithCalendar(Calendar cal1, Calendar cal2)
+            throws SQLException
+    {
+        println("Running " + getName() + "() with " +
+                cal1.getTimeZone().getDisplayName() + " and " +
+                cal2.getTimeZone().getDisplayName());
+
+        CallableStatement cs = prepareCall(
+                "call NON_NUMERIC_TYPES_IN_AND_OUT_PROC(?,?,?,?,?,?,?,?)");
+
+        Date d = Date.valueOf("2010-04-14");
+        Time t = Time.valueOf("12:23:24");
+        Timestamp ts = new Timestamp(System.currentTimeMillis());
+        //The following call to set nanosecs is different in 10.5 release
+        //compared to 10.6 and higher. The reason for this is that the
+        //jira DERBY-2602 can't be backported to 10.5 release. Backport
+        //of DERBY-2602 to 10.5 will cause the network server to break
+        //since different releases of 10.5 will have different behavior.
+        //Because of the absence of the fix for DERBY-2602 in 10.5
+        //codeline, we can't handle the nanosec values.
+//        ts.setNanos(123456789);
+        ts.setNanos(123456000);
+
+        cs.setDate(1, d, cal1);
+        cs.setTime(2, t, cal1);
+        cs.setTimestamp(3, ts, cal1);
+        cs.setNull(4, Types.VARBINARY); // we don't care about VARBINARY here
+
+        cs.registerOutParameter (5, java.sql.Types.DATE);
+        cs.registerOutParameter (6, java.sql.Types.TIME);
+        cs.registerOutParameter (7, java.sql.Types.TIMESTAMP);
+        cs.registerOutParameter (8, java.sql.Types.VARBINARY);
+
+        cs.execute();
+
+        assertSameDate(d, cal1, cs.getDate(5, cal2), cal2);
+        assertSameTime(t, cal1, cs.getTime(6, cal2), cal2);
+        assertSameTimestamp(ts, cal1, cs.getTimestamp(7, cal2), cal2);
+    }
+
+    /**
+     * Assert that two {@code java.util.Date} values have the same
+     * representation of their date components (year, month and day) in their
+     * respective time zones.
+     *
+     * @param expected the expected date
+     * @param cal1 a calendar representing the time zone of the expected date
+     * @param actual the actual date
+     * @param cal2 a calendar representing the time zone of the actual date
+     */
+    private void assertSameDate(java.util.Date expected, Calendar cal1,
+                                java.util.Date actual, Calendar cal2) {
+        cal1.clear();
+        cal1.setTime(expected);
+        int expectedYear = cal1.get(Calendar.YEAR);
+        int expectedMonth = cal1.get(Calendar.MONTH);
+        int expectedDay = cal1.get(Calendar.DAY_OF_MONTH);
+
+        cal2.clear();
+        cal2.setTime(actual);
+        assertEquals("year", expectedYear, cal2.get(Calendar.YEAR));
+        assertEquals("month", expectedMonth, cal2.get(Calendar.MONTH));
+        assertEquals("day", expectedDay, cal2.get(Calendar.DAY_OF_MONTH));
+    }
+
+    /**
+     * Assert that two {@code java.util.Date} values have the same
+     * representation of their time components (hour, minute, second) in their
+     * respective time zones.
+     *
+     * @param expected the expected time
+     * @param cal1 a calendar representing the time zone of the expected time
+     * @param actual the actual time
+     * @param cal2 a calendar representing the time zone of the actual time
+     */
+    private void assertSameTime(java.util.Date expected, Calendar cal1,
+                                java.util.Date actual, Calendar cal2) {
+        cal1.clear();
+        cal1.setTime(expected);
+        int expectedHour = cal1.get(Calendar.HOUR_OF_DAY);
+        int expectedMinute = cal1.get(Calendar.MINUTE);
+        int expectedSecond = cal1.get(Calendar.SECOND);
+
+        cal2.clear();
+        cal2.setTime(actual);
+        assertEquals("hour", expectedHour, cal2.get(Calendar.HOUR_OF_DAY));
+        assertEquals("minute", expectedMinute, cal2.get(Calendar.MINUTE));
+        assertEquals("second", expectedSecond, cal2.get(Calendar.SECOND));
+    }
+
+    /**
+     * Assert that two Timestamp values have the same representation in their
+     * respective time zones.
+     *
+     * @param expected the expected time
+     * @param cal1 a calendar representing the time zone of the expected time
+     * @param actual the actual time
+     * @param cal2 a calendar representing the time zone of the actual time
+     */
+    private void assertSameTimestamp(Timestamp expected, Calendar cal1,
+                                     Timestamp actual, Calendar cal2) {
+        assertSameDate(expected, cal1, actual, cal2);
+        assertSameTime(expected, cal1, actual, cal2);
+        assertEquals("nanos", expected.getNanos(), actual.getNanos());
+    }
+
+    /**
      * Calls a SQL procedure that takes INOUT parameters of various types.
      * @throws SQLException 
      */



Mime
View raw message