summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Branson <andrew.branson@cern.ch>2013-02-28 16:07:53 +0100
committerAndrew Branson <andrew.branson@cern.ch>2013-02-28 16:07:53 +0100
commitb077dac8ae8ffc79202a6b7dd2a008eef22489d3 (patch)
treee74dba1b962006c5dafe3ca0a6861d17e97b6016
parent288f3c6045d8f6b1e4d9b0b02a99d76f3d3683e1 (diff)
First unit tests, and their detected correction to the
CompositeActivityDef schema.
-rw-r--r--src/main/resources/boot/OD/CompositeActivityDef.xsd2
-rw-r--r--src/test/java/MainTest.java100
-rw-r--r--src/test/java/ScriptTest.java47
3 files changed, 101 insertions, 48 deletions
diff --git a/src/main/resources/boot/OD/CompositeActivityDef.xsd b/src/main/resources/boot/OD/CompositeActivityDef.xsd
index acdcae4..0b94df1 100644
--- a/src/main/resources/boot/OD/CompositeActivityDef.xsd
+++ b/src/main/resources/boot/OD/CompositeActivityDef.xsd
@@ -14,7 +14,7 @@
<xs:sequence>
<xs:element name="GraphModelCastorData">
<xs:complexType>
- <xs:choice maxOccurs="unbounded">
+ <xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="ActivitySlotDef">
<xs:complexType>
<xs:sequence>
diff --git a/src/test/java/MainTest.java b/src/test/java/MainTest.java
new file mode 100644
index 0000000..7aad26e
--- /dev/null
+++ b/src/test/java/MainTest.java
@@ -0,0 +1,100 @@
+import java.util.HashMap;
+import java.util.Properties;
+import java.util.StringTokenizer;
+
+import org.custommonkey.xmlunit.XMLUnit;
+
+import com.c2kernel.common.ObjectNotFoundException;
+import com.c2kernel.persistency.outcome.OutcomeValidator;
+import com.c2kernel.persistency.outcome.Schema;
+import com.c2kernel.persistency.outcome.SchemaValidator;
+import com.c2kernel.process.Gateway;
+import com.c2kernel.scripting.Script;
+import com.c2kernel.utils.FileStringUtility;
+import com.c2kernel.utils.Logger;
+import com.c2kernel.utils.Resource;
+
+public class MainTest {
+
+
+
+ public MainTest() throws Exception {
+ Logger.addLogStream(System.out, 1);
+ Properties props = FileStringUtility.loadConfigFile(MainTest.class.getResource("properties.conf").getPath());
+ Gateway.init(props);
+ Resource.initKernelBaseURL();
+ XMLUnit.setIgnoreWhitespace(true);
+ XMLUnit.setIgnoreComments(true);
+ }
+
+ public void testBootItems() throws Exception {
+ HashMap<String, OutcomeValidator> validators = new HashMap<String, OutcomeValidator>();
+ validators.put("CA", new OutcomeValidator(getSchema("CompositeActivityDef", 0, "boot/OD/CompositeActivityDef.xsd")));
+ validators.put("EA", new OutcomeValidator(getSchema("ElementaryActivityDef", 0, "boot/OD/ElementaryActivityDef.xsd")));
+ validators.put("SC", new OutcomeValidator(getSchema("Script", 0, "boot/OD/Script.xsd")));
+ validators.put("OD", new SchemaValidator());
+
+ String bootItems = FileStringUtility.url2String(Resource.getKernelResourceURL("boot/allbootitems.txt"));
+ StringTokenizer str = new StringTokenizer(bootItems, "\n\r");
+ while (str.hasMoreTokens()) {
+ String thisItem = str.nextToken();
+ Logger.msg(1, "Validating "+thisItem);
+ int delim = thisItem.indexOf('/');
+ String itemType = thisItem.substring(0,delim);
+ OutcomeValidator validator = validators.get(itemType);
+ String data = Resource.getTextResource(null, "boot/"+thisItem+(itemType.equals("OD")?".xsd":".xml"));
+ assert data!=null;
+ String errors = validator.validate(data);
+ if (errors.length() > 0) {
+ Logger.error("Kernel resource "+thisItem+" has errors :"+errors);
+ }
+ assert errors.length()==0;
+
+ if (itemType.equals("CA") && !itemType.equals("EA")) {
+ Logger.msg(1, "Remarshalling "+thisItem);
+ Object unmarshalled = Gateway.getMarshaller().unmarshall(data);
+ assert unmarshalled!=null;
+ String remarshalled = Gateway.getMarshaller().marshall(unmarshalled);
+ errors = validator.validate(remarshalled);
+ if (errors.length() > 0) {
+ Logger.error("Remarshalled resource "+thisItem+" has errors :"+errors);
+ }
+ assert errors.length()==0;
+
+// Diff xmlDiff = new Diff(data, remarshalled);
+// if (!xmlDiff.identical()) {
+// Logger.msg("Difference found in remarshalled "+thisItem+": "+xmlDiff.toString());
+// Logger.msg("Original: "+data);
+// Logger.msg("Remarshalled: "+remarshalled);
+// }
+// assert xmlDiff.identical();
+ }
+ }
+ }
+
+ private Schema getSchema(String name, int version, String resPath) throws ObjectNotFoundException {
+ return new Schema(name, version, false, Resource.getTextResource(null, resPath));
+ }
+
+ public void testScriptParsing() throws Exception {
+ OutcomeValidator valid = new OutcomeValidator(getSchema("Script", 0, "boot/OD/Script.xsd"));
+ String testScriptString = FileStringUtility.url2String(MainTest.class.getResource("TestScript.xml"));
+ String errors = valid.validate(testScriptString);
+ if (errors.length()>0) {
+ Logger.error("Test script not valid to schema: "+errors);
+ }
+ assert errors.length()==0;
+
+ Script testScript = new Script(testScriptString);
+ assert testScript.getInputParams().size()==1;
+ assert testScript.getInputParams().get("test")!=null;
+
+ testScript.setInputParamValue("test", "Test");
+ 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/java/ScriptTest.java b/src/test/java/ScriptTest.java
deleted file mode 100644
index 1cac1b5..0000000
--- a/src/test/java/ScriptTest.java
+++ /dev/null
@@ -1,47 +0,0 @@
-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");
- }
-
-
-}