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/11/07 10:21:54 UTC

[sling-org-apache-sling-testing-sling-mock] 04/19: SLING-4362 make sure JCR namespaces are correctly registered in JCR mock on initialization through sling mock

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.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git

commit e8d2807282fd7137f2d44db069277af5c2acccc3
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Mon Feb 2 11:25:43 2015 +0000

    SLING-4362 make sure JCR namespaces are correctly registered in JCR mock on initialization through sling mock
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1656442 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  2 +-
 .../context/ContextResourceResolverFactory.java    |  1 +
 ...urceResolverTest.java => JcrNamespaceTest.java} |  9 +-
 .../jcrmock/resource/JcrResourceResolverTest.java  |  1 +
 .../resource/SlingCrudResourceResolverTest.java    |  1 +
 .../sling/resource/AbstractJcrNamespaceTest.java   | 95 ++++++++++++++++++++++
 .../resources/json-import-samples/SLING-4362.json  |  5 ++
 7 files changed, 107 insertions(+), 7 deletions(-)

diff --git a/pom.xml b/pom.xml
index bde9257..b9bb5d2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -55,7 +55,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.testing.jcr-mock</artifactId>
-            <version>1.1.2</version>
+            <version>1.1.3-SNAPSHOT</version>
             <scope>compile</scope>
         </dependency>
         <dependency>
diff --git a/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java b/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java
index 8df107f..006a448 100644
--- a/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java
+++ b/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java
@@ -71,6 +71,7 @@ final class ContextResourceResolverFactory {
         Session session = resolver.adaptTo(Session.class);
         NamespaceRegistry namespaceRegistry = session.getWorkspace().getNamespaceRegistry();
         namespaceRegistry.registerNamespace("sling", "http://sling.apache.org/jcr/sling/1.0");
+        resolver.close();
     }
 
     private static void initializeJcrJackrabbit(ResourceResolverFactory factory) {
diff --git a/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/JcrResourceResolverTest.java b/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/JcrNamespaceTest.java
similarity index 79%
copy from src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/JcrResourceResolverTest.java
copy to src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/JcrNamespaceTest.java
index 0e358f9..e1e3a4c 100644
--- a/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/JcrResourceResolverTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/JcrNamespaceTest.java
@@ -19,14 +19,11 @@
 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.AbstractJcrResourceResolverTest;
+import org.apache.sling.testing.mock.sling.resource.AbstractJcrNamespaceTest;
 
-/**
- * Implements simple write and read resource and values test. JCR API is used to
- * create the test data.
- */
-public class JcrResourceResolverTest extends AbstractJcrResourceResolverTest {
+public class JcrNamespaceTest extends AbstractJcrNamespaceTest {
 
+    @Override
     protected ResourceResolverType getResourceResolverType() {
         return ResourceResolverType.JCR_MOCK;
     }
diff --git a/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/JcrResourceResolverTest.java b/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/JcrResourceResolverTest.java
index 0e358f9..9a7da28 100644
--- a/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/JcrResourceResolverTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/JcrResourceResolverTest.java
@@ -27,6 +27,7 @@ import org.apache.sling.testing.mock.sling.resource.AbstractJcrResourceResolverT
  */
 public class JcrResourceResolverTest extends AbstractJcrResourceResolverTest {
 
+    @Override
     protected ResourceResolverType getResourceResolverType() {
         return ResourceResolverType.JCR_MOCK;
     }
diff --git a/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/SlingCrudResourceResolverTest.java b/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/SlingCrudResourceResolverTest.java
index 005ab6c..b46da3e 100644
--- a/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/SlingCrudResourceResolverTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/resource/SlingCrudResourceResolverTest.java
@@ -27,6 +27,7 @@ import org.apache.sling.testing.mock.sling.resource.AbstractSlingCrudResourceRes
  */
 public class SlingCrudResourceResolverTest extends AbstractSlingCrudResourceResolverTest {
 
+    @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
new file mode 100644
index 0000000..f7acd25
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractJcrNamespaceTest.java
@@ -0,0 +1,95 @@
+/*
+ * 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.assertEquals;
+
+import javax.jcr.NamespaceRegistry;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceUtil;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.testing.mock.sling.MockSling;
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.apache.sling.testing.mock.sling.loader.ContentLoader;
+import org.junit.Rule;
+import org.junit.Test;
+
+/**
+ * Validates correct registering and mapping of JCR namespaces, esp. the sling namespace. 
+ */
+public abstract class AbstractJcrNamespaceTest {
+    
+    @Rule
+    public SlingContext context = new SlingContext(getResourceResolverType());
+
+    protected abstract ResourceResolverType getResourceResolverType();
+    
+    @Test
+    public void testSling4362_WithSlingNamespace() throws RepositoryException {
+        ResourceResolver resolver = MockSling.newResourceResolver(getResourceResolverType());
+        
+        NamespaceRegistry namespaceRegistry = resolver.adaptTo(Session.class).getWorkspace().getNamespaceRegistry();
+        namespaceRegistry.registerNamespace("sling", "http://mock/sling");
+        
+        ContentLoader contentLoader = new ContentLoader(resolver);
+        contentLoader.json("/json-import-samples/SLING-4362.json", "/content/foo");
+
+        Resource resource = resolver.getResource("/content/foo");
+        
+        ValueMap props = ResourceUtil.getValueMap(resource);
+        assertEquals("fooType", props.get("sling:resourceType"));
+        assertEquals("fooType", resource.getResourceType());
+    }
+
+    @Test
+    public void testSling4362_WithoutSlingNamespace() throws RepositoryException {
+        ResourceResolver resolver = MockSling.newResourceResolver(getResourceResolverType());
+        
+        ContentLoader contentLoader = new ContentLoader(resolver);
+        contentLoader.json("/json-import-samples/SLING-4362.json", "/content/foo");
+
+        Resource resource = resolver.getResource("/content/foo");
+        
+        ValueMap props = ResourceUtil.getValueMap(resource);
+        assertEquals("fooType", props.get("sling:resourceType"));
+        
+        // without proper sling namespace definition it is expected that the JCR resource implementation cannot detect the resource type properly
+        assertEquals("nt:unstructured", resource.getResourceType());
+    }
+
+    @Test
+    public void testSling4362_WithoutSlingNamespace_ViaContextRule() throws RepositoryException {
+        ResourceResolver resolver = context.resourceResolver();
+        
+        ContentLoader contentLoader = new ContentLoader(resolver);
+        contentLoader.json("/json-import-samples/SLING-4362.json", "/content/foo");
+
+        Resource resource = resolver.getResource("/content/foo");
+        
+        ValueMap props = ResourceUtil.getValueMap(resource);
+        assertEquals("fooType", props.get("sling:resourceType"));
+        assertEquals("fooType", resource.getResourceType());
+    }
+
+}
diff --git a/src/test/resources/json-import-samples/SLING-4362.json b/src/test/resources/json-import-samples/SLING-4362.json
new file mode 100644
index 0000000..7870490
--- /dev/null
+++ b/src/test/resources/json-import-samples/SLING-4362.json
@@ -0,0 +1,5 @@
+{
+	"jcr:primaryType":"nt:unstructured",
+	"sling:resourceType": "fooType",
+	"thisIs": "child"
+}

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