Exemplo n.º 1
0
  /** Test of callMethod method, of class ScriptingContainer. */
  @Test
  public void testCallMethod_4args_1() {
    System.out.println("callMethod(receiver, methodName, singleArg, returnType)");
    Object receiver = null;
    String methodName = "";
    Object singleArg = null;
    Class<Object> returnType = null;
    ScriptingContainer instance = new ScriptingContainer();
    Object expResult = null;
    Object result = instance.callMethod(receiver, methodName, singleArg, returnType);
    assertEquals(expResult, result);

    String filename = "org/jruby/embed/ruby/list_printer_1.rb";
    receiver = instance.runScriptlet(PathType.CLASSPATH, filename);
    methodName = "print_list";
    String[] hellos = {"你好", "こんにちは", "Hello", "Здравствуйте"};
    singleArg = Arrays.asList(hellos);
    StringWriter writer = new StringWriter();
    instance.setWriter(writer);
    instance.callMethod(receiver, methodName, singleArg, null);
    expResult = "Hello >> Здравствуйте >> こんにちは >> 你好: 4 in total";
    assertEquals(expResult, writer.toString().trim());

    instance.getVarMap().clear();
    instance = null;
  }
Exemplo n.º 2
0
  /** Test of callMethod method, of class ScriptingContainer. */
  @Test
  public void testCallMethod_4args_2() {
    System.out.println("callMethod(receiver, methodName, args, returnType)");
    Object receiver = null;
    String methodName = "";
    Object[] args = null;
    Class<Object> returnType = null;
    ScriptingContainer instance = new ScriptingContainer();
    Object expResult = null;
    Object result = instance.callMethod(receiver, methodName, args, returnType);
    assertEquals(expResult, result);

    String filename = "org/jruby/embed/ruby/quadratic_formula.rb";
    receiver = instance.runScriptlet(PathType.CLASSPATH, filename);
    methodName = "solve";
    args = new Double[] {12.0, -21.0, -6.0};
    List<Double> solutions = instance.callMethod(receiver, methodName, args, List.class);
    assertEquals(2, solutions.size());
    assertEquals(new Double(-0.25), solutions.get(0));
    assertEquals(new Double(2.0), solutions.get(1));

    args = new Double[] {1.0, 1.0, 1.0};
    try {
      solutions = instance.callMethod(receiver, methodName, args, List.class);
    } catch (RuntimeException e) {
      Throwable t = e.getCause().getCause();
      assertTrue(t.getMessage().contains("RangeError"));
    }

    instance.getVarMap().clear();
    instance = null;
  }
Exemplo n.º 3
0
  /** Test of callMethod method, of class ScriptingContainer. */
  @Test
  public void testCallMethod_4args_3() {
    // Sharing local variables over method call doesn't work.
    // Should delete methods with unit argument?
    System.out.println("callMethod(receiver, methodName, returnType, unit)");
    Object receiver = null;
    String methodName = "";
    Class<Object> returnType = null;
    EmbedEvalUnit unit = null;
    ScriptingContainer instance = new ScriptingContainer(LocalVariableBehavior.PERSISTENT);
    instance.getProvider().getRubyInstanceConfig().setJRubyHome(System.getProperty("user.dir"));
    Object expResult = null;
    Object result = instance.callMethod(receiver, methodName, returnType, unit);
    assertEquals(expResult, result);

    String text =
        "songs:\n"
            + "- Hey Soul Sister\n"
            + "- Who Says\n"
            + "- Apologize\n"
            + "podcasts:\n"
            + "- Java Posse\n"
            + "- Stack Overflow";
    String filename = "org/jruby/embed/ruby/yaml_dump.rb";
    StringWriter writer = new StringWriter();
    instance.setWriter(writer);
    // local variable doesn't work in this case, so instance variable is used.
    instance.put("@text", text);
    unit = instance.parse(PathType.CLASSPATH, filename);
    receiver = unit.run();
    methodName = "dump";
    result = instance.callMethod(receiver, methodName, null, unit);
    expResult =
        "songs: Hey Soul Sister, Who Says, Apologize\npodcasts: Java Posse, Stack Overflow\n";
    assertEquals(expResult, writer.toString());

    instance.getVarMap().clear();
    instance = null;
  }
Exemplo n.º 4
0
  /** Test of callMethod method, of class ScriptingContainer. */
  @Test
  public void testCallMethod_3args() {
    System.out.println("callMethod(receiver, methodName, returnType)");
    Object receiver = null;
    String methodName = "";
    Class<Object> returnType = null;
    String basedir = System.getProperty("user.dir");
    String[] paths = {basedir + "/lib/ruby/1.8"};
    ScriptingContainer instance = new ScriptingContainer();
    instance.getProvider().setLoadPaths(Arrays.asList(paths));
    Object expResult = null;
    Object result = instance.callMethod(receiver, methodName, returnType);
    assertEquals(expResult, result);

    String filename = "org/jruby/embed/ruby/next_year_1.rb";
    receiver = instance.runScriptlet(PathType.CLASSPATH, filename);
    int next_year = instance.callMethod(receiver, "get_year", Integer.class);
    assertEquals(getNextYear(), next_year);

    String script =
        "def volume\n"
            + "  (Math::PI * (@r ** 2.0) * @h)/3.0\n"
            + "end\n"
            + "def surface_area\n"
            + "  Math::PI * @r * Math.sqrt((@r ** 2.0) + (@h ** 2.0)) + Math::PI * (@r ** 2.0)\n"
            + "end";
    receiver = instance.runScriptlet(script);
    instance.put("@r", 1.0);
    instance.put("@h", Math.sqrt(3.0));
    double volume = instance.callMethod(receiver, "volume", Double.class);
    assertEquals(1.813799, volume, 0.000001);
    double surface_area = instance.callMethod(receiver, "surface_area", Double.class);
    assertEquals(9.424778, surface_area, 0.000001);

    instance.getVarMap().clear();
    instance = null;
  }