You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2013/09/14 21:33:57 UTC

svn commit: r1523300 - in /lucene/dev/branches/lucene5207/lucene/expressions/src: java/org/apache/lucene/expressions/js/JavascriptCompiler.java test/org/apache/lucene/expressions/js/TestCustomFunctions.java

Author: rmuir
Date: Sat Sep 14 19:33:57 2013
New Revision: 1523300

URL: http://svn.apache.org/r1523300
Log:
LUCENE-5207: add some checks and tests for illegal stuff

Modified:
    lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java
    lucene/dev/branches/lucene5207/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java

Modified: lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java?rev=1523300&r1=1523299&r2=1523300&view=diff
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java (original)
+++ lucene/dev/branches/lucene5207/lucene/expressions/src/java/org/apache/lucene/expressions/js/JavascriptCompiler.java Sat Sep 14 19:33:57 2013
@@ -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.");
     }

Modified: lucene/dev/branches/lucene5207/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5207/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java?rev=1523300&r1=1523299&r2=1523300&view=diff
==============================================================================
--- lucene/dev/branches/lucene5207/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java (original)
+++ lucene/dev/branches/lucene5207/lucene/expressions/src/test/org/apache/lucene/expressions/js/TestCustomFunctions.java Sat Sep 14 19:33:57 2013
@@ -85,4 +85,60 @@ public class TestCustomFunctions extends
     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"));
+    }
+  }
 }