Author: sagara
Date: Wed May 23 08:30:43 2012
New Revision: 1341783

URL: http://svn.apache.org/viewvc?rev=1341783&view=rev
Log:
Merged  r1341780  to the 1.6 branch.

Modified:
    axis/axis2/java/core/branches/1_6/   (props changed)
    
axis/axis2/java/core/branches/1_6/modules/adb/src/org/apache/axis2/databinding/utils/ConverterUtil.java
    
axis/axis2/java/core/branches/1_6/modules/adb/test/org/apache/axis2/databinding/utils/ConverterUtilTest.java

Propchange: axis/axis2/java/core/branches/1_6/
------------------------------------------------------------------------------
  Merged /axis/axis2/java/core/trunk:r1341780

Modified: 
axis/axis2/java/core/branches/1_6/modules/adb/src/org/apache/axis2/databinding/utils/ConverterUtil.java
URL: 
http://svn.apache.org/viewvc/axis/axis2/java/core/branches/1_6/modules/adb/src/org/apache/axis2/databinding/utils/ConverterUtil.java?rev=1341783&r1=1341782&r2=1341783&view=diff
==============================================================================
--- 
axis/axis2/java/core/branches/1_6/modules/adb/src/org/apache/axis2/databinding/utils/ConverterUtil.java
 (original)
+++ 
axis/axis2/java/core/branches/1_6/modules/adb/src/org/apache/axis2/databinding/utils/ConverterUtil.java
 Wed May 23 08:30:43 2012
@@ -889,6 +889,7 @@ public class ConverterUtil {
         int second = 0;
         long miliSecond = 0;
         int timeZoneOffSet = TimeZone.getDefault().getRawOffset();
+        boolean haveTimeZone;
 
 
         if ((source != null) && (source.length() >= 19)) {
@@ -908,18 +909,22 @@ public class ConverterUtil {
 
             int milliSecondPartLength = 0;
 
-            if (source.length() > 19)  {
+            if (source.length() == 19) {
+                haveTimeZone = false;
+            } else {
                 String rest = source.substring(19);
                 if (rest.startsWith(".")) {
                     // i.e this have the ('.'s+) part
                     if (rest.endsWith("Z")) {
                         // this is in gmt time zone
+                        haveTimeZone = true;
                         timeZoneOffSet = 0;
                         calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
                         miliSecond = Integer.parseInt(rest.substring(1, 
rest.lastIndexOf("Z")));
                         milliSecondPartLength = 
rest.substring(1,rest.lastIndexOf("Z")).trim().length();
                     } else if ((rest.lastIndexOf("+") > 0) || 
(rest.lastIndexOf("-") > 0)) {
                         // this is given in a general time zione
+                        haveTimeZone = true;
                         String timeOffSet = null;
                         if (rest.lastIndexOf("+") > 0) {
                             timeOffSet = rest.substring(rest.lastIndexOf("+") 
+ 1);
@@ -945,6 +950,7 @@ public class ConverterUtil {
 
                     } else {
                         // i.e it does not have time zone
+                        haveTimeZone = false;
                         miliSecond = Integer.parseInt(rest.substring(1));
                         milliSecondPartLength = 
rest.substring(1).trim().length();
                     }
@@ -953,9 +959,11 @@ public class ConverterUtil {
                     if (rest.startsWith("Z")) {
                         calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
                         // this is in gmt time zone
+                        haveTimeZone = true;
                         timeZoneOffSet = 0;
                     } else if (rest.startsWith("+") || rest.startsWith("-")) {
                         // this is given in a general time zione
+                        haveTimeZone = true;
                         if (rest.charAt(3) != ':') {
                             throw new RuntimeException("invalid time zone 
format (" + source
                                     + ") without : at correct place");
@@ -990,7 +998,8 @@ public class ConverterUtil {
             calendar.set(Calendar.MILLISECOND, (int)miliSecond);
             calendar.set(Calendar.ZONE_OFFSET, timeZoneOffSet);
             // set the day light offset only if the time zone is present
-            if (source.length() > 19){
+            // set the day light offset only if the time zone is present
+            if (haveTimeZone) {
                 calendar.set(Calendar.DST_OFFSET, 0);
             }
 

Modified: 
axis/axis2/java/core/branches/1_6/modules/adb/test/org/apache/axis2/databinding/utils/ConverterUtilTest.java
URL: 
http://svn.apache.org/viewvc/axis/axis2/java/core/branches/1_6/modules/adb/test/org/apache/axis2/databinding/utils/ConverterUtilTest.java?rev=1341783&r1=1341782&r2=1341783&view=diff
==============================================================================
--- 
axis/axis2/java/core/branches/1_6/modules/adb/test/org/apache/axis2/databinding/utils/ConverterUtilTest.java
 (original)
+++ 
axis/axis2/java/core/branches/1_6/modules/adb/test/org/apache/axis2/databinding/utils/ConverterUtilTest.java
 Wed May 23 08:30:43 2012
@@ -130,6 +130,41 @@ public class ConverterUtilTest extends T
         System.out.println("String   ==> " + "2007-02-15T14:54:29.399-05:30");
         System.out.println("calendar ==> " + 
simpleDateFormat.format(calendar.getTime()));
         System.out.println("calendar ==> " + 
ConverterUtil.convertToString(calendar));
+        
+        TimeZone currentTimeZone = TimeZone.getDefault();
+        System.out.println("before  Test: TimeZone is 
"+TimeZone.getDefault().getDisplayName());
+        try {
+            // run tests with default JVM time zone
+            this.internalTestConvertToDateTime();
+
+            // We use two time zone with GMT+1. One north and one south. 
+            // We hope that one is currently using daylight savings and the
+            // other is not using it.
+
+            // run tests with time zone "Europe/Berlin"
+            System.out.println( "setting time zone to Europe/Berlin" );
+            TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
+            this.internalTestConvertToDateTime();
+            
+            // run tests with time zone "Africa/Windhoek"
+            System.out.println( "setting time zone to Africa/Windhoek" );
+            TimeZone.setDefault(TimeZone.getTimeZone("Africa/Windhoek"));
+            this.internalTestConvertToDateTime();
+
+            // run tests with time zone "Australia/Darwin"
+            System.out.println( "setting time zone to Australia/Darwin" );
+            TimeZone.setDefault(TimeZone.getTimeZone("Australia/Darwin"));
+            this.internalTestConvertToDateTime();
+                        
+            // run tests with time zone "US/Mountain"
+            System.out.println( "setting time zone to US/Mountain" );
+            TimeZone.setDefault(TimeZone.getTimeZone("US/Mountain"));
+            this.internalTestConvertToDateTime();
+
+        } finally {
+            TimeZone.setDefault( currentTimeZone );
+            System.out.println("after   Test: TimeZone is 
"+TimeZone.getDefault().getDisplayName());
+        }
 
     }
 
@@ -195,4 +230,275 @@ public class ConverterUtilTest extends T
                assertEquals("Not expected content", inStr, outStr);
        }
 
+       /**
+     * Used to by formatCalendar* to format millisecond field.
+     * @param millis the millisecond value
+     * @param digits the number of digits to use
+     *        (0=none)
+     * @return something like ".123" or "" (when digits=0)
+     */
+    private static String formatMillis(int millis, int digits) {
+        if (digits == 0) return "";
+        StringBuffer sb = new StringBuffer(16);
+        sb.append('.');
+        sb.append(millis);
+        while( sb.length() > digits && sb.charAt(sb.length()-1) == '0' ) {
+            sb.deleteCharAt(sb.length()-1); // remove trailing '0'
+        }
+        while( sb.length() < digits+1 ) {
+            sb.append('0'); // add trailing '0'
+        }
+        return sb.toString();
+    }
+    
+    /**
+     * Format a Calendar object to a schema datetime string.
+     * Used by testConvertToDateTime().
+     * @param c the object to format
+     * @param millisDigits number of digits used for millisecond field
+     *                     (0 = none)
+     * @param tz the time zone to use (null = default time zone)
+     * @return something like "1111-22-33T44:55:66.789"
+     */
+    private static String formatCalendarXsd(Calendar c, int millisDigits, 
TimeZone tz)
+    {
+        String formatString = "yyyy-MM-dd'T'HH:mm:ss";
+        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatString);
+        if (tz != null) simpleDateFormat.setTimeZone(tz);
+        StringBuffer sb = new 
StringBuffer(simpleDateFormat.format(c.getTime()));
+        sb.append(formatMillis(c.get(Calendar.MILLISECOND), millisDigits));
+        return sb.toString();
+    }
+    
+    /**
+     * Format a Calendar object to a schema datetime string with time zone
+     * Used by testConvertToDateTime().
+     * @param c the object to format
+     * @param millisDigits number of digits used for millisecond field
+     *                     (0 = none)
+     * @param tz the timezone to use (null = default timezone)
+     * @return something like "1111-22-33T44:55:66.789+01:00"
+     */
+    private static String formatCalendarXsdWithTz(Calendar c, int 
millisDigits, TimeZone tz)
+    {
+        String formatString = "yyyy-MM-dd'T'HH:mm:ssZ";
+        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatString);
+        if (tz != null) simpleDateFormat.setTimeZone(tz);
+        StringBuffer sb = new 
StringBuffer(simpleDateFormat.format(c.getTime()));
+        sb.insert(19, formatMillis(c.get(Calendar.MILLISECOND), millisDigits));
+        return sb.insert(sb.length()-2, ':').toString(); // fix tz format
+    }
+    
+    /**
+     * Format a Calendar object to a schema datetime string with time zone UTC
+     * Used by testConvertToDateTime().
+     * @param c the object to format
+     * @param millisDigits number of digits used for millisecond field
+     *                     (0 = none)
+     * @return something like "1111-22-33T44:55:66.789Z"
+     */
+    private static String formatCalendarXsdZulu(Calendar c, int millisDigits)
+    {
+        String formatString = "yyyy-MM-dd'T'HH:mm:ss";
+        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatString);
+        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+        StringBuffer sb = new 
StringBuffer(simpleDateFormat.format(c.getTime()));
+        sb.append(formatMillis(c.get(Calendar.MILLISECOND), millisDigits));
+        sb.append('Z');
+        return sb.toString();
+    }
+    
+    private void internalTestConvertToDateTime()
+    {
+        System.out.println("testing with TimeZone "
+                + TimeZone.getDefault().getDisplayName());
+        System.out.println("uses daylight time: "
+                + TimeZone.getDefault().useDaylightTime());
+        System.out.println("we are in daylight time: "
+                + TimeZone.getDefault().inDaylightTime(new Date()));
+
+        String testValue;  // holds the testvalue
+        TimeZone timeZone; // the (custom) time zone of testvalue
+        Calendar calendar; // the value to test
+        String back;       // the value converted back by convertToString
+        
+        // local date without daylight savings in "Europe/Berlin"
+        testValue = "2007-02-15T14:54:29";
+        timeZone = null;
+        calendar = ConverterUtil.convertToDateTime(testValue);
+        back = ConverterUtil.convertToString(calendar);
+        System.out.println("testValue ==> " + testValue);
+        System.out.println("calendar  ==> " + 
formatCalendarXsdWithTz(calendar, 0, null));
+        System.out.println("back      ==> " + back);
+        assertEquals(testValue, 
+                     formatCalendarXsd(calendar, 0, null));
+        assertEquals("should be in local timezone",
+                     
TimeZone.getDefault().getOffset(calendar.getTimeInMillis()),
+                     
calendar.get(Calendar.ZONE_OFFSET)+calendar.get(Calendar.DST_OFFSET));
+
+        // local date with daylight savings in "Europe/Berlin"
+        testValue = "2007-05-27T23:20:33";
+        timeZone = null;
+        calendar = ConverterUtil.convertToDateTime(testValue);
+        back = ConverterUtil.convertToString(calendar);
+        System.out.println("testValue ==> " + testValue);
+        System.out.println("calendar  ==> " + 
formatCalendarXsdWithTz(calendar, 0, null));
+        System.out.println("back      ==> " + back);
+        assertEquals(testValue, 
+                     formatCalendarXsd(calendar, 0, null));
+        assertEquals("should be in local timezone",
+                     
TimeZone.getDefault().getOffset(calendar.getTimeInMillis()),
+                     
calendar.get(Calendar.ZONE_OFFSET)+calendar.get(Calendar.DST_OFFSET));
+
+        // local date with milliseconds without daylight savings
+        testValue = "2007-02-15T14:54:29.399";
+        timeZone = null;
+        calendar = ConverterUtil.convertToDateTime(testValue);
+        back = ConverterUtil.convertToString(calendar);
+        System.out.println("testValue ==> " + testValue);
+        System.out.println("calendar  ==> " + 
formatCalendarXsdWithTz(calendar, 3, null));
+        System.out.println("back      ==> " + back);
+        assertEquals(testValue,
+                     formatCalendarXsd(calendar, 3, null));
+        assertEquals("should be in local timezone",
+                     
TimeZone.getDefault().getOffset(calendar.getTimeInMillis()),
+                     
calendar.get(Calendar.ZONE_OFFSET)+calendar.get(Calendar.DST_OFFSET));
+
+        // local date with milliseconds with daylight savings
+        testValue = "2009-06-12T23:20:33.2";
+        timeZone = null;
+        calendar = ConverterUtil.convertToDateTime(testValue);
+        back = ConverterUtil.convertToString(calendar);
+        System.out.println("testValue ==> " + testValue);
+        System.out.println("calendar  ==> " + 
formatCalendarXsdWithTz(calendar, 1, null));
+        System.out.println("back      ==> " + back);
+        assertEquals(testValue, 
+                     formatCalendarXsd(calendar, 1, null));
+        assertEquals("should be in local timezone",
+                     
TimeZone.getDefault().getOffset(calendar.getTimeInMillis()),
+                     
calendar.get(Calendar.ZONE_OFFSET)+calendar.get(Calendar.DST_OFFSET));
+
+        // date with time zone (positive) no milliseconds
+        testValue = "2007-02-15T14:54:29+05:30";
+        timeZone = TimeZone.getTimeZone("GMT+05:30"); //custom time zone
+        calendar = ConverterUtil.convertToDateTime(testValue);
+        back = ConverterUtil.convertToString(calendar);
+        System.out.println("testValue ==> " + testValue);
+        System.out.println("calendar  ==> " + 
formatCalendarXsdWithTz(calendar, 0, timeZone));
+        System.out.println("back      ==> " + back);
+        // make sure that timeZone.getRawOffset() is sufficient
+        assertFalse ("custom time zone should not use day light saving", 
+                     timeZone.useDaylightTime());
+        assertEquals(testValue, 
+                     formatCalendarXsdWithTz(calendar, 0, timeZone));
+        assertEquals("should be in timezone +05:30",
+                     timeZone.getRawOffset(),
+                     
calendar.get(Calendar.ZONE_OFFSET)+calendar.get(Calendar.DST_OFFSET));
+
+        // date with time zone (negative) with milliseconds
+        testValue = "2007-02-15T14:54:29.399-05:30";
+        timeZone = TimeZone.getTimeZone("GMT-05:30"); //custom time zone
+        calendar = ConverterUtil.convertToDateTime(testValue);
+        back = ConverterUtil.convertToString(calendar);
+        System.out.println("testValue ==> " + testValue);
+        System.out.println("calendar  ==> " + 
formatCalendarXsdWithTz(calendar, 3, timeZone));
+        System.out.println("back      ==> " + back);
+        // make sure that timeZone.getRawOffset() is sufficient
+        assertFalse ("custom time zone should not use day light saving", 
+                     timeZone.useDaylightTime());
+        assertEquals(testValue, 
+                     formatCalendarXsdWithTz(calendar, 3, timeZone));
+        assertEquals("should be in timezone -05:30",
+                     timeZone.getRawOffset(),
+                     
calendar.get(Calendar.ZONE_OFFSET)+calendar.get(Calendar.DST_OFFSET));
+
+        // date with time zone and milliseconds
+        testValue = "2007-02-15T14:54:29.399+05:30";
+        timeZone = TimeZone.getTimeZone("GMT+05:30"); //custom time zone
+        calendar = ConverterUtil.convertToDateTime(testValue);
+        back = ConverterUtil.convertToString(calendar);
+        System.out.println("testValue ==> " + testValue);
+        System.out.println("calendar  ==> " + 
formatCalendarXsdWithTz(calendar, 3, timeZone));
+        System.out.println("back      ==> " + back);
+        // make sure that timeZone.getRawOffset() is sufficient
+        assertFalse ("custom time zone should not use day light saving", 
+                     timeZone.useDaylightTime());
+        assertEquals(testValue, 
+                     formatCalendarXsdWithTz(calendar, 3, timeZone));
+        assertEquals("should be in timezone +05:30",
+                     timeZone.getRawOffset(),
+                     
calendar.get(Calendar.ZONE_OFFSET)+calendar.get(Calendar.DST_OFFSET));
+
+        // date within UTC (Zulu time zone)
+        testValue = "2007-02-15T14:54:29Z";
+        timeZone = TimeZone.getTimeZone("GMT"); //UTC time zone
+        calendar = ConverterUtil.convertToDateTime(testValue);
+        back = ConverterUtil.convertToString(calendar);
+        System.out.println("testValue ==> " + testValue);
+        System.out.println("calendar  ==> " + 
formatCalendarXsdWithTz(calendar, 0, timeZone));
+        System.out.println("back      ==> " + back);
+        assertEquals(testValue, 
+                     formatCalendarXsdZulu(calendar,0));
+        assertEquals("should be in UTC",
+                     0,
+                     
calendar.get(Calendar.ZONE_OFFSET)+calendar.get(Calendar.DST_OFFSET));
+
+        // date within UTC (Zulu time zone) with milliseconds
+        testValue = "2007-02-15T14:54:29.399Z";
+        timeZone = TimeZone.getTimeZone("GMT"); //UTC time zone
+        calendar = ConverterUtil.convertToDateTime(testValue);
+        back = ConverterUtil.convertToString(calendar);
+        System.out.println("testValue ==> " + testValue);
+        System.out.println("calendar  ==> " + 
formatCalendarXsdWithTz(calendar, 3, timeZone));
+        System.out.println("back      ==> " + back);
+        assertEquals(testValue, 
+                     formatCalendarXsdZulu(calendar, 3));
+        assertEquals("should be in UTC",
+                     0,
+                     
calendar.get(Calendar.ZONE_OFFSET)+calendar.get(Calendar.DST_OFFSET));
+
+        // date within UTC (Zulu time zone) with milliseconds
+        testValue = "2006-12-11T23:57:16.62Z";
+        timeZone = TimeZone.getTimeZone("GMT"); //UTC time zone
+        calendar = ConverterUtil.convertToDateTime(testValue);
+        back = ConverterUtil.convertToString(calendar);
+        System.out.println("testValue ==> " + testValue);
+        System.out.println("calendar  ==> " + 
formatCalendarXsdWithTz(calendar, 2, timeZone));
+        System.out.println("back      ==> " + back);
+        assertEquals(testValue, 
+                     formatCalendarXsdZulu(calendar, 2));
+        assertEquals("should be in UTC",
+                     0,
+                     
calendar.get(Calendar.ZONE_OFFSET)+calendar.get(Calendar.DST_OFFSET));
+
+        // date within UTC (Zulu time zone) with milliseconds
+        testValue = "2012-05-18T13:57:01.7Z";
+        timeZone = TimeZone.getTimeZone("GMT"); //UTC time zone
+        calendar = ConverterUtil.convertToDateTime(testValue);
+        back = ConverterUtil.convertToString(calendar);
+        System.out.println("testValue ==> " + testValue);
+        System.out.println("calendar  ==> " + 
formatCalendarXsdWithTz(calendar, 1, timeZone));
+        System.out.println("back      ==> " + back);
+        assertEquals(testValue, 
+                     formatCalendarXsdZulu(calendar, 1));
+        assertEquals("should be in UTC",
+                     0,
+                     
calendar.get(Calendar.ZONE_OFFSET)+calendar.get(Calendar.DST_OFFSET));
+
+        // date within UTC (Zulu time zone) with milliseconds
+        testValue = "2012-05-17T13:57:01.123000000Z";
+        timeZone = TimeZone.getTimeZone("GMT"); //UTC time zone
+        calendar = ConverterUtil.convertToDateTime(testValue);
+        back = ConverterUtil.convertToString(calendar);
+        System.out.println("testValue ==> " + testValue);
+        System.out.println("calendar  ==> " + 
formatCalendarXsdWithTz(calendar, 9, timeZone));
+        System.out.println("back      ==> " + back);
+        assertEquals(testValue, 
+                     formatCalendarXsdZulu(calendar, 9));
+        assertEquals("should be in UTC",
+                     0,
+                     
calendar.get(Calendar.ZONE_OFFSET)+calendar.get(Calendar.DST_OFFSET));        
+    }
+
+
 }


Reply via email to