You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by st...@apache.org on 2012/04/25 14:33:25 UTC

svn commit: r1330239 - in /jackrabbit/oak/trunk/oak-it/mk: pom.xml src/main/java/org/apache/jackrabbit/mk/test/AbstractMicroKernelIT.java src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java

Author: stefan
Date: Wed Apr 25 12:33:24 2012
New Revision: 1330239

URL: http://svn.apache.org/viewvc?rev=1330239&view=rev
Log:
OAK-12: Implement a test suite for the MicroKernel (WIP)

Modified:
    jackrabbit/oak/trunk/oak-it/mk/pom.xml
    jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/AbstractMicroKernelIT.java
    jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java

Modified: jackrabbit/oak/trunk/oak-it/mk/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-it/mk/pom.xml?rev=1330239&r1=1330238&r2=1330239&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-it/mk/pom.xml (original)
+++ jackrabbit/oak/trunk/oak-it/mk/pom.xml Wed Apr 25 12:33:24 2012
@@ -41,6 +41,11 @@
       <version>${project.version}</version>
     </dependency>
     <dependency>
+      <groupId>com.googlecode.json-simple</groupId>
+      <artifactId>json-simple</artifactId>
+      <version>1.1</version>
+    </dependency>
+    <dependency>
       <groupId>ch.qos.logback</groupId>
       <artifactId>logback-classic</artifactId>
       <version>1.0.1</version>

Modified: jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/AbstractMicroKernelIT.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/AbstractMicroKernelIT.java?rev=1330239&r1=1330238&r2=1330239&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/AbstractMicroKernelIT.java (original)
+++ jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/AbstractMicroKernelIT.java Wed Apr 25 12:33:24 2012
@@ -21,10 +21,16 @@ import java.util.Collection;
 import java.util.ServiceLoader;
 
 import org.apache.jackrabbit.mk.api.MicroKernel;
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.runners.Parameterized.Parameters;
 
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
 /**
  * Abstract base class for {@link MicroKernel} integration tests.
  */
@@ -67,6 +73,13 @@ public abstract class AbstractMicroKerne
     protected MicroKernel mk;
 
     /**
+     * A JSON parser instance that can be used for parsing JSON-format data;
+     * {@code JSONParser} instances are not <i>not</i> thread-safe.
+     * @see #getJSONParser()
+     */
+    private JSONParser parser;
+
+    /**
      * Creates a {@link MicroKernel} test case for a cluster of the given
      * size created using the given {@link MicroKernelFixture} service.
      *
@@ -110,4 +123,57 @@ public abstract class AbstractMicroKerne
         fixture.tearDownCluster(mks);
     }
 
+    /**
+     * Returns a {@code JSONParser} instance for parsing JSON format data.
+     * This method returns a cached instance.
+     * <p/>
+     * {@code JSONParser} instances are <i>not</i> thread-safe. Multi-threaded
+     * unit tests should therefore override this method and return a fresh
+     * instance on every invocation.
+     *
+     * @return a {@code JSONParser} instance
+     */
+    protected synchronized JSONParser getJSONParser() {
+        if (parser == null) {
+            parser = new JSONParser();
+        }
+        return parser;
+    }
+
+    //--------------------------------< utility methods for parsing json data >
+    /**
+     * Parses the provided string into a {@code JSONObject}.
+     *
+     * @param json string to be parsed
+     * @return a {@code JSONObject}
+     * @throws {@code AssertionError} if the string cannot be parsed into a {@code JSONObject}
+     */
+    protected JSONObject parseJSONObject(String json) throws AssertionError {
+        JSONParser parser = getJSONParser();
+        try {
+            Object obj = parser.parse(json);
+            assertTrue(obj instanceof JSONObject);
+            return (JSONObject) obj;
+        } catch (Exception e) {
+            throw new AssertionError("not a valid JSON object: " + e.getMessage());
+        }
+    }
+
+    /**
+     * Parses the provided string into a {@code JSONObject}.
+     *
+     * @param json string to be parsed
+     * @return a {@code JSONArray}
+     * @throws {@code AssertionError} if the string cannot be parsed into a {@code JSONArray}
+     */
+    protected JSONArray parseJSONArray(String json) throws AssertionError {
+        JSONParser parser = getJSONParser();
+        try {
+            Object obj = parser.parse(json);
+            assertTrue(obj instanceof JSONArray);
+            return (JSONArray) obj;
+        } catch (Exception e) {
+            throw new AssertionError("not a valid JSON array: " + e.getMessage());
+        }
+    }
 }

Modified: jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java?rev=1330239&r1=1330238&r2=1330239&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java (original)
+++ jackrabbit/oak/trunk/oak-it/mk/src/main/java/org/apache/jackrabbit/mk/test/MicroKernelIT.java Wed Apr 25 12:33:24 2012
@@ -16,9 +16,9 @@
  */
 package org.apache.jackrabbit.mk.test;
 
-import junit.framework.Assert;
-
 import org.apache.jackrabbit.mk.api.MicroKernelException;
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
@@ -68,8 +68,8 @@ public class MicroKernelIT extends Abstr
     public void getNodes() {
         String head = mk.getHeadRevision();
 
-        String json = mk.getNodes("/test", head, 0, 0, -1, null);
-        assertTrue(json.contains("stringProp"));
+        JSONObject jsonObj = parseJSONObject(mk.getNodes("/test", head, 0, 0, -1, null));
+        assertTrue(jsonObj.containsKey("stringProp"));
     }
 
     @Test
@@ -88,14 +88,14 @@ public class MicroKernelIT extends Abstr
 
         try {
             mk.nodeExists("/test", nonExistingRev);
-            Assert.fail("Success with non-existing revision: " + nonExistingRev);
+            fail("Success with non-existing revision: " + nonExistingRev);
         } catch (MicroKernelException e) {
             // expected
         }
 
         try {
             mk.getNodes("/test", nonExistingRev, 0, 0, -1, null);
-            Assert.fail("Success with non-existing revision: " + nonExistingRev);
+            fail("Success with non-existing revision: " + nonExistingRev);
         } catch (MicroKernelException e) {
             // expected
         }
@@ -109,7 +109,7 @@ public class MicroKernelIT extends Abstr
         try {
             String path = "/test/";
             mk.getNodes(path, head);
-            Assert.fail("Success with invalid path: " + path);
+            fail("Success with invalid path: " + path);
         } catch (IllegalArgumentException e) {
             // expected
         } catch (MicroKernelException e) {
@@ -303,9 +303,14 @@ public class MicroKernelIT extends Abstr
         // add a node /branch/foo in branchRev
         branchRev = mk.commit("", "+\"/branch/foo\":{}", branchRev, "");
 
-        String hist = mk.getRevisionHistory(0, -1);
         // make sure branchRev doesn't show up in revision history
-        assertFalse(hist.contains(branchRev));
+        String hist = mk.getRevisionHistory(0, -1);
+        JSONArray ar = parseJSONArray(hist);
+        for (Object entry : ar) {
+            assertTrue(entry instanceof JSONObject);
+            JSONObject rev = (JSONObject) entry;
+            assertFalse(branchRev.equals(rev.get("id")));
+        }
 
         // add a node /test123 in head
         mk.commit("", "+\"/test123\":{}", null, "");