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 2019/12/20 00:56:28 UTC

[commons-text] branch master updated: Text 174: ScriptStringLookup does not accept ":" (#126)

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


The following commit(s) were added to refs/heads/master by this push:
     new 2e8f42f  Text 174: ScriptStringLookup does not accept ":" (#126)
2e8f42f is described below

commit 2e8f42fb6c502e373e39e8c22dd6ac2aac92defa
Author: furkilic <fu...@gmail.com>
AuthorDate: Fri Dec 20 01:56:22 2019 +0100

    Text 174: ScriptStringLookup does not accept ":" (#126)
    
    * Prepare Tests For change
    
    * TEST-174: Limit the number of split to 2
    
    * TEXT-174: Correct CheckStyle Violation
    
    * TEXT-174: Add Test with no column
    
    * TEXT-174: Corrections asked by @kinow
    
    Co-authored-by: furkan-kilic <fu...@gmail.com>
---
 .../apache/commons/text/lookup/ScriptStringLookup.java  | 17 +++++++++--------
 .../commons/text/lookup/ScriptStringLookupTest.java     | 13 +++++++++++++
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java b/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java
index 701d013..3029d73 100644
--- a/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java
@@ -25,9 +25,9 @@ import javax.script.ScriptEngineManager;
 import org.apache.commons.text.StringSubstitutor;
 
 /**
- * Looks up keys from an XML document.
+ * Executes the script with the given engine name.
  * <p>
- * Looks up the value for a given key in the format "Document:Key".
+ * Execute the script with the engine name in the format "EngineName:Script".
  * </p>
  * <p>
  * For example: {@code "javascript:3 + 4"}.
@@ -55,24 +55,25 @@ final class ScriptStringLookup extends AbstractStringLookup {
     }
 
     /**
-     * Looks up the value for the key in the format "DocumentPath:XPath".
+     * Execute the script with the engine name in the format "EngineName:Script".
+     * Extra colons will be ignored.
      * <p>
-     * For example: "com/domain/document.xml:/path/to/node".
+     * For example: {@code "javascript:3 + 4"}.
      * </p>
      *
      * @param key
-     *            the key to be looked up, may be null
-     * @return The value associated with the key.
+     *            the engine:script to execute, may be null
+     * @return The value returned by the execution.
      */
     @Override
     public String lookup(final String key) {
         if (key == null) {
             return null;
         }
-        final String[] keys = key.split(SPLIT_STR);
+        final String[] keys = key.split(SPLIT_STR, 2);
         final int keyLen = keys.length;
         if (keyLen != 2) {
-            throw IllegalArgumentExceptions.format("Bad script key format [%s]; expected format is DocumentPath:Key.",
+            throw IllegalArgumentExceptions.format("Bad script key format [%s]; expected format is EngineName:Script.",
                     key);
         }
         final String engineName = keys[0];
diff --git a/src/test/java/org/apache/commons/text/lookup/ScriptStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/ScriptStringLookupTest.java
index 2d8d73a..1680f90 100644
--- a/src/test/java/org/apache/commons/text/lookup/ScriptStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/ScriptStringLookupTest.java
@@ -58,4 +58,17 @@ public class ScriptStringLookupTest {
         Assertions.assertEquals("Hello World!", ScriptStringLookup.INSTANCE.lookup("javascript:\"Hello World!\""));
     }
 
+    @Test
+    public void testScriptUsingMultipleColons() {
+        Assertions.assertEquals("It Works",
+         ScriptStringLookup.INSTANCE.lookup("javascript:true ? \"It Works\" : \"It Does Not Work\" "));
+    }
+
+    @Test
+    public void testScriptMissingColon() {
+        assertThrows(IllegalArgumentException.class, () -> {
+            ScriptStringLookup.INSTANCE.lookup("javascript=\"test\"");
+        });
+    }
+
 }