summaryrefslogtreecommitdiff
path: root/src/test/java/MainTest.java
blob: 26898ac2ca0783a088faee2611c4267e45ec3d96 (plain)
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
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.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);
		}
	}
	
	/**
	 * @throws Exception
	 */
	public void testMapFiles() throws Exception {

		Properties wAppProperties = new Properties();
		wAppProperties.put(CastorXMLUtility.CASTOR_XML_SERIALIZER_FACTORY,
				"org.exolab.castor.xml.XercesXMLSerializerFactory");

		new CastorXMLUtility(Gateway.getResource(), wAppProperties, "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");
    	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 : "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");
	            castorTime+=(now-then);
	            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();
            }
        }
        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);
		assert errors.length()==0 : "Test script not valid to schema: "+errors;
		
		Script testScript = new Script(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";
	}
}