Skip to content

Commit

Permalink
add more script tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Oct 10, 2010
1 parent a14b73b commit 18d8e9d
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ public interface ExecutableScript {
* Executes the script.
*/
Object run(Map<String, Object> vars);

/**
* Unwraps a possible script value. For example, when passing vars and expecting the returned value to
* be part of the vars.
*/
Object unwrap(Object value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ public interface ScriptEngineService {

Object execute(Object compiledScript, Map<String, Object> vars);

Object unwrap(Object value);

void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public class MvelScriptEngineService extends AbstractComponent implements Script
return new MvelExecutableScript(compiledScript, vars);
}

@Override public Object unwrap(Object value) {
return value;
}

public static class MvelExecutableScript implements ExecutableScript {

private final Object compiledScript;
Expand All @@ -94,5 +98,9 @@ public MvelExecutableScript(Object compiledScript, Map vars) {
vars.putAll(this.vars);
return MVEL.executeExpression(compiledScript, vars);
}

@Override public Object unwrap(Object value) {
return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public class GroovyScriptEngineService extends AbstractComponent implements Scri
}
}

@Override public Object unwrap(Object value) {
return value;
}

private String generateScriptName() {
return "Script" + counter.incrementAndGet() + ".groovy";
}
Expand All @@ -105,5 +109,9 @@ public GroovyExecutableScript(Script script) {
script.getBinding().getVariables().putAll(vars);
return script.run();
}

@Override public Object unwrap(Object value) {
return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public class JavaScriptScriptEngineService extends AbstractComponent implements
}
}

@Override public Object unwrap(Object value) {
return ScriptValueConverter.unwrapValue(value);
}

private String generateScriptName() {
return "Script" + counter.incrementAndGet() + ".js";
}
Expand Down Expand Up @@ -153,6 +157,10 @@ public JavaScriptExecutableScript(Script script, Scriptable scope) {
Context.exit();
}
}

@Override public Object unwrap(Object value) {
return ScriptValueConverter.unwrapValue(value);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,8 @@ else if ("Date".equals(className)) {
* Convert an object from any repository serialized value to a valid script object.
* This includes converting Collection multi-value properties into JavaScript Array objects.
*
* @param services Repository Services Registry
* @param scope Scripting scope
* @param qname QName of the property value for conversion
* @param value Property value
* @param scope Scripting scope
* @param value Property value
* @return Value safe for scripting usage
*/
public static Object wrapValue(Scriptable scope, Object value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ public class JavaScriptScriptEngineTests {
assertThat((String) ((Map<String, Object>) obj1.get("obj2")).get("prop2"), equalTo("value2"));
}

@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 testAccessListInScript() {
Map<String, Object> vars = new HashMap<String, Object>();
Map<String, Object> obj2 = MapBuilder.<String, Object>newMapBuilder().put("prop2", "value2").map();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public class PythonScriptEngineService extends AbstractComponent implements Scri
return ret.__tojava__(Object.class);
}

@Override public Object unwrap(Object value) {
return unwrapValue(value);
}

@Override public void close() {
interp.cleanup();
}
Expand Down Expand Up @@ -106,5 +110,20 @@ public PythonExecutableScript(PyCode code, Map<String, Object> vars) {
}
return ret.__tojava__(Object.class);
}

@Override public Object unwrap(Object value) {
return unwrapValue(value);
}
}


public static Object unwrapValue(Object value) {
if (value == null) {
return null;
} else if (value instanceof PyObject) {
// seems like this is enough, inner PyDictionary will do the conversion for us for example, so expose it directly
return ((PyObject) value).__tojava__(Object.class);
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ public class PythonScriptEngineTests {
assertThat(((String) o), equalTo("2"));
}

@Test public void testObjectMapInter() {
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'] = { '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 testAccessListInScript() {
Map<String, Object> vars = new HashMap<String, Object>();
Map<String, Object> obj2 = MapBuilder.<String, Object>newMapBuilder().put("prop2", "value2").map();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class PythonScriptSearchTests {
node.close();
}

@Test public void testJavaScriptFilter() throws Exception {
@Test public void testPythonFilter() throws Exception {
client.admin().indices().prepareCreate("test").execute().actionGet();
client.prepareIndex("test", "type1", "1")
.setSource(jsonBuilder().startObject().field("test", "value beck").field("num1", 1.0f).endObject())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public class RubyScriptEngineService extends AbstractComponent implements Script
return null; //To change body of implemented methods use File | Settings | File Templates.
}

@Override public Object unwrap(Object value) {
return null;
}

@Override public void close() {
container.clear();
}
Expand Down

0 comments on commit 18d8e9d

Please sign in to comment.