Hi all.
I did a test upgrade to 1.7.0 and some of my tests for JRuby’s
behaviour are failing so I just want to check to see what’s going on.
-
These fail because e.getCause() is an EvalFailedException, whereas
it was previously RaiseException. In both cases running the script in
MRI causes an exception to be “raised”… so I guess now I want to
also know what causes "Raise"Exception.@Test
public void testErrorBehaviourForErrorFromJavaSide() throws
Exception
{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByExtension(“rb”);try { Runnable runnable = new Runnable() { @Override public void run() { throw new RuntimeException("Got an error"); } }; engine.put("runnable", runnable); engine.put("$runnable", runnable); engine.eval("$runnable.run()"); fail("Expected ScriptException"); } catch (ScriptException e) { RaiseException cause = (RaiseException) e.getCause(); // Now we expect the real exception in here. RuntimeException realCause = (RuntimeException)
cause.getCause();
assertEquals("Wrong runtime exception", "Got an error",
realCause.getMessage());
}
}
@Test
public void testErrorBehaviourForUnknownSymbol() throws Exception
{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByExtension("rb");
try
{
engine.eval("# extra lines to confirm that \n" +
"# the line number is accurate \n" +
"no_method_with_this_name");
fail("Expected ScriptException");
}
catch (ScriptException e)
{
EvalFailedException cause = (EvalFailedException)
e.getCause();
// Neither printStackTrace nor toString should throw
exceptions.
cause.toString();
String stackTrace = ExceptionUtils.getFullStackTrace(cause);
// Checking that the string starts like this, which is the
hack I applied.
// MRI starts with this:
// -:1: undefined local variable or method
assertTrue(“Not seeing the expected error message”,
stackTrace.contains("(NameError) undefined
local variable or method `no_method_with_this_name’ for
main:Object"));
assertTrue(“Not seeing the expected line reference”,
stackTrace.contains("(:3)"));
}
}
-
These both fail with ParseFailedException (“invalid multibyte char
(US-ASCII)”) even though I’m passing the script as a string (plus I’m
not using US-ASCII in the first place, so I don’t know where JRuby is
pulling that from…) This looks like it might be a particularly nasty
bug.@Test
public void testUnicodeStringOutputDirectlyViaScriptEngine()
throws Exception
{
String orig = “\u30C6\u30B9\u30C8”;ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByExtension("rb"); ScriptContext context = new SimpleScriptContext(); StringWriter writer = new StringWriter(); context.setWriter(writer); Object returnedResult = engine.eval("str = \"" + orig + "\" ;
puts str ; str", context);
//noinspection ErrorNotRethrown
try
{
assertEquals("Wrong result returned", orig, returnedResult);
assertEquals("Wrong result printed", orig,
writer.toString().trim());
fail("Expected AssertionError. This is a good thing -
update the test.");
}
catch (AssertionError e)
{
// Currently expected due to the bug.
}
}
@Test
public void testUnicodeStringOutputDirectlyViaScriptContainer()
throws Exception
{
String orig = “\u30C6\u30B9\u30C8”;
ScriptingContainer container = new ScriptingContainer();
StringWriter writer = new StringWriter();
container.setWriter(writer);
Object returnedResult = container.runScriptlet("str = \"" +
orig + “” ; puts str ; str");
//noinspection ErrorNotRethrown
try
{
assertEquals("Wrong result returned", orig, returnedResult);
assertEquals("Wrong result printed", orig,
writer.toString().trim());
fail("Expected AssertionError. This is a good thing -
update the test.");
}
catch (AssertionError e)
{
// Currently expected due to the bug.
}
}
TX