sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sseif...@apache.org
Subject svn commit: r1758415 - in /sling/trunk/contrib/extensions/contextaware-config/impl/src: main/java/org/apache/sling/contextaware/config/resource/impl/ test/java/org/apache/sling/contextaware/config/resource/impl/
Date Tue, 30 Aug 2016 16:18:12 GMT
Author: sseifert
Date: Tue Aug 30 16:18:12 2016
New Revision: 1758415

URL: http://svn.apache.org/viewvc?rev=1758415&view=rev
Log:
SLING-5886 add impl and unit tests for getContextPath and getAllContextPaths

Modified:
    sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/ConfigurationResourceResolverImpl.java
    sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/ConfigurationResourceResolverImplTest.java

Modified: sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/ConfigurationResourceResolverImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/ConfigurationResourceResolverImpl.java?rev=1758415&r1=1758414&r2=1758415&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/ConfigurationResourceResolverImpl.java
(original)
+++ sling/trunk/contrib/extensions/contextaware-config/impl/src/main/java/org/apache/sling/contextaware/config/resource/impl/ConfigurationResourceResolverImpl.java
Tue Aug 30 16:18:12 2016
@@ -24,6 +24,8 @@ import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceUtil;
@@ -76,20 +78,20 @@ public class ConfigurationResourceResolv
 
 
     public List<String> getResolvePaths(final Resource contentResource) {
-        final List<String> refs = new ArrayList<String>();
+        final List<String> refs = new ArrayList<>();
 
         // find property reference
-        String ref = this.findConfigRef(contentResource);
+        ConfigReference ref = this.findConfigRef(contentResource);
 
         if (ref == null) {
             // nothing found so far, check if we are in a configured tree itself
             if (isAllowedConfigPath(contentResource.getPath())) {
-                ref = contentResource.getPath();
+                ref = new ConfigReference(contentResource, contentResource.getPath());
             }
         }
 
         if ( ref != null ) {
-            refs.add(ref);
+            refs.add(ref.getConfigReference());
         }
 
         // finally add the global fallbacks
@@ -117,7 +119,7 @@ public class ConfigurationResourceResolv
     }
 
 
-   private String findConfigRef(final Resource startResource) {
+   private ConfigReference findConfigRef(final Resource startResource) {
         // start at resource, go up
         Resource resource = startResource;
         while (resource != null) {
@@ -139,7 +141,7 @@ public class ConfigurationResourceResolv
                     }
 
                     if (ref != null) {
-                        return ref;
+                        return new ConfigReference(resource, ref);
                     }
 
                 } else {
@@ -153,6 +155,16 @@ public class ConfigurationResourceResolv
         // if hit root and nothing found, return null
         return null;
     }
+   
+    private Stream<ConfigReference> findAllConfigRefs(final Resource startResource)
{
+        ConfigReference ref = findConfigRef(startResource);
+        if (ref == null) {
+            return Stream.of();
+        }
+        else {
+            return Stream.concat(Stream.of(ref), findAllConfigRefs(ref.getContentResource().getParent()));
+        }
+    }
 
     private String getReference(final Resource resource) {
         final String ref = resource.getValueMap().get(PROPERTY_CONFIG, String.class);
@@ -221,8 +233,8 @@ public class ConfigurationResourceResolv
             logger.trace("- searching for list '{}'", name);
         }
 
-        final Set<String> names = new HashSet<String>();
-        final List<Resource> result = new ArrayList<Resource>();
+        final Set<String> names = new HashSet<>();
+        final List<Resource> result = new ArrayList<>();
         int idx = 1;
         for (String path : this.getResolvePaths(contentResource)) {
             Resource item = contentResource.getResourceResolver().getResource(path + "/"
+ name);
@@ -255,17 +267,36 @@ public class ConfigurationResourceResolv
 
     @Override
     public String getContextPath(Resource resource) {
-        // TODO: this is only a dummy implementation
-        String[] pathParts = resource.getPath().split("/");
-        return "/" + pathParts[1] + "/" + pathParts[2];
+        ConfigReference ref = findConfigRef(resource);
+        if (ref != null) {
+            return ref.getContentResource().getPath();
+        }
+        else {
+            return null;
+        }
     }
 
     @Override
-    public List<String> getAllContextPaths(Resource resource) {
-        // TODO: this is only a dummy implementation
-        List<String> items = new ArrayList<String>();
-        items.add(getContextPath(resource));
-        return items;
+    public Collection<String> getAllContextPaths(Resource resource) {
+        return findAllConfigRefs(resource)
+                .map(ref -> ref.getContentResource().getPath())
+                .collect(Collectors.toList());
+    }
+    
+    private static class ConfigReference {
+        private final Resource contentResource;
+        private final String configReference;
+        
+        public ConfigReference(Resource contentResource, String configReference) {
+            this.contentResource = contentResource;
+            this.configReference = configReference;
+        }
+        public Resource getContentResource() {
+            return contentResource;
+        }
+        public String getConfigReference() {
+            return configReference;
+        }
     }
 
 }

Modified: sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/ConfigurationResourceResolverImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/ConfigurationResourceResolverImplTest.java?rev=1758415&r1=1758414&r2=1758415&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/ConfigurationResourceResolverImplTest.java
(original)
+++ sling/trunk/contrib/extensions/contextaware-config/impl/src/test/java/org/apache/sling/contextaware/config/resource/impl/ConfigurationResourceResolverImplTest.java
Tue Aug 30 16:18:12 2016
@@ -32,6 +32,7 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 
 public class ConfigurationResourceResolverImplTest {
@@ -49,12 +50,14 @@ public class ConfigurationResourceResolv
         underTest = context.registerInjectActivateService(new ConfigurationResourceResolverImpl());
 
         // content resources
-        site1Page1 = context.create().resource("/content/site1/page1", ImmutableMap.<String,
Object>builder()
+        context.create().resource("/content/site1", ImmutableMap.<String, Object>builder()
                 .put("sling:config", "/config/site1")
                 .build());
-        site2Page1 = context.create().resource("/content/site2/page1", ImmutableMap.<String,
Object>builder()
+        site1Page1 = context.create().resource("/content/site1/page1");
+        site2Page1 = context.create().resource("/content/site2", ImmutableMap.<String,
Object>builder()
                 .put("sling:config", "/config/site2")
                 .build());
+        site2Page1 = context.create().resource("/content/site2/page1");
 
         // configuration
         context.create().resource("/libs/test");
@@ -67,12 +70,6 @@ public class ConfigurationResourceResolv
     }
 
     @Test
-    public void testGetValueMapContextPath() {
-        assertEquals("/content/site1", underTest.getContextPath(site1Page1));
-        assertEquals("/content/site2", underTest.getContextPath(site2Page1));
-    }
-
-    @Test
     public void testGetResource() {
         assertEquals("/config/site1/test", underTest.getResource(site1Page1, "test").getPath());
         assertEquals("/libs/test", underTest.getResource(site2Page1, "test").getPath());
@@ -102,4 +99,17 @@ public class ConfigurationResourceResolv
             assertTrue(expectedPaths.remove(rsrc.getPath()));
         }
     }
+
+    @Test
+    public void testGetContextPath() {
+        assertEquals("/content/site1", underTest.getContextPath(site1Page1));
+        assertEquals("/content/site2", underTest.getContextPath(site2Page1));
+    }
+
+    @Test
+    public void testGetAllContextPaths() {
+        assertEquals(ImmutableList.of("/content/site1"), underTest.getAllContextPaths(site1Page1));
+        assertEquals(ImmutableList.of("/content/site2"), underTest.getAllContextPaths(site2Page1));
+    }
+
 }



Mime
View raw message