Exemple #1
0
  /** 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);
  }
Exemple #2
0
 /** 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();
 }
Exemple #3
0
 /** 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);
 }
Exemple #4
0
 /** 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);
 }
Exemple #5
0
 /** 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);
 }
Exemple #6
0
  /** 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);
  }
Exemple #7
0
  /** 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);
  }
Exemple #8
0
  /** 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);
  }