@Test public void testJavaScriptObjectToMap() { Map<String, Object> vars = new HashMap<String, Object>(); Object o = se.execute( se.compile( "var obj1 = {}; obj1.prop1 = 'value1'; obj1.obj2 = {}; obj1.obj2.prop2 = 'value2'; obj1"), vars); Map obj1 = (Map) o; assertThat((String) obj1.get("prop1"), equalTo("value1")); assertThat((String) ((Map<String, Object>) obj1.get("obj2")).get("prop2"), equalTo("value2")); }
@Test public void testMapAccess() { Map<String, Object> vars = new HashMap<String, Object>(); Map<String, Object> obj2 = MapBuilder.<String, Object>newMapBuilder().put("prop2", "value2").map(); Map<String, Object> obj1 = MapBuilder.<String, Object>newMapBuilder() .put("prop1", "value1") .put("obj2", obj2) .put("l", Lists.newArrayList("2", "1")) .map(); vars.put("obj1", obj1); Object o = se.execute(se.compile("obj1"), vars); assertThat(o, instanceOf(Map.class)); obj1 = (Map<String, Object>) o; assertThat((String) obj1.get("prop1"), equalTo("value1")); assertThat((String) ((Map<String, Object>) obj1.get("obj2")).get("prop2"), equalTo("value2")); o = se.execute(se.compile("obj1.l[0]"), vars); assertThat(((String) o), equalTo("2")); }
@Test public void testChangingVarsCrossExecution2() { Map<String, Object> vars = new HashMap<String, Object>(); Object compiledScript = se.compile("value"); ExecutableScript script = se.executable(compiledScript, vars); script.setNextVar("value", 1); Object o = script.run(); assertThat(((Number) o).intValue(), equalTo(1)); script.setNextVar("value", 2); o = script.run(); assertThat(((Number) o).intValue(), equalTo(2)); }
@Test public void testAccessListInScript() { Map<String, Object> vars = new HashMap<String, Object>(); Map<String, Object> obj2 = MapBuilder.<String, Object>newMapBuilder().put("prop2", "value2").map(); Map<String, Object> obj1 = MapBuilder.<String, Object>newMapBuilder().put("prop1", "value1").put("obj2", obj2).map(); vars.put("l", Lists.newArrayList("1", "2", "3", obj1)); Object o = se.execute(se.compile("l.length"), vars); assertThat(((Number) o).intValue(), equalTo(4)); o = se.execute(se.compile("l[0]"), vars); assertThat(((String) o), equalTo("1")); o = se.execute(se.compile("l[3]"), vars); obj1 = (Map<String, Object>) o; assertThat((String) obj1.get("prop1"), equalTo("value1")); assertThat((String) ((Map<String, Object>) obj1.get("obj2")).get("prop2"), equalTo("value2")); o = se.execute(se.compile("l[3].prop1"), vars); assertThat(((String) o), equalTo("value1")); }
@Test public void testJavaScriptObjectMapInter() { Map<String, Object> vars = new HashMap<String, Object>(); Map<String, Object> ctx = new HashMap<String, Object>(); Map<String, Object> obj1 = new HashMap<String, Object>(); obj1.put("prop1", "value1"); ctx.put("obj1", obj1); vars.put("ctx", ctx); se.execute( se.compile("ctx.obj2 = {}; ctx.obj2.prop2 = 'value2'; ctx.obj1.prop1 = 'uvalue1'"), vars); ctx = (Map<String, Object>) se.unwrap(vars.get("ctx")); assertThat(ctx.containsKey("obj1"), equalTo(true)); assertThat((String) ((Map<String, Object>) ctx.get("obj1")).get("prop1"), equalTo("uvalue1")); assertThat(ctx.containsKey("obj2"), equalTo(true)); assertThat((String) ((Map<String, Object>) ctx.get("obj2")).get("prop2"), equalTo("value2")); }
@Test public void testSimpleEquation() { Map<String, Object> vars = new HashMap<String, Object>(); Object o = se.execute(se.compile("1 + 2"), vars); assertThat(((Number) o).intValue(), equalTo(3)); }