/** Test of eval method, of class JRubyEngine. */ @Test public void testEval() throws Exception { System.out.println("eval"); BSFManager.registerScriptingEngine( "jruby", "org.jruby.embed.bsf.JRubyEngine", new String[] {"rb"}); BSFManager manager = new BSFManager(); JRubyEngine instance = new JRubyEngine(); instance.initialize(manager, "jruby", null); String file = ""; int line = 0; int col = 0; Object expr = null; Object expResult = null; Object result = instance.eval(file, line, col, expr); assertEquals(expResult, result); expResult = "HELLO WORLD!"; result = instance.eval("<script>", 0, 0, "message=\"Hello \" + \"World!\"\nmessage.upcase"); assertEquals(expResult, result); manager.declareBean("greet", "Heeey", String.class); result = manager.eval("jruby", "<script>", 0, 0, "message=$greet + \" World!\""); expResult = "Heeey World!"; assertEquals(expResult, result); }
/** Test of terminate method, of class JRubyEngine. */ @Test public void testTerminate() throws BSFException { System.out.println("terminate"); BSFManager manager = new BSFManager(); JRubyEngine instance = new JRubyEngine(); instance.initialize(manager, "jruby", null); instance.terminate(); }
/** Test of handleException method, of class JRubyEngine. */ @Test public void testHandleException() throws BSFException { System.out.println("handleException"); BSFManager manager = new BSFManager(); JRubyEngine instance = new JRubyEngine(); instance.initialize(manager, "jruby", null); BSFException bsfExcptn = new BSFException(BSFException.REASON_EXECUTION_ERROR, "test", new NullPointerException()); instance.handleException(bsfExcptn); }
/** Test of undeclareBean method, of class JRubyEngine. */ @Test public void testUndeclareBean() throws Exception { System.out.println("undeclareBean"); BSFManager manager = new BSFManager(); JRubyEngine instance = new JRubyEngine(); instance.initialize(manager, "jruby", null); manager.declareBean("abc", "aaabbbccc", String.class); BSFDeclaredBean bean = (BSFDeclaredBean) manager.getObjectRegistry().lookup("abc"); instance.undeclareBean(bean); }
/** Test of initialize method, of class JRubyEngine. */ @Test public void testInitialize() throws Exception { System.out.println("initialize"); BSFManager manager = new BSFManager(); ; String language = ""; Vector someDeclaredBeans = null; JRubyEngine instance = new JRubyEngine(); instance.initialize(manager, language, someDeclaredBeans); }
@Test public void testTermination() throws ScriptException { logger1.info("Termination Test"); ScriptEngineManager manager = new ScriptEngineManager(); JRubyEngine instance = (JRubyEngine) manager.getEngineByName("jruby"); instance.eval("$x='GVar'"); StringWriter sw = new StringWriter(); instance.getContext().setWriter(sw); instance.eval("at_exit { puts \"#{$x} in an at_exit block\" }"); String expResult = ""; assertEquals(expResult, sw.toString().trim()); sw = new StringWriter(); instance.getContext().setWriter(sw); instance .getContext() .setAttribute("org.jruby.embed.termination", true, ScriptContext.ENGINE_SCOPE); instance.eval(""); expResult = "GVar in an at_exit block"; assertEquals(expResult, sw.toString().trim()); instance .getContext() .setAttribute("org.jruby.embed.termination", false, ScriptContext.ENGINE_SCOPE); instance = null; }
/** Test of exec method, of class JRubyEngine. */ @Test public void testExec() throws Exception { System.out.println("exec"); BSFManager manager = new BSFManager(); JRubyEngine instance = new JRubyEngine(); instance.initialize(manager, "jruby", null); String file = ""; int line = 0; int col = 0; Object expr = null; instance.exec(file, line, col, expr); String partone = "def partone\n" + "impression = \"Sooooo Gooood!\"\n" + "f = File.new(\"" + basedir + "/build/test-results/bsfeval.txt\", \"w\")\n" + "begin\n" + "f.puts impression\n" + "ensure\n" + "f.close\n" + "end\n" + "end\n" + "partone"; String parttwo = "def parttwo\n" + "f = File.open \"" + basedir + "/build/test-results/bsfeval.txt\"\n" + "begin\n" + "comment = f.gets\n" + "return comment\n" + "ensure\n" + "f.close\n" + "end\n" + "end\n" + "parttwo"; instance.exec("<script>", 0, 0, partone); Object expResult = "Sooooo Gooood!\n"; Object result = instance.eval("<script>", 0, 0, parttwo); assertEquals(expResult, result); }
@Test public void testPathTyp() throws BSFException { System.out.println("PathType"); BSFManager.registerScriptingEngine( "jruby", "org.jruby.embed.bsf.JRubyEngine", new String[] {"rb"}); BSFManager manager = new BSFManager(); JRubyEngine instance = (JRubyEngine) manager.loadScriptingEngine("jruby"); Object receiver = instance.eval("org/jruby/embed/ruby/radioactive_decay.rb", 0, 0, PathType.CLASSPATH); String method = "amount_after_years"; Object[] args = new Object[2]; args[0] = 10.0; args[1] = 1000; // Plutonium manager.declareBean("h", 24100, Long.class); Object result = instance.call(receiver, method, args); assertEquals(9.716, (Double) result, 0.001); }
@Test public void testRubyVersion() throws BSFException { System.out.println("RubyVersion"); BSFManager.registerScriptingEngine( "jruby", "org.jruby.embed.bsf.JRubyEngine", new String[] {"rb"}); BSFManager manager = new BSFManager(); JRubyEngine instance = (JRubyEngine) manager.loadScriptingEngine("jruby"); Object result = instance.eval("org/jruby/embed/ruby/block-param-scope.rb", 0, 0, PathType.CLASSPATH); String expResult = "cat"; assertEquals(expResult, ((String) result).trim()); // Ruby 1.9 mode is somehow broken in 1.5.0dev BSFManager.registerScriptingEngine( "jruby19", "org.jruby.embed.bsf.JRubyEngine", new String[] {"rb"}); instance = (JRubyEngine) manager.loadScriptingEngine("jruby19"); result = instance.eval("org/jruby/embed/ruby/block-param-scope.rb", 0, 0, PathType.CLASSPATH); expResult = "bear"; assertEquals(expResult, ((String) result).trim()); }
/** Test of call method, of class JRubyEngine. */ @Test public void testCall() throws Exception { System.out.println("call"); BSFManager.registerScriptingEngine( "jruby", "org.jruby.embed.bsf.JRubyEngine", new String[] {"rb"}); BSFManager manager = new BSFManager(); JRubyEngine instance = (JRubyEngine) manager.loadScriptingEngine("jruby"); instance.initialize(manager, "jruby", null); Object recv = null; String method = ""; Object[] args = null; Object expResult = null; Object result = instance.call(recv, method, args); assertEquals(expResult, result); String script = "# Radioactive decay\n" + "def amount_after_years(q0, t)\n" + "q0 * Math.exp(1.0 / $h * Math.log(1.0/2.0) * t)\n" + "end\n" + "def years_to_amount(q0, q)\n" + "$h * (Math.log(q) - Math.log(q0)) / Math.log(1.0/2.0)\n" + "end"; recv = manager.eval("jruby", "radioactive_decay", 0, 0, script); method = "amount_after_years"; args = new Object[2]; args[0] = 10.0; args[1] = 1000; // Radium manager.declareBean("h", 1599, Long.class); result = instance.call(recv, method, args); assertEquals(6.482, (Double) result, 0.001); method = "years_to_amount"; args[0] = 10.0; args[1] = 1.0; result = instance.call(recv, method, args); assertEquals(5311.8, (Double) result, 0.1); }
/** Test of apply method, of class JRubyEngine. */ @Test public void testApply() throws BSFException { System.out.println("apply"); BSFManager manager = new BSFManager(); JRubyEngine instance = new JRubyEngine(); instance.initialize(manager, "jruby", null); String file = ""; int line = 0; int col = 0; Object funcBody = null; Vector paramNames = new Vector(); Vector args = new Vector(); Object expResult = null; Object result = instance.apply(file, line, col, funcBody, paramNames, args); assertEquals(expResult, result); expResult = new Long(144); result = instance.apply("<script>", 0, 0, "x=144", null, null); assertEquals(expResult, result); expResult = new Double(12.0); result = instance.apply("<script>", 0, 0, "Math.sqrt x", null, null); assertEquals(expResult, result); paramNames.add("message"); args.add("red small beans and often used in a form of paste."); result = instance.apply("<script>", 0, 0, "ret=\"Azuki beans are #{message}\"", paramNames, args); expResult = "Azuki beans are red small beans and often used in a form of paste."; assertEquals(expResult, result); paramNames.clear(); args.clear(); paramNames.add("correction"); args.add("usually"); result = instance.apply("<script>", 0, 0, "ret = ret.gsub(/often/, correction)", paramNames, args); expResult = "Azuki beans are red small beans and usually used in a form of paste."; assertEquals(expResult, result); }