/** * <a * href="http://sourceforge.net/tracker/?func=detail&aid=2081602&group_id=4075&atid=104075">Sourceforge * issue "NullPointerException Thrown by Overriden Method" - ID: 2081602</a>. Overriding a method * which is invoked from super-constructor issues a NPE. */ @Test @Category(KnownIssue.class) public void sourceforge_issue_2081602() throws Exception { // Interpreter.DEBUG = true; Callable result = (Callable) TestUtil.eval( "Object echo(msg, x) {", " print(msg + ' ' + x);", " return x;", "}", "public class A implements " + Callable.class.getName() + " {", " int _id;", " public A (int id) {", " print (\" A.<init> \" + id);", " setId(id);", " }", " public void setId (int id) {", " print (\" A.setId \" + id);", " _id = id;", " }", " public Object call() { return _id; }", "}", "public class B extends A {", " public B (int id) {", " super (echo(\" B.<init>\", id * 3));", " }", " public void setId (int id) {", " print (\" B.setId \" + id);", " super.setId(id * 5);", " }", "}", "return new B (1);"); Assert.assertEquals(15, result.call()); }
/** * <a * href="http://sourceforge.net/tracker/?func=detail&aid=2081602&group_id=4075&atid=104075">Sourceforge * issue "NullPointerException Thrown by Overriden Method" - ID: 2081602</a>. Just a "learning * test" to check the call flow for constructors of generated classes. * * @see #sourceforge_issue_2081602 */ @Test public void sourceforge_issue_2081602_learning_test() throws Exception { final Object result = TestUtil.eval( "Object echo(msg, x) {", " print(msg + ' ' + x);", " return x;", "}", "public class A implements java.util.concurrent.Callable {", " int _id;", " public A (int id) {", " _id = echo(\"A\", id);", " }", " public Object call() { return _id; }", "}", "public class B extends A {", " public B (int id) {", " super (echo(\"B\", id * 2));", " }", "}", "return new B (2);"); Assert.assertEquals(4, ((java.util.concurrent.Callable) result).call()); }
@Test public void misc_tests() throws Exception { Assert.assertEquals(true, TestUtil.eval("return true == true;")); Assert.assertEquals(true, TestUtil.eval("return false == false;")); Assert.assertEquals(false, TestUtil.eval("return true == false;")); Assert.assertEquals(false, TestUtil.eval("return false == true;")); try { TestUtil.eval("throw new RuntimeException();"); Assert.fail(); } catch (TargetError e) { Assert.assertTrue(e.getTarget().getClass() == RuntimeException.class); } Assert.assertEquals( "foobar", TestUtil.eval( "String a;", "try {", " a = \"foobar\";", "} catch (Exception e) {", " throw e;", "}", "return a;")); String script = "boolean fieldBool = false;\n" + "int fieldInt = 0;\n" + "Boolean fieldBool2 = false;\n" + "void run() {\n" + "fieldBool = ! fieldBool;\n" + "fieldBool2 = ! fieldBool2;\n" + "fieldInt++;\n" + "System.out.println(\"fieldBool: \"+fieldBool);\n" + "System.out.println(\"fieldBool2: \"+fieldBool2);\n" + "System.out.println(\"fieldInt: \"+fieldInt);\n" + "}\n"; Interpreter bsh = new Interpreter(); bsh.eval(script); Runnable runnable = (Runnable) bsh.getInterface(Runnable.class); runnable.run(); }