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".

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1525194 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2013-09-21 03:42:51 +00:00
parent 2f169863cb
commit f6e125ce66
1 changed files with 27 additions and 0 deletions

View File

@ -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 + ")"));
}
}
}