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 runScriptlet method, of class ScriptingContainer. */
  @Test
  public void testRunScriptlet_String() {
    System.out.println("runScriptlet(script)");
    String script = null;
    ScriptingContainer instance = new ScriptingContainer();
    Object expResult = null;
    Object result = instance.runScriptlet(script);
    assertEquals(expResult, result);
    script = "";
    expResult = "";
    result = instance.runScriptlet(script);
    // Maybe bug. This should return "", but RubyNil.
    // assertEquals(expResult, result);

    script = "def say_something()" + "\"いけてるね! > JRuby\"\n" + "end\n" + "say_something";
    result = instance.runScriptlet(script);
    expResult = "いけてるね! > JRuby";
    assertEquals(expResult, result);

    // unicode escape
    String str = "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c";
    result = instance.runScriptlet("given_str = \"" + str + "\"");
    expResult = "こんにちは世界";
    assertEquals(expResult, result);

    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 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.º 5
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.º 6
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.º 7
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.º 8
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.º 9
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.º 10
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.º 11
0
  /** Test of setWriter method, of class ScriptingContainer. */
  @Test
  public void testSetWriter() {
    System.out.println("setWriter");
    Writer writer = null;
    ScriptingContainer instance = new ScriptingContainer();
    instance.setWriter(writer);

    String filename = System.getProperty("user.dir") + "/test/quiet.rb";
    writer = new StringWriter();
    Writer errorWriter = new StringWriter();
    instance.setWriter(writer);
    instance.setErrorWriter(errorWriter);
    Object result = instance.runScriptlet(PathType.ABSOLUTE, filename);
    String expResult = "foo";
    // This never successes.
    // assertEquals(expResult, result);
  }