Exemple #1
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 #2
0
  @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);
  }