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

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

commit 86dbe2173cc5f5030d6f482b77b78ce1f6ea089c
Author: Stefan Seifert <[email protected]>
AuthorDate: Sun Dec 11 13:12:06 2016 +0000

    SLING-6387 Allow to control autocommit mode for ContentLoader (patch 
provided by Dirk Rudolph, with modifications)
    
    git-svn-id: 
https://svn.apache.org/repos/asf/sling/branches/testing/mocks/sling-mock-1.x@1773570
 13f79535-47bb-0310-9956-ffa450edef68
---
 .../mock/sling/context/SlingContextImpl.java       |  24 +-
 .../testing/mock/sling/context/package-info.java   |   2 +-
 .../testing/mock/sling/junit/package-info.java     |   2 +-
 .../testing/mock/sling/loader/ContentLoader.java   |  25 ++-
 .../testing/mock/sling/loader/package-info.java    |   2 +-
 .../loader/ContentLoaderAutoCommitTest.java}       |  16 +-
 .../AbstractContentLoaderAutoCommitTest.java       | 242 +++++++++++++++++++++
 .../loader/ContentLoaderAutoCommitTest.java}       |  17 +-
 8 files changed, 310 insertions(+), 20 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
 
b/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
index 361e3e4..00a0e1b 100644
--- 
a/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
+++ 
b/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
@@ -94,6 +94,7 @@ public class SlingContextImpl extends OsgiContextImpl {
     protected MockSlingHttpServletResponse response;
     protected SlingScriptHelper slingScriptHelper;
     protected ContentLoader contentLoader;
+    protected ContentLoader contentLoaderAutoCommit;
     protected ContentBuilder contentBuilder;
     protected ResourceBuilder resourceBuilder;
     protected UniqueRoot uniqueRoot;
@@ -215,6 +216,7 @@ public class SlingContextImpl extends OsgiContextImpl {
         this.response = null;
         this.slingScriptHelper = null;
         this.contentLoader = null;
+        this.contentLoaderAutoCommit = null;
         this.contentBuilder = null;
         this.resourceBuilder = null;
         this.uniqueRoot = null;
@@ -292,10 +294,26 @@ public class SlingContextImpl extends OsgiContextImpl {
      * @return Content loader
      */
     public ContentLoader load() {
-        if (this.contentLoader == null) {
-            this.contentLoader = new ContentLoader(resourceResolver(), 
bundleContext());
+        return load(true);
+    }
+
+    /**
+     * @param autoCommit Automatically commit changes after loading content 
(default: true)
+     * @return Content loader
+     */
+    public ContentLoader load(boolean autoCommit) {
+        if (autoCommit) {
+            if (this.contentLoaderAutoCommit == null) {
+                this.contentLoaderAutoCommit = new 
ContentLoader(resourceResolver(), bundleContext(), true);
+            }
+            return this.contentLoaderAutoCommit;
+        }
+        else {
+            if (this.contentLoader == null) {
+                this.contentLoader = new ContentLoader(resourceResolver(), 
bundleContext(), false);
+            }
+            return this.contentLoader;
         }
-        return this.contentLoader;
     }
 
     /**
diff --git 
a/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java 
b/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java
index e7f08ad..cf0963d 100644
--- 
a/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java
+++ 
b/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java
@@ -19,5 +19,5 @@
 /**
  * Sling context implementation for unit tests.
  */
[email protected]("3.4")
[email protected]("3.5")
 package org.apache.sling.testing.mock.sling.context;
diff --git 
a/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java 
b/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java
index f8109b3..dd040c1 100644
--- a/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java
+++ b/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java
@@ -19,5 +19,5 @@
 /**
  * Rule for providing easy access to Sling context in JUnit tests.
  */
[email protected]("4.0")
[email protected]("4.1")
 package org.apache.sling.testing.mock.sling.junit;
diff --git 
a/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java 
b/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java
index 7729618..63fbadb 100644
--- 
a/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java
+++ 
b/src/main/java/org/apache/sling/testing/mock/sling/loader/ContentLoader.java
@@ -52,7 +52,7 @@ import com.google.common.collect.ImmutableSet;
 
 /**
  * Imports JSON data and binary data into Sling resource hierarchy.
- * After all import operations from json or binaries {@link 
ResourceResolver#commit()} is called.
+ * After all import operations from json or binaries {@link 
ResourceResolver#commit()} is called (when autocommit mode is active).
  */
 public final class ContentLoader {
 
@@ -77,6 +77,7 @@ public final class ContentLoader {
     private final ResourceResolver resourceResolver;
     private final BundleContext bundleContext;
     private final DateFormat calendarFormat;
+    private final boolean autoCommit;
 
     /**
      * @param resourceResolver Resource resolver
@@ -90,9 +91,19 @@ public final class ContentLoader {
      * @param bundleContext Bundle context
      */
     public ContentLoader(ResourceResolver resourceResolver, BundleContext 
bundleContext) {
+        this (resourceResolver, bundleContext, true);
+    }
+
+    /**
+     * @param resourceResolver Resource resolver
+     * @param bundleContext Bundle context
+     * @param autoCommit Automatically commit changes after loading content 
(default: true)
+     */
+    public ContentLoader(ResourceResolver resourceResolver, BundleContext 
bundleContext, boolean autoCommit) {
         this.resourceResolver = resourceResolver;
         this.bundleContext = bundleContext;
         this.calendarFormat = new 
SimpleDateFormat(JsonItemWriter.ECMA_DATE_FORMAT, 
JsonItemWriter.DATE_FORMAT_LOCALE);
+        this.autoCommit = autoCommit;
     }
 
     /**
@@ -175,7 +186,9 @@ public final class ContentLoader {
             String jsonString = convertToJsonString(inputStream).trim();
             JSONObject json = new JSONObject(jsonString);
             Resource resource = this.createResource(parentResource, childName, 
json);
-            resourceResolver.commit();
+            if (autoCommit) {
+                resourceResolver.commit();
+            }
             return resource;
         } catch (JSONException ex) {
             throw new RuntimeException(ex);
@@ -470,7 +483,9 @@ public final class ContentLoader {
             resourceResolver.create(file, JcrConstants.JCR_CONTENT,
                     ImmutableMap.<String, Object> 
builder().put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_RESOURCE)
                             .put(JcrConstants.JCR_DATA, 
inputStream).put(JcrConstants.JCR_MIMETYPE, mimeType).build());
-            resourceResolver.commit();
+            if (autoCommit) {
+                resourceResolver.commit();
+            }
             return file;
         } catch (PersistenceException ex) {
             throw new RuntimeException("Unable to create resource at " + 
parentResource.getPath() + "/" + name, ex);
@@ -586,7 +601,9 @@ public final class ContentLoader {
             Resource resource = resourceResolver.create(parentResource, name,
                     ImmutableMap.<String, Object> 
builder().put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_RESOURCE)
                             .put(JcrConstants.JCR_DATA, 
inputStream).put(JcrConstants.JCR_MIMETYPE, mimeType).build());
-            resourceResolver.commit();
+            if (autoCommit) {
+                resourceResolver.commit();
+            }
             return resource;
         } catch (PersistenceException ex) {
             throw new RuntimeException("Unable to create resource at " + 
parentResource.getPath() + "/" + name, ex);
diff --git 
a/src/main/java/org/apache/sling/testing/mock/sling/loader/package-info.java 
b/src/main/java/org/apache/sling/testing/mock/sling/loader/package-info.java
index 464320c..0851487 100644
--- a/src/main/java/org/apache/sling/testing/mock/sling/loader/package-info.java
+++ b/src/main/java/org/apache/sling/testing/mock/sling/loader/package-info.java
@@ -19,5 +19,5 @@
 /**
  * Helpers for importing test content into the mocked repositories / resource 
hierarchies.
  */
[email protected]("1.0")
[email protected]("1.1")
 package org.apache.sling.testing.mock.sling.loader;
diff --git 
a/src/main/java/org/apache/sling/testing/mock/sling/loader/package-info.java 
b/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/loader/ContentLoaderAutoCommitTest.java
similarity index 65%
copy from 
src/main/java/org/apache/sling/testing/mock/sling/loader/package-info.java
copy to 
src/test/java/org/apache/sling/testing/mock/sling/jcrmock/loader/ContentLoaderAutoCommitTest.java
index 464320c..bd76ee5 100644
--- a/src/main/java/org/apache/sling/testing/mock/sling/loader/package-info.java
+++ 
b/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/loader/ContentLoaderAutoCommitTest.java
@@ -16,8 +16,14 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-/**
- * Helpers for importing test content into the mocked repositories / resource 
hierarchies.
- */
[email protected]("1.0")
-package org.apache.sling.testing.mock.sling.loader;
+package org.apache.sling.testing.mock.sling.jcrmock.loader;
+
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import 
org.apache.sling.testing.mock.sling.loader.AbstractContentLoaderAutoCommitTest;
+
+public class ContentLoaderAutoCommitTest extends 
AbstractContentLoaderAutoCommitTest {
+
+    @Override protected ResourceResolverType getResourceResolverType() {
+        return ResourceResolverType.JCR_MOCK;
+    }
+}
diff --git 
a/src/test/java/org/apache/sling/testing/mock/sling/loader/AbstractContentLoaderAutoCommitTest.java
 
b/src/test/java/org/apache/sling/testing/mock/sling/loader/AbstractContentLoaderAutoCommitTest.java
new file mode 100644
index 0000000..b0df5fd
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/testing/mock/sling/loader/AbstractContentLoaderAutoCommitTest.java
@@ -0,0 +1,242 @@
+/*
+ * 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.testing.mock.sling.loader;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Rule;
+import org.junit.Test;
+
+public abstract class AbstractContentLoaderAutoCommitTest {
+
+    private static String DEST_RES_NAME = "dest";
+    private static String MIME_TYPE_JSON = "application/json";
+    private static String CLP_CONTENT = "/json-import-samples/content.json";
+    private static byte[] MEM_CONTENT = ("{"
+            + "\"jcr:primaryType\":\"sling:Folder\""
+            + "}").getBytes();
+
+    private int destResCount = 1;
+
+    @Rule
+    public SlingContext context = new SlingContext(getResourceResolverType());
+
+    protected abstract ResourceResolverType getResourceResolverType();
+
+    @Test
+    public void testJsonAutocommitExplicitly() {
+        doTestJsonAutocommitExplicitly(false, new HasChangesAssertion());
+        doTestJsonAutocommitExplicitly(true, new NoChangesAssertion());
+    }
+
+    private void doTestJsonAutocommitExplicitly(final boolean commit, final 
Runnable assertion) {
+        final ContentLoader loader = context.load(commit);
+
+        final Resource a1 = context.create().resource(nextDestResource());
+        loader.json(CLP_CONTENT, a1, "child");
+        assertion.run();
+
+        loader.json(CLP_CONTENT, nextDestResource());
+        assertion.run();
+
+        final InputStream data = new ByteArrayInputStream(MEM_CONTENT);
+        final Resource a3 = context.create().resource(nextDestResource());
+        loader.json(data, a3, "child");
+        assertion.run();
+
+        final InputStream data2 = new ByteArrayInputStream(MEM_CONTENT);
+        loader.json(data2, nextDestResource());
+        assertion.run();
+    }
+
+    @Test
+    public void testJsonAutocommitImplicitly() {
+        final ContentLoader loader = context.load();
+        final Runnable assertion = new NoChangesAssertion();
+
+        final Resource r = context.create().resource(nextDestResource());
+        loader.json(CLP_CONTENT, r, "child");
+        assertion.run();
+
+        loader.json(CLP_CONTENT, nextDestResource());
+        assertion.run();
+
+        final InputStream data = new ByteArrayInputStream(MEM_CONTENT);
+        final Resource r2 = context.create().resource(nextDestResource());
+        loader.json(data, r2, "child");
+        assertion.run();
+
+        final InputStream data2 = new ByteArrayInputStream(MEM_CONTENT);
+        loader.json(data2, nextDestResource());
+        assertion.run();
+    }
+
+    @Test
+    public void testBinaryFileAutocommitExplicitly() {
+        doTestBinaryFileAutocommitExplicitly(false, new HasChangesAssertion());
+        doTestBinaryFileAutocommitExplicitly(true, new NoChangesAssertion());
+    }
+
+    private void doTestBinaryFileAutocommitExplicitly(final boolean commit, 
final Runnable assertion) {
+        final ContentLoader loader = context.load(commit);
+
+        loader.binaryFile(CLP_CONTENT, nextDestResource());
+        assertion.run();
+
+        loader.binaryFile(CLP_CONTENT, nextDestResource(), MIME_TYPE_JSON);
+        assertion.run();
+
+        final InputStream data = new ByteArrayInputStream(MEM_CONTENT);
+        loader.binaryFile(data, nextDestResource());
+        assertion.run();
+
+        final InputStream data2 = new ByteArrayInputStream(MEM_CONTENT);
+        loader.binaryFile(data2, nextDestResource(), MIME_TYPE_JSON);
+        assertion.run();
+
+        final InputStream data3 = new ByteArrayInputStream(MEM_CONTENT);
+        final Resource r = context.create().resource(nextDestResource());
+        loader.binaryFile(data3, r, "child");
+        assertion.run();
+
+        final InputStream data4 = new ByteArrayInputStream(MEM_CONTENT);
+        final Resource r2 = context.create().resource(nextDestResource());
+        loader.binaryFile(data4, r2, "child", MIME_TYPE_JSON);
+        assertion.run();
+    }
+
+    @Test
+    public void testBinaryFileAutocommitImplicitly() {
+        final ContentLoader loader = context.load();
+        final Runnable assertion = new NoChangesAssertion();
+
+        loader.binaryFile(CLP_CONTENT, nextDestResource());
+        assertion.run();
+
+        loader.binaryFile(CLP_CONTENT, nextDestResource(), MIME_TYPE_JSON);
+        assertion.run();
+
+        final InputStream data = new ByteArrayInputStream(MEM_CONTENT);
+        loader.binaryFile(data, nextDestResource());
+        assertion.run();
+
+        final InputStream data2 = new ByteArrayInputStream(MEM_CONTENT);
+        loader.binaryFile(data2, nextDestResource(), MIME_TYPE_JSON);
+        assertion.run();
+
+        final InputStream data3 = new ByteArrayInputStream(MEM_CONTENT);
+        final Resource r = context.create().resource(nextDestResource());
+        loader.binaryFile(data3, r, "child");
+        assertion.run();
+
+        final InputStream data4 = new ByteArrayInputStream(MEM_CONTENT);
+        final Resource r2 = context.create().resource(nextDestResource());
+        loader.binaryFile(data4, r2, "child", MIME_TYPE_JSON);
+        assertion.run();
+    }
+
+    @Test
+    public void testBinaryResourceAutocommitExplicitly() {
+        doTestBinaryResourceAutocommitExplicitly(false, new 
HasChangesAssertion());
+        doTestBinaryResourceAutocommitExplicitly(true, new 
NoChangesAssertion());
+    }
+
+    private void doTestBinaryResourceAutocommitExplicitly(final boolean 
commit, final Runnable assertion) {
+        final ContentLoader loader = context.load(commit);
+
+        loader.binaryResource(CLP_CONTENT, nextDestResource());
+        assertion.run();
+
+        loader.binaryResource(CLP_CONTENT, nextDestResource(), MIME_TYPE_JSON);
+        assertion.run();
+
+        final InputStream data = new ByteArrayInputStream(MEM_CONTENT);
+        loader.binaryResource(data, nextDestResource());
+        assertion.run();
+
+        final InputStream data2 = new ByteArrayInputStream(MEM_CONTENT);
+        loader.binaryResource(data2, nextDestResource(), MIME_TYPE_JSON);
+        assertion.run();
+
+        final InputStream data3 = new ByteArrayInputStream(MEM_CONTENT);
+        final Resource r = context.create().resource(nextDestResource());
+        loader.binaryResource(data3, r, "child");
+        assertion.run();
+
+        final InputStream data4 = new ByteArrayInputStream(MEM_CONTENT);
+        final Resource r2 = context.create().resource(nextDestResource());
+        loader.binaryResource(data4, r2, "child", MIME_TYPE_JSON);
+        assertion.run();
+    }
+
+    @Test
+    public void testBinaryResourceAutocommitImplicitly() {
+        final ContentLoader loader = context.load();
+        final Runnable assertion = new NoChangesAssertion();
+
+        loader.binaryResource(CLP_CONTENT, nextDestResource());
+        assertion.run();
+
+        loader.binaryResource(CLP_CONTENT, nextDestResource(), MIME_TYPE_JSON);
+        assertion.run();
+
+        final InputStream data = new ByteArrayInputStream(MEM_CONTENT);
+        loader.binaryResource(data, nextDestResource());
+        assertion.run();
+
+        final InputStream data2 = new ByteArrayInputStream(MEM_CONTENT);
+        loader.binaryResource(data2, nextDestResource(), MIME_TYPE_JSON);
+        assertion.run();
+
+        final InputStream data3 = new ByteArrayInputStream(MEM_CONTENT);
+        final Resource r = context.create().resource(nextDestResource());
+        loader.binaryResource(data3, r, "child");
+        assertion.run();
+
+        final InputStream data4 = new ByteArrayInputStream(MEM_CONTENT);
+        final Resource r2 = context.create().resource(nextDestResource());
+        loader.binaryResource(data4, r2, "child", MIME_TYPE_JSON);
+        assertion.run();
+    }
+
+    private synchronized String nextDestResource() {
+        return '/' + DEST_RES_NAME + destResCount++;
+    }
+
+    private class HasChangesAssertion implements Runnable {
+        @Override
+        public void run() {
+            assertTrue(context.resourceResolver().hasChanges());
+        }
+    }
+
+    private class NoChangesAssertion implements Runnable {
+        @Override
+        public void run() {
+            assertFalse(context.resourceResolver().hasChanges());
+        }
+    }
+}
diff --git 
a/src/main/java/org/apache/sling/testing/mock/sling/loader/package-info.java 
b/src/test/java/org/apache/sling/testing/mock/sling/rrmock/loader/ContentLoaderAutoCommitTest.java
similarity index 64%
copy from 
src/main/java/org/apache/sling/testing/mock/sling/loader/package-info.java
copy to 
src/test/java/org/apache/sling/testing/mock/sling/rrmock/loader/ContentLoaderAutoCommitTest.java
index 464320c..21a48d6 100644
--- a/src/main/java/org/apache/sling/testing/mock/sling/loader/package-info.java
+++ 
b/src/test/java/org/apache/sling/testing/mock/sling/rrmock/loader/ContentLoaderAutoCommitTest.java
@@ -16,8 +16,15 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-/**
- * Helpers for importing test content into the mocked repositories / resource 
hierarchies.
- */
[email protected]("1.0")
-package org.apache.sling.testing.mock.sling.loader;
+package org.apache.sling.testing.mock.sling.rrmock.loader;
+
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import 
org.apache.sling.testing.mock.sling.loader.AbstractContentLoaderAutoCommitTest;
+
+public class ContentLoaderAutoCommitTest extends 
AbstractContentLoaderAutoCommitTest {
+
+    @Override
+    protected ResourceResolverType getResourceResolverType() {
+        return ResourceResolverType.RESOURCERESOLVER_MOCK;
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to