Author: tommaso
Date: Fri Oct 14 10:50:42 2016
New Revision: 1764869

URL: http://svn.apache.org/viewvc?rev=1764869&view=rev
Log:
SLING-6054 - fixed missing paths check

Added:
    
sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/trigger/impl/AbstractJcrEventTriggerTest.java
   (with props)
Modified:
    sling/trunk/contrib/extensions/distribution/core/pom.xml
    
sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/trigger/impl/AbstractJcrEventTrigger.java
    
sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/trigger/impl/JcrEventDistributionTriggerFactory.java

Modified: sling/trunk/contrib/extensions/distribution/core/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/core/pom.xml?rev=1764869&r1=1764868&r2=1764869&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/distribution/core/pom.xml (original)
+++ sling/trunk/contrib/extensions/distribution/core/pom.xml Fri Oct 14 
10:50:42 2016
@@ -119,6 +119,12 @@
             <version>2.6.4</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            
<artifactId>org.apache.sling.testing.resourceresolver-mock</artifactId>
+            <version>1.1.14</version>
+            <scope>test</scope>
+        </dependency>
         <!-- SLING -->
         <dependency>
             <groupId>org.apache.sling</groupId>

Modified: 
sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/trigger/impl/AbstractJcrEventTrigger.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/trigger/impl/AbstractJcrEventTrigger.java?rev=1764869&r1=1764868&r2=1764869&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/trigger/impl/AbstractJcrEventTrigger.java
 (original)
+++ 
sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/trigger/impl/AbstractJcrEventTrigger.java
 Fri Oct 14 10:50:42 2016
@@ -26,6 +26,7 @@ import javax.jcr.observation.EventIterat
 import javax.jcr.observation.EventListener;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -70,7 +71,8 @@ public abstract class AbstractJcrEventTr
 
     private final Scheduler scheduler;
 
-    AbstractJcrEventTrigger(SlingRepository repository, Scheduler scheduler, 
ResourceResolverFactory resolverFactory, String path, String serviceUser) {
+    AbstractJcrEventTrigger(SlingRepository repository, Scheduler scheduler, 
ResourceResolverFactory resolverFactory,
+                            String path, String serviceUser) {
         this.resolverFactory = resolverFactory;
         if (path == null || serviceUser == null) {
             throw new IllegalArgumentException("path and service are 
required");
@@ -107,7 +109,7 @@ public abstract class AbstractJcrEventTr
         }
     }
 
-    private class JcrEventDistributionTriggerListener implements EventListener 
{
+    class JcrEventDistributionTriggerListener implements EventListener {
         private final DistributionRequestHandler requestHandler;
 
         public JcrEventDistributionTriggerListener(DistributionRequestHandler 
requestHandler) {
@@ -145,7 +147,7 @@ public abstract class AbstractJcrEventTr
         }
     }
 
-    private void addToList(DistributionRequest request, 
List<DistributionRequest> requestList) {
+    void addToList(DistributionRequest request, List<DistributionRequest> 
requestList) {
         DistributionRequest lastRequest = requestList.isEmpty() ? null : 
requestList.get(requestList.size() - 1);
 
         if (lastRequest == null || 
!lastRequest.getRequestType().equals(request.getRequestType())) {
@@ -157,20 +159,20 @@ public abstract class AbstractJcrEventTr
             allPaths.addAll(Arrays.asList(lastRequest.getPaths()));
             allPaths.addAll(Arrays.asList(request.getPaths()));
 
-            addMissingPaths(request, allPaths);
+            addMissingPaths(allPaths);
 
             lastRequest = new 
SimpleDistributionRequest(lastRequest.getRequestType(), allPaths.toArray(new 
String[allPaths.size()]));
             requestList.set(requestList.size() - 1, lastRequest);
         }
     }
 
-    private void addMissingPaths(DistributionRequest request, Set<String> 
allPaths) {
-        List<String> requestPaths = Arrays.asList(request.getPaths());
+    void addMissingPaths(Set<String> allPaths) {
+        Set<String> newPaths = new HashSet<String>();
 
-        for (String path : requestPaths) {
+        for (String path : allPaths) {
             for (String existingPath : allPaths) {
                 // in case a requested path is descendant of an existing path, 
also add its siblings
-                if (existingPath.length() > path.length() && 
path.startsWith(existingPath)) {
+                if (path.length() > existingPath.length() && 
path.startsWith(existingPath)) {
                     ResourceResolver resourceResolver = null;
                     try {
                         resourceResolver = 
DistributionUtils.loginService(resolverFactory, serviceUser);
@@ -179,7 +181,7 @@ public abstract class AbstractJcrEventTr
                             for (Resource child : 
resource.getParent().getChildren()) {
                                 String childPath = child.getPath();
                                 if (!childPath.equals(path)) {
-                                    allPaths.add(childPath);
+                                    newPaths.add(childPath);
                                 }
                             }
                         } else {
@@ -194,8 +196,12 @@ public abstract class AbstractJcrEventTr
             }
         }
 
+        if (!newPaths.isEmpty()) {
+            allPaths.addAll(newPaths);
+        }
     }
 
+
     public void enable() {
 
     }
@@ -266,7 +272,7 @@ public abstract class AbstractJcrEventTr
     }
 
 
-    private boolean hasDeepPaths(DistributionRequest distributionRequest) {
+    private static boolean hasDeepPaths(DistributionRequest 
distributionRequest) {
         if 
(!DistributionRequestType.ADD.equals(distributionRequest.getRequestType())) {
             return false;
         }

Modified: 
sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/trigger/impl/JcrEventDistributionTriggerFactory.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/trigger/impl/JcrEventDistributionTriggerFactory.java?rev=1764869&r1=1764868&r2=1764869&view=diff
==============================================================================
--- 
sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/trigger/impl/JcrEventDistributionTriggerFactory.java
 (original)
+++ 
sling/trunk/contrib/extensions/distribution/core/src/main/java/org/apache/sling/distribution/trigger/impl/JcrEventDistributionTriggerFactory.java
 Fri Oct 14 10:50:42 2016
@@ -85,13 +85,12 @@ public class JcrEventDistributionTrigger
 
 
     @Activate
-    public void activate(BundleContext bundleContext, Map<String, Object> 
config) {
+    public void activate(Map<String, Object> config) {
         String path = PropertiesUtil.toString(config.get(PATH), null);
         String serviceName = 
SettingsUtils.removeEmptyEntry(PropertiesUtil.toString(config.get(SERVICE_NAME),
 null));
         String[] ignoredPathsPatterns = 
PropertiesUtil.toStringArray(config.get(IGNORED_PATHS_PATTERNS), null);
         ignoredPathsPatterns = 
SettingsUtils.removeEmptyEntries(ignoredPathsPatterns);
 
-
         trigger = new JcrEventDistributionTrigger(repository, scheduler, 
resolverFactory, path, serviceName, ignoredPathsPatterns);
         trigger.enable();
     }

Added: 
sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/trigger/impl/AbstractJcrEventTriggerTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/trigger/impl/AbstractJcrEventTriggerTest.java?rev=1764869&view=auto
==============================================================================
--- 
sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/trigger/impl/AbstractJcrEventTriggerTest.java
 (added)
+++ 
sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/trigger/impl/AbstractJcrEventTriggerTest.java
 Fri Oct 14 10:50:42 2016
@@ -0,0 +1,99 @@
+/*
+ * 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.distribution.trigger.impl;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.observation.Event;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.commons.scheduler.Scheduler;
+import org.apache.sling.distribution.DistributionRequest;
+import org.apache.sling.distribution.DistributionRequestType;
+import org.apache.sling.distribution.SimpleDistributionRequest;
+import org.apache.sling.jcr.api.SlingRepository;
+import org.apache.sling.testing.resourceresolver.MockHelper;
+import org.apache.sling.testing.resourceresolver.MockResourceResolverFactory;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+
+/**
+ *
+ */
+public class AbstractJcrEventTriggerTest {
+
+    private MockHelper helper;
+    private MockResourceResolverFactory rrf;
+
+    @Before
+    public void setUp() throws Exception {
+        rrf = new MockResourceResolverFactory();
+        ResourceResolver resourceResolver = rrf.getResourceResolver(null);
+        helper = 
MockHelper.create(resourceResolver).resource("/a").resource("b").resource("c").resource("d")
+                .resource("e").resource("f").resource("g").p("foo", 
true).resource(".h").p("foo", false);
+        helper.commit();
+    }
+
+    @Test//"SLING-6054"
+    public void addToListTest() throws Exception {
+        SlingRepository repository = mock(SlingRepository.class);
+        Scheduler scheduler = mock(Scheduler.class);
+        String path = "/";
+        String serviceUser = "service-user";
+        AbstractJcrEventTrigger trigger = new 
AbstractJcrEventTrigger(repository, scheduler, rrf, path, serviceUser) {
+            @Override
+            protected DistributionRequest processEvent(Event event) throws 
RepositoryException {
+                return null;
+            }
+        };
+
+        String descendant = "/a/b/c/d/e/f/h";
+        String ancestor = "/a/b/c/d";
+
+        List<DistributionRequest> requests = new 
LinkedList<DistributionRequest>();
+        requests.add(new 
SimpleDistributionRequest(DistributionRequestType.ADD, descendant));
+        DistributionRequest newRequest = new 
SimpleDistributionRequest(DistributionRequestType.ADD, ancestor);
+        trigger.addToList(newRequest, requests);
+
+        assertEquals(1, requests.size());
+        assertEquals(3, requests.get(0).getPaths().length);
+        String[] paths = requests.get(0).getPaths();
+        assertEquals(ancestor, paths[0]);
+        assertEquals("/a/b/c/d/e/f/g", paths[1]); // the missing path is added
+        assertEquals(descendant, paths[2]);
+
+        // invert order of requests
+        requests = new LinkedList<DistributionRequest>();
+        requests.add(new 
SimpleDistributionRequest(DistributionRequestType.ADD, ancestor));
+        newRequest = new 
SimpleDistributionRequest(DistributionRequestType.ADD, descendant);
+        trigger.addToList(newRequest, requests);
+
+        assertEquals(1, requests.size());
+        assertEquals(3, requests.get(0).getPaths().length);
+        paths = requests.get(0).getPaths();
+        assertEquals(ancestor, paths[0]);
+        assertEquals("/a/b/c/d/e/f/g", paths[1]); // the missing path is added
+        assertEquals(descendant, paths[2]);
+    }
+
+}
\ No newline at end of file

Propchange: 
sling/trunk/contrib/extensions/distribution/core/src/test/java/org/apache/sling/distribution/trigger/impl/AbstractJcrEventTriggerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to