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 09:59:54 UTC

[sling-org-apache-sling-resourcemerger] 13/31: SLING-4340 : SLING-4247 created odd side-effects

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

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

commit 677a077ff97e843b8b49da5e54910d477ebac7ab
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Thu Jan 29 08:01:43 2015 +0000

    SLING-4340 : SLING-4247 created odd side-effects
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/resourcemerger@1655545 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/resourcemerger/impl/MergedResource.java  |  26 ++--
 .../resourcemerger/impl/MergedResourceTest.java    | 142 +++++++++++++++++++++
 2 files changed, 159 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
index 7b00b98..daa45e1 100644
--- a/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
+++ b/src/main/java/org/apache/sling/resourcemerger/impl/MergedResource.java
@@ -41,6 +41,9 @@ public class MergedResource extends AbstractResource {
     /** Resource type. */
     private final String resourceType;
 
+    /** Resource super type. */
+    private final String resourceSuperType;
+
     /** Resource meta data. */
     private final ResourceMetadata metadata = new ResourceMetadata();
 
@@ -64,18 +67,24 @@ public class MergedResource extends AbstractResource {
         this.path = (relativePath.length() == 0 ? mergeRootPath : mergeRootPath + "/" + relativePath);
         this.properties = new DeepReadValueMapDecorator(this, new MergedValueMap(valueMaps));
         // get resource type
-        String rt = this.properties.get(ResourceResolver.PROPERTY_RESOURCE_TYPE, String.class);
+        final String slingPropRT = this.properties.get(ResourceResolver.PROPERTY_RESOURCE_TYPE, String.class);
+        String rt = slingPropRT;
         if (rt == null) {
             rt = relativePath.length() == 0 ? "/" : relativePath;
-            // use the resource type of the last resource in the set that provides one
-            for(final Resource rsrc : mappedResources) {
-                final String value = rsrc.getResourceType();
-                if ( value != null ) {
-                    rt = value;
-                }
+        }
+        // use the resource type of the last resource in the set that provides one
+        for(final Resource rsrc : mappedResources) {
+            final String value = rsrc.getResourceType();
+            if ( value != null ) {
+                rt = value;
             }
         }
         this.resourceType = rt;
+        if ( !rt.equals(slingPropRT) ) {
+            this.resourceSuperType = slingPropRT;
+        } else {
+            this.resourceSuperType = null;
+        }
         metadata.put(MergedResourceConstants.METADATA_FLAG, true);
         final String[] resourcePaths = new String[mappedResources.size()];
         int i = 0;
@@ -104,8 +113,7 @@ public class MergedResource extends AbstractResource {
      * {@inheritDoc}
      */
     public String getResourceSuperType() {
-        // So far, there's no concept of resource super type for a merged resource
-        return null;
+        return this.resourceSuperType;
     }
 
     /**
diff --git a/src/test/java/org/apache/sling/resourcemerger/impl/MergedResourceTest.java b/src/test/java/org/apache/sling/resourcemerger/impl/MergedResourceTest.java
new file mode 100644
index 0000000..591037d
--- /dev/null
+++ b/src/test/java/org/apache/sling/resourcemerger/impl/MergedResourceTest.java
@@ -0,0 +1,142 @@
+/*
+ * 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.resourcemerger.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.api.wrappers.ValueMapDecorator;
+import org.apache.sling.testing.resourceresolver.MockResource;
+import org.junit.Test;
+
+public class MergedResourceTest {
+
+    @Test public void testResourceTypeByMethod() throws Exception {
+
+        final MockResource r1 = new MockResource("/a", null, null) {
+
+            @Override
+            public String getResourceType() {
+                return "a";
+            }
+        };
+        final MockResource r2 = new MockResource("/b", null, null) {
+
+            @Override
+            public String getResourceType() {
+                return "b";
+            }
+        };
+        final MockResource r3 = new MockResource("/c", null, null) {
+
+            @Override
+            public String getResourceType() {
+                return "c";
+            }
+        };
+
+        final List<Resource> resources = new ArrayList<Resource>();
+        resources.add(r1);
+        resources.add(r2);
+        resources.add(r3);
+
+        final MergedResource mr = new MergedResource(null, "/a", "a", resources, Collections.EMPTY_LIST);
+
+        assertEquals("c", mr.getResourceType());
+        assertNull(mr.getResourceSuperType());
+    }
+
+    @Test public void testResourceTypeByValueMap() throws Exception {
+        final ValueMap vm1 = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"vma"));
+        final MockResource r1 = new MockResource("/a", vm1, null);
+        final ValueMap vm2 = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"vmb"));
+        final MockResource r2 = new MockResource("/b", vm2, null);
+        final ValueMap vm3 = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"vmc"));
+        final MockResource r3 = new MockResource("/c", vm3, null);
+
+        final List<Resource> resources = new ArrayList<Resource>();
+        resources.add(r1);
+        resources.add(r2);
+        resources.add(r3);
+
+        final List<ValueMap> valueMaps = new ArrayList<ValueMap>();
+        valueMaps.add(vm1);
+        valueMaps.add(vm2);
+        valueMaps.add(vm3);
+
+        final MergedResource mr = new MergedResource(null, "/a", "a", resources, valueMaps);
+
+        assertEquals("vmc", mr.getResourceType());
+        assertNull(mr.getResourceSuperType());
+    }
+
+    @Test public void testResourceTypeMixed() throws Exception {
+        final ValueMap vm1 = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"vma"));
+        final MockResource r1 = new MockResource("/a", vm1, null) {
+
+            @Override
+            public String getResourceType() {
+                return "a";
+            }
+        };
+        final ValueMap vm2 = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"vmb"));
+        final MockResource r2 = new MockResource("/b", vm2, null) {
+
+            @Override
+            public String getResourceType() {
+                return "b";
+            }
+        };
+        final ValueMap vm3 = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object)"vmc"));
+        final MockResource r3 = new MockResource("/c", vm3, null) {
+
+            @Override
+            public String getResourceType() {
+                return "c";
+            }
+        };
+
+        final List<Resource> resources = new ArrayList<Resource>();
+        resources.add(r1);
+        resources.add(r2);
+        resources.add(r3);
+
+        final List<ValueMap> valueMaps = new ArrayList<ValueMap>();
+        valueMaps.add(vm1);
+        valueMaps.add(vm2);
+        valueMaps.add(vm3);
+
+        final MergedResource mr1 = new MergedResource(null, "/a", "a", resources, valueMaps);
+
+        assertEquals("c", mr1.getResourceType());
+        assertEquals("vmc", mr1.getResourceSuperType());
+
+        final MergedResource mr2 = new MergedResource(null, "/a", "a", resources, Collections.singletonList(vm2));
+
+        assertEquals("c", mr2.getResourceType());
+        assertEquals("vmb", mr2.getResourceSuperType());
+    }
+}

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