Exemplo n.º 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;
  }
Exemplo n.º 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;
  }
Exemplo n.º 3
0
  /** Test of getProvider method, of class ScriptingContainer. */
  @Test
  public void testGetProvider() {
    System.out.println("getProvider");
    ScriptingContainer instance = new ScriptingContainer();
    LocalContextProvider result = instance.getProvider();
    assertTrue(result instanceof ThreadSafeLocalContextProvider);
    instance = null;

    instance = new ScriptingContainer(LocalContextScope.THREADSAFE);
    result = instance.getProvider();
    assertTrue(result instanceof ThreadSafeLocalContextProvider);
    instance = null;

    instance = new ScriptingContainer(LocalContextScope.SINGLETHREAD);
    result = instance.getProvider();
    assertTrue(result instanceof SingleThreadLocalContextProvider);
    instance = null;

    instance = new ScriptingContainer(LocalContextScope.SINGLETON);
    result = instance.getProvider();
    assertTrue(result instanceof SingletonLocalContextProvider);
    instance = null;
  }
Exemplo n.º 4
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.º 5
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;
  }