Skip to content

Apache Lucene open-source search software

LUCENE-5207: Add a test that checks if the stack trace of an exception thrown from a Javascript function contains the original expression source code as the "filename".

Uwe Schindlerf6e125ce66c9
diff --git a/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java b/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java
index ed6d897da2f..50ad143be46 100644
--- a/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java
+++ b/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java
@@ -17,6 +17,8 @@ package org.apache.lucene.expressions.js;
* limitations under the License.
*/
+import java.io.PrintWriter;
+import java.io.StringWriter;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
@@ -233,4 +235,29 @@ public class TestCustomFunctions extends LuceneTestCase {
assertTrue(e.getMessage().contains("is not declared by a class which is accessible by the given parent ClassLoader"));
}
}
+
+ static String MESSAGE = "This should not happen but it happens";
+
+ public static class StaticThrowingException {
+ public static double method() { throw new ArithmeticException(MESSAGE); }
+ }
+
+ /** the method throws an exception. We should check the stack trace that it contains the source code of the expression as file name. */
+ public void testThrowingException() throws Exception {
+ Map<String,Method> functions = new HashMap<String,Method>();
+ functions.put("foo", StaticThrowingException.class.getMethod("method"));
+ String source = "3 * foo() / 5";
+ Expression expr = JavascriptCompiler.compile(source, functions, getClass().getClassLoader());
+ try {
+ expr.evaluate(0, null);
+ fail();
+ } catch (ArithmeticException e) {
+ assertEquals(MESSAGE, e.getMessage());
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ e.printStackTrace(pw);
+ pw.flush();
+ assertTrue(sw.toString().contains("JavascriptCompiler$CompiledExpression.evaluate(" + source + ")"));
+ }
+ }
}