public void testEval_context() throws Exception {
logger1.info("eval with context");
System.setProperty("org.jruby.embed.localvariable.behavior", "transient");
JRubyEngineFactory factory = new JRubyEngineFactory();
JRubyEngine engine = (JRubyEngine) factory.getScriptEngine();
ScriptContext context = new SimpleScriptContext();
StringWriter writer = new StringWriter();
StringWriter errorWriter = new StringWriter();
context.setWriter(writer);
context.setErrorWriter(errorWriter);
context.setAttribute("message", "Hello World!!!!!", ScriptContext.ENGINE_SCOPE);
engine.setContext(context);
String script = "puts message";
JRubyCompiledScript instance = (JRubyCompiledScript) engine.compile(script);
Object expResult = "Hello World!!!!!";
instance.eval(context);
Object result = writer.toString().trim();
assertEquals(expResult, result);
writer.close();
writer = new StringWriter();
context.setWriter(writer);
context.setAttribute("@message", "Say Hey.", ScriptContext.ENGINE_SCOPE);
engine.setContext(context);
script = "puts @message";
instance = (JRubyCompiledScript) engine.compile(script);
expResult = "Say Hey.";
instance.eval(context);
result = writer.toString().trim();
assertEquals(expResult, result);
context.setAttribute("@message", "Yeah!", ScriptContext.ENGINE_SCOPE);
engine.setContext(context);
expResult = "Say Hey.\nYeah!";
instance.eval(context);
result = writer.toString().trim();
assertEquals(expResult, result);
writer.close();
writer = new StringWriter();
context.setWriter(writer);
context.setAttribute("$message", "Hiya.", ScriptContext.ENGINE_SCOPE);
engine.setContext(context);
script = "puts $message";
instance = (JRubyCompiledScript) engine.compile(script);
expResult = "Hiya.";
instance.eval(context);