src/test/java/org/apidesign/java4browser/StaticMethodTest.java
changeset 2 4679bf342a1f
parent 1 48b1dce93691
child 3 e44f0155d946
     1.1 --- a/src/test/java/org/apidesign/java4browser/StaticMethodTest.java	Mon Aug 27 05:17:08 2012 +0200
     1.2 +++ b/src/test/java/org/apidesign/java4browser/StaticMethodTest.java	Mon Aug 27 05:47:44 2012 +0200
     1.3 @@ -32,23 +32,58 @@
     1.4   */
     1.5  public class StaticMethodTest {
     1.6      @Test public void threePlusFour() throws Exception {
     1.7 -        Invocable i = compileClass("StaticMethod.class");
     1.8 -        
     1.9 -        Object ret = i.invokeFunction("org_apidesign_java4browser_StaticMethod_sumIII", 3, 4);
    1.10 -        assertEquals(ret, Double.valueOf(7), "Should be seven");
    1.11 +        assertExec(
    1.12 +            "Should be seven", 
    1.13 +            "org_apidesign_java4browser_StaticMethod_sumIII", 
    1.14 +            Double.valueOf(7), 
    1.15 +            3, 4
    1.16 +        );
    1.17      }
    1.18  
    1.19      @Test public void powerOfThree() throws Exception {
    1.20 -        Invocable i = compileClass("StaticMethod.class");
    1.21 -        
    1.22 -        Object ret = i.invokeFunction("org_apidesign_java4browser_StaticMethod_powerFF", 3.0f);
    1.23 -        assertEquals(ret, Double.valueOf(9), "Should be nine");
    1.24 +        assertExec(
    1.25 +            "Should be nine", 
    1.26 +            "org_apidesign_java4browser_StaticMethod_powerFF", 
    1.27 +            Double.valueOf(9),
    1.28 +            3.0f
    1.29 +        );
    1.30      }
    1.31  
    1.32 -    static Invocable compileClass(String name) throws ScriptException, IOException {
    1.33 +    @Test public void doubleWithoutLong() throws Exception {
    1.34 +        assertExec(
    1.35 +            "Should be two",
    1.36 +            "org_apidesign_java4browser_StaticMethod_minusDDJ", 
    1.37 +            Double.valueOf(2),
    1.38 +            3.0d, 1l
    1.39 +        );
    1.40 +    }
    1.41 +    
    1.42 +    private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    1.43 +        StringBuilder sb = new StringBuilder();
    1.44 +        Invocable i = compileClass("StaticMethod.class", sb);
    1.45 +        
    1.46 +        Object ret = null;
    1.47 +        try {
    1.48 +            ret = i.invokeFunction(methodName, args);
    1.49 +        } catch (NoSuchMethodException ex) {
    1.50 +            fail("Cannot find method in " + sb, ex);
    1.51 +        }
    1.52 +        if (ret == null && expRes == null) {
    1.53 +            return;
    1.54 +        }
    1.55 +        if (expRes.equals(ret)) {
    1.56 +            return;
    1.57 +        }
    1.58 +        assertEquals(ret, expRes, msg + "\n" + sb);
    1.59 +        
    1.60 +    }
    1.61 +
    1.62 +    static Invocable compileClass(String name, StringBuilder sb) throws ScriptException, IOException {
    1.63          InputStream is = StaticMethodTest.class.getResourceAsStream(name);
    1.64          assertNotNull(is, "Class file found");
    1.65 -        StringBuilder sb = new StringBuilder();
    1.66 +        if (sb == null) {
    1.67 +            sb = new StringBuilder();
    1.68 +        }
    1.69          ByteCodeToJavaScript.compile(name, is, sb);
    1.70          ScriptEngineManager sem = new ScriptEngineManager();
    1.71          ScriptEngine js = sem.getEngineByExtension("js");