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);
}
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);
}
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]);
}
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"));
}
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]);
}
Aggregations