You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/06/27 14:41:45 UTC

[commons-text] 03/09: Add org.apache.commons.text.lookup.StringLookupFactory.functionStringLookup(Function).

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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git

commit bb9b4e8e15518ea38f917b837b1d8238356f85a1
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 10:03:25 2020 -0400

    Add org.apache.commons.text.lookup.StringLookupFactory.functionStringLookup(Function<String,
    V>).
---
 ...ubstitutorWithInterpolatorStringLookupTest.java | 27 ++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java b/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
index 2c94e5f..890e94f 100644
--- a/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/StringSubstitutorWithInterpolatorStringLookupTest.java
@@ -36,6 +36,11 @@ public class StringSubstitutorWithInterpolatorStringLookupTest {
         testCustomMapWithDefaults(true);
     }
 
+    @Test
+    public void testCustomFunctionWithDefaults() {
+        testCustomFunctionWithDefaults(true);
+    }
+
     private void testCustomMapWithDefaults(final boolean addDefaultLookups) {
         final String key = "key";
         final String value = "value";
@@ -53,12 +58,34 @@ public class StringSubstitutorWithInterpolatorStringLookupTest {
         Assertions.assertEquals("value", strSubst.replace("${customLookup:key}"));
     }
 
+    private void testCustomFunctionWithDefaults(final boolean addDefaultLookups) {
+        final String key = "key";
+        final String value = "value";
+        final Map<String, String> map = new HashMap<>();
+        map.put(key, value);
+        final StringLookup mapStringLookup = StringLookupFactory.INSTANCE.functionStringLookup(k -> map.get(k));
+        final Map<String, StringLookup> stringLookupMap = new HashMap<>();
+        stringLookupMap.put("customLookup", mapStringLookup);
+        final StringSubstitutor strSubst = new StringSubstitutor(
+            StringLookupFactory.INSTANCE.interpolatorStringLookup(stringLookupMap, null, addDefaultLookups));
+        if (addDefaultLookups) {
+            final String spKey = "user.name";
+            Assertions.assertEquals(System.getProperty(spKey), strSubst.replace("${sys:" + spKey + "}"));
+        }
+        Assertions.assertEquals("value", strSubst.replace("${customLookup:key}"));
+    }
+
     @Test
     public void testCustomMapWithoutDefaults() {
         testCustomMapWithDefaults(false);
     }
 
     @Test
+    public void testCustomFunctionWithoutDefaults() {
+        testCustomFunctionWithDefaults(false);
+    }
+
+    @Test
     public void testDefaultInterpolator() {
         // Used to cut and paste into the docs.
         // @formatter:off