sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1654307 - in /sling/trunk: bundles/commons/json/src/main/java/org/apache/sling/commons/json/sling/ bundles/commons/json/src/test/java/org/apache/sling/commons/json/sling/ launchpad/builder/src/main/bundles/ launchpad/integration-tests/src/...
Date Fri, 23 Jan 2015 18:47:01 GMT
Author: bdelacretaz
Date: Fri Jan 23 18:47:01 2015
New Revision: 1654307

URL: http://svn.apache.org/r1654307
Log:
SLING-4258 - JSON representation of Calendar values should preserve timezone - contributed
by santiago garcĂ­a pimentel, thanks!

Modified:
    sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/sling/JsonObjectCreator.java
    sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/sling/JsonObjectCreatorTest.java
    sling/trunk/launchpad/builder/src/main/bundles/list.xml
    sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/SlingDateValuesTest.java

Modified: sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/sling/JsonObjectCreator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/sling/JsonObjectCreator.java?rev=1654307&r1=1654306&r2=1654307&view=diff
==============================================================================
--- sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/sling/JsonObjectCreator.java
(original)
+++ sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/sling/JsonObjectCreator.java
Fri Jan 23 18:47:01 2015
@@ -26,6 +26,7 @@ import java.util.Calendar;
 import java.util.Iterator;
 import java.util.Locale;
 import java.util.Map;
+import java.util.TimeZone;
 
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceUtil;
@@ -119,10 +120,11 @@ public abstract class JsonObjectCreator
     /** The Locale used to format date values */
     static final Locale DATE_FORMAT_LOCALE = Locale.US;
 
-    private static final DateFormat CALENDAR_FORMAT = new SimpleDateFormat(ECMA_DATE_FORMAT,
DATE_FORMAT_LOCALE);
 
-    private static synchronized String format(final Calendar date) {
-        return CALENDAR_FORMAT.format(date.getTime());
+    private static String format(final Calendar date) {
+        DateFormat formatter = new SimpleDateFormat(ECMA_DATE_FORMAT, DATE_FORMAT_LOCALE);
+        formatter.setTimeZone(date.getTimeZone());
+        return formatter.format(date.getTime());
     }
 
     /** Dump only a value in the correct format */

Modified: sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/sling/JsonObjectCreatorTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/sling/JsonObjectCreatorTest.java?rev=1654307&r1=1654306&r2=1654307&view=diff
==============================================================================
--- sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/sling/JsonObjectCreatorTest.java
(original)
+++ sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/sling/JsonObjectCreatorTest.java
Fri Jan 23 18:47:01 2015
@@ -22,6 +22,7 @@ import static org.mockito.Mockito.when;
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
+import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
@@ -29,6 +30,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.TimeZone;
 import java.util.UUID;
 
 import org.apache.sling.api.resource.Resource;
@@ -63,6 +65,7 @@ public class JsonObjectCreatorTest {
 
     private static final Object SAME = new Object();
     private static final int NCHILDREN = 3;
+    private static final String ECMA_DATE_FORMAT = "EEE MMM dd yyyy HH:mm:ss 'GMT'Z";
 
     @Before
     public void setup() {
@@ -151,12 +154,26 @@ public class JsonObjectCreatorTest {
     @Test
     public void testCalendar() throws JSONException {
         final Calendar nowCalendar = Calendar.getInstance();
-        final String ECMA_DATE_FORMAT = "EEE MMM dd yyyy HH:mm:ss 'GMT'Z";
         final String nowString = new SimpleDateFormat(ECMA_DATE_FORMAT, JsonObjectCreator.DATE_FORMAT_LOCALE).format(nowCalendar.getTime());
         assertGet(nowCalendar, nowString);
     }
 
     @Test
+    public void testCalendarTimezones() throws JSONException {
+        final int[] offsets = { -14400000, -4200000, 14400000, 4300000 };
+        for (int offset : offsets) {
+            for (String tzId : TimeZone.getAvailableIDs(offset)) {
+                final TimeZone tz = TimeZone.getTimeZone(tzId);
+                final Calendar cal = Calendar.getInstance(tz);
+                DateFormat fmt = new SimpleDateFormat(ECMA_DATE_FORMAT, Locale.ENGLISH);
+                fmt.setTimeZone(tz);
+                final String nowString = fmt.format(cal.getTime());
+                assertGet(cal, nowString);
+            }
+        }
+    }
+    
+    @Test
     public void testStream() throws JSONException {
         final byte [] bytes = "Hello there".getBytes();
         final InputStream stream = new ByteArrayInputStream(bytes);
@@ -174,4 +191,4 @@ public class JsonObjectCreatorTest {
         }
     }
 
-}
+}
\ No newline at end of file

Modified: sling/trunk/launchpad/builder/src/main/bundles/list.xml
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/builder/src/main/bundles/list.xml?rev=1654307&r1=1654306&r2=1654307&view=diff
==============================================================================
--- sling/trunk/launchpad/builder/src/main/bundles/list.xml (original)
+++ sling/trunk/launchpad/builder/src/main/bundles/list.xml Fri Jan 23 18:47:01 2015
@@ -386,7 +386,7 @@
         <bundle>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.commons.json</artifactId>
-            <version>2.0.10</version>
+            <version>2.0.11-SNAPSHOT</version>
         </bundle>
         <bundle>
             <groupId>org.apache.felix</groupId>

Modified: sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/SlingDateValuesTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/SlingDateValuesTest.java?rev=1654307&r1=1654306&r2=1654307&view=diff
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/SlingDateValuesTest.java
(original)
+++ sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/SlingDateValuesTest.java
Fri Jan 23 18:47:01 2015
@@ -17,10 +17,14 @@
 package org.apache.sling.launchpad.webapp.integrationtest.servlets.post;
 
 import java.io.IOException;
+import java.text.DateFormat;
 import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Date;
 import java.util.Locale;
+import java.util.TimeZone;
 
+import org.apache.jackrabbit.util.ISO8601;
 import org.apache.sling.commons.testing.integration.HttpTestBase;
 import org.apache.sling.commons.testing.integration.NameValuePairList;
 import org.apache.sling.servlets.post.SlingPostConstants;
@@ -33,12 +37,14 @@ import org.slf4j.LoggerFactory;
 
 public class SlingDateValuesTest extends HttpTestBase {
 
+    private static final String ECMA_FORMAT = "EEE MMM dd yyyy HH:mm:ss 'GMT'Z";
+
     public static final String TEST_BASE_PATH = "/sling-tests";
     
     private final Logger log = LoggerFactory.getLogger(getClass());
 
     private final SimpleDateFormat[] testFormats = new SimpleDateFormat[]{
-        new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.US),
+        new SimpleDateFormat(ECMA_FORMAT, Locale.US),
         new SimpleDateFormat("dd.MM.yyyy HH:mm:ss", Locale.US),
         new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US),
         new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US)
@@ -76,7 +82,7 @@ public class SlingDateValuesTest extends
     }
 
     public void testDateValues() throws IOException {
-        SimpleDateFormat ecmaFmt = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z",
Locale.US);
+        SimpleDateFormat ecmaFmt = new SimpleDateFormat(ECMA_FORMAT, Locale.US);
         Date now = new Date();
         Date date2 = new Date(10000042L);
         String nowStr = ecmaFmt.format(now);
@@ -87,4 +93,29 @@ public class SlingDateValuesTest extends
             doDateTest(nowStr, testStr, date2Str, test2Str);
         }
     }
+    
+    
+    public void testDateTimezones() throws IOException{
+        
+        final int [] offsets = { -14400000, -4200000, 14400000, 4300000 };
+        for(int offset : offsets) {
+            for(String tzId : TimeZone.getAvailableIDs(offset)) {
+                final TimeZone tz =TimeZone.getTimeZone(tzId);
+                final Calendar cal = Calendar.getInstance(tz);
+                DateFormat fmt = new SimpleDateFormat(ECMA_FORMAT, Locale.ENGLISH);
+                fmt.setTimeZone(tz);
+                final String ecmaNow = fmt.format(cal.getTime());
+                final String isoNow=ISO8601.format(cal);
+                
+                final Calendar cal2= Calendar.getInstance(tz);
+                final String ecmaNow2 = fmt.format(cal2.getTime());
+                final String isoNow2=ISO8601.format(cal2);   
+                    
+                doDateTest(ecmaNow, isoNow, ecmaNow2, isoNow2);
+            }
+        }
+        
+        
+    }
+    
 }
\ No newline at end of file



Mime
View raw message