Exemplo n.º 1
0
  /** Test of runScriptlet method, of class ScriptingContainer. */
  @Test
  public void testRunScriptlet_InputStream_String() throws FileNotFoundException {
    System.out.println("runScriptlet(istream, filename)");
    InputStream istream = null;
    String filename = "";
    ScriptingContainer instance = new ScriptingContainer();
    Object expResult = null;
    Object result = instance.runScriptlet(istream, filename);
    assertEquals(expResult, result);

    filename = "org/jruby/embed/ruby/law_of_cosines.rb";
    istream = getClass().getClassLoader().getResourceAsStream(filename);
    instance.put("@a", 2.0);
    instance.put("@b", 2 * Math.sqrt(3.0));
    instance.put("@c", 2.0);
    List<Double> angles = (List<Double>) instance.runScriptlet(istream, filename);
    // this result goes to 30.00000000000004,30.00000000000004,120.0.
    // these should be 30.0, 30.0, 120.0. conversion precision error?
    System.out.println(angles.get(0) + ", " + angles.get(1) + ", " + angles.get(2));
    assertEquals(30.0, angles.get(0), 0.00001);
    assertEquals(30.0, angles.get(1), 0.00001);
    assertEquals(120.0, angles.get(2), 0.00001);

    instance.getVarMap().clear();
    instance = null;
  }
Exemplo n.º 2
0
  /** Test of parse method, of class ScriptingContainer. */
  @Test
  public void testParse_3args_2() {
    System.out.println("parse(type, filename, lines)");
    PathType type = null;
    String filename = "";
    int[] lines = null;

    String basedir = System.getProperty("user.dir");
    String[] paths = {basedir + "/lib/ruby/1.8"};
    ScriptingContainer instance = new ScriptingContainer();
    instance.getProvider().setLoadPaths(Arrays.asList(paths));
    EmbedEvalUnit result;
    try {
      result = instance.parse(type, filename, lines);
    } catch (RuntimeException e) {
      assertTrue(e.getCause() instanceof FileNotFoundException);
    }

    filename = basedir + "/test/org/jruby/embed/ruby/next_year.rb";
    result = instance.parse(PathType.ABSOLUTE, filename);
    IRubyObject ret = result.run();
    assertEquals(getNextYear(), ret.toJava(Integer.class));

    StringWriter writer = new StringWriter();
    instance.setWriter(writer);
    String[] planets = {
      "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"
    };
    instance.put("@list", Arrays.asList(planets));
    filename = "/test/org/jruby/embed/ruby/list_printer.rb";
    result = instance.parse(PathType.RELATIVE, filename);
    ret = result.run();
    String expResult =
        "Mercury >> Venus >> Earth >> Mars >> Jupiter >> Saturn >> Uranus >> Neptune: 8 in total";
    assertEquals(expResult, writer.toString().trim());

    writer = new StringWriter();
    instance.setWriter(writer);
    planets = new String[] {"水星", "金星", "地球", "火星", "木星", "土星", "天王星", "海王星"};
    instance.put("@list", Arrays.asList(planets));
    filename = "org/jruby/embed/ruby/list_printer.rb";
    result = instance.parse(PathType.CLASSPATH, filename);
    ret = result.run();
    expResult = "水星 >> 金星 >> 地球 >> 火星 >> 木星 >> 土星 >> 天王星 >> 海王星: 8 in total";
    assertEquals(expResult, writer.toString().trim());

    filename = "org/jruby/embed/ruby/raises_parse_error.rb";
    writer = new StringWriter();
    instance.setErrorWriter(writer);
    try {
      instance.parse(PathType.CLASSPATH, filename, 2);
    } catch (Exception e) {
      System.out.println(writer.toString());
      assertTrue(writer.toString().contains(filename + ":7:"));
    }

    instance.getVarMap().clear();
    instance = null;
  }
Exemplo n.º 3
0
  /** Test of runScriptlet method, of class ScriptingContainer. */
  @Test
  public void testRunScriptlet_PathType_String() {
    System.out.println("runScriptlet(type, filename)");
    PathType type = null;
    String filename = "";
    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;
    try {
      result = instance.parse(type, filename);
    } catch (RuntimeException e) {
      assertTrue(e.getCause() instanceof FileNotFoundException);
    }

    // absolute path
    filename = basedir + "/test/org/jruby/embed/ruby/next_year.rb";
    result = instance.runScriptlet(PathType.ABSOLUTE, filename);
    // perhaps, a return type should be in a method argument
    // since implicit cast results in a Long type
    expResult = new Long(getNextYear());
    assertEquals(expResult, result);

    instance.setAttribute(AttributeName.BASE_DIR, basedir + "/test/org/jruby/embed");
    filename = "/ruby/next_year.rb";
    result = instance.runScriptlet(PathType.RELATIVE, filename);
    assertEquals(expResult, result);
    instance.removeAttribute(AttributeName.BASE_DIR);

    StringWriter writer = new StringWriter();
    instance.setWriter(writer);
    String[] radioactive_isotopes = {
      "Uranium", "Plutonium", "Carbon", "Radium", "Einstenium", "Nobelium"
    };
    instance.put("@list", Arrays.asList(radioactive_isotopes));
    filename = "/test/org/jruby/embed/ruby/list_printer.rb";
    result = instance.runScriptlet(PathType.RELATIVE, filename);
    expResult = "Uranium >> Plutonium >> Carbon >> Radium >> Einstenium >> Nobelium: 6 in total";
    assertEquals(expResult, writer.toString().trim());

    writer = new StringWriter();
    instance.setWriter(writer);
    radioactive_isotopes = new String[] {"ウラン", "プルトニウム", "炭素", "ラジウム", "アインスタイニウム", "ノーベリウム"};
    instance.put("@list", Arrays.asList(radioactive_isotopes));
    filename = "org/jruby/embed/ruby/list_printer.rb";
    result = instance.runScriptlet(PathType.CLASSPATH, filename);
    expResult = "ウラン >> プルトニウム >> 炭素 >> ラジウム >> アインスタイニウム >> ノーベリウム: 6 in total";
    assertEquals(expResult, writer.toString().trim());

    instance.getVarMap().clear();
    instance = null;
  }
Exemplo n.º 4
0
  /** Test of get method, of class ScriptingContainer. */
  @Test
  public void testGet() {
    System.out.println("get");
    ScriptingContainer instance = new ScriptingContainer();
    String key = null;
    try {
      instance.get(key);
    } catch (NullPointerException e) {
      assertEquals("key is null", e.getMessage());
    }
    key = "";
    try {
      instance.get(key);
    } catch (IllegalArgumentException e) {
      assertEquals("key is empty", e.getMessage());
    }
    key = "a";
    Object expResult = null;
    Object result = instance.get(key);
    assertEquals(expResult, result);

    instance.put("@name", "camellia");
    assertEquals("camellia", instance.get("@name"));
    instance.put("COLOR", "red");
    assertEquals("red", instance.get("COLOR"));
    // class variables doesn't work
    // varMap.put("@@season", "spring");
    // assertEquals("spring", instance.get("@@season"));
    instance.put("$category", "flower");
    assertEquals("flower", instance.get("$category"));

    // Bug. Can't retrieve instance variables from Ruby.
    instance.runScriptlet("@eular = 2.718281828");
    // assertEquals(2.718281828, instance.get("@eular"));
    instance.runScriptlet("@name = \"holly\"");
    // assertEquals("holly", instance.get("@name"));
    instance.runScriptlet("$category = \"bush\"");
    assertEquals("bush", instance.get("$category"));

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

    instance = new ScriptingContainer(LocalVariableBehavior.PERSISTENT);
    instance.runScriptlet("ivalue = 200000");
    assertEquals(200000L, instance.get("ivalue"));

    instance.getVarMap().clear();
    instance = null;
  }
Exemplo n.º 5
0
  /** Test of parse method, of class ScriptingContainer. */
  @Test
  public void testParse_3args_1() throws FileNotFoundException {
    System.out.println("parse(reader, filename, lines)");
    Reader reader = null;
    String filename = "";
    int[] lines = null;
    ScriptingContainer instance = new ScriptingContainer();
    EmbedEvalUnit expResult = null;
    EmbedEvalUnit result = instance.parse(reader, filename, lines);
    assertEquals(expResult, result);

    String basedir = System.getProperty("user.dir");
    filename = basedir + "/test/org/jruby/embed/ruby/iteration.rb";
    reader = new FileReader(filename);
    instance.put("@t", 2);
    result = instance.parse(reader, filename);
    IRubyObject ret = result.run();
    String expStringResult = "Trick or Treat!\nTrick or Treat!\n\nHmmm...I'd like trick.";
    assertEquals(expStringResult, ret.toJava(String.class));

    // line number test
    filename = basedir + "/test/org/jruby/embed/ruby/raises_parse_error.rb";
    reader = new FileReader(filename);
    StringWriter writer = new StringWriter();
    instance.setErrorWriter(writer);
    try {
      instance.parse(reader, filename, 2);
    } catch (Exception e) {
      System.out.println(writer.toString());
      assertTrue(writer.toString().contains(filename + ":7:"));
    }

    instance.getVarMap().clear();
    instance = null;
  }
Exemplo n.º 6
0
  /** Test of parse method, of class ScriptingContainer. */
  @Test
  public void testParse_3args_3() throws FileNotFoundException {
    System.out.println("parse(istream, filename, lines)");
    InputStream istream = null;
    String filename = "";
    int[] lines = null;
    ScriptingContainer instance = new ScriptingContainer();
    EmbedEvalUnit expResult = null;
    EmbedEvalUnit result = instance.parse(istream, filename, lines);
    assertEquals(expResult, result);

    String basedir = System.getProperty("user.dir");
    filename = basedir + "/test/org/jruby/embed/ruby/law_of_cosines.rb";
    istream = new FileInputStream(filename);
    result = instance.parse(istream, filename);
    instance.put("@a", 1);
    instance.put("@b", 1);
    instance.put("@c", 1);
    IRubyObject ret = result.run();
    List<Double> angles = (List) ret.toJava(List.class);
    // this result goes to 60.00000000000001,60.00000000000001,59.99999999999999.
    // these should be 60.0, 60.0, 60.0. conversion precision error?
    for (double angle : angles) {
      assertEquals(60.0, angle, 0.00001);
    }

    filename = basedir + "/test/org/jruby/embed/ruby/raises_parse_error.rb";
    StringWriter writer = new StringWriter();
    instance.setErrorWriter(writer);
    istream = new FileInputStream(filename);
    try {
      instance.parse(istream, filename, 2);
    } catch (Exception e) {
      System.out.println(writer.toString());
      assertTrue(writer.toString().contains(filename + ":7:"));
    }

    instance.getVarMap().clear();
    instance = null;
  }
Exemplo n.º 7
0
  /** Test of getInstance method, of class ScriptingContainer. */
  @Test
  public void testGetInstance() {
    System.out.println("getInstance");
    Object receiver = null;
    Class<Object> clazz = null;
    ScriptingContainer instance = new ScriptingContainer();
    Object expResult = null;
    Object result = instance.getInstance(receiver, clazz);
    assertEquals(expResult, result);

    // calculates Plutonium decay
    instance.put("$h", 24100.0); // half-life of Plutonium is 24100 years.
    String filename = "org/jruby/embed/ruby/radioactive_decay.rb";
    receiver = instance.runScriptlet(PathType.CLASSPATH, filename);
    result = instance.getInstance(receiver, RadioActiveDecay.class);
    double initial = 10.0; // 10.0 g
    double years = 1000; // 1000 years
    double amount_left = ((RadioActiveDecay) result).amountAfterYears(initial, years);
    assertEquals(9.716483752784367, amount_left, 0.00000000001);
    amount_left = 1.0;
    years = ((RadioActiveDecay) result).yearsToAmount(initial, amount_left);
    assertEquals(80058.46708678544, years, 0.00000000001);

    // calculates position and velocity after some seconds have past
    instance.put("initial_velocity", 16.0);
    instance.put("initial_height", 32.0);
    instance.put("system", "english");
    filename = "org/jruby/embed/ruby/position_function.rb";
    receiver = instance.runScriptlet(PathType.CLASSPATH, filename);
    result = instance.getInstance(receiver, PositionFunction.class);
    double time = 2.0;
    double position = ((PositionFunction) result).getPosition(time);
    assertEquals(0.0, position, 0.01);
    double velocity = ((PositionFunction) result).getVelocity(time);
    assertEquals(-48.0, velocity, 0.01);
    List<String> units = ((PositionFunction) result).getUnits();
    assertEquals("ft./sec", units.get(0));
    assertEquals("ft.", units.get(1));
  }
Exemplo n.º 8
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;
  }
Exemplo n.º 9
0
  /** Test of newRuntimeAdapter method, of class ScriptingContainer. */
  @Test
  public void testNewRuntimeAdapter() {
    System.out.println("newRuntimeAdapter");
    ScriptingContainer instance = new ScriptingContainer();
    EmbedRubyRuntimeAdapter result = instance.newRuntimeAdapter();
    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\n"
            + "return volume, surface_area";
    instance.put("@r", 1.0);
    instance.put("@h", Math.sqrt(3.0));
    EmbedEvalUnit unit = result.parse(script);
    IRubyObject ret = unit.run();
    List<Double> rightCircularCone = (List<Double>) ret.toJava(List.class);
    assertEquals(1.813799, rightCircularCone.get(0), 0.000001);
    assertEquals(9.424778, rightCircularCone.get(1), 0.000001);

    instance.getVarMap().clear();
    instance = null;
  }
Exemplo n.º 10
0
  /** Test of put method, of class ScriptingContainer. */
  @Test
  public void testPut() {
    System.out.println("put");
    ScriptingContainer instance = new ScriptingContainer();
    String key = null;
    try {
      instance.get(key);
    } catch (NullPointerException e) {
      assertEquals("key is null", e.getMessage());
    }
    key = "";
    try {
      instance.get(key);
    } catch (IllegalArgumentException e) {
      assertEquals("key is empty", e.getMessage());
    }
    key = "a";
    Object value = null;
    Object expResult = null;
    Object result = instance.put(key, value);
    Object newValue = "xyz";
    result = instance.put(key, newValue);
    assertEquals(expResult, result);
    expResult = "xyz";
    assertEquals(expResult, instance.get(key));

    StringWriter sw = new StringWriter();
    instance.setWriter(sw);
    instance.put("x", 144.0);
    instance.runScriptlet("puts Math.sqrt(x)");
    assertEquals("12.0", sw.toString().trim());

    sw = new StringWriter();
    instance.setWriter(sw);
    instance.put("@x", 256.0);
    instance.runScriptlet("puts Math.sqrt(@x)");
    assertEquals("16.0", sw.toString().trim());

    sw = new StringWriter();
    instance.setWriter(sw);
    instance.put("$x", 9.0);
    instance.runScriptlet("puts Math.sqrt($x)");
    assertEquals("3.0", sw.toString().trim());

    sw = new StringWriter();
    instance.setWriter(sw);
    instance.put("KMTOMI", 0.621);
    instance.runScriptlet("puts \"1 km is #{KMTOMI} miles.\"");
    assertEquals("1 km is 0.621 miles.", sw.toString().trim());

    instance.getVarMap().clear();
    instance = null;
  }
Exemplo n.º 11
0
  /** Test of parse method, of class ScriptingContainer. */
  @Test
  public void testParse_String_intArr() {
    System.out.println("parse");
    String script = null;
    int[] lines = null;
    ScriptingContainer instance = new ScriptingContainer();
    EmbedEvalUnit expResult = null;
    EmbedEvalUnit result = instance.parse(script, lines);
    assertEquals(expResult, result);

    script = "";
    Ruby runtime = JavaEmbedUtils.initialize(new ArrayList());
    Node node = runtime.parseEval(script, "<script>", null, 0);
    IRubyObject expRet = runtime.runInterpreter(node);
    result = instance.parse(script);
    IRubyObject ret = result.run();
    assertEquals(expRet.toJava(String.class), ret.toJava(String.class));
    // Maybe bug. This returns RubyNil, but it should be ""
    // assertEquals("", ret.toJava(String.class));

    script = "def say_something()" + "\"はろ〜、わぁ〜るど!\"\n" + "end\n" + "say_something";
    expRet = runtime.runInterpreter(runtime.parseEval(script, "<script>", null, 0));
    ret = instance.parse(script).run();
    assertEquals(expRet.toJava(String.class), ret.toJava(String.class));

    // sharing variables
    instance.put("what", "Trick or Treat.");
    script = "\"Did you say, #{what}?\"";
    result = instance.parse(script);
    ret = result.run();
    assertEquals("Did you say, Trick or Treat.?", ret.toJava(String.class));

    // line number test
    script = "puts \"Hello World!!!\"\nputs \"Have a nice day!";
    StringWriter writer = new StringWriter();
    instance.setErrorWriter(writer);
    try {
      instance.parse(script, 1);
    } catch (Exception e) {
      assertTrue(writer.toString().contains("<script>:3:"));
    }

    instance.getVarMap().clear();
    instance = null;
  }
Exemplo n.º 12
0
  /** Test of runScriptlet method, of class ScriptingContainer. */
  @Test
  public void testRunScriptlet_Reader_String() throws FileNotFoundException {
    System.out.println("runScriptlet(reader, filename)");
    Reader reader = null;
    String filename = "";
    ScriptingContainer instance = new ScriptingContainer();
    Object expResult = null;
    Object result = instance.runScriptlet(reader, filename);
    assertEquals(expResult, result);

    String basedir = System.getProperty("user.dir");
    filename = basedir + "/test/org/jruby/embed/ruby/iteration.rb";
    reader = new FileReader(filename);
    instance.put("@t", 3);
    result = instance.runScriptlet(reader, filename);
    expResult = "Trick or Treat!\nTrick or Treat!\nTrick or Treat!\n\nHmmm...I'd like trick.";
    assertEquals(expResult, result);

    instance.getVarMap().clear();
    instance = null;
  }
Exemplo n.º 13
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;
  }