Search in sources :

Example 11 with Expression

use of org.apache.lucene.expressions.Expression in project lucene-solr by apache.

the class TestCustomFunctions method testNamespaces.

/** test that namespaces work with custom expressions. */
public void testNamespaces() throws Exception {
    Map<String, Method> functions = new HashMap<>();
    functions.put("foo.bar", getClass().getMethod("zeroArgMethod"));
    String source = "foo.bar()";
    Expression expr = JavascriptCompiler.compile(source, functions, getClass().getClassLoader());
    assertEquals(5, expr.evaluate(null), DELTA);
}
Also used : HashMap(java.util.HashMap) Expression(org.apache.lucene.expressions.Expression) Method(java.lang.reflect.Method)

Example 12 with Expression

use of org.apache.lucene.expressions.Expression in project lucene-solr by apache.

the class TestCustomFunctions method testTwoMethods.

/** tests a map with 2 functions */
public void testTwoMethods() throws Exception {
    Map<String, Method> functions = new HashMap<>();
    functions.put("foo", getClass().getMethod("zeroArgMethod"));
    functions.put("bar", getClass().getMethod("oneArgMethod", double.class));
    Expression expr = JavascriptCompiler.compile("foo() + bar(3)", functions, getClass().getClassLoader());
    assertEquals(11, expr.evaluate(null), DELTA);
}
Also used : HashMap(java.util.HashMap) Expression(org.apache.lucene.expressions.Expression) Method(java.lang.reflect.Method)

Example 13 with Expression

use of org.apache.lucene.expressions.Expression in project lucene-solr by apache.

the class TestJavascriptCompiler method doTestValidVariable.

void doTestValidVariable(String variable, String output) throws Exception {
    Expression e = JavascriptCompiler.compile(variable);
    assertNotNull(e);
    assertEquals(1, e.variables.length);
    assertEquals(output, e.variables[0]);
}
Also used : Expression(org.apache.lucene.expressions.Expression)

Example 14 with Expression

use of org.apache.lucene.expressions.Expression in project lucene-solr by apache.

the class TestCustomFunctions method testClassLoader.

/** uses this test with a different classloader and tries to
   * register it using the default classloader, which should fail */
public void testClassLoader() throws Exception {
    ClassLoader thisLoader = getClass().getClassLoader();
    Loader childLoader = new Loader(thisLoader);
    Class<?> fooClass = childLoader.createFakeClass();
    Method barMethod = fooClass.getMethod("bar");
    Map<String, Method> functions = Collections.singletonMap("bar", barMethod);
    assertNotSame(thisLoader, fooClass.getClassLoader());
    assertNotSame(thisLoader, barMethod.getDeclaringClass().getClassLoader());
    // this should pass:
    Expression expr = JavascriptCompiler.compile("bar()", functions, childLoader);
    assertEquals(2.0, expr.evaluate(null), DELTA);
    // use our classloader, not the foreign one, which should fail!
    IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
        JavascriptCompiler.compile("bar()", functions, thisLoader);
    });
    assertTrue(expected.getMessage().contains("is not declared by a class which is accessible by the given parent ClassLoader"));
    // mix foreign and default functions
    Map<String, Method> mixedFunctions = new HashMap<>(JavascriptCompiler.DEFAULT_FUNCTIONS);
    mixedFunctions.putAll(functions);
    expr = JavascriptCompiler.compile("bar()", mixedFunctions, childLoader);
    assertEquals(2.0, expr.evaluate(null), DELTA);
    expr = JavascriptCompiler.compile("sqrt(20)", mixedFunctions, childLoader);
    assertEquals(Math.sqrt(20), expr.evaluate(null), DELTA);
    // use our classloader, not the foreign one, which should fail!
    expected = expectThrows(IllegalArgumentException.class, () -> {
        JavascriptCompiler.compile("bar()", mixedFunctions, thisLoader);
    });
    assertTrue(expected.getMessage().contains("is not declared by a class which is accessible by the given parent ClassLoader"));
}
Also used : Expression(org.apache.lucene.expressions.Expression) HashMap(java.util.HashMap) Method(java.lang.reflect.Method)

Example 15 with Expression

use of org.apache.lucene.expressions.Expression in project lucene-solr by apache.

the class TestJavascriptCompiler method testVariableNormalization.

public void testVariableNormalization() throws Exception {
    // multiple double quotes
    Expression x = JavascriptCompiler.compile("foo[\"a\"][\"b\"]");
    assertEquals("foo['a']['b']", x.variables[0]);
    // single and double in the same var
    x = JavascriptCompiler.compile("foo['a'][\"b\"]");
    assertEquals("foo['a']['b']", x.variables[0]);
    // escapes remain the same in single quoted strings
    x = JavascriptCompiler.compile("foo['\\\\\\'\"']");
    assertEquals("foo['\\\\\\'\"']", x.variables[0]);
    // single quotes are escaped
    x = JavascriptCompiler.compile("foo[\"'\"]");
    assertEquals("foo['\\'']", x.variables[0]);
    // double quotes are unescaped
    x = JavascriptCompiler.compile("foo[\"\\\"\"]");
    assertEquals("foo['\"']", x.variables[0]);
    // backslash escapes are kept the same
    x = JavascriptCompiler.compile("foo['\\\\'][\"\\\\\"]");
    assertEquals("foo['\\\\']['\\\\']", x.variables[0]);
}
Also used : Expression(org.apache.lucene.expressions.Expression)

Aggregations

Expression (org.apache.lucene.expressions.Expression)18 Method (java.lang.reflect.Method)8 HashMap (java.util.HashMap)8 ParseException (java.text.ParseException)4 SimpleBindings (org.apache.lucene.expressions.SimpleBindings)4 SortField (org.apache.lucene.search.SortField)4 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 AccessControlContext (java.security.AccessControlContext)1 VariableContext (org.apache.lucene.expressions.js.VariableContext)1 FacetResult (org.apache.lucene.facet.FacetResult)1 Facets (org.apache.lucene.facet.Facets)1 FacetsCollector (org.apache.lucene.facet.FacetsCollector)1 TaxonomyFacetSumValueSource (org.apache.lucene.facet.taxonomy.TaxonomyFacetSumValueSource)1 TaxonomyReader (org.apache.lucene.facet.taxonomy.TaxonomyReader)1 DirectoryTaxonomyReader (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 ValueSource (org.apache.lucene.queries.function.ValueSource)1 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1