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

bdelacretaz pushed a commit to branch SLING-10224/withMainIT
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-launcher.git


The following commit(s) were added to refs/heads/SLING-10224/withMainIT by this 
push:
     new 9a2b081  SLING-10224 - add ITs for variable substitution
9a2b081 is described below

commit 9a2b0813dc00135e41ad57e0bb6f7ef5cda8c702
Author: Bertrand Delacretaz <[email protected]>
AuthorDate: Thu Mar 18 12:45:03 2021 +0100

    SLING-10224 - add ITs for variable substitution
---
 .../feature/launcher/impl/VariableSubstitutor.java | 39 ++++++++++++++
 .../launcher/impl/launchers/FrameworkLauncher.java | 18 ++-----
 .../apache/sling/feature/launcher/impl/MainIT.java |  3 ++
 .../launcher/impl/VariableSubstitutorIT.java       | 62 ++++++++++++++++++++++
 4 files changed, 107 insertions(+), 15 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/feature/launcher/impl/VariableSubstitutor.java 
b/src/main/java/org/apache/sling/feature/launcher/impl/VariableSubstitutor.java
new file mode 100644
index 0000000..5058570
--- /dev/null
+++ 
b/src/main/java/org/apache/sling/feature/launcher/impl/VariableSubstitutor.java
@@ -0,0 +1,39 @@
+/*
+ * 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.feature.launcher.impl;
+
+import org.apache.commons.text.StringSubstitutor;
+import org.apache.commons.text.lookup.StringLookup;
+import org.apache.sling.feature.launcher.spi.LauncherRunContext;
+
+/** StringSubstitutor that uses a LauncherRunContext */
+public class VariableSubstitutor extends StringSubstitutor {
+    public VariableSubstitutor(LauncherRunContext context) {
+        super(new StringLookup() {
+
+            @Override
+            public String lookup(final String key) {
+                // Normally if a variable cannot be found, StrSubstitutor will
+                // leave the raw variable in place. We need to replace it with
+                // nothing in that case.
+                final String value = context.getFrameworkProperties().get(key);
+                return value == null ? "" : value;
+            }
+        });
+        setEnableSubstitutionInVariables(true);
+    }
+}
\ No newline at end of file
diff --git 
a/src/main/java/org/apache/sling/feature/launcher/impl/launchers/FrameworkLauncher.java
 
b/src/main/java/org/apache/sling/feature/launcher/impl/launchers/FrameworkLauncher.java
index 6c5f6ee..e88a566 100644
--- 
a/src/main/java/org/apache/sling/feature/launcher/impl/launchers/FrameworkLauncher.java
+++ 
b/src/main/java/org/apache/sling/feature/launcher/impl/launchers/FrameworkLauncher.java
@@ -23,10 +23,9 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.Callable;
 
-import org.apache.commons.text.StringSubstitutor;
-import org.apache.commons.text.lookup.StringLookup;
 import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.launcher.impl.VariableSubstitutor;
 import org.apache.sling.feature.launcher.spi.Launcher;
 import org.apache.sling.feature.launcher.spi.LauncherPrepareContext;
 import org.apache.sling.feature.launcher.spi.LauncherRunContext;
@@ -53,22 +52,11 @@ public class FrameworkLauncher implements Launcher {
      */
     @Override
     public int run(final LauncherRunContext context, final ClassLoader cl) 
throws Exception {
-        StringSubstitutor ss = new StringSubstitutor(new StringLookup() {
-
-            @Override
-            public String lookup(final String key) {
-                // Normally if a variable cannot be found, StrSubstitutor will
-                // leave the raw variable in place. We need to replace it with
-                // nothing in that case.
-                final String value = context.getFrameworkProperties().get(key);
-                return value == null ? "" : value;
-            }
-        });
-        ss.setEnableSubstitutionInVariables(true);
+        final VariableSubstitutor vs = new VariableSubstitutor(context);
 
         Map<String, String> properties = new HashMap<>();
         context.getFrameworkProperties().forEach((key, value) -> {
-            properties.put(key, ss.replace(value).replace("{dollar}", "$"));
+            properties.put(key, vs.replace(value).replace("{dollar}", "$"));
         });
         if (context.getLogger().isDebugEnabled()) {
             context.getLogger().debug("Bundles:");
diff --git a/src/test/java/org/apache/sling/feature/launcher/impl/MainIT.java 
b/src/test/java/org/apache/sling/feature/launcher/impl/MainIT.java
index 28aa517..3a270bc 100644
--- a/src/test/java/org/apache/sling/feature/launcher/impl/MainIT.java
+++ b/src/test/java/org/apache/sling/feature/launcher/impl/MainIT.java
@@ -35,6 +35,9 @@ import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
+/** Run this as part of integration tests to help make sure we
+ *  embed the right Apache commons classes.
+*/
 public class MainIT {
 
     protected static class SystemExitException extends SecurityException {
diff --git 
a/src/test/java/org/apache/sling/feature/launcher/impl/VariableSubstitutorIT.java
 
b/src/test/java/org/apache/sling/feature/launcher/impl/VariableSubstitutorIT.java
new file mode 100644
index 0000000..cab834b
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/feature/launcher/impl/VariableSubstitutorIT.java
@@ -0,0 +1,62 @@
+/*
+ * 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.feature.launcher.impl;
+
+import org.apache.sling.feature.launcher.spi.LauncherRunContext;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/** Run this as part of integration tests to help make sure we
+ *  embed the right Apache commons classes.
+*/
+public class VariableSubstitutorIT {
+    private VariableSubstitutor vs;
+
+    @Before
+    public void setup() {
+        final LauncherRunContext context = mock(LauncherRunContext.class);
+
+        final Map<String, String> props = new HashMap<>();
+        props.put("1","one exactly");
+        props.put("two", "here's ${1} and two");
+
+        when(context.getFrameworkProperties()).thenReturn(props);
+        vs = new VariableSubstitutor(context);
+    }
+
+    @Test
+    public void simpleReplacement() {
+        assertEquals("It is one exactly and two", vs.replace("It is ${1} and 
two"));
+    }
+
+    @Test
+    public void recursiveReplacement() {
+        assertEquals("Now here's one exactly and two for testing", 
vs.replace("Now ${two} for testing"));
+    }
+
+    @Test
+    public void missingKey() {
+        assertEquals("one exactly #", vs.replace("${1} ${notFoundOder}#"));
+    }
+}

Reply via email to