コード例 #1
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;
  }
コード例 #2
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;
  }
コード例 #3
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;
  }
コード例 #4
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;
  }
コード例 #5
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);
  }
コード例 #6
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;
  }