1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
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");
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 = 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);
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);
}
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, 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");
}
}
|