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:20:08 UTC

[sling-org-apache-sling-testing-resourceresolver-mock] annotated tag org.apache.sling.testing.resourceresolver-mock-0.3.0 created (now 5939360)

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

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


      at 5939360  (tag)
 tagging 74fb07fa3e8e56197b28acea19b3d79f8832502d (commit)
      by sseifert
      on Thu Sep 25 08:49:47 2014 +0000

- Log -----------------------------------------------------------------
org.apache.sling.testing.resourceresolver-mock-0.3.0
-----------------------------------------------------------------------

This annotated tag includes the following new commits:

     new cb4683b  Add new testing module for easy mocking of a resource resolver
     new 3f7aa2f  Prepare release
     new 831dff1  [maven-release-plugin] prepare release org.apache.sling.testing.resourceresolver-mock-0.1.0
     new 3b6ae6f  [maven-release-plugin] prepare for next development iteration
     new 23d5aa0  Back to current snapshot
     new 2766d00  Use released versions after release.
     new 3888b8d  Correct reactor pom and update to parent pom 16
     new 6121894  Update to latest parent pom
     new 865d7f5  Update to parent pom 18
     new af4baf2  SLING-3354 : Search paths are returned in wrong order
     new 3a63d32  SLING-3355 : Add helper methods for creating resources
     new 518a6e2  SLING-3355 : Add helper methods for creating resources
     new 812de29  Refactoring and javadoc updates
     new e71efcd  [maven-release-plugin] prepare release org.apache.sling.testing.resourceresolver-mock-0.2.0
     new 8104e98  [maven-release-plugin] prepare for next development iteration
     new b03ae42  SLING-3417 : Search paths must end with a slash
     new 58cd751  Update to parent pom v19
     new bad61a3  Updated to parent version 20
     new 7d7fab1  SLING-3847 : MockResourceResolver: resource order is not preserved in listChildren method. Apply patch from Stefan Seifert
     new 6eecd9a  SLING-3853 : MockResourceResolver does not extend SlingAdaptable. Apply patch from Stefan Seifert
     new 0aac1b6  Update to latest Sling API
     new b290dc8  Use latest API snapshot during release
     new c41abfc  Use released SLING API
     new 1ad5976  SLING-3889 Make compatible to JCR resource
     new acc328c  SLING-3889 fix issue where creating a resources fails when deleting it before without committing refactor unit tests and add a new unit test suite for create/delete scenarios
     new d27619f  SLING-3889 Emulate feature of JCR resource implementation that allows adapting to InputStream for nt:file and nt:resource nodes.
     new d0ebcd3  SLING-3889 Add optional support for namespace mangling and unmangling
     new 1e5d468  [maven-release-plugin] prepare release org.apache.sling.testing.resourceresolver-mock-0.3.0
     new 74fb07f  [maven-release-plugin]  copy for tag org.apache.sling.testing.resourceresolver-mock-0.3.0

The 29 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


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

[sling-org-apache-sling-testing-resourceresolver-mock] 15/15: [maven-release-plugin] copy for tag org.apache.sling.testing.resourceresolver-mock-0.3.0

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 74fb07fa3e8e56197b28acea19b3d79f8832502d
Author: sseifert <ss...@unknown>
AuthorDate: Thu Sep 25 08:49:47 2014 +0000

    [maven-release-plugin]  copy for tag org.apache.sling.testing.resourceresolver-mock-0.3.0
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/tags/org.apache.sling.testing.resourceresolver-mock-0.3.0@1627485 13f79535-47bb-0310-9956-ffa450edef68

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

[sling-org-apache-sling-testing-resourceresolver-mock] 08/15: Use latest API snapshot during release

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit b290dc888ffbabe9399ec209a78b0f9da60d311f
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Aug 27 09:57:55 2014 +0000

    Use latest API snapshot during release
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1620846 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index f90580d..df6fdf2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -62,7 +62,7 @@
          <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
-            <version>2.7.1-SNAPSHOT</version>
+            <version>2.8.1-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         <dependency>

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

[sling-org-apache-sling-testing-resourceresolver-mock] 14/15: [maven-release-plugin] prepare release org.apache.sling.testing.resourceresolver-mock-0.3.0

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 1e5d46860f8f6b648d55e7aea97b0dd3a443bf9d
Author: sseifert <ss...@unknown>
AuthorDate: Thu Sep 25 08:46:00 2014 +0000

    [maven-release-plugin] prepare release org.apache.sling.testing.resourceresolver-mock-0.3.0
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1627484 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/pom.xml b/pom.xml
index 9cccf1c..ed63e42 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
     </parent>
 
     <artifactId>org.apache.sling.testing.resourceresolver-mock</artifactId>
-    <version>0.2.1-SNAPSHOT</version>
+    <version>0.3.0</version>
 
     <name>Apache Sling Testing Resource Resolver Mock</name>
     <description>
@@ -40,9 +40,9 @@
     </properties>
     
     <scm>
-        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock</connection>
-        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock</developerConnection>
-        <url>http://svn.apache.org/viewvc/sling/trunk/testing/resourceresolver-mock</url>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/tags/org.apache.sling.testing.resourceresolver-mock-0.3.0</connection>
+        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/tags/org.apache.sling.testing.resourceresolver-mock-0.3.0</developerConnection>
+        <url>http://svn.apache.org/viewvc/sling/tags/org.apache.sling.testing.resourceresolver-mock-0.3.0</url>
     </scm>
 
     <reporting>

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

[sling-org-apache-sling-testing-resourceresolver-mock] 01/15: [maven-release-plugin] prepare for next development iteration

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 8104e986b54f47bccc27d0a9db0a4d4f06e97785
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Thu Jan 30 14:26:00 2014 +0000

    [maven-release-plugin] prepare for next development iteration
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1562840 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/pom.xml b/pom.xml
index e15c363..4d419e8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
     </parent>
 
     <artifactId>org.apache.sling.testing.resourceresolver-mock</artifactId>
-    <version>0.2.0</version>
+    <version>0.2.1-SNAPSHOT</version>
 
     <name>Apache Sling Testing Resource Resolver Mock</name>
     <description>
@@ -40,9 +40,9 @@
     </properties>
     
     <scm>
-        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/tags/org.apache.sling.testing.resourceresolver-mock-0.2.0</connection>
-        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/tags/org.apache.sling.testing.resourceresolver-mock-0.2.0</developerConnection>
-        <url>http://svn.apache.org/viewvc/sling/tags/org.apache.sling.testing.resourceresolver-mock-0.2.0</url>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock</connection>
+        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock</developerConnection>
+        <url>http://svn.apache.org/viewvc/sling/trunk/testing/resourceresolver-mock</url>
     </scm>
 
     <reporting>

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

[sling-org-apache-sling-testing-resourceresolver-mock] 05/15: SLING-3847 : MockResourceResolver: resource order is not preserved in listChildren method. Apply patch from Stefan Seifert

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 7d7fab18aa50d3a858d76e53557a3f2cbe39ef76
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Aug 15 16:16:42 2014 +0000

    SLING-3847 : MockResourceResolver: resource order is not preserved in listChildren method. Apply patch from Stefan Seifert
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1618220 13f79535-47bb-0310-9956-ffa450edef68
---
 .../org/apache/sling/testing/resourceresolver/MockResourceResolver.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
index a28b62b..1c383c0 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
@@ -135,7 +135,7 @@ public class MockResourceResolver implements ResourceResolver {
     @Override
     public Iterator<Resource> listChildren(final Resource parent) {
         final String prefixPath = parent.getPath() + "/";
-        final Map<String, Map<String, Object>> candidates = new HashMap<String, Map<String,Object>>();
+        final Map<String, Map<String, Object>> candidates = new LinkedHashMap<String, Map<String,Object>>();
         synchronized ( this.resources ) {
             for(final Map.Entry<String, Map<String, Object>> e : this.resources.entrySet()) {
                 if (e.getKey().startsWith(prefixPath) && e.getKey().lastIndexOf('/') < prefixPath.length() ) {

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

[sling-org-apache-sling-testing-resourceresolver-mock] 10/15: SLING-3889 Make compatible to JCR resource

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 1ad5976725f17c0dcd66a042ec1149cf63cb53f7
Author: sseifert <ss...@unknown>
AuthorDate: Tue Sep 9 12:47:37 2014 +0000

    SLING-3889 Make compatible to JCR resource
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1623814 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  14 ++
 ...MockResource.java => MockPropertyResource.java} |  51 ++----
 .../testing/resourceresolver/MockResource.java     |  17 +-
 .../resourceresolver/MockResourceResolver.java     |  30 +++-
 .../testing/resourceresolver/MockValueMap.java     | 109 ++++++++++++
 .../SlingCrudResourceResolverTest.java             | 187 +++++++++++++++++++++
 6 files changed, 365 insertions(+), 43 deletions(-)

diff --git a/pom.xml b/pom.xml
index 681b67d..a38f9d1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -69,10 +69,24 @@
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.compendium</artifactId>
             <version>4.3.0</version>
+            <scope>compile</scope>
         </dependency>
         <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>servlet-api</artifactId>
         </dependency>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.4</version>
+            <scope>compile</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
+    
 </project>
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockPropertyResource.java
similarity index 60%
copy from src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
copy to src/main/java/org/apache/sling/testing/resourceresolver/MockPropertyResource.java
index 7b80e2f..a6884f1 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockPropertyResource.java
@@ -18,31 +18,29 @@
  */
 package org.apache.sling.testing.resourceresolver;
 
-import java.util.Map;
-
 import org.apache.sling.api.resource.AbstractResource;
-import org.apache.sling.api.resource.ModifiableValueMap;
 import org.apache.sling.api.resource.ResourceMetadata;
 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.api.wrappers.ModifiableValueMapDecorator;
-import org.apache.sling.api.wrappers.ValueMapDecorator;
-
-public class MockResource extends AbstractResource {
 
+/**
+ * Resource that wraps a property value.
+ */
+class MockPropertyResource extends AbstractResource {
+    
     private final String path;
-
-    private final Map<String, Object> props;
-
-    private final ResourceMetadata rm = new ResourceMetadata();
-
+    private final ValueMap props;
+    private final String key;
     private final ResourceResolver resolver;
+    private final ResourceMetadata rm = new ResourceMetadata();
 
-    public MockResource(final String path,
-            final Map<String, Object> props,
+    public MockPropertyResource(final String path,
+            final ValueMap props,
             final ResourceResolver resolver) {
         this.path = path;
         this.props = props;
+        this.key = ResourceUtil.getName(path);
         this.resolver = resolver;
     }
 
@@ -53,12 +51,12 @@ public class MockResource extends AbstractResource {
 
     @Override
     public String getResourceType() {
-        return (String)this.props.get(ResourceResolver.PROPERTY_RESOURCE_TYPE);
+        return null;
     }
 
     @Override
     public String getResourceSuperType() {
-        return (String)this.props.get("sling:resourceSuperType");
+        return null;
     }
 
     @Override
@@ -71,26 +69,13 @@ public class MockResource extends AbstractResource {
         return this.resolver;
     }
 
-    @SuppressWarnings("unchecked")
     @Override
-    public <AdapterType> AdapterType adaptTo(final Class<AdapterType> type) {
-        if ( type == ValueMap.class ) {
-            return (AdapterType)new ValueMapDecorator(this.props);
-        } else if ( type == ModifiableValueMap.class ) {
-            ((MockResourceResolver)this.resolver).addChanged(this.path, this.props);
-            return (AdapterType)new ModifiableValueMapDecorator(this.props);
+    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
+        AdapterType value = props.get(key, type);
+        if (value!=null) {
+            return value;
         }
         return super.adaptTo(type);
     }
 
-    @Override
-    public ValueMap getValueMap() {
-        return new ValueMapDecorator(this.props);
-    }
-
-    @Override
-    public String toString() {
-        return "MockResource [path=" + path + ", props=" + props + "]";
-    }
-
 }
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
index 7b80e2f..a8ed2f5 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
@@ -32,17 +32,19 @@ public class MockResource extends AbstractResource {
 
     private final String path;
 
-    private final Map<String, Object> props;
+    private final ValueMap props;
 
     private final ResourceMetadata rm = new ResourceMetadata();
 
     private final ResourceResolver resolver;
 
+    static final String JCR_PRIMARYTYPE = "jcr:primaryType";
+    
     public MockResource(final String path,
             final Map<String, Object> props,
             final ResourceResolver resolver) {
         this.path = path;
-        this.props = props;
+        this.props = (props instanceof MockValueMap) ? (MockValueMap)props : new MockValueMap(props);
         this.resolver = resolver;
     }
 
@@ -53,12 +55,17 @@ public class MockResource extends AbstractResource {
 
     @Override
     public String getResourceType() {
-        return (String)this.props.get(ResourceResolver.PROPERTY_RESOURCE_TYPE);
+        String resourceType = this.props.get(ResourceResolver.PROPERTY_RESOURCE_TYPE, String.class);
+        if (resourceType == null) {
+            // fallback to jcr:primaryType if not resouce type exists (to mimick JCR resource behavior)
+            resourceType = this.props.get(JCR_PRIMARYTYPE, String.class);
+        }
+        return resourceType;
     }
 
     @Override
     public String getResourceSuperType() {
-        return (String)this.props.get("sling:resourceSuperType");
+        return this.props.get("sling:resourceSuperType", String.class);
     }
 
     @Override
@@ -85,7 +92,7 @@ public class MockResource extends AbstractResource {
 
     @Override
     public ValueMap getValueMap() {
-        return new ValueMapDecorator(this.props);
+        return this.props;
     }
 
     @Override
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
index 05e41c4..b5c4682 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
@@ -36,6 +36,8 @@ import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.PersistenceException;
 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.osgi.service.event.Event;
 
 public class MockResourceResolver extends SlingAdaptable implements ResourceResolver {
@@ -86,6 +88,25 @@ public class MockResourceResolver extends SlingAdaptable implements ResourceReso
 
     @Override
     public Resource getResource(final String path) {
+        Resource resource = getResourceInternal(path);
+        
+        // if not resource found check if this is a reference to a property
+        if (resource == null) {
+            String name = ResourceUtil.getName(path);
+            String parentPath = ResourceUtil.getParent(path);
+            Resource parentResource = getResourceInternal(parentPath);
+            if (parentResource!=null) {
+                ValueMap props = parentResource.getValueMap();
+                if (props.containsKey(name)) {
+                    return new MockPropertyResource(path, props, this);
+                }
+            }
+        }
+        
+        return resource;
+    }
+    
+    private Resource getResourceInternal(final String path) {
         if ( path.startsWith("/") ) {
             if ( this.deletedResources.contains(path) ) {
                 return null;
@@ -186,7 +207,6 @@ public class MockResourceResolver extends SlingAdaptable implements ResourceReso
     @Override
     public ResourceResolver clone(Map<String, Object> authenticationInfo)
             throws LoginException {
-        // TODO Auto-generated method stub
         return null;
     }
 
@@ -202,7 +222,6 @@ public class MockResourceResolver extends SlingAdaptable implements ResourceReso
 
     @Override
     public String getUserID() {
-        // TODO Auto-generated method stub
         return null;
     }
 
@@ -254,9 +273,10 @@ public class MockResourceResolver extends SlingAdaptable implements ResourceReso
         if ( properties == null ) {
             properties = new HashMap<String, Object>();
         }
-        this.temporaryResources.put(path, properties);
-
-        return new MockResource(path, properties, this);
+        
+        Resource mockResource = new MockResource(path, properties, this);
+        this.temporaryResources.put(path, mockResource.getValueMap());
+        return mockResource;
     }
 
     @Override
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java
new file mode 100644
index 0000000..4fe009a
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockValueMap.java
@@ -0,0 +1,109 @@
+/*
+ * 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.resourceresolver;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.sling.api.wrappers.ValueMapDecorator;
+
+/**
+ * ValueMap for mocked resources to mimick JCR-like behavior.
+ * <p>Implements the following conversions:</p>
+ * <ul>
+ * <li>Converts all Date values to Calendar objects internally and vice versa.</li>
+ * <li>Converts InputStream to byte array and vice versa.</li>
+ * <ul>
+ */
+public class MockValueMap extends ValueMapDecorator {
+    
+    public MockValueMap() {
+        this(new HashMap<String, Object>());
+    }
+
+    public MockValueMap(Map<String,Object> map) {
+        super(convertForWrite(map));
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public <T> T get(String name, Class<T> type) {
+        if (type==Date.class) {
+            Calendar calendar = super.get(name, Calendar.class);
+            if (calendar != null) {
+                return (T)calendar.getTime();
+            }
+            else {
+                return null;
+            }
+        }
+        else if (type==InputStream.class) {
+            byte[] data = super.get(name, byte[].class);
+            if (data!=null) {
+                return (T)new ByteArrayInputStream(data);
+            }
+            else {
+                return null;
+            }
+        }
+        return super.get(name, type);
+    }
+
+    @Override
+    public Object put(String key, Object value) {
+        return super.put(key, convertForWrite(value));
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public void putAll(Map<? extends String, ?> map) {
+        super.putAll((Map<? extends String, ?>)convertForWrite(map));
+    }
+    
+    private static Object convertForWrite(Object value) {
+        if (value instanceof Date) {
+            Calendar calendar = Calendar.getInstance();
+            calendar.setTime((Date)value);
+            value = calendar;
+        }
+        else if (value instanceof InputStream) {
+            try {
+                value = IOUtils.toByteArray((InputStream)value);
+            } catch (IOException ex) {
+                throw new RuntimeException("Unable to convert input stream to byte array.");
+            }
+        }
+        return value;
+    }
+    
+    private static Map<String, Object> convertForWrite(Map<String, Object> map) {
+        Map<String,Object> newMap = new HashMap<String, Object>();
+        for (Map.Entry<String, Object> entry : map.entrySet()) {
+            newMap.put(entry.getKey(), convertForWrite(entry.getValue()));
+        }
+        return newMap;
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java b/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java
new file mode 100644
index 0000000..617773f
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java
@@ -0,0 +1,187 @@
+/*
+ * 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.resourceresolver;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.PersistenceException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ValueMap;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Implements simple write and read resource and values test
+ * Sling CRUD API is used to create the test data.
+ */
+public class SlingCrudResourceResolverTest {
+
+    private static final String STRING_VALUE = "value1";
+    private static final String[] STRING_ARRAY_VALUE = new String[] { "value1", "value2" };
+    private static final int INTEGER_VALUE = 25;
+    private static final double DOUBLE_VALUE = 3.555d;
+    private static final boolean BOOLEAN_VALUE = true;
+    private static final Date DATE_VALUE = new Date(10000);
+    private static final Calendar CALENDAR_VALUE = Calendar.getInstance();
+    private static final byte[] BINARY_VALUE = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
+
+    private static final String NT_UNSTRUCTURED = "nt:unstructured";
+
+    private ResourceResolver resourceResolver;
+    protected Resource testRoot;
+
+    @Before
+    public final void setUp() throws IOException, LoginException {
+        this.resourceResolver = new MockResourceResolverFactory().getResourceResolver(null);
+
+        // prepare some test data using Sling CRUD API
+        Resource rootNode = getTestRootResource();
+
+        Map<String, Object> props = new HashMap<String, Object>();
+        props.put(MockResource.JCR_PRIMARYTYPE, NT_UNSTRUCTURED);
+        props.put("stringProp", STRING_VALUE);
+        props.put("stringArrayProp", STRING_ARRAY_VALUE);
+        props.put("integerProp", INTEGER_VALUE);
+        props.put("doubleProp", DOUBLE_VALUE);
+        props.put("booleanProp", BOOLEAN_VALUE);
+        props.put("dateProp", DATE_VALUE);
+        props.put("calendarProp", CALENDAR_VALUE);
+        props.put("binaryProp", new ByteArrayInputStream(BINARY_VALUE));
+        Resource node1 = this.resourceResolver.create(rootNode, "node1", props);
+
+        this.resourceResolver.create(node1, "node11", ValueMap.EMPTY);
+        this.resourceResolver.create(node1, "node12", ValueMap.EMPTY);
+
+        this.resourceResolver.commit();
+    }
+
+    @After
+    public final void tearDown() {
+        this.testRoot = null;
+    }
+
+    /**
+     * Return a test root resource, created on demand, with a unique path
+     * @throws PersistenceException
+     */
+    private Resource getTestRootResource() throws PersistenceException {
+        if (this.testRoot == null) {
+            final Resource root = this.resourceResolver.getResource("/");
+            this.testRoot = this.resourceResolver.create(root, "test", ValueMap.EMPTY);
+        }
+        return this.testRoot;
+    }
+
+    @Test
+    public void testSimpleProperties() throws IOException {
+        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+        assertNotNull(resource1);
+        assertEquals("node1", resource1.getName());
+
+        ValueMap props = resource1.getValueMap();
+        assertEquals(STRING_VALUE, props.get("stringProp", String.class));
+        assertArrayEquals(STRING_ARRAY_VALUE, props.get("stringArrayProp", String[].class));
+        assertEquals((Integer) INTEGER_VALUE, props.get("integerProp", Integer.class));
+        assertEquals(DOUBLE_VALUE, props.get("doubleProp", Double.class), 0.0001);
+        assertEquals(BOOLEAN_VALUE, props.get("booleanProp", Boolean.class));
+    }
+
+    @Test
+    public void testDateProperty() throws IOException {
+        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+        ValueMap props = resource1.getValueMap();
+        assertEquals(DATE_VALUE, props.get("dateProp", Date.class));
+    }
+
+    @Test
+    public void testDatePropertyToCalendar() throws IOException {
+        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+        ValueMap props = resource1.getValueMap();
+        Calendar calendarValue = props.get("dateProp", Calendar.class);
+        assertNotNull(calendarValue);
+        assertEquals(DATE_VALUE, calendarValue.getTime());
+    }
+    
+    @Test
+    public void testCalendarProperty() throws IOException {
+        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+        ValueMap props = resource1.getValueMap();
+        assertEquals(CALENDAR_VALUE.getTime(), props.get("calendarProp", Calendar.class).getTime());
+    }
+
+    @Test
+    public void testCalendarPropertyToDate() throws IOException {
+        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+        ValueMap props = resource1.getValueMap();
+        Date dateValue = props.get("calendarProp", Date.class);
+        assertNotNull(dateValue);
+        assertEquals(CALENDAR_VALUE.getTime(), dateValue);
+    }
+    
+    @Test
+    public void testListChildren() throws IOException {
+        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+
+        Iterator<Resource> children = resource1.listChildren();
+        assertEquals("node11", children.next().getName());
+        assertEquals("node12", children.next().getName());
+        assertFalse(children.hasNext());
+    }
+
+    @Test
+    public void testBinaryData() throws IOException {
+        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+
+        Resource binaryPropResource = resource1.getChild("binaryProp");
+        InputStream is = binaryPropResource.adaptTo(InputStream.class);
+        byte[] dataFromResource = IOUtils.toByteArray(is);
+        is.close();
+        assertArrayEquals(BINARY_VALUE, dataFromResource);
+
+        // read second time to ensure not the original input stream was returned
+        // and this time using another syntax
+        InputStream is2 = resource1.getValueMap().get("binaryProp", InputStream.class);
+        byte[] dataFromResource2 = IOUtils.toByteArray(is2);
+        is2.close();
+        assertArrayEquals(BINARY_VALUE, dataFromResource2);
+    }
+
+    @Test
+    public void testPrimaryTypeResourceType() throws PersistenceException {
+        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+        assertEquals(NT_UNSTRUCTURED, resource1.getResourceType());
+    }
+
+}

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

[sling-org-apache-sling-testing-resourceresolver-mock] 09/15: Use released SLING API

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit c41abfc77d8ea97324eec79b14596aeaa268ba31
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Tue Sep 2 06:11:02 2014 +0000

    Use released SLING API
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1621927 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index df6fdf2..681b67d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -62,7 +62,7 @@
          <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
-            <version>2.8.1-SNAPSHOT</version>
+            <version>2.8.0</version>
             <scope>provided</scope>
         </dependency>
         <dependency>

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

[sling-org-apache-sling-testing-resourceresolver-mock] 04/15: Updated to parent version 20

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit bad61a3d78007c324f3687d5c6dcbf1d0d8763fa
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Fri Aug 1 19:16:26 2014 +0000

    Updated to parent version 20
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1615208 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index ad3db51..1ff228a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -23,7 +23,7 @@
     <parent>
         <groupId>org.apache.sling</groupId>
         <artifactId>sling</artifactId>
-        <version>19</version>
+        <version>20</version>
         <relativePath>../../parent/pom.xml</relativePath>
     </parent>
 

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

[sling-org-apache-sling-testing-resourceresolver-mock] 13/15: SLING-3889 Add optional support for namespace mangling and unmangling

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit d0ebcd3da39f48e1d51ff189ec7bffa11af71466
Author: sseifert <ss...@unknown>
AuthorDate: Wed Sep 17 13:59:50 2014 +0000

    SLING-3889 Add optional support for namespace mangling and unmangling
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1625594 13f79535-47bb-0310-9956-ffa450edef68
---
 .../resourceresolver/MockResourceResolver.java     | 47 +++++++++++--
 .../MockResourceResolverFactoryOptions.java        | 11 +++
 .../testing/resourceresolver/NamespaceMangler.java | 80 ++++++++++++++++++++++
 .../resourceresolver/NamespaceManglerTest.java     | 51 ++++++++++++++
 .../NamespaceManglingResourceResolverTest.java     | 56 +++++++++++++++
 5 files changed, 240 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
index c79839c..1fee01f 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
@@ -18,6 +18,9 @@
  */
 package org.apache.sling.testing.resourceresolver;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URLEncoder;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -62,12 +65,30 @@ public class MockResourceResolver extends SlingAdaptable implements ResourceReso
 
     @Override
     public Resource resolve(final HttpServletRequest request, final String absPath) {
-        return this.getResource(absPath);
+        String path = absPath;
+
+        // split off query string or fragment that may be appendend to the URL
+        String urlRemainder = null;
+        int urlRemainderPos = Math.min(path.indexOf('?'), path.indexOf('#'));
+        if (urlRemainderPos >= 0) {
+          urlRemainder = path.substring(urlRemainderPos);
+          path = path.substring(0, urlRemainderPos);
+        }
+        
+        // unmangle namespaces
+        if (options.isMangleNamespacePrefixes()) {
+            path = NamespaceMangler.unmangleNamespaces(path);
+        }
+
+        // build full path again
+        path = path + (urlRemainder != null ? urlRemainder : "");
+
+        return this.getResource(path);
     }
 
     @Override
     public Resource resolve(final String absPath) {
-        return this.getResource(absPath);
+        return resolve(null, absPath);
     }
 
     @Override
@@ -78,14 +99,30 @@ public class MockResourceResolver extends SlingAdaptable implements ResourceReso
 
     @Override
     public String map(final String resourcePath) {
-        return resourcePath;
+        return map(null, resourcePath);
     }
 
     @Override
     public String map(final HttpServletRequest request, final String resourcePath) {
-        return resourcePath;
-    }
+        String path = resourcePath;
+
+        // split off query string or fragment that may be appendend to the URL
+        String urlRemainder = null;
+        int urlRemainderPos = Math.min(path.indexOf('?'), path.indexOf('#'));
+        if (urlRemainderPos >= 0) {
+          urlRemainder = path.substring(urlRemainderPos);
+          path = path.substring(0, urlRemainderPos);
+        }
+        
+        // mangle namespaces
+        if (options.isMangleNamespacePrefixes()) {
+            path = NamespaceMangler.mangleNamespaces(path);
+        }
 
+        // build full path again
+        return path + (urlRemainder != null ? urlRemainder : "");
+    }
+    
     @Override
     public Resource getResource(final String path) {
         Resource resource = getResourceInternal(path);
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java
index 734d804..d173953 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java
@@ -28,6 +28,8 @@ public class MockResourceResolverFactoryOptions {
     private EventAdmin eventAdmin;
 
     private String[] searchPaths = new String[] {"/apps/", "/libs/"};
+    
+    private boolean mangleNamespacePrefixes;
 
     public EventAdmin getEventAdmin() {
         return eventAdmin;
@@ -49,4 +51,13 @@ public class MockResourceResolverFactoryOptions {
         this.searchPaths = searchPaths;
         return this;
     }
+
+    public boolean isMangleNamespacePrefixes() {
+        return mangleNamespacePrefixes;
+    }
+
+    public void setMangleNamespacePrefixes(boolean mangleNamespacePrefixes) {
+        this.mangleNamespacePrefixes = mangleNamespacePrefixes;
+    }
+    
 }
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/NamespaceMangler.java b/src/main/java/org/apache/sling/testing/resourceresolver/NamespaceMangler.java
new file mode 100644
index 0000000..5348312
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/NamespaceMangler.java
@@ -0,0 +1,80 @@
+/*
+ * 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.resourceresolver;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+final class NamespaceMangler {
+
+    private static final String MANGLED_NAMESPACE_PREFIX = "_";
+    private static final String MANGLED_NAMESPACE_SUFFIX = "_";
+    private static final char NAMESPACE_SEPARATOR = ':';
+    private static final Pattern NAMESPACE_PATTERN = Pattern.compile("/([^:/]+):");
+    private static final Pattern MANGLED_NAMESPACE_PATTERN = Pattern.compile("/_([^_/]+)_");
+
+    private NamespaceMangler() {
+        // static methods only
+    }
+
+    /**
+     * Mangle the namespaces in the given path for usage in sling-based URLs.
+     * <p>
+     * Example: /path/jcr:content to /path/_jcr_content
+     * </p>
+     * @param path Path to mangle
+     * @return Mangled path
+     */
+    public static String mangleNamespaces(String path) {
+        if (path == null) {
+            return null;
+        }
+        Matcher matcher = NAMESPACE_PATTERN.matcher(path);
+        StringBuffer sb = new StringBuffer();
+        while (matcher.find()) {
+            String replacement = "/" + MANGLED_NAMESPACE_PREFIX + matcher.group(1) + MANGLED_NAMESPACE_SUFFIX;
+            matcher.appendReplacement(sb, replacement);
+        }
+        matcher.appendTail(sb);
+        return sb.toString();
+    }
+
+    /**
+     * Unmangle the namespaces in the given path for usage in sling-based URLs.
+     * <p>
+     * Example: /path/_jcr_content to /path/jcr:content
+     * </p>
+     * @param path Path to unmangle
+     * @return Unmangled path
+     */
+    public static String unmangleNamespaces(String path) {
+        if (path == null) {
+            return null;
+        }
+        Matcher matcher = MANGLED_NAMESPACE_PATTERN.matcher(path);
+        StringBuffer sb = new StringBuffer();
+        while (matcher.find()) {
+            String replacement = "/" + matcher.group(1) + NAMESPACE_SEPARATOR;
+            matcher.appendReplacement(sb, replacement);
+        }
+        matcher.appendTail(sb);
+        return sb.toString();
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglerTest.java b/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglerTest.java
new file mode 100644
index 0000000..53d11bb
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglerTest.java
@@ -0,0 +1,51 @@
+/*
+ * 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.resourceresolver;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Map;
+
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableMap;
+
+public class NamespaceManglerTest {
+    
+    private Map<String, String> TEST_PATHS = ImmutableMap.<String, String>builder()
+            .put("/content/aa/bb/content.png", "/content/aa/bb/content.png")
+            .put("/content/aa/bb/jcr:content.png", "/content/aa/bb/_jcr_content.png")
+            .put("/content/aa/bb/jcr:content/anotherpath/xyz:abc", "/content/aa/bb/_jcr_content/anotherpath/_xyz_abc")
+            .build();
+
+    @Test
+    public void testMangleNamespaces() throws Exception {
+        for (Map.Entry<String, String> entry : TEST_PATHS.entrySet()) {
+            assertEquals(entry.getValue(), NamespaceMangler.mangleNamespaces(entry.getKey()));
+        }
+    }
+
+    @Test
+    public void testUnmangleNamespaces() throws Exception {
+        for (Map.Entry<String, String> entry : TEST_PATHS.entrySet()) {
+            assertEquals(entry.getKey(), NamespaceMangler.unmangleNamespaces(entry.getValue()));
+        }
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglingResourceResolverTest.java b/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglingResourceResolverTest.java
new file mode 100644
index 0000000..033db8a
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/resourceresolver/NamespaceManglingResourceResolverTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.resourceresolver;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.ValueMap;
+import org.junit.Before;
+import org.junit.Test;
+
+public class NamespaceManglingResourceResolverTest {
+
+    private ResourceResolver resolver;
+    
+    @Before
+    public void setUp() throws Exception {
+        MockResourceResolverFactoryOptions options = new MockResourceResolverFactoryOptions();
+        options.setMangleNamespacePrefixes(true);
+        ResourceResolverFactory factory = new MockResourceResolverFactory(options);
+        resolver = factory.getResourceResolver(null);
+        
+        Resource res1 = resolver.create(resolver.getResource("/"), "res1", ValueMap.EMPTY);
+        Resource content = resolver.create(res1, "jcr:content", ValueMap.EMPTY);
+        resolver.create(content, "res2", ValueMap.EMPTY);
+    }
+    
+    @Test
+    public void testMap() {
+        assertEquals("/res1/_jcr_content/res2", resolver.map("/res1/jcr:content/res2"));
+    }
+    
+    @Test
+    public void testResolve() {
+        assertEquals("/res1/jcr:content/res2", resolver.resolve("/res1/_jcr_content/res2").getPath());
+    }
+    
+}

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

[sling-org-apache-sling-testing-resourceresolver-mock] 07/15: Update to latest Sling API

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 0aac1b6fce60942cb0e3dc5be3c01ffa5c9facb7
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Tue Aug 26 07:36:22 2014 +0000

    Update to latest Sling API
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1620523 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  2 +-
 .../testing/resourceresolver/MockResource.java     |  6 +++
 .../resourceresolver/MockResourceResolver.java     |  6 ++-
 .../MockResourceResolverFactory.java               | 43 ++++++++++++++++++++--
 4 files changed, 52 insertions(+), 5 deletions(-)

diff --git a/pom.xml b/pom.xml
index 1ff228a..f90580d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -62,7 +62,7 @@
          <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
-            <version>2.5.0</version>
+            <version>2.7.1-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
index d99db9c..7b80e2f 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
@@ -71,6 +71,7 @@ public class MockResource extends AbstractResource {
         return this.resolver;
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     public <AdapterType> AdapterType adaptTo(final Class<AdapterType> type) {
         if ( type == ValueMap.class ) {
@@ -83,6 +84,11 @@ public class MockResource extends AbstractResource {
     }
 
     @Override
+    public ValueMap getValueMap() {
+        return new ValueMapDecorator(this.props);
+    }
+
+    @Override
     public String toString() {
         return "MockResource [path=" + path + ", props=" + props + "]";
     }
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
index 2269d7e..05e41c4 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
@@ -48,8 +48,12 @@ public class MockResourceResolver extends SlingAdaptable implements ResourceReso
 
     private final MockResourceResolverFactoryOptions options;
 
+    private final MockResourceResolverFactory factory;
+
     public MockResourceResolver(final MockResourceResolverFactoryOptions options,
+            final MockResourceResolverFactory factory,
             final Map<String, Map<String, Object>> resources) {
+        this.factory = factory;
         this.options = options;
         this.resources = resources;
     }
@@ -193,7 +197,7 @@ public class MockResourceResolver extends SlingAdaptable implements ResourceReso
 
     @Override
     public void close() {
-        // nothing to do
+        this.factory.closed(this);
     }
 
     @Override
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactory.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactory.java
index 086b6fc..39b66a7 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactory.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactory.java
@@ -21,6 +21,7 @@ package org.apache.sling.testing.resourceresolver;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.Stack;
 
 import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.ResourceResolver;
@@ -63,18 +64,54 @@ public class MockResourceResolverFactory implements ResourceResolverFactory {
     @Override
     public ResourceResolver getResourceResolver(
             final Map<String, Object> authenticationInfo) throws LoginException {
-        return new MockResourceResolver(options, resources);
+        final ResourceResolver result = new MockResourceResolver(options, this, resources);
+        Stack<ResourceResolver> resolverStack = resolverStackHolder.get();
+        if ( resolverStack == null ) {
+            resolverStack = new Stack<ResourceResolver>();
+            resolverStackHolder.set(resolverStack);
+        }
+        resolverStack.push(result);
+        return result;
     }
 
     @Override
     public ResourceResolver getAdministrativeResourceResolver(
             final Map<String, Object> authenticationInfo) throws LoginException {
-        return new MockResourceResolver(options, resources);
+        return new MockResourceResolver(options, this, resources);
     }
 
     @Override
     public ResourceResolver getServiceResourceResolver(
             Map<String, Object> authenticationInfo) throws LoginException {
-        return new MockResourceResolver(options, resources);
+        return new MockResourceResolver(options, this, resources);
+    }
+
+    /**
+     * Thread local holding the resource resolver stack
+     */
+    private ThreadLocal<Stack<ResourceResolver>> resolverStackHolder = new ThreadLocal<Stack<ResourceResolver>>();
+
+    /**
+     * @see org.apache.sling.api.resource.ResourceResolverFactory#getThreadResourceResolver()
+     */
+    @Override
+    public ResourceResolver getThreadResourceResolver() {
+        ResourceResolver result = null;
+        final Stack<ResourceResolver> resolverStack = resolverStackHolder.get();
+        if ( resolverStack != null && !resolverStack.isEmpty() ) {
+            result = resolverStack.peek();
+        }
+        return result;
+    }
+
+    /**
+     * Inform about a closed resource resolver.
+     * Make sure to remove it from the current thread context.
+     */
+    public void closed(final ResourceResolver resourceResolverImpl) {
+        final Stack<ResourceResolver> resolverStack = resolverStackHolder.get();
+        if ( resolverStack != null ) {
+            resolverStack.remove(resourceResolverImpl);
+        }
     }
 }

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

[sling-org-apache-sling-testing-resourceresolver-mock] 11/15: SLING-3889 fix issue where creating a resources fails when deleting it before without committing refactor unit tests and add a new unit test suite for create/delete scenarios

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit acc328c05ac7bee9e6049d939f64e5ca7dcb3b2c
Author: sseifert <ss...@unknown>
AuthorDate: Tue Sep 9 14:13:45 2014 +0000

    SLING-3889 fix issue where creating a resources fails when deleting it before without committing
    refactor unit tests and add a new unit test suite for create/delete scenarios
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1623828 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |   6 +
 .../resourceresolver/MockResourceResolver.java     |   2 +-
 .../CreateDeleteResourceResolverTest.java          | 136 +++++++++++++++++++++
 .../SlingCrudResourceResolverTest.java             |  97 ++++++---------
 4 files changed, 183 insertions(+), 58 deletions(-)

diff --git a/pom.xml b/pom.xml
index a38f9d1..9cccf1c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -87,6 +87,12 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+            <version>15.0</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
     
 </project>
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
index b5c4682..c79839c 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
@@ -265,7 +265,7 @@ public class MockResourceResolver extends SlingAdaptable implements ResourceReso
             throw new PersistenceException("Path already exists: " + path);
         }
         synchronized ( this.resources ) {
-            if ( this.resources.containsKey(path) ) {
+            if ( this.resources.containsKey(path) && !this.deletedResources.contains(path) ) {
                 throw new PersistenceException("Path already exists: " + path);
             }
         }
diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/CreateDeleteResourceResolverTest.java b/src/test/java/org/apache/sling/testing/resourceresolver/CreateDeleteResourceResolverTest.java
new file mode 100644
index 0000000..dba669c
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/resourceresolver/CreateDeleteResourceResolverTest.java
@@ -0,0 +1,136 @@
+/*
+ * 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.resourceresolver;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.PersistenceException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ValueMap;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableMap;
+
+/**
+ * Tests combinations of create and deletion of resources and conflict detection.
+ */
+public class CreateDeleteResourceResolverTest {
+        
+    private static final Map<String,Object> PROPS1 = ImmutableMap.<String, Object>builder()
+            .put("prop1", "value1").build();
+    private static final Map<String,Object> PROPS2 = ImmutableMap.<String, Object>builder()
+            .put("prop2", "value2").build();
+
+    private ResourceResolver resourceResolver;
+    private Resource testRoot;
+
+    @Before
+    public final void setUp() throws IOException, LoginException {
+        resourceResolver = new MockResourceResolverFactory().getResourceResolver(null);
+        Resource root = resourceResolver.getResource("/");
+        testRoot = resourceResolver.create(root, "test", ValueMap.EMPTY);
+    }
+
+    @Test
+    public void testCreateDeleteCreate() throws PersistenceException {
+        // create new node without commit
+        Resource resource = resourceResolver.create(testRoot, "node", PROPS1);
+        assertEquals(PROPS1, resource.getValueMap());
+        
+        // delete node without commit
+        resourceResolver.delete(resource);
+        assertNull(resourceResolver.getResource(testRoot.getPath() + "/node"));
+
+        // create node again with different properties without commit
+        resource = resourceResolver.create(testRoot, "node", PROPS2);
+        assertEquals(PROPS2, resource.getValueMap());
+    }
+
+    @Test
+    public void testCreateDeleteCreateCommit() throws PersistenceException {
+        // create new node without commit
+        Resource resource = resourceResolver.create(testRoot, "node", PROPS1);
+        assertEquals(PROPS1, resource.getValueMap());
+        
+        // delete node without commit
+        resourceResolver.delete(resource);
+        assertNull(resourceResolver.getResource(testRoot.getPath() + "/node"));
+
+        // create node again with different properties with commit
+        resource = resourceResolver.create(testRoot, "node", PROPS2);
+        assertEquals(PROPS2, resource.getValueMap());
+        resourceResolver.commit();
+    }
+
+    @Test
+    public void testCreateCommitDeleteCreateCommit() throws PersistenceException {
+        // create new node with commit
+        Resource resource = resourceResolver.create(testRoot, "node", PROPS1);
+        assertEquals(PROPS1, resource.getValueMap());
+        resourceResolver.commit();
+        
+        // delete node without commit
+        resourceResolver.delete(resource);
+        assertNull(resourceResolver.getResource(testRoot.getPath() + "/node"));
+
+        // create node again with different properties with commit
+        resource = resourceResolver.create(testRoot, "node", PROPS2);
+        assertEquals(PROPS2, resource.getValueMap());
+        resourceResolver.commit();
+    }
+
+    @Test
+    public void testCreateCommitDeleteCommitCreateCommit() throws PersistenceException {
+        // create new node with commit
+        Resource resource = resourceResolver.create(testRoot, "node", PROPS1);
+        assertEquals(PROPS1, resource.getValueMap());
+        resourceResolver.commit();
+        
+        // delete node with commit
+        resourceResolver.delete(resource);
+        assertNull(resourceResolver.getResource(testRoot.getPath() + "/node"));
+        resourceResolver.commit();
+
+        // create node again with different properties with commit
+        resource = resourceResolver.create(testRoot, "node", PROPS2);
+        assertEquals(PROPS2, resource.getValueMap());
+        resourceResolver.commit();
+    }
+
+    @Test(expected=PersistenceException.class)
+    public void testCreatePathAlreadyExists() throws PersistenceException {
+        resourceResolver.create(testRoot, "node", PROPS1);
+        resourceResolver.create(testRoot, "node", PROPS2);
+    }
+
+    @Test(expected=PersistenceException.class)
+    public void testCreateCommitPathAlreadyExists() throws PersistenceException {
+        resourceResolver.create(testRoot, "node", PROPS1);
+        resourceResolver.commit();
+        resourceResolver.create(testRoot, "node", PROPS2);
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java b/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java
index 617773f..33021dd 100644
--- a/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java
+++ b/src/test/java/org/apache/sling/testing/resourceresolver/SlingCrudResourceResolverTest.java
@@ -20,7 +20,6 @@ package org.apache.sling.testing.resourceresolver;
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 
 import java.io.ByteArrayInputStream;
@@ -28,9 +27,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.Calendar;
 import java.util.Date;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
+import java.util.List;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.sling.api.resource.LoginException;
@@ -38,12 +35,14 @@ import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ValueMap;
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+
 /**
- * Implements simple write and read resource and values test
+ * Implements simple write and read resource and values test.
  * Sling CRUD API is used to create the test data.
  */
 public class SlingCrudResourceResolverTest {
@@ -60,53 +59,37 @@ public class SlingCrudResourceResolverTest {
     private static final String NT_UNSTRUCTURED = "nt:unstructured";
 
     private ResourceResolver resourceResolver;
-    protected Resource testRoot;
+    private Resource testRoot;
 
     @Before
     public final void setUp() throws IOException, LoginException {
-        this.resourceResolver = new MockResourceResolverFactory().getResourceResolver(null);
-
-        // prepare some test data using Sling CRUD API
-        Resource rootNode = getTestRootResource();
-
-        Map<String, Object> props = new HashMap<String, Object>();
-        props.put(MockResource.JCR_PRIMARYTYPE, NT_UNSTRUCTURED);
-        props.put("stringProp", STRING_VALUE);
-        props.put("stringArrayProp", STRING_ARRAY_VALUE);
-        props.put("integerProp", INTEGER_VALUE);
-        props.put("doubleProp", DOUBLE_VALUE);
-        props.put("booleanProp", BOOLEAN_VALUE);
-        props.put("dateProp", DATE_VALUE);
-        props.put("calendarProp", CALENDAR_VALUE);
-        props.put("binaryProp", new ByteArrayInputStream(BINARY_VALUE));
-        Resource node1 = this.resourceResolver.create(rootNode, "node1", props);
-
-        this.resourceResolver.create(node1, "node11", ValueMap.EMPTY);
-        this.resourceResolver.create(node1, "node12", ValueMap.EMPTY);
-
-        this.resourceResolver.commit();
-    }
-
-    @After
-    public final void tearDown() {
-        this.testRoot = null;
-    }
-
-    /**
-     * Return a test root resource, created on demand, with a unique path
-     * @throws PersistenceException
-     */
-    private Resource getTestRootResource() throws PersistenceException {
-        if (this.testRoot == null) {
-            final Resource root = this.resourceResolver.getResource("/");
-            this.testRoot = this.resourceResolver.create(root, "test", ValueMap.EMPTY);
-        }
-        return this.testRoot;
+        resourceResolver = new MockResourceResolverFactory().getResourceResolver(null);
+
+        Resource root = resourceResolver.getResource("/");
+        testRoot = resourceResolver.create(root, "test", ValueMap.EMPTY);
+
+        Resource node1 = resourceResolver.create(testRoot, "node1",
+            ImmutableMap.<String, Object>builder()
+                .put(MockResource.JCR_PRIMARYTYPE, NT_UNSTRUCTURED)
+                .put("stringProp", STRING_VALUE)
+                .put("stringArrayProp", STRING_ARRAY_VALUE)
+                .put("integerProp", INTEGER_VALUE)
+                .put("doubleProp", DOUBLE_VALUE)
+                .put("booleanProp", BOOLEAN_VALUE)
+                .put("dateProp", DATE_VALUE)
+                .put("calendarProp", CALENDAR_VALUE)
+                .put("binaryProp", new ByteArrayInputStream(BINARY_VALUE))
+                .build());
+
+        resourceResolver.create(node1, "node11", ValueMap.EMPTY);
+        resourceResolver.create(node1, "node12", ValueMap.EMPTY);
+
+        resourceResolver.commit();
     }
 
     @Test
     public void testSimpleProperties() throws IOException {
-        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+        Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
         assertNotNull(resource1);
         assertEquals("node1", resource1.getName());
 
@@ -120,14 +103,14 @@ public class SlingCrudResourceResolverTest {
 
     @Test
     public void testDateProperty() throws IOException {
-        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+        Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
         ValueMap props = resource1.getValueMap();
         assertEquals(DATE_VALUE, props.get("dateProp", Date.class));
     }
 
     @Test
     public void testDatePropertyToCalendar() throws IOException {
-        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+        Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
         ValueMap props = resource1.getValueMap();
         Calendar calendarValue = props.get("dateProp", Calendar.class);
         assertNotNull(calendarValue);
@@ -136,14 +119,14 @@ public class SlingCrudResourceResolverTest {
     
     @Test
     public void testCalendarProperty() throws IOException {
-        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+        Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
         ValueMap props = resource1.getValueMap();
         assertEquals(CALENDAR_VALUE.getTime(), props.get("calendarProp", Calendar.class).getTime());
     }
 
     @Test
     public void testCalendarPropertyToDate() throws IOException {
-        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+        Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
         ValueMap props = resource1.getValueMap();
         Date dateValue = props.get("calendarProp", Date.class);
         assertNotNull(dateValue);
@@ -152,17 +135,17 @@ public class SlingCrudResourceResolverTest {
     
     @Test
     public void testListChildren() throws IOException {
-        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+        Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
 
-        Iterator<Resource> children = resource1.listChildren();
-        assertEquals("node11", children.next().getName());
-        assertEquals("node12", children.next().getName());
-        assertFalse(children.hasNext());
+        List<Resource> children = Lists.newArrayList(resource1.listChildren());
+        assertEquals(2, children.size());
+        assertEquals("node11", children.get(0).getName());
+        assertEquals("node12", children.get(1).getName());
     }
 
     @Test
     public void testBinaryData() throws IOException {
-        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+        Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
 
         Resource binaryPropResource = resource1.getChild("binaryProp");
         InputStream is = binaryPropResource.adaptTo(InputStream.class);
@@ -180,7 +163,7 @@ public class SlingCrudResourceResolverTest {
 
     @Test
     public void testPrimaryTypeResourceType() throws PersistenceException {
-        Resource resource1 = this.resourceResolver.getResource(getTestRootResource().getPath() + "/node1");
+        Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
         assertEquals(NT_UNSTRUCTURED, resource1.getResourceType());
     }
 

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

[sling-org-apache-sling-testing-resourceresolver-mock] 12/15: SLING-3889 Emulate feature of JCR resource implementation that allows adapting to InputStream for nt:file and nt:resource nodes.

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit d27619f7acfc36b0cc6ec4f41cd7e6fa2942d18a
Author: sseifert <ss...@unknown>
AuthorDate: Tue Sep 16 21:45:17 2014 +0000

    SLING-3889 Emulate feature of JCR resource implementation that allows adapting to InputStream for nt:file and nt:resource nodes.
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1625410 13f79535-47bb-0310-9956-ffa450edef68
---
 .../testing/resourceresolver/MockResource.java     | 33 +++++++-
 .../resourceresolver/NtFileResourceTest.java       | 96 ++++++++++++++++++++++
 2 files changed, 128 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
index a8ed2f5..1b10a0d 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResource.java
@@ -18,10 +18,12 @@
  */
 package org.apache.sling.testing.resourceresolver;
 
+import java.io.InputStream;
 import java.util.Map;
 
 import org.apache.sling.api.resource.AbstractResource;
 import org.apache.sling.api.resource.ModifiableValueMap;
+import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ValueMap;
@@ -39,6 +41,10 @@ public class MockResource extends AbstractResource {
     private final ResourceResolver resolver;
 
     static final String JCR_PRIMARYTYPE = "jcr:primaryType";
+    static final String JCR_CONTENT = "jcr:content";
+    static final String JCR_DATA = "jcr:data";
+    static final String NT_RESOURCE = "nt:resource";
+    static final String NT_FILE = "nt:file";
     
     public MockResource(final String path,
             final Map<String, Object> props,
@@ -83,12 +89,37 @@ public class MockResource extends AbstractResource {
     public <AdapterType> AdapterType adaptTo(final Class<AdapterType> type) {
         if ( type == ValueMap.class ) {
             return (AdapterType)new ValueMapDecorator(this.props);
-        } else if ( type == ModifiableValueMap.class ) {
+        }
+        else if ( type == ModifiableValueMap.class ) {
             ((MockResourceResolver)this.resolver).addChanged(this.path, this.props);
             return (AdapterType)new ModifiableValueMapDecorator(this.props);
         }
+        else if ( type == InputStream.class ) {
+            InputStream is = getFileResourceInputStream();
+            if (is!=null) {
+                return (AdapterType)is;
+            }
+        }
         return super.adaptTo(type);
     }
+    
+    /**
+     * Emulate feature of JCR resource implementation that allows adapting to InputStream for nt:file and nt:resource nodes.
+     * @return InputStream or null if adaption not possible.
+     */
+    private InputStream getFileResourceInputStream() {
+        String resourceType = getResourceType();
+        if (NT_RESOURCE.equals(resourceType)) {
+            return getValueMap().get(JCR_DATA, InputStream.class);
+        }
+        else if (NT_FILE.equals(resourceType)) {
+            Resource contentResource = getChild(JCR_CONTENT);
+            if (contentResource != null) {
+                return contentResource.getValueMap().get(JCR_DATA, InputStream.class);
+            }
+        }
+        return null;
+    }
 
     @Override
     public ValueMap getValueMap() {
diff --git a/src/test/java/org/apache/sling/testing/resourceresolver/NtFileResourceTest.java b/src/test/java/org/apache/sling/testing/resourceresolver/NtFileResourceTest.java
new file mode 100644
index 0000000..fef5bc5
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/resourceresolver/NtFileResourceTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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.resourceresolver;
+
+import static org.apache.sling.testing.resourceresolver.MockResource.JCR_CONTENT;
+import static org.apache.sling.testing.resourceresolver.MockResource.JCR_DATA;
+import static org.apache.sling.testing.resourceresolver.MockResource.JCR_PRIMARYTYPE;
+import static org.apache.sling.testing.resourceresolver.MockResource.NT_FILE;
+import static org.apache.sling.testing.resourceresolver.MockResource.NT_RESOURCE;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.io.IOUtils;
+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.api.resource.ValueMap;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableMap;
+
+/**
+ * Implements simple write and read resource and values test.
+ * Sling CRUD API is used to create the test data.
+ */
+public class NtFileResourceTest {
+
+    private static final byte[] BINARY_VALUE = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
+
+    private ResourceResolver resourceResolver;
+    private Resource testRoot;
+
+    @Before
+    public final void setUp() throws IOException, LoginException {
+        resourceResolver = new MockResourceResolverFactory().getResourceResolver(null);
+        Resource root = resourceResolver.getResource("/");
+        testRoot = resourceResolver.create(root, "test", ValueMap.EMPTY);
+    }
+
+    @Test
+    public void testNtFile() throws IOException {
+        Resource file = resourceResolver.create(testRoot, "ntFile", ImmutableMap.<String, Object>builder()
+                .put(JCR_PRIMARYTYPE, NT_FILE)
+                .build());
+        resourceResolver.create(file, JCR_CONTENT, ImmutableMap.<String, Object>builder() 
+            .put(JCR_PRIMARYTYPE, NT_RESOURCE)
+            .put(JCR_DATA, new ByteArrayInputStream(BINARY_VALUE))
+            .build());
+
+        String path = testRoot.getPath() + "/ntFile";
+        Resource resource = resourceResolver.getResource(path);
+        InputStream is = resource.adaptTo(InputStream.class);
+        assertNotNull(is);
+        
+        assertArrayEquals(BINARY_VALUE, IOUtils.toByteArray(is));
+        is.close();
+    }
+
+    @Test
+    public void testNtResource() throws IOException {
+        resourceResolver.create(testRoot, "ntResource", ImmutableMap.<String, Object>builder()
+                .put(JCR_PRIMARYTYPE, NT_RESOURCE)
+                .put(JCR_DATA, new ByteArrayInputStream(BINARY_VALUE))
+                .build());
+
+        String path = testRoot.getPath() + "/ntResource";
+        Resource resource = resourceResolver.getResource(path);
+        InputStream is = resource.adaptTo(InputStream.class);
+        assertNotNull(is);
+        
+        assertArrayEquals(BINARY_VALUE, IOUtils.toByteArray(is));
+        is.close();
+    }
+
+}

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

[sling-org-apache-sling-testing-resourceresolver-mock] 03/15: Update to parent pom v19

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 58cd751e3f6bfd6e0f681a5bc7f3fb294cad1428
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Mon Mar 31 14:39:20 2014 +0000

    Update to parent pom v19
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1583337 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index 4d419e8..ad3db51 100644
--- a/pom.xml
+++ b/pom.xml
@@ -23,7 +23,7 @@
     <parent>
         <groupId>org.apache.sling</groupId>
         <artifactId>sling</artifactId>
-        <version>18</version>
+        <version>19</version>
         <relativePath>../../parent/pom.xml</relativePath>
     </parent>
 

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

[sling-org-apache-sling-testing-resourceresolver-mock] 06/15: SLING-3853 : MockResourceResolver does not extend SlingAdaptable. Apply patch from Stefan Seifert

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 6eecd9afb844234e09c59ab59d12dfadf06ddb67
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Fri Aug 15 16:18:33 2014 +0000

    SLING-3853 : MockResourceResolver does not extend SlingAdaptable. Apply patch from Stefan Seifert
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1618221 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/testing/resourceresolver/MockResourceResolver.java      | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
index 1c383c0..2269d7e 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
@@ -31,13 +31,14 @@ import java.util.Set;
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.sling.api.SlingConstants;
+import org.apache.sling.api.adapter.SlingAdaptable;
 import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.osgi.service.event.Event;
 
-public class MockResourceResolver implements ResourceResolver {
+public class MockResourceResolver extends SlingAdaptable implements ResourceResolver {
 
     private final Map<String, Map<String, Object>> resources;
 
@@ -54,11 +55,6 @@ public class MockResourceResolver implements ResourceResolver {
     }
 
     @Override
-    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
-        return null;
-    }
-
-    @Override
     public Resource resolve(final HttpServletRequest request, final String absPath) {
         return this.getResource(absPath);
     }

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

[sling-org-apache-sling-testing-resourceresolver-mock] 02/15: SLING-3417 : Search paths must end with a slash

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit b03ae42b18f57d26431d758aa4daf715910a6456
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Wed Feb 26 06:52:42 2014 +0000

    SLING-3417 : Search paths must end with a slash
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1571935 13f79535-47bb-0310-9956-ffa450edef68
---
 .../testing/resourceresolver/MockResourceResolverFactoryOptions.java    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java
index 6572797..734d804 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java
@@ -27,7 +27,7 @@ public class MockResourceResolverFactoryOptions {
 
     private EventAdmin eventAdmin;
 
-    private String[] searchPaths = new String[] {"/apps", "/libs"};
+    private String[] searchPaths = new String[] {"/apps/", "/libs/"};
 
     public EventAdmin getEventAdmin() {
         return eventAdmin;

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