コード例 #1
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;
  }
コード例 #2
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;
  }