Author: radu Date: Wed Jul 13 10:04:14 2016 New Revision: 1752383 URL: http://svn.apache.org/viewvc?rev=1752383&view=rev Log: SLING-5838 - ResourceChangeListenerInfo does not expand relative paths correctly
* made sure that all the configuration paths for ResourceChangeListeners are normalised before storing them in ResourceChangeListenerInfo * added tests to check the behaviour Added: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/observation/ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfoTest.java Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfo.java Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfo.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfo.java?rev=1752383&r1=1752382&r2=1752383&view=diff ============================================================================== --- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfo.java (original) +++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfo.java Wed Jul 13 10:04:14 2016 @@ -28,6 +28,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Set; +import org.apache.sling.api.resource.ResourceUtil; import org.apache.sling.api.resource.observation.ExternalResourceChangeListener; import org.apache.sling.api.resource.observation.ResourceChange.ChangeType; import org.apache.sling.api.resource.path.PathSet; @@ -61,16 +62,17 @@ public class ResourceChangeListenerInfo final String paths[] = toStringArray(ref.getProperty(PATHS), null); if ( paths != null ) { for(final String p : paths) { - if ( p.isEmpty() ) { + String normalisedPath = ResourceUtil.normalize(p); + if (!".".equals(p) && normalisedPath.isEmpty()) { configValid = false; - } else if ( p.startsWith("/") ) { - pathsSet.add(p); + } else if ( normalisedPath.startsWith("/") ) { + pathsSet.add(normalisedPath); } else { for(final String sp : searchPaths) { if ( p.equals(".") ) { pathsSet.add(sp); } else { - pathsSet.add(sp + p); + pathsSet.add(ResourceUtil.normalize(sp + normalisedPath)); } } } Added: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfoTest.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfoTest.java?rev=1752383&view=auto ============================================================================== --- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfoTest.java (added) +++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfoTest.java Wed Jul 13 10:04:14 2016 @@ -0,0 +1,50 @@ +/******************************************************************************* + * 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.resourceresolver.impl.observation; + +import java.util.Set; + +import org.apache.sling.api.resource.observation.ResourceChangeListener; +import org.junit.Test; +import org.osgi.framework.ServiceReference; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ResourceChangeListenerInfoTest { + + @Test + public void testGetExpandedRelativePaths() { + ServiceReference reference = mock(ServiceReference.class); + when(reference.getProperty(ResourceChangeListener.PATHS)).thenReturn(new String[] {"./**/*.html"}); + final ResourceChangeListenerInfo rcli = new ResourceChangeListenerInfo(reference, new String[] {"/apps/", "/libs/"}); + Set<String> paths = rcli.getPaths().toStringSet(); + assertTrue("PathSet " + paths.toString() + " does not contain /apps/**/*.html.", paths.contains("/apps/**/*.html")); + assertTrue("PathSet " + paths.toString() + " does not contain /libs/**/*.html.", paths.contains("/libs/**/*.html")); + } + + @Test + public void testDotPathConfig() { + ServiceReference reference = mock(ServiceReference.class); + when(reference.getProperty(ResourceChangeListener.PATHS)).thenReturn(new String[] {"."}); + final ResourceChangeListenerInfo rcli = new ResourceChangeListenerInfo(reference, new String[] {"/apps/", "/libs/"}); + Set<String> paths = rcli.getPaths().toStringSet(); + assertTrue("PathSet " + paths.toString() + " does not contain /apps/", paths.contains("/apps/")); + assertTrue("PathSet " + paths.toString() + " does not contain /libs/.", paths.contains("/libs/")); + } +}