This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-installer-provider-jcr.git
The following commit(s) were added to refs/heads/master by this push:
new c772615 SLING-9031 SLING-8548 support enhanced run modes (#5)
c772615 is described below
commit c772615f22660146c7bfa6c20eaa9f301c70168b
Author: Konrad Windszus <[email protected]>
AuthorDate: Thu Jul 30 08:14:54 2020 +0200
SLING-9031 SLING-8548 support enhanced run modes (#5)
Both OR-logic and NOT operator are now supported
---
pom.xml | 18 +++++--
.../provider/jcr/impl/FolderNameFilter.java | 31 +++++------
.../provider/jcr/impl/InstallerConfig.java | 2 +-
.../provider/jcr/impl/FolderNameFilterTest.java | 48 +++++++----------
.../provider/jcr/impl/JcrInstallTestBase.java | 11 +++-
.../installer/provider/jcr/impl/MiscUtil.java | 2 +-
.../installer/provider/jcr/impl/MockSettings.java | 61 ----------------------
7 files changed, 58 insertions(+), 115 deletions(-)
diff --git a/pom.xml b/pom.xml
index e558c1f..ddfe754 100644
--- a/pom.xml
+++ b/pom.xml
@@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.sling</groupId>
<artifactId>sling-bundle-parent</artifactId>
- <version>38</version>
+ <version>39</version>
<relativePath />
</parent>
@@ -66,12 +66,10 @@
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.settings</artifactId>
- <version>1.0.0</version>
+ <version>1.3.11-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
- <!-- this version must embed the same version of
org.apache.felix.configadmin as this pom.xml to be able to read
- the .config files being generated by the write back feature -->
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.installer.core</artifactId>
<version>3.10.0</version>
@@ -110,6 +108,18 @@
<version>${jackrabbit.version}</version>
<scope>provided</scope>
</dependency>
+ <!-- Testing dependencies -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>3.3.3</version>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>jackrabbit-core</artifactId>
diff --git
a/src/main/java/org/apache/sling/installer/provider/jcr/impl/FolderNameFilter.java
b/src/main/java/org/apache/sling/installer/provider/jcr/impl/FolderNameFilter.java
index d8be90f..4e94d90 100644
---
a/src/main/java/org/apache/sling/installer/provider/jcr/impl/FolderNameFilter.java
+++
b/src/main/java/org/apache/sling/installer/provider/jcr/impl/FolderNameFilter.java
@@ -21,12 +21,11 @@ package org.apache.sling.installer.provider.jcr.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
-import java.util.Set;
import java.util.regex.Pattern;
+import org.apache.sling.settings.SlingSettingsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -40,7 +39,7 @@ import org.slf4j.LoggerFactory;
class FolderNameFilter {
private final Pattern pattern;
private final String regexp;
- private final Set<String> runModes;
+ private final SlingSettingsService slingSettings;
private final String [] rootPaths;
private final Map<String, Integer> rootPriorities = new HashMap<String,
Integer>();
private final Logger log = LoggerFactory.getLogger(getClass());
@@ -52,11 +51,11 @@ class FolderNameFilter {
public static final int RUNMODE_PRIORITY_BOOST = 1;
public static final int DEFAULT_ROOT_PRIORITY = 99;
- FolderNameFilter(final String [] rootsConfig, final String regexp, final
Set<String> runModes) {
+ FolderNameFilter(final String [] rootsConfig, final String regexp, final
SlingSettingsService slingSettings) {
final List<RootPathInfo> rootPathInfos = new ArrayList<RootPathInfo>();
this.regexp = regexp;
this.pattern = Pattern.compile(regexp);
- this.runModes = runModes;
+ this.slingSettings = slingSettings;
// Each entry in rootsConfig is like /libs:100, where 100
// is the priority.
@@ -132,7 +131,6 @@ class FolderNameFilter {
*/
int getPriority(final String path) {
int result = 0;
- List<String> modes = null;
boolean match = false;
// If path contains dots after the last /, remove suffixes
@@ -146,11 +144,11 @@ class FolderNameFilter {
if(lastSlash > 0) {
prefix = prefix.substring(lastSlash);
}
- if(prefix.indexOf(DOT) > 0) {
+ String modes = null;
+ if (prefix.indexOf(DOT) > 0) {
int pos = 0;
- modes = new LinkedList<String>();
+ String pathAndRunmodes = prefix;
while( (pos = prefix.lastIndexOf(DOT)) >= 0) {
- modes.add(prefix.substring(pos + 1));
prefix = prefix.substring(0, pos);
if(pattern.matcher(prefix).matches()) {
result = getRootPriority(path);
@@ -160,13 +158,12 @@ class FolderNameFilter {
// If path prefix matches, check that all our runmodes match
if(result > 0) {
- for(String m : modes) {
- if(runModes.contains(m)) {
- result += RUNMODE_PRIORITY_BOOST;
- } else {
- result = 0;
- break;
- }
+ modes = pathAndRunmodes.substring(pos+1);
+ int runModeMatchCount =
slingSettings.getBestRunModeMatchCountFromSpec(modes);
+ if (runModeMatchCount > 0) {
+ result += RUNMODE_PRIORITY_BOOST * runModeMatchCount;
+ } else {
+ result = 0;
}
}
@@ -189,7 +186,7 @@ class FolderNameFilter {
}
public String toString() {
- return getClass().getSimpleName() + " (" + regexp + "), RunModes=" +
runModes;
+ return getClass().getSimpleName() + " (" + regexp + "), active run
modes=" + slingSettings.getRunModes();
}
int getRootPriority(String path) {
diff --git
a/src/main/java/org/apache/sling/installer/provider/jcr/impl/InstallerConfig.java
b/src/main/java/org/apache/sling/installer/provider/jcr/impl/InstallerConfig.java
index 05b02e7..7c243b7 100644
---
a/src/main/java/org/apache/sling/installer/provider/jcr/impl/InstallerConfig.java
+++
b/src/main/java/org/apache/sling/installer/provider/jcr/impl/InstallerConfig.java
@@ -78,7 +78,7 @@ public class InstallerConfig {
// Setup folder filtering and watching
this.folderNameFilter = new
FolderNameFilter(configuration.sling_jcrinstall_search_path(),
- folderNameRegexp, settings.getRunModes());
+ folderNameRegexp, settings);
this.roots = folderNameFilter.getRootPaths();
// setup default path for new configurations
diff --git
a/src/test/java/org/apache/sling/installer/provider/jcr/impl/FolderNameFilterTest.java
b/src/test/java/org/apache/sling/installer/provider/jcr/impl/FolderNameFilterTest.java
index e9d3966..de83a3f 100644
---
a/src/test/java/org/apache/sling/installer/provider/jcr/impl/FolderNameFilterTest.java
+++
b/src/test/java/org/apache/sling/installer/provider/jcr/impl/FolderNameFilterTest.java
@@ -25,8 +25,13 @@ import static org.junit.Assert.assertTrue;
import java.util.HashSet;
import java.util.Set;
+import org.apache.sling.settings.SlingSettingsService;
+import org.apache.sling.settings.impl.SlingSettingsServiceImpl;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+@RunWith(MockitoJUnitRunner.class)
public class FolderNameFilterTest {
public static final String DEFAULT_REGEXP = ".*/install$";
public static final String CONFIG_REGEXP = ".*/config$";
@@ -36,7 +41,7 @@ public class FolderNameFilterTest {
public void testParseRootPaths() {
{
final String [] paths = { "a", "b/" };
- final FolderNameFilter f = new FolderNameFilter(paths,
DEFAULT_REGEXP, new HashSet<String>());
+ final FolderNameFilter f = new FolderNameFilter(paths,
DEFAULT_REGEXP, createSlingSettingsService(""));
assertEquals("/a", f.getRootPaths()[0]);
assertEquals("/b", f.getRootPaths()[1]);
assertEquals(FolderNameFilter.DEFAULT_ROOT_PRIORITY,
f.getRootPriority("/a/foo"));
@@ -45,7 +50,7 @@ public class FolderNameFilterTest {
}
{
final String [] paths = { "a:100", "/b/: 200 " };
- final FolderNameFilter f = new FolderNameFilter(paths,
DEFAULT_REGEXP, new HashSet<String>());
+ final FolderNameFilter f = new FolderNameFilter(paths,
DEFAULT_REGEXP, createSlingSettingsService(""));
assertEquals("/b", f.getRootPaths()[0]);
assertEquals("/a", f.getRootPaths()[1]);
assertEquals(100, f.getRootPriority("/a/foo"));
@@ -53,7 +58,7 @@ public class FolderNameFilterTest {
}
{
final String [] paths = { "a/:NOT_AN_INTEGER", "/b/: 200 " };
- final FolderNameFilter f = new FolderNameFilter(paths,
DEFAULT_REGEXP, new HashSet<String>());
+ final FolderNameFilter f = new FolderNameFilter(paths,
DEFAULT_REGEXP, createSlingSettingsService(""));
assertEquals("/b", f.getRootPaths()[0]);
assertEquals("/a", f.getRootPaths()[1]);
assertEquals(FolderNameFilter.DEFAULT_ROOT_PRIORITY,
f.getRootPriority("/a/foo"));
@@ -63,7 +68,7 @@ public class FolderNameFilterTest {
@Test
public void testNoRunMode() {
- final FolderNameFilter f = new FolderNameFilter(ROOTS, DEFAULT_REGEXP,
new HashSet<String>());
+ final FolderNameFilter f = new FolderNameFilter(ROOTS, DEFAULT_REGEXP,
createSlingSettingsService(""));
assertTrue("Test 1", f.getPriority("/libs/install") > 0);
assertFalse("Test 2", f.getPriority("/libs/install.bar") > 0);
}
@@ -71,8 +76,7 @@ public class FolderNameFilterTest {
@Test
public void testSingleMode() {
final Set<String> m = new HashSet<String>();
- m.add("dev");
- final FolderNameFilter f = new FolderNameFilter(ROOTS, DEFAULT_REGEXP,
m);
+ final FolderNameFilter f = new FolderNameFilter(ROOTS, DEFAULT_REGEXP,
createSlingSettingsService("dev"));
assertTrue("Test 1", f.getPriority("/libs/install") > 0);
assertFalse("Test 2", f.getPriority("/libs/install.bar") > 0);
assertTrue("Test 3", f.getPriority("/libs/install.dev") > 0);
@@ -84,11 +88,7 @@ public class FolderNameFilterTest {
@Test
public void testThreeModes() {
- final Set<String> m = new HashSet<String>();
- m.add("dev");
- m.add("web");
- m.add("staging");
- final FolderNameFilter f = new FolderNameFilter(ROOTS, DEFAULT_REGEXP,
m);
+ final FolderNameFilter f = new FolderNameFilter(ROOTS, DEFAULT_REGEXP,
createSlingSettingsService("dev,web,staging"));
assertTrue("Test 1",f.getPriority("/libs/install") > 0);
assertFalse("Test 2",f.getPriority("/libs/install.bar") > 0);
assertTrue("Test 3",f.getPriority("/libs/install.dev") > 0);
@@ -106,20 +106,14 @@ public class FolderNameFilterTest {
@Test
public void testRootPriorities() {
- final Set<String> m = new HashSet<String>();
- m.add("dev");
- final FolderNameFilter f = new FolderNameFilter(ROOTS, DEFAULT_REGEXP,
m);
+ final FolderNameFilter f = new FolderNameFilter(ROOTS, DEFAULT_REGEXP,
createSlingSettingsService("dev"));
assertEquals("/libs root", new Integer(100),
(Integer)f.getPriority("/libs/install"));
assertEquals("/apps root", new Integer(200),
(Integer)f.getPriority("/apps/install"));
}
@Test
public void testRunModePriorities() {
- final Set<String> m = new HashSet<String>();
- m.add("dev");
- m.add("prod");
- m.add("staging");
- final FolderNameFilter f = new FolderNameFilter(ROOTS, DEFAULT_REGEXP,
m);
+ final FolderNameFilter f = new FolderNameFilter(ROOTS, DEFAULT_REGEXP,
createSlingSettingsService("dev,prod,staging"));
assertEquals("Matches no runmode", new Integer(100),
(Integer)f.getPriority("/libs/install"));
assertEquals("Matches dev runmode", new Integer(201),
(Integer)f.getPriority("/apps/install.dev"));
assertEquals("Matches staging runmode", new Integer(201),
(Integer)f.getPriority("/apps/install.staging"));
@@ -130,11 +124,7 @@ public class FolderNameFilterTest {
@Test
public void testAuthorPriorities() {
- final Set<String> m = new HashSet<String>();
- m.add("author");
- m.add("mycompany");
- m.add("dev");
- final FolderNameFilter f = new FolderNameFilter(ROOTS, CONFIG_REGEXP,
m);
+ final FolderNameFilter f = new FolderNameFilter(ROOTS, CONFIG_REGEXP,
createSlingSettingsService("author,mycompany,dev"));
assertEquals("", new Integer(201),
(Integer)f.getPriority("/apps/somewhere/runmodes/config.author"));
assertEquals("", new Integer(202),
(Integer)f.getPriority("/apps/somewhere/runmodes/config.author.dev"));
assertEquals("", new Integer(203),
(Integer)f.getPriority("/apps/somewhere/runmodes/config.author.dev.mycompany"));
@@ -142,11 +132,7 @@ public class FolderNameFilterTest {
@Test
public void testDotsInPath() {
- final Set<String> m = new HashSet<String>();
- m.add("dev");
- m.add("prod");
- m.add("staging");
- final FolderNameFilter f = new FolderNameFilter(ROOTS, DEFAULT_REGEXP,
m);
+ final FolderNameFilter f = new FolderNameFilter(ROOTS, DEFAULT_REGEXP,
createSlingSettingsService("dev,prod,staging"));
assertEquals("Matches no runmode", new Integer(100),
(Integer)f.getPriority("/libs/foo.bar/install"));
assertEquals("Matches dev runmode", new Integer(201),
(Integer)f.getPriority("/apps/foo.bar/install.dev"));
assertEquals("Matches staging runmode", new Integer(201),
(Integer)f.getPriority("/apps/foo.bar/install.staging"));
@@ -154,4 +140,8 @@ public class FolderNameFilterTest {
assertEquals("Matches three runmodes (B)", new Integer(203),
(Integer)f.getPriority("/apps/foo.bar/install.dev.prod.staging"));
assertEquals("Matches three runmodes (C)", new Integer(103),
(Integer)f.getPriority("/libs/foo.bar/install.dev.prod.staging"));
}
+
+ private SlingSettingsService createSlingSettingsService(String runModes) {
+ return new SlingSettingsServiceImpl(runModes);
+ }
}
diff --git
a/src/test/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallTestBase.java
b/src/test/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallTestBase.java
index d08e8c2..61576f8 100644
---
a/src/test/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallTestBase.java
+++
b/src/test/java/org/apache/sling/installer/provider/jcr/impl/JcrInstallTestBase.java
@@ -21,15 +21,20 @@ package org.apache.sling.installer.provider.jcr.impl;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import java.util.Collections;
+
import javax.jcr.Session;
import org.apache.sling.commons.testing.jcr.EventHelper;
import org.apache.sling.installer.api.OsgiInstaller;
+import org.apache.sling.settings.SlingSettingsService;
+import org.apache.sling.settings.impl.SlingSettingsServiceImpl;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
+import org.osgi.framework.Constants;
/** Base test class with common utilities */
public abstract class JcrInstallTestBase {
@@ -43,6 +48,7 @@ public abstract class JcrInstallTestBase {
protected ContentHelper contentHelper;
protected JcrInstaller installer;
protected MockOsgiInstaller osgiInstaller;
+ protected SlingSettingsServiceImpl slingSettings;
@Before
public void setUp() throws Exception {
@@ -56,8 +62,9 @@ public abstract class JcrInstallTestBase {
}
osgiInstaller = new MockOsgiInstaller();
context.registerService(OsgiInstaller.class, osgiInstaller);
- context.runMode(MiscUtil.RUN_MODES);
-
+ // use custom slingSettings (sling Mocks does not yet support newest
version)
+ slingSettings = new SlingSettingsServiceImpl(MiscUtil.RUN_MODES);
+ context.registerService(SlingSettingsService.class, slingSettings,
Collections.singletonMap(Constants.SERVICE_RANKING, 1000));
installer = new JcrInstaller();
context.registerInjectActivateService(installer);
Thread.sleep(1000);
diff --git
a/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java
b/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java
index 3f6319f..a580b50 100644
--- a/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java
+++ b/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java
@@ -26,7 +26,7 @@ import org.apache.sling.commons.testing.jcr.EventHelper;
class MiscUtil {
public static String SEARCH_PATHS [] = { "/libs/", "/apps/" };
- public static String RUN_MODES [] = { "dev", "staging" };
+ public static String RUN_MODES = "dev,staging";
static private void waitForCycles(JcrInstaller installer, long
initialCycleCount, int expectedCycles, long timeoutMsec) throws Exception {
final long endTime = System.currentTimeMillis() + timeoutMsec;
diff --git
a/src/test/java/org/apache/sling/installer/provider/jcr/impl/MockSettings.java
b/src/test/java/org/apache/sling/installer/provider/jcr/impl/MockSettings.java
deleted file mode 100644
index 8f3af6f..0000000
---
a/src/test/java/org/apache/sling/installer/provider/jcr/impl/MockSettings.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * 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.installer.provider.jcr.impl;
-
-import java.net.URL;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Set;
-
-import org.apache.sling.settings.SlingSettingsService;
-
-public class MockSettings implements SlingSettingsService {
-
- private final Set<String> runModes;
-
- public MockSettings(final String[] modes) {
- this.runModes = new HashSet<String>();
- this.runModes.addAll(Arrays.asList(modes));
- }
-
- public Set<String> getRunModes() {
- return this.runModes;
- }
-
- public URL getSlingHome() {
- // TODO Auto-generated method stub
- return null;
- }
-
- public String getSlingHomePath() {
- // TODO Auto-generated method stub
- return null;
- }
-
- public String getSlingId() {
- // TODO Auto-generated method stub
- return null;
- }
-
- public String getAbsolutePathWithinSlingHome(String relativePath) {
- // TODO Auto-generated method stub
- return null;
- }
-
-}