You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:20:50 UTC

[sling-org-apache-sling-testing-resourceresolver-mock] 11/25: SLING-4039 Support ISO8601 date string to Calendar conversion

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.testing.resourceresolver-mock-1.1.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-resourceresolver-mock.git

commit 2224d2dfa552c222ad7d80b1d73ebb41ea02edb1
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Mon Oct 13 09:47:50 2014 +0000

    SLING-4039 Support ISO8601 date string to Calendar conversion
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1631324 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                              |  7 +++++++
 .../sling/testing/resourceresolver/MockValueMap.java | 20 ++++++++++++++++----
 .../SlingCrudResourceResolverTest.java               | 14 ++++++++++++++
 3 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/pom.xml b/pom.xml
index f70bc14..ace2f03 100644
--- a/pom.xml
+++ b/pom.xml
@@ -73,6 +73,7 @@
         <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>servlet-api</artifactId>
+            <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>commons-io</groupId>
@@ -80,6 +81,12 @@
             <version>2.4</version>
             <scope>compile</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.jackrabbit</groupId>
+            <artifactId>jackrabbit-jcr-commons</artifactId>
+            <version>2.5.3</version>
+            <scope>compile</scope>
+        </dependency>
 
         <dependency>
             <groupId>junit</groupId>
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java
index 4fe009a..cb618a0 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java
@@ -27,6 +27,7 @@ import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.jackrabbit.util.ISO8601;
 import org.apache.sling.api.wrappers.ValueMapDecorator;
 
 /**
@@ -50,8 +51,16 @@ public class MockValueMap extends ValueMapDecorator {
     @SuppressWarnings("unchecked")
     @Override
     public <T> T get(String name, Class<T> type) {
-        if (type==Date.class) {
-            Calendar calendar = super.get(name, Calendar.class);
+        if (type == Calendar.class) {
+            // Support conversion of String to Calendar if value conforms to ISO8601 date format
+            Object value = get(name);
+            if (value instanceof String) {
+                return (T)ISO8601.parse((String)value);
+            }
+        }
+        else if (type == Date.class) {
+            // Support conversion from Calendar to Date
+            Calendar calendar = get(name, Calendar.class);
             if (calendar != null) {
                 return (T)calendar.getTime();
             }
@@ -59,8 +68,9 @@ public class MockValueMap extends ValueMapDecorator {
                 return null;
             }
         }
-        else if (type==InputStream.class) {
-            byte[] data = super.get(name, byte[].class);
+        else if (type == InputStream.class) {
+            // Support conversion from byte array to InputStream
+            byte[] data = get(name, byte[].class);
             if (data!=null) {
                 return (T)new ByteArrayInputStream(data);
             }
@@ -84,11 +94,13 @@ public class MockValueMap extends ValueMapDecorator {
     
     private static Object convertForWrite(Object value) {
         if (value instanceof Date) {
+            // Store Date values as Calendar values 
             Calendar calendar = Calendar.getInstance();
             calendar.setTime((Date)value);
             value = calendar;
         }
         else if (value instanceof InputStream) {
+            // Store InputStream values as byte array
             try {
                 value = IOUtils.toByteArray((InputStream)value);
             } catch (IOException ex) {
diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java b/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java
index 6893cf5..7c0f9c8 100644
--- a/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java
+++ b/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java
@@ -30,7 +30,9 @@ import java.util.Date;
 import java.util.List;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.jackrabbit.util.ISO8601;
 import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.ModifiableValueMap;
 import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
@@ -135,6 +137,18 @@ public class SlingCrudResourceResolverTest {
     }
     
     @Test
+    public void testStringToCalendarConversion() throws IOException {
+        Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
+        ModifiableValueMap modProps = resource1.adaptTo(ModifiableValueMap.class);
+        modProps.put("dateISO8601String", ISO8601.format(CALENDAR_VALUE));
+        resourceResolver.commit();
+        
+        resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
+        ValueMap props = ResourceUtil.getValueMap(resource1);
+        assertEquals(CALENDAR_VALUE.getTime(), props.get("calendarProp", Calendar.class).getTime());
+    }
+    
+    @Test
     public void testListChildren() throws IOException {
         Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
 

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.