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.lifecycle.instance.stateMachine.StateMachine; 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; public class MainTest { public static void main(String[] args) throws Exception { MainTest me = new MainTest(); me.testBootItems(); me.testScriptParsing(); me.testStateMachine(); } public MainTest() throws Exception { Logger.addLogStream(System.out, 1); Properties props = FileStringUtility.loadConfigFile(MainTest.class.getResource("server.conf").getPath()); Gateway.init(props); XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreComments(true); } public void testBootItems() throws Exception { HashMap validators = new HashMap(); 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("SM", new OutcomeValidator(getSchema("StateMachine", 0, "boot/OD/StateMachine.xsd"))); validators.put("OD", new SchemaValidator()); String bootItems = FileStringUtility.url2String(Gateway.getResource().getKernelResourceURL("boot/allbootitems.txt")); StringTokenizer str = new StringTokenizer(bootItems, "\n\r"); long castorTime=0; 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 = Gateway.getResource().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") || itemType.equals("SM")) { Logger.msg(1, "Remarshalling "+thisItem); long then = System.currentTimeMillis(); Object unmarshalled = Gateway.getMarshaller().unmarshall(data); assert unmarshalled!=null; String remarshalled = Gateway.getMarshaller().marshall(unmarshalled); long now = System.currentTimeMillis(); Logger.msg("Marshall/remarshall of "+thisItem+" took "+(now-then)+"ms"); castorTime+=(now-then); errors = validator.validate(remarshalled); if (errors.length() > 0) { Logger.error("Remarshalled resource "+thisItem+" has errors :"+errors+"\nRemarshalled form:\n"+remarshalled); } 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(); } } Logger.msg("Total Castor marshall time: "+castorTime+"ms"); } private static Schema getSchema(String name, int version, String resPath) throws ObjectNotFoundException { return new Schema(name, version, Gateway.getResource().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"); } public void testStateMachine() throws Exception { Logger.msg("Validating test state machine"); String smXml = FileStringUtility.url2String(MainTest.class.getResource("TestStateMachine.xml")); StateMachine sm = (StateMachine)Gateway.getMarshaller().unmarshall(smXml); sm.validate(); assert sm.isCoherent(); } }