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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/**
* This file is part of the CRISTAL-iSE kernel.
* Copyright (c) 2001-2014 The CRISTAL Consortium. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
* http://www.fsf.org/licensing/licenses/lgpl.html
*/
import java.util.HashMap;
import java.util.Properties;
import java.util.StringTokenizer;
import org.custommonkey.xmlunit.XMLUnit;
import com.c2kernel.common.ObjectNotFound;
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.CastorXMLUtility;
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() {
try {
Logger.addLogStream(System.out, 1);
Properties props = FileStringUtility.loadConfigFile(MainTest.class.getResource("server.conf").getPath());
Gateway.init(props);
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreComments(true);
} catch (Exception ex) {
Logger.error(ex);
}
}
public void testMapFiles() throws Exception {
new CastorXMLUtility(Gateway.getResource(), Gateway.getProperties(), Gateway.getResource().getKernelResourceURL("mapFiles/"));
}
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("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");
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 : "Boot "+itemType+" data item "+thisItem+" not found";
String errors = validator.validate(data);
assert errors.length()==0 : "Kernel resource "+thisItem+" has errors :"+errors;
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");
errors = validator.validate(remarshalled);
assert errors.length()==0 : "Remarshalled resource "+thisItem+" has errors :"+errors+"\nRemarshalled form:\n"+remarshalled;
// 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();
}
if (itemType.equals("SC")) {
Logger.msg(1, "Parsing script "+thisItem);
new Script(thisItem, 0, data);
}
}
}
private static Schema getSchema(String name, int version, String resPath) throws ObjectNotFound {
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);
assert errors.length()==0 : "Test script not valid to schema: "+errors;
Script testScript = new Script("TestScript", 0, testScriptString);
assert testScript.getInputParams().size()==1 : "Script input param count wrong";
assert testScript.getInputParams().get("test")!=null : "Could not retrieve script input param value";
testScript.setInputParamValue("test", "Test");
assert testScript.getInputParams().get("test").getInitialised() : "Script is not initialized when it should be";
Object result = testScript.execute();
assert result!=null : "Script returned null result";
assert result instanceof String : "Script failed to return a String";
assert ((String)result).equals("TestTest") : "Script failed to produce correct result: "+result;
}
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() : "Test StateMachine is reporting that it is not coherent";
}
}
|