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/10/18 23:25:21 UTC

[sling-org-apache-sling-resource-inventory] 24/33: SLING-6468 - Improve code coverage for Sling Inventory Printer

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

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resource-inventory.git

commit 1239ad946231dbc5bb29a75d08ee6f4719db4850
Author: Timothée Maret <tm...@apache.org>
AuthorDate: Mon Jan 16 13:51:21 2017 +0000

    SLING-6468 - Improve code coverage for Sling Inventory Printer
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1779030 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  19 +++
 .../inventory/impl/JsonObjectCreatorTest.java      | 131 +++++++++++++++++++++
 .../inventory/impl/ResourceTraversorTest.java      |  71 +++++++++++
 3 files changed, 221 insertions(+)

diff --git a/pom.xml b/pom.xml
index 8e046fe..9cd4bb1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -94,5 +94,24 @@
             <version>3.0</version>
             <scope>provided</scope>
         </dependency>
+        <!-- TESTING -->
+
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.testing.resourceresolver-mock</artifactId>
+            <version>1.1.16</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-all</artifactId>
+            <version>1.9.5</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/src/test/java/org/apache/sling/resource/inventory/impl/JsonObjectCreatorTest.java b/src/test/java/org/apache/sling/resource/inventory/impl/JsonObjectCreatorTest.java
new file mode 100644
index 0000000..4b072b0
--- /dev/null
+++ b/src/test/java/org/apache/sling/resource/inventory/impl/JsonObjectCreatorTest.java
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.resource.inventory.impl;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.commons.json.JSONArray;
+import org.apache.sling.commons.json.JSONObject;
+import org.apache.sling.testing.resourceresolver.MockHelper;
+import org.apache.sling.testing.resourceresolver.MockResource;
+import org.apache.sling.testing.resourceresolver.MockResourceResolverFactory;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class JsonObjectCreatorTest {
+
+    private ResourceResolver resolver;
+
+    @Before
+    public void setup() throws LoginException {
+        resolver = new MockResourceResolverFactory()
+                .getAdministrativeResourceResolver(null);
+    }
+
+    @Test
+    public void testCreate() throws Exception {
+
+        Map<String, Object> properties = new HashMap<String, Object>();
+        properties.put("byte", 0x0A);
+        properties.put("int", 10);
+        properties.put("long", 10L);
+        properties.put("float", 10.0f);
+        properties.put("double", 10.0d);
+        properties.put("string", "10");
+        properties.put("boolean", false);
+        properties.put("object", new Object(){
+            public String toString() {
+                return "object";
+            }
+        });
+        Resource resource = new MockResource("/some/path", properties, resolver);
+
+        JSONObject json = JsonObjectCreator.create(resource);
+
+        assertEquals(10, json.get("byte"));
+        assertEquals(10, json.get("int"));
+        assertEquals("10.0", json.get("float"));
+        assertEquals("10.0", json.get("double"));
+        assertEquals("10", json.get("string"));
+        assertEquals(false, json.get("boolean"));
+        assertEquals("object", json.get("object"));
+    }
+
+    @Test
+    public void testCreateArray() throws Exception {
+
+        Map<String, Object> properties = new HashMap<String, Object>();
+        properties.put("emptyArray", new Object[]{});
+        properties.put("stringArray", new String[]{"10","100"});
+        properties.put("intArray", new int[]{10, 100});
+        properties.put("doubleArray", new double[]{10d, 100d});
+        properties.put("byteArray", new byte[]{0x0A, 0x64});
+        properties.put("floatArray", new float[]{10.0f, 100.0f});
+        properties.put("shortArray", new short[]{10, 100});
+        properties.put("longArray", new long[]{10, 100});
+        properties.put("booleanArray", new boolean[]{true, false});
+        properties.put("charArray", new char[]{'a', 'b'});
+        Resource resource = new MockResource("/some/path", properties, resolver);
+
+        JSONObject json = JsonObjectCreator.create(resource);
+        assertEquals(0, json.getJSONArray("emptyArray").length());
+        JSONArray array;
+        array = json.getJSONArray("stringArray");
+        assertEquals("10", array.get(0));
+        array = json.getJSONArray("intArray");
+        assertEquals(10, array.get(0));
+        array = json.getJSONArray("doubleArray");
+        assertEquals("10.0", array.get(0));
+        array = json.getJSONArray("byteArray");
+        assertEquals("10", array.get(0));
+        array = json.getJSONArray("floatArray");
+        assertEquals("10.0", array.get(0));
+        array = json.getJSONArray("shortArray");
+        assertEquals("10", array.get(0));
+        array = json.getJSONArray("longArray");
+        assertEquals(10L, array.get(0));
+        array = json.getJSONArray("booleanArray");
+        assertEquals(true, array.get(0));
+        array = json.getJSONArray("charArray");
+        assertEquals("a", array.get(0));
+
+    }
+
+    @Test
+    public void testCreateDeep() throws Exception {
+
+        MockHelper.create(resolver)
+                .resource("/some")
+                .p("p1", "v1")
+                .resource("/some/path")
+                .p("p2", "v2")
+                .commit();
+        Resource resource = resolver.getResource("/some");
+
+        JSONObject json = JsonObjectCreator.create(resource);
+        assertEquals("v1", json.get("p1"));
+        JSONObject path = json.getJSONObject("path");
+        assertEquals("v2", path.get("p2"));
+
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/resource/inventory/impl/ResourceTraversorTest.java b/src/test/java/org/apache/sling/resource/inventory/impl/ResourceTraversorTest.java
new file mode 100644
index 0000000..4a6a767
--- /dev/null
+++ b/src/test/java/org/apache/sling/resource/inventory/impl/ResourceTraversorTest.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.resource.inventory.impl;
+
+import java.util.Collections;
+
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.commons.json.JSONObject;
+import org.apache.sling.testing.resourceresolver.MockHelper;
+import org.apache.sling.testing.resourceresolver.MockResource;
+import org.apache.sling.testing.resourceresolver.MockResourceResolverFactory;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class ResourceTraversorTest {
+
+    private ResourceResolver resolver;
+
+    @Before
+    public void setup() throws LoginException {
+        resolver = new MockResourceResolverFactory()
+                .getAdministrativeResourceResolver(null);
+    }
+
+    @Test
+    public void testCollectResources() throws Exception {
+        MockHelper.create(resolver)
+                .resource("/some")
+                .p("p1", "v1")
+                .resource("/some/path")
+                .p("p2", "v2")
+                .commit();
+        Resource resource = resolver.getResource("/some");
+        ResourceTraversor traversor = new ResourceTraversor(resource);
+        traversor.collectResources();
+        JSONObject json = traversor.getJSONObject();
+        assertEquals("v1", json.get("p1"));
+        JSONObject path = json.getJSONObject("path");
+        assertNotNull(path);
+        assertEquals("v2", path.get("p2"));
+
+    }
+
+    @Test
+    public void testGetJSONObject() throws Exception {
+        Resource resource = new MockResource("/some/path", Collections.<String, Object>singletonMap("p1", "v1"), resolver);
+        JSONObject json = new ResourceTraversor(resource).getJSONObject();
+        assertEquals("v1", json.get("p1"));
+
+    }
+
+}
\ No newline at end of file

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