You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ck...@apache.org on 2022/01/15 00:11:19 UTC

[logging-log4j2] branch release-2.x updated: Strengthen coverage of StrSubstitutor class (#696)

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

ckozak pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new fc734a7  Strengthen coverage of StrSubstitutor class (#696)
fc734a7 is described below

commit fc734a78094f91c5c1c08b02c705f75d6cfcde81
Author: Arie van Deursen <av...@users.noreply.github.com>
AuthorDate: Sat Jan 15 01:11:14 2022 +0100

    Strengthen coverage of StrSubstitutor class (#696)
    
    This commit
    
    - adds test cases for all examples listed in the `StrSubstitutor` javadoc
    - applies small fixes to the javadoc
    - adds test cases for value escape separator `:\\-` with and without a matcher for such escaped separators
    - results in full statement coverage of the `substitute` method
    - improves the branch coverage of the `substitute` method
    - improves the coverage of some helper methods.
---
 .../logging/log4j/core/lookup/StrSubstitutor.java  |  4 +-
 .../log4j/core/lookup/StrSubstitutorTest.java      | 94 ++++++++++++++++++++++
 2 files changed, 96 insertions(+), 2 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java
index 514ac54..2f7c95c 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java
@@ -59,7 +59,7 @@ import org.apache.logging.log4j.util.Strings;
  * The following example demonstrates this:
  * </p>
  * <pre>
- * Map valuesMap = HashMap();
+ * Map valuesMap = new HashMap&lt;&gt;();
  * valuesMap.put(&quot;animal&quot;, &quot;quick brown fox&quot;);
  * valuesMap.put(&quot;target&quot;, &quot;lazy dog&quot;);
  * String templateString = &quot;The ${animal} jumped over the ${target}.&quot;;
@@ -80,7 +80,7 @@ import org.apache.logging.log4j.util.Strings;
  * The following shows an example with variable default value settings:
  * </p>
  * <pre>
- * Map valuesMap = HashMap();
+ * Map valuesMap = new HashMap&lt;&gt;();
  * valuesMap.put(&quot;animal&quot;, &quot;quick brown fox&quot;);
  * valuesMap.put(&quot;target&quot;, &quot;lazy dog&quot;);
  * String templateString = &quot;The ${animal} jumped over the ${target}. ${undefined.number:-1234567890}.&quot;;
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java
index c00b85f..efb492c 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java
@@ -45,6 +45,100 @@ public class StrSubstitutorTest {
         System.setProperty(TESTKEY, TESTVAL);
     }
 
+    @Test
+    public void testJavaDocExample() {
+        final Map<String, String> valuesMap = new HashMap<>();
+        valuesMap.put("animal", "quick brown fox");
+        valuesMap.put("target", "lazy dog");
+        final String templateString = "The ${animal} jumped over the ${target}.";
+        final StrSubstitutor sub = new StrSubstitutor(valuesMap);
+        final String resolvedString = sub.replace(templateString);
+        assertEquals("The quick brown fox jumped over the lazy dog.", resolvedString);
+    }
+
+    @Test
+    public void testDelimiterExampleFromJavaDoc() {
+        final Map<String, String> valuesMap = new HashMap<>();
+        valuesMap.put("animal", "quick brown fox");
+        valuesMap.put("target", "lazy dog");
+        final String templateString = "The ${animal} jumped over the ${target}. ${undefined.number:-1234567890}";
+        final StrSubstitutor sub = new StrSubstitutor(valuesMap);
+        final String resolvedString = sub.replace(templateString);
+        assertEquals("The quick brown fox jumped over the lazy dog. 1234567890", resolvedString);
+    }
+
+    @Test
+    public void testEscapedRecursionExampleFromJavaDoc() {
+        final Map<String, String> valuesMap = new HashMap<>();
+        valuesMap.put("name", "x");
+        final String templateString = "The variable $${${name}} must be used.";
+        final StrSubstitutor sub = new StrSubstitutor(valuesMap);
+
+        sub.setEnableSubstitutionInVariables(false);
+        final String resolvedString = sub.replace(templateString);
+        assertEquals("The variable ${x} must be used.", resolvedString);
+
+        // Due to escaping enabling recursion now should make no difference.
+        sub.setEnableSubstitutionInVariables(true);
+        final String resolvedStringWithRecursion = sub.replace(templateString);
+        assertEquals(resolvedString, resolvedStringWithRecursion);
+    }
+
+    @Test
+    public void testPrePostfixRecursionExampleFromJavaDoc() {
+        final Map<String, String> valuesMap = new HashMap<>();
+        valuesMap.put("name", "x");
+        final String templateString = "The variable ${$[name]} must be used.";
+        final StrSubstitutor sub = new StrSubstitutor(valuesMap, "$[", "]");
+        final String resolvedString = sub.replace(templateString);
+        assertEquals("The variable ${x} must be used.", resolvedString);
+    }
+
+    @Test
+    public void testRecursionExampleFromJavaDoc() {
+        final Map<String, String> valuesMap = new HashMap<>();
+        valuesMap.put("name", "x");
+        valuesMap.put("x", "3");
+        final String templateString = "The value ${${name}} must be used.";
+        final StrSubstitutor sub = new StrSubstitutor(valuesMap);
+
+        sub.setEnableSubstitutionInVariables(false);
+        assertEquals("The value ${${name}} must be used.", sub.replace(templateString));
+
+        sub.setEnableSubstitutionInVariables(true);
+        assertEquals("The value 3 must be used.", sub.replace(templateString));
+    }
+
+    @Test
+    public void testValueEscapeDelimiter() {
+        final Map<String, String> valuesMap = new HashMap<>();
+        // Example from MainMapLookup. Key contains ":-"
+        valuesMap.put("main:--file", "path/file.txt");
+        // Create substitutor, initially without support for escaping :-
+        final StrSubstitutor sub = new StrSubstitutor(
+                new PropertiesLookup(valuesMap),
+                StrSubstitutor.DEFAULT_PREFIX,
+                StrSubstitutor.DEFAULT_SUFFIX,
+                StrSubstitutor.DEFAULT_ESCAPE,
+                StrSubstitutor.DEFAULT_VALUE_DELIMITER,
+                null // Ensure valueEscapeMatcher == null
+        );
+        // regular default values work without a valueEscapeMatcher.
+        assertEquals("3", sub.replace("${y:-3}"));
+        // variables with ':-' are treated as if they have a default value
+        assertEquals("-file", sub.replace("${main:--file}"));
+        // yet escaping doesn't work anymore (results in the original string).
+        assertEquals("${main:\\--file}", sub.replace("${main:\\--file}"));
+
+        // ensure there is valueEscapeMatcher (by resetting the value delimiter)
+        sub.setValueDelimiter(StrSubstitutor.DEFAULT_VALUE_DELIMITER_STRING);
+        // now the escaped variable with ":-" in it will be resolved.
+        assertEquals("path/file.txt", sub.replace("${main:\\--file}"));
+        // default values continue to work:
+        assertEquals("no help", sub.replace("${main:\\--help:-no help}"));
+        // even in minimalistic corner case:
+        assertEquals("", sub.replace("${:\\-:-}"));
+    }
 
     @Test
     public void testDefault() {