You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2015/01/15 17:59:44 UTC

svn commit: r1652172 - /sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/sling/JsonObjectCreatorTest.java

Author: bdelacretaz
Date: Thu Jan 15 16:59:44 2015
New Revision: 1652172

URL: http://svn.apache.org/r1652172
Log:
SLING-4258 - basic tests added

Modified:
    sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/sling/JsonObjectCreatorTest.java

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=1652172&r1=1652171&r2=1652172&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 Thu Jan 15 16:59:44 2015
@@ -20,10 +20,15 @@ import static org.junit.Assert.assertEqu
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.when;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.UUID;
 
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
@@ -44,23 +49,78 @@ public class JsonObjectCreatorTest {
     private Resource resource;
     
     @Mock
-    private ResourceResolver resourceResolver;  
+    private ResourceResolver resourceResolver;
+    
+    private Map<String, Object> props;
+    private static final String RESOURCE_NAME = "testResource";  
+    private static final String PATH = "/" + RESOURCE_NAME;
+    
+    private static final Object SAME = new Object();
     
     @Before
     public void setup() {
-        final Map<String, Object> props = new HashMap<String, Object>();
-        props.put("foo",  "bar");
-        final ValueMap values = new ValueMapDecorator(props);
+        props = new HashMap<String, Object>();
         
-        when(resource.getResourceResolver()).thenReturn(resourceResolver);
-        when(resource.adaptTo(ValueMap.class)).thenReturn(values);
         final List<Resource> children = new ArrayList<Resource>();
         when(resourceResolver.listChildren(any(Resource.class))).thenReturn(children.iterator());
+        when(resource.getResourceResolver()).thenReturn(resourceResolver);
+        when(resource.getPath()).thenReturn(PATH);
+    }
+    
+    private void assertGet(Object data) throws JSONException {
+        assertGet(data, SAME);
+    }
+    
+    private void assertGet(Object data, Object expected) throws JSONException {
+        final String key = UUID.randomUUID().toString();
+        props.put(key, data);
+        when(resource.adaptTo(ValueMap.class)).thenReturn(new ValueMapDecorator(props));
+        final JSONObject j = JsonObjectCreator.create(resource, 1);
+        
+        final String getKey = data instanceof InputStream ? ":"  + key : key;
+        assertEquals(expected == SAME ? data : expected, j.get(getKey));
+    }
+    
+    @Test
+    public void testSimpleTypes() throws JSONException {
+        assertGet("bar");
+        assertGet(true);
+        assertGet(123);
+        assertGet(456.78);
+        assertGet(System.currentTimeMillis());
+    }
+    
+    @Test
+    public void testStringValue() throws JSONException {
+        final String value = "the string";
+        when(resource.adaptTo(String.class)).thenReturn(value);
+        final JSONObject j = JsonObjectCreator.create(resource, 1);
+        assertEquals(value, j.get(RESOURCE_NAME));
     }
     
     @Test
-    public void testBasicJSON() throws JSONException {
+    public void testStringArray() throws JSONException {
+        final String [] values = { "A", "B" };
+        when(resource.adaptTo(String[].class)).thenReturn(values);
         final JSONObject j = JsonObjectCreator.create(resource, 1);
-        assertEquals("bar", j.get("foo"));
+        assertEquals("A", j.getJSONArray(RESOURCE_NAME).get(0));
+        assertEquals("B", j.getJSONArray(RESOURCE_NAME).get(1));
+    }
+    
+    @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).format(nowCalendar.getTime());
+        assertGet(nowCalendar, nowString);
+    }
+    
+    @Test
+    public void testStream() throws JSONException {
+        final byte [] bytes = "Hello there".getBytes();
+        final InputStream stream = new ByteArrayInputStream(bytes);
+        // TODO not sure why we don't get the actual length here
+        assertGet(stream, -1L);
     }
+    
 }