diff options
| author | Andrew Branson <andrew.branson@cern.ch> | 2013-02-27 22:25:58 +0100 |
|---|---|---|
| committer | Andrew Branson <andrew.branson@cern.ch> | 2013-02-27 22:25:58 +0100 |
| commit | ba1d538b588dc57066213a94803584ea9016e1e7 (patch) | |
| tree | ef5cb41e8eeda495c819b3afcbf92ae464991768 | |
| parent | efb8494445128c827ff5182f5ef4732a73b3ea0d (diff) | |
Script testing and resulting fixes
| -rw-r--r-- | src/main/java/com/c2kernel/scripting/Script.java | 15 | ||||
| -rw-r--r-- | src/test/java/ScriptTest.java | 47 | ||||
| -rw-r--r-- | src/test/resources/TestScript.xml | 7 |
3 files changed, 66 insertions, 3 deletions
diff --git a/src/main/java/com/c2kernel/scripting/Script.java b/src/main/java/com/c2kernel/scripting/Script.java index ab3c0e6..7e0c3f2 100644 --- a/src/main/java/com/c2kernel/scripting/Script.java +++ b/src/main/java/com/c2kernel/scripting/Script.java @@ -51,6 +51,14 @@ public class Script ScriptEngine engine;
ScriptContext context;
+ /** For testing. Parses a given script xml, instead of loading it from Items.
+ * @param xml
+ * @throws ScriptParsingException
+ * @throws ParameterException
+ */
+ public Script(String xml) throws ScriptParsingException, ParameterException {
+ parseScriptXML(xml);
+ }
/**
* Loads script xml and parses it for script source, parameters and output specifications.
* First tries to load the script from resource path /scriptFiles/scriptName_scriptVersion.xml
@@ -157,7 +165,7 @@ public class Script engine.setContext(context);
}
- public void loadScript(String scriptName, String scriptVersion) throws ScriptingEngineException
+ private void loadScript(String scriptName, String scriptVersion) throws ScriptingEngineException
{
try
{
@@ -420,7 +428,8 @@ public class Script throw new ScriptingEngineException("Execution aborted, the following declared parameters were not set: \n" + missingParams.toString());
for (Parameter outputParam : mOutputParams.values()) {
- if (outputParam.getName() == null) continue; // If the name is null then it's the return type. don't pre-register it
+ System.out.println("Output param: "+outputParam.getName());
+ if (outputParam.getName() == null || outputParam.getName().length()==0) continue; // If the name is null then it's the return type. don't pre-register it
Logger.msg(8, "Script.setOutput() - Initialising output bean '" + outputParam.getName() + "'");
Object emptyObject;
try {
@@ -468,7 +477,7 @@ public class Script for (Parameter outputParam : mOutputParams.values()) {
String outputName = outputParam.getName();
Object outputValue;
- if (outputName == null)
+ if (outputName == null || outputName.length()==0)
outputValue = returnValue;
else
outputValue = context.getBindings(ScriptContext.ENGINE_SCOPE).get(outputParam.getName());
diff --git a/src/test/java/ScriptTest.java b/src/test/java/ScriptTest.java new file mode 100644 index 0000000..1cac1b5 --- /dev/null +++ b/src/test/java/ScriptTest.java @@ -0,0 +1,47 @@ +import com.c2kernel.persistency.outcome.Outcome;
+import com.c2kernel.persistency.outcome.OutcomeValidator;
+import com.c2kernel.persistency.outcome.Schema;
+import com.c2kernel.scripting.Script;
+import com.c2kernel.utils.FileStringUtility;
+import com.c2kernel.utils.Logger;
+import com.c2kernel.utils.Resource;
+
+public class ScriptTest {
+
+ String testScriptString;
+ Script testScript;
+
+ public ScriptTest() throws Exception {
+ Resource.initKernelBaseURL();
+ Logger.addLogStream(System.out, 9);
+ testScriptString = FileStringUtility.url2String(ScriptTest.class.getResource("TestScript.xml"));
+ }
+
+ public void testScriptValid() throws Exception {
+
+ String schemaText = Resource.getTextResource(null, "boot/OD/Script.xsd");
+ Schema scriptSchema = new Schema("Script", 0, false, schemaText);
+ OutcomeValidator valid = new OutcomeValidator(scriptSchema);
+ Outcome script = new Outcome("/Script/0/0", testScriptString);
+ String errors = valid.validate(script);
+ System.out.println("Script validation errors: "+errors);
+ assert errors.length() == 0;
+ }
+
+ public void testParsing() throws Exception {
+ testScript = new Script(testScriptString);
+ System.out.println("Param size: "+testScript.getInputParams().size());
+ assert testScript.getInputParams().size()==1;
+ System.out.println("Param 1: "+testScript.getInputParams().get("test"));
+ assert testScript.getInputParams().get("test")!=null;
+ testScript.setInputParamValue("test", "Test");
+ System.out.println("Param 1 initialized: "+testScript.getInputParams().get("test").getInitialised());
+ assert testScript.getInputParams().get("test").getInitialised();
+ Object result = testScript.execute();
+ assert result!=null;
+ assert result instanceof String;
+ assert ((String)result).equals("TestTest");
+ }
+
+
+}
diff --git a/src/test/resources/TestScript.xml b/src/test/resources/TestScript.xml new file mode 100644 index 0000000..5b85fc3 --- /dev/null +++ b/src/test/resources/TestScript.xml @@ -0,0 +1,7 @@ +<cristalscript>
+ <param name="test" type="java.lang.String"/>
+ <output type="java.lang.String"/>
+ <script language="javascript" name="TestScript"><![CDATA[
+ test+test;
+ ]]></script>
+</cristalscript>
|
