LUCENE-5207: add some checks and tests for illegal stuff

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene5207@1523300 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2013-09-14 19:33:57 +00:00
parent cdc36f41d2
commit 591a3b1ca1
2 changed files with 64 additions and 0 deletions

View File

@ -712,6 +712,14 @@ public class JavascriptCompiler {
if (!Modifier.isStatic(method.getModifiers())) {
throw new IllegalArgumentException(method + " is not static.");
}
if (!Modifier.isPublic(method.getModifiers())) {
throw new IllegalArgumentException(method + " is not public.");
}
for (Class<?> clazz : method.getParameterTypes()) {
if (!clazz.equals(double.class)) {
throw new IllegalArgumentException(method + " must take only double parameters");
}
}
if (method.getReturnType() != double.class) {
throw new IllegalArgumentException(method + " does not return a double.");
}

View File

@ -85,4 +85,60 @@ public class TestCustomFunctions extends LuceneTestCase {
Expression expr = JavascriptCompiler.compile("foo() + bar(3)", functions, getClass().getClassLoader());
assertEquals(11, expr.evaluate(0, null), DELTA);
}
public static String bogusReturnType() { return "bogus!"; }
/** wrong return type: must be double */
public void testWrongReturnType() throws Exception {
Map<String,Method> functions = new HashMap<String,Method>();
functions.put("foo", getClass().getMethod("bogusReturnType"));
try {
JavascriptCompiler.compile("foo()", functions, getClass().getClassLoader());
fail();
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("does not return a double"));
}
}
public static double bogusParameterType(String s) { return 0; }
/** wrong param type: must be doubles */
public void testWrongParameterType() throws Exception {
Map<String,Method> functions = new HashMap<String,Method>();
functions.put("foo", getClass().getMethod("bogusParameterType", String.class));
try {
JavascriptCompiler.compile("foo(2)", functions, getClass().getClassLoader());
fail();
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("must take only double parameters"));
}
}
public double nonStaticMethod() { return 0; }
/** wrong modifiers: must be static */
public void testWrongNotStatic() throws Exception {
Map<String,Method> functions = new HashMap<String,Method>();
functions.put("foo", getClass().getMethod("nonStaticMethod"));
try {
JavascriptCompiler.compile("foo()", functions, getClass().getClassLoader());
fail();
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("is not static"));
}
}
static double nonPublicMethod() { return 0; }
/** wrong modifiers: must be public */
public void testWrongNotPublic() throws Exception {
Map<String,Method> functions = new HashMap<String,Method>();
functions.put("foo", getClass().getDeclaredMethod("nonPublicMethod"));
try {
JavascriptCompiler.compile("foo()", functions, getClass().getClassLoader());
fail();
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("is not public"));
}
}
}