lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cpoersc...@apache.org
Subject lucene-solr:branch_6x: SOLR-9412: Add failOnMissingParams option to MacroExpander, add TestMacroExpander class. (Jon Dorando, Christine Poerschke)
Date Mon, 22 Aug 2016 12:54:54 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/branch_6x 9722f006b -> c2a8814df


SOLR-9412: Add failOnMissingParams option to MacroExpander, add TestMacroExpander class. (Jon
Dorando, Christine Poerschke)


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/c2a8814d
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/c2a8814d
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/c2a8814d

Branch: refs/heads/branch_6x
Commit: c2a8814df23ddbb07c6d02fd72e07334aad88692
Parents: 9722f00
Author: Christine Poerschke <cpoerschke@apache.org>
Authored: Mon Aug 22 10:36:07 2016 +0100
Committer: Christine Poerschke <cpoerschke@apache.org>
Committed: Mon Aug 22 12:03:06 2016 +0100

----------------------------------------------------------------------
 solr/CHANGES.txt                                |   4 +
 .../solr/request/macro/MacroExpander.java       |  12 ++
 .../solr/request/macro/TestMacroExpander.java   | 116 +++++++++++++++++++
 3 files changed, 132 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/c2a8814d/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 6aaa764..6b64f1f 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -34,7 +34,11 @@ Bug Fixes
 
 * SOLR-9310: PeerSync fails on a node restart due to IndexFingerPrint mismatch (Pushkar Raste,
noble)
 
+Other Changes
+----------------------
 
+* SOLR-9412: Add failOnMissingParams option to MacroExpander, add TestMacroExpander class.
+  (Jon Dorando, Christine Poerschke)
 
 ==================  6.2.0 ==================
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/c2a8814d/solr/core/src/java/org/apache/solr/request/macro/MacroExpander.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/request/macro/MacroExpander.java b/solr/core/src/java/org/apache/solr/request/macro/MacroExpander.java
index ff8a19b..7db4cb0 100644
--- a/solr/core/src/java/org/apache/solr/request/macro/MacroExpander.java
+++ b/solr/core/src/java/org/apache/solr/request/macro/MacroExpander.java
@@ -34,10 +34,16 @@ public class MacroExpander {
   private String macroStart = MACRO_START;
   private char escape = '\\';
   private int level;
+  private final boolean failOnMissingParams;
 
 
   public MacroExpander(Map<String,String[]> orig) {
+    this(orig, false);
+  }
+
+  public MacroExpander(Map<String,String[]> orig, boolean failOnMissingParams) {
     this.orig = orig;
+    this.failOnMissingParams = failOnMissingParams;
   }
 
   public static Map<String,String[]> expand(Map<String,String[]> params) {
@@ -163,8 +169,14 @@ public class MacroExpander {
         String replacement = replacementList!=null ? replacementList[0] : defVal;
         if (replacement != null) {
           String expandedReplacement = expand(replacement);
+          if (failOnMissingParams && expandedReplacement == null) {
+            return null;
+          }
           sb.append(expandedReplacement);
         }
+        else if (failOnMissingParams) {
+          return null;
+        }
 
       } catch (SyntaxError syntaxError) {
         // append the part we would have skipped

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/c2a8814d/solr/core/src/test/org/apache/solr/request/macro/TestMacroExpander.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/request/macro/TestMacroExpander.java b/solr/core/src/test/org/apache/solr/request/macro/TestMacroExpander.java
new file mode 100644
index 0000000..5b16a11
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/request/macro/TestMacroExpander.java
@@ -0,0 +1,116 @@
+/*
+ * 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.solr.request.macro;
+
+import java.util.Map;
+import java.util.HashMap;
+
+import org.apache.lucene.util.LuceneTestCase;
+import org.junit.Test;
+
+/*
+ * Tests for the MacroExpander
+ */
+public class TestMacroExpander extends LuceneTestCase {
+
+  @Test
+  public void testExamples() {
+    final Map<String,String[]> testParams = new HashMap<String,String[]>();
+    final MacroExpander me;
+    // example behavior unaffected by absence or value of failOnMissingParams
+    if (random().nextBoolean()) {
+      me = new MacroExpander(testParams);
+    } else {
+      final boolean failOnMissingParams = random().nextBoolean();
+      me = new MacroExpander(testParams, failOnMissingParams);
+    }
+
+    //default examples: https://cwiki.apache.org/confluence/display/solr/Parameter+Substitution
+    // and http://yonik.com/solr-query-parameter-substitution/
+
+    //using params
+    String[] lowParams = {"50"};
+    testParams.put("low",lowParams);
+    String[] highParams = {"100"};
+    testParams.put("high",highParams);
+
+    String testQuery = "q=popularity:[ ${low} TO ${high} ]";
+
+    assertEquals("q=popularity:[ 50 TO 100 ]", me.expand(testQuery));
+
+    //using default values
+    testQuery = "q=popularity:[ ${low:10} TO ${high:20} ]";
+    assertEquals("q=popularity:[ 50 TO 100 ]", me.expand(testQuery));
+
+    testParams.clear();
+    assertEquals("q=popularity:[ 10 TO 20 ]", me.expand(testQuery));
+
+    //multiple levels of substitutions
+    testQuery = "q=${pop_query}";
+    String[] popQueryParams = {"${pop_field}:[ ${low} TO ${high} ] AND inStock:true"};
+    String[] popFieldParams = {"popularity"};
+    testParams.put("low",lowParams);
+    testParams.put("high",highParams);
+    testParams.put("pop_query",popQueryParams);
+    testParams.put("pop_field",popFieldParams);
+
+    assertEquals("q=popularity:[ 50 TO 100 ] AND inStock:true", me.expand(testQuery));
+
+    //end default examples
+  }
+
+  @Test
+  public void testOnMissingParams() {
+    final Map<String,String[]> testParams = new HashMap<String,String[]>();
+    final MacroExpander meSkipOnMissingParams = new MacroExpander(testParams);
+    final MacroExpander meFailOnMissingParams = new MacroExpander(testParams, true);
+
+    final String low = "50";
+    final String high = "100";
+    testParams.put("low", new String[]{ low });
+    testParams.put("high", new String[]{ high });
+
+    final String testQuery = "q=popularity:[ ${low} TO ${high} ]";
+
+    //when params all present the expansion results match
+    final String expandedQuery = "q=popularity:[ "+low+" TO "+high+" ]";
+    assertEquals(expandedQuery, meSkipOnMissingParams.expand(testQuery));
+    assertEquals(expandedQuery, meFailOnMissingParams.expand(testQuery));
+
+    //when param(s) missing and have no default the expansion results differ
+    final String expandedLow;
+    final String expandedHigh;
+    if (random().nextBoolean()) { // keep low
+      expandedLow = low;
+    } else {
+      expandedLow = "";
+      testParams.remove("low");
+    }
+    if (random().nextBoolean()) { // keep high
+      expandedHigh = high;
+    } else {
+      expandedHigh = "";
+      testParams.remove("high");
+    }
+    assertEquals("q=popularity:[ "+expandedLow+" TO "+expandedHigh+" ]",
+        meSkipOnMissingParams.expand(testQuery));
+    if (testParams.size() < 2) { // at least one of the two parameters missing
+      assertEquals(null, meFailOnMissingParams.expand(testQuery));
+    }
+  }
+
+}


Mime
View raw message