sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romb...@apache.org
Subject [sling-org-apache-sling-testing-sling-mock] 18/37: SLING-5067 sling-mock: "uniqueRoot()" to simplify creation and cleanup of unique root paths in repository
Date Tue, 07 Nov 2017 10:23:34 GMT
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.6.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git

commit 8c79b8109abc0ee3f7668982a8cb1fa721b27e5e
Author: Stefan Seifert <sseifert@apache.org>
AuthorDate: Sun Sep 27 09:46:11 2015 +0000

    SLING-5067 sling-mock: "uniqueRoot()" to simplify creation and cleanup of unique root
paths in repository
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1705520
13f79535-47bb-0310-9956-ffa450edef68
---
 .../mock/sling/context/SlingContextImpl.java       |  18 ++-
 .../testing/mock/sling/context/UniqueRoot.java     | 143 +++++++++++++++++++++
 .../testing/mock/sling/junit/package-info.java     |   2 +-
 .../sling/jcrmock/resource/UniqueRootTest.java}    |  18 ++-
 .../sling/resource/AbstractJcrNamespaceTest.java   |   7 +-
 .../resource/AbstractJcrResourceResolverTest.java  |   3 +-
 .../sling/resource/AbstractUniqueRootTest.java     |  57 ++++++++
 .../sling/rrmock/resource/UniqueRootTest.java}     |  18 ++-
 8 files changed, 250 insertions(+), 16 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 2370420..8c31c06 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
@@ -77,6 +77,7 @@ public class SlingContextImpl extends OsgiContextImpl {
     protected SlingScriptHelper slingScriptHelper;
     protected ContentLoader contentLoader;
     protected ContentBuilder contentBuilder;
+    protected UniqueRoot uniqueRoot;
 
     /**
      * @param resourceResolverType Resource resolver type
@@ -136,8 +137,9 @@ public class SlingContextImpl extends OsgiContextImpl {
      * Teardown actions after test method execution
      */
     protected void tearDown() {
-
+        
         if (this.resourceResolver != null) {
+            
             // revert potential unsaved changes in resource resolver/JCR session
             this.resourceResolver.revert();
             Session session = this.resourceResolver.adaptTo(Session.class);
@@ -148,6 +150,13 @@ public class SlingContextImpl extends OsgiContextImpl {
                     // ignore
                 }
             }
+            
+            // remove unique roots
+            if (this.uniqueRoot != null) {
+                this.uniqueRoot.cleanUp();
+            }
+            
+            // close resource resolver
             this.resourceResolver.close();
         }
 
@@ -305,5 +314,12 @@ public class SlingContextImpl extends OsgiContextImpl {
             slingSettings.setRunModes(newRunModes);
         }
     }
+    
+    public UniqueRoot uniqueRoot() {
+        if (uniqueRoot == null) {
+            uniqueRoot = new UniqueRoot(this);
+        }
+        return uniqueRoot;
+    }
 
 }
diff --git a/src/main/java/org/apache/sling/testing/mock/sling/context/UniqueRoot.java b/src/main/java/org/apache/sling/testing/mock/sling/context/UniqueRoot.java
new file mode 100644
index 0000000..20f836b
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/mock/sling/context/UniqueRoot.java
@@ -0,0 +1,143 @@
+/*
+ * 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.context;
+
+import static org.apache.sling.jcr.resource.JcrResourceConstants.NT_SLING_ORDERED_FOLDER;
+
+import java.util.UUID;
+
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.sling.api.resource.PersistenceException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.ImmutableMap;
+
+/**
+ * Manages unique root paths in JCR repository.
+ * This is important for resource resolver types like JCR_JACKRABBIT and JCR_OAK 
+ * where the repository is not cleand for each test run. This class provides
+ * unique root paths for each run, and cleans them up when done.
+ */
+public class UniqueRoot {
+    
+    private final SlingContextImpl context;
+    
+    protected final String uniquePathPart;
+    
+    private Resource contentRoot;
+    private Resource appsRoot;
+    private Resource libsRoot;
+    
+    private static final Logger log = LoggerFactory.getLogger(UniqueRoot.class);
+    
+    protected UniqueRoot(SlingContextImpl context) {
+        this.context = context;
+        // generate unique path part by using a UUID
+        uniquePathPart = UUID.randomUUID().toString();
+    }
+    
+    /**
+     * Get or create resource with given JCR primary type
+     * @param path Path
+     * @param primaryType JCR primary type
+     * @return Resource (never null)
+     */
+    protected final Resource getOrCreateResource(String path, String primaryType) {
+        try {
+            return ResourceUtil.getOrCreateResource(context.resourceResolver(), path, 
+                    ImmutableMap.<String,Object>of(JcrConstants.JCR_PRIMARYTYPE, primaryType),
+                    null, true);
+        }
+        catch (PersistenceException ex) {
+            throw new RuntimeException("Unable to create resource at " + path + ": " + ex.getMessage(),
ex);
+        }
+    }
+    
+    /**
+     * Gets (and creates if required) a unique path at <code>/content/xxx</code>.
+     * The path (incl. all children) is automatically removed when the unit test completes.
+     * @return Unique content path
+     */
+    public final String content() {
+        if (contentRoot == null) {
+            contentRoot = getOrCreateResource("/content/" + uniquePathPart, NT_SLING_ORDERED_FOLDER);
+        }
+        return contentRoot.getPath();
+    }
+        
+    /**
+     * Gets (and creates if required) a unique path at <code>/apps/xxx</code>.
+     * The path (incl. all children) is automatically removed when the unit test completes.
+     * @return Unique content path
+     */
+    public final String apps() {
+        if (appsRoot == null) {
+            appsRoot = getOrCreateResource("/apps/" + uniquePathPart, NT_SLING_ORDERED_FOLDER);
+        }
+        return appsRoot.getPath();
+    }
+        
+    /**
+     * Gets (and creates if required) a unique path at <code>/libs/xxx</code>.
+     * The path (incl. all children) is automatically removed when the unit test completes.
+     * @return Unique content path
+     */
+    public final String libs() {
+        if (libsRoot == null) {
+            libsRoot = getOrCreateResource("/libs/" + uniquePathPart, NT_SLING_ORDERED_FOLDER);
+        }
+        return libsRoot.getPath();
+    }
+    
+    /**
+     * Cleanup is called when the unit test rule completes a unit test run.
+     * All resources created have to be removed.
+     */
+    protected void cleanUp() {
+        deleteResources(contentRoot, appsRoot, libsRoot);
+    }
+    
+    /**
+     * Deletes the given set of resources and commits afterwards.
+     * @param resources Resources to be deleted
+     */
+    protected final void deleteResources(Resource... resources) {
+        for (Resource resource : resources) {
+            if (resource != null) {
+                try {
+                    context.resourceResolver().delete(resource);
+                }
+                catch (PersistenceException ex) {
+                    log.warn("Unable to delete root path " + resource.getPath(), ex);
+                }
+            }
+        }
+        try {
+            context.resourceResolver().commit();
+        }
+        catch (PersistenceException ex) {
+            log.warn("Unable to commit root path deletions.", ex);
+        }
+            
+    }
+    
+}
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 f60a75d..b7888e3 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.
  */
-@aQute.bnd.annotation.Version("3.0")
+@aQute.bnd.annotation.Version("3.1")
 package org.apache.sling.testing.mock.sling.junit;
diff --git a/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java b/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/UniqueRootTest.java
similarity index 66%
copy from src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java
copy to src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/UniqueRootTest.java
index f60a75d..73c6793 100644
--- a/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java
+++ b/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/UniqueRootTest.java
@@ -16,8 +16,16 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-/**
- * Rule for providing easy access to Sling context in JUnit tests.
- */
-@aQute.bnd.annotation.Version("3.0")
-package org.apache.sling.testing.mock.sling.junit;
+package org.apache.sling.testing.mock.sling.jcrmock.resource;
+
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.apache.sling.testing.mock.sling.resource.AbstractUniqueRootTest;
+
+public class UniqueRootTest extends AbstractUniqueRootTest {
+
+    @Override
+    protected ResourceResolverType getResourceResolverType() {
+        return ResourceResolverType.JCR_MOCK;
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractJcrNamespaceTest.java
b/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractJcrNamespaceTest.java
index 297073d..5185419 100644
--- a/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractJcrNamespaceTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractJcrNamespaceTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.testing.mock.sling.resource;
 
+import static org.apache.sling.jcr.resource.JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY;
 import static org.junit.Assert.assertEquals;
 
 import javax.jcr.NamespaceRegistry;
@@ -58,7 +59,7 @@ public abstract class AbstractJcrNamespaceTest {
         Resource resource = resolver.getResource("/content/foo");
         
         ValueMap props = ResourceUtil.getValueMap(resource);
-        assertEquals("fooType", props.get("sling:resourceType"));
+        assertEquals("fooType", props.get(SLING_RESOURCE_TYPE_PROPERTY));
         assertEquals("fooType", resource.getResourceType());
     }
 
@@ -72,7 +73,7 @@ public abstract class AbstractJcrNamespaceTest {
         Resource resource = resolver.getResource("/content/foo");
         
         ValueMap props = ResourceUtil.getValueMap(resource);
-        assertEquals("fooType", props.get("sling:resourceType"));
+        assertEquals("fooType", props.get(SLING_RESOURCE_TYPE_PROPERTY));
         
         // since SLING-4773 sling namespace is readly registered in the MockJcrResourceResolverAdapter,
so this will still work here
         assertEquals("fooType", resource.getResourceType());
@@ -88,7 +89,7 @@ public abstract class AbstractJcrNamespaceTest {
         Resource resource = resolver.getResource("/content/foo");
         
         ValueMap props = ResourceUtil.getValueMap(resource);
-        assertEquals("fooType", props.get("sling:resourceType"));
+        assertEquals("fooType", props.get(SLING_RESOURCE_TYPE_PROPERTY));
         assertEquals("fooType", resource.getResourceType());
     }
 
diff --git a/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractJcrResourceResolverTest.java
b/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractJcrResourceResolverTest.java
index 2681fdd..8012308 100644
--- a/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractJcrResourceResolverTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractJcrResourceResolverTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.testing.mock.sling.resource;
 
+import static org.apache.sling.jcr.resource.JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -162,7 +163,7 @@ public abstract class AbstractJcrResourceResolverTest {
         Resource parent = resourceResolver.getResource(getTestRootNode().getPath());
 
         Resource child = resourceResolver.create(parent, "nodeTypeResource", ImmutableMap.<String,
Object> builder()
-                .put("sling:resourceType", JcrConstants.NT_UNSTRUCTURED).build());
+                .put(SLING_RESOURCE_TYPE_PROPERTY, JcrConstants.NT_UNSTRUCTURED).build());
         assertNotNull(child);
         assertEquals(JcrConstants.NT_UNSTRUCTURED, child.getResourceType());
         assertEquals(JcrConstants.NT_UNSTRUCTURED, child.adaptTo(Node.class).getPrimaryNodeType().getName());
diff --git a/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractUniqueRootTest.java
b/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractUniqueRootTest.java
new file mode 100644
index 0000000..0ddd658
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractUniqueRootTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.resource;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+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 AbstractUniqueRootTest {
+    
+    @Rule
+    public SlingContext context = new SlingContext(getResourceResolverType());
+
+    protected abstract ResourceResolverType getResourceResolverType();
+
+    @Test
+    public void testContent() {
+        String path = context.uniqueRoot().content();
+        assertNotNull(context.resourceResolver().getResource(path));
+        assertTrue(path.matches("^/content/[^/]+"));
+    }
+
+    @Test
+    public void testApps() throws Exception {
+        String path = context.uniqueRoot().apps();
+        assertNotNull(context.resourceResolver().getResource(path));
+        assertTrue(path.matches("^/apps/[^/]+"));
+    }
+
+    @Test
+    public void testLibs() throws Exception {
+        String path = context.uniqueRoot().libs();
+        assertNotNull(context.resourceResolver().getResource(path));
+        assertTrue(path.matches("^/libs/[^/]+"));
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java b/src/test/java/org/apache/sling/testing/mock/sling/rrmock/resource/UniqueRootTest.java
similarity index 66%
copy from src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java
copy to src/test/java/org/apache/sling/testing/mock/sling/rrmock/resource/UniqueRootTest.java
index f60a75d..879459d 100644
--- a/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java
+++ b/src/test/java/org/apache/sling/testing/mock/sling/rrmock/resource/UniqueRootTest.java
@@ -16,8 +16,16 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-/**
- * Rule for providing easy access to Sling context in JUnit tests.
- */
-@aQute.bnd.annotation.Version("3.0")
-package org.apache.sling.testing.mock.sling.junit;
+package org.apache.sling.testing.mock.sling.rrmock.resource;
+
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.apache.sling.testing.mock.sling.resource.AbstractUniqueRootTest;
+
+public class UniqueRootTest extends AbstractUniqueRootTest {
+
+    @Override
+    protected ResourceResolverType getResourceResolverType() {
+        return ResourceResolverType.RESOURCERESOLVER_MOCK;
+    }
+
+}

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

Mime
View raw message