001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.scxml;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.NotSerializableException;
024: import java.io.ObjectInputStream;
025: import java.io.ObjectOutputStream;
026: import java.net.URL;
027: import java.util.List;
028: import java.util.Set;
029:
030: import junit.framework.Assert;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.commons.scxml.env.SimpleDispatcher;
035: import org.apache.commons.scxml.env.Tracer;
036: import org.apache.commons.scxml.env.jexl.JexlContext;
037: import org.apache.commons.scxml.env.jexl.JexlEvaluator;
038: import org.apache.commons.scxml.io.SCXMLDigester;
039: import org.apache.commons.scxml.model.SCXML;
040: import org.apache.commons.scxml.model.TransitionTarget;
041: import org.xml.sax.ErrorHandler;
042:
043: /**
044: * Helper methods for running SCXML unit tests.
045: */
046: public class SCXMLTestHelper {
047:
048: /**
049: * Serialized Commons SCXML object model temporary store.
050: * Assumes the default build artifacts are generated in the
051: * "target" directory (so it can be removed via a clean build).
052: */
053: public static final String SERIALIZATION_DIR = "target/serialization";
054: public static final String SERIALIZATION_FILE_PREFIX = SERIALIZATION_DIR
055: + "/scxml";
056: public static final String SERIALIZATION_FILE_SUFFIX = ".ser";
057:
058: public static SCXML digest(final URL url) {
059: return digest(url, null, null);
060: }
061:
062: public static SCXML digest(final URL url, final List customActions) {
063: return digest(url, null, customActions);
064: }
065:
066: public static SCXML digest(final URL url,
067: final ErrorHandler errHandler) {
068: return digest(url, errHandler, null);
069: }
070:
071: public static SCXML digest(final URL url,
072: final ErrorHandler errHandler, final List customActions) {
073: Assert.assertNotNull(url);
074: // SAX ErrorHandler may be null
075: SCXML scxml = null;
076: try {
077: scxml = SCXMLDigester
078: .digest(url, errHandler, customActions);
079: } catch (Exception e) {
080: Log log = LogFactory.getLog(SCXMLTestHelper.class);
081: log.error(e.getMessage(), e);
082: Assert.fail(e.getMessage());
083: }
084: Assert.assertNotNull(scxml);
085: SCXML roundtrip = testModelSerializability(scxml);
086: return roundtrip;
087: }
088:
089: public static SCXMLExecutor getExecutor(final URL url) {
090: SCXML scxml = digest(url);
091: Evaluator evaluator = new JexlEvaluator();
092: return getExecutor(evaluator, scxml);
093: }
094:
095: public static SCXMLExecutor getExecutor(final URL url,
096: final Evaluator evaluator) {
097: SCXML scxml = digest(url);
098: return getExecutor(evaluator, scxml);
099: }
100:
101: public static SCXMLExecutor getExecutor(final URL url,
102: final ErrorHandler errHandler) {
103: SCXML scxml = digest(url, errHandler);
104: Evaluator evaluator = new JexlEvaluator();
105: return getExecutor(evaluator, scxml);
106: }
107:
108: public static SCXMLExecutor getExecutor(SCXML scxml) {
109: return getExecutor(scxml, null);
110: }
111:
112: public static SCXMLExecutor getExecutor(SCXML scxml,
113: SCXMLSemantics semantics) {
114: Context context = new JexlContext();
115: Evaluator evaluator = new JexlEvaluator();
116: EventDispatcher ed = new SimpleDispatcher();
117: Tracer trc = new Tracer();
118: return getExecutor(context, evaluator, scxml, ed, trc,
119: semantics);
120: }
121:
122: public static SCXMLExecutor getExecutor(Evaluator evaluator,
123: SCXML scxml) {
124: EventDispatcher ed = new SimpleDispatcher();
125: Tracer trc = new Tracer();
126: Context context = new JexlContext();
127: return getExecutor(context, evaluator, scxml, ed, trc);
128: }
129:
130: public static SCXMLExecutor getExecutor(final URL url,
131: final Context ctx, final Evaluator evaluator) {
132: SCXML scxml = digest(url);
133: EventDispatcher ed = new SimpleDispatcher();
134: Tracer trc = new Tracer();
135: return getExecutor(ctx, evaluator, scxml, ed, trc);
136: }
137:
138: public static SCXMLExecutor getExecutor(final SCXML scxml,
139: final Context ctx, final Evaluator evaluator) {
140: EventDispatcher ed = new SimpleDispatcher();
141: Tracer trc = new Tracer();
142: return getExecutor(ctx, evaluator, scxml, ed, trc);
143: }
144:
145: public static SCXMLExecutor getExecutor(Context context,
146: Evaluator evaluator, SCXML scxml, EventDispatcher ed,
147: Tracer trc) {
148: return getExecutor(context, evaluator, scxml, ed, trc, null);
149: }
150:
151: public static SCXMLExecutor getExecutor(Context context,
152: Evaluator evaluator, SCXML scxml, EventDispatcher ed,
153: Tracer trc, SCXMLSemantics semantics) {
154: Assert.assertNotNull(evaluator);
155: Assert.assertNotNull(context);
156: Assert.assertNotNull(scxml);
157: Assert.assertNotNull(ed);
158: Assert.assertNotNull(trc);
159: SCXMLExecutor exec = null;
160: try {
161: if (semantics == null) {
162: exec = new SCXMLExecutor(evaluator, ed, trc);
163: } else {
164: exec = new SCXMLExecutor(evaluator, ed, trc, semantics);
165: }
166: exec.addListener(scxml, trc);
167: exec.setRootContext(context);
168: exec.setSuperStep(true);
169: exec.setStateMachine(scxml);
170: exec.go();
171: } catch (Exception e) {
172: Log log = LogFactory.getLog(SCXMLTestHelper.class);
173: log.error(e.getMessage(), e);
174: Assert.fail(e.getMessage());
175: }
176: Assert.assertNotNull(exec);
177: return exec;
178: }
179:
180: public static TransitionTarget lookupTransitionTarget(
181: SCXMLExecutor exec, String id) {
182: return (TransitionTarget) exec.getStateMachine().getTargets()
183: .get(id);
184: }
185:
186: public static Context lookupContext(SCXMLExecutor exec,
187: TransitionTarget tt) {
188: return exec.getSCInstance().lookupContext(tt);
189: }
190:
191: public static Context lookupContext(SCXMLExecutor exec, String id) {
192: TransitionTarget tt = lookupTransitionTarget(exec, id);
193: if (tt == null) {
194: return null;
195: }
196: return exec.getSCInstance().lookupContext(tt);
197: }
198:
199: public static Set fireEvent(SCXMLExecutor exec, String name) {
200: TriggerEvent[] evts = { new TriggerEvent(name,
201: TriggerEvent.SIGNAL_EVENT, null) };
202: try {
203: exec.triggerEvents(evts);
204: } catch (Exception e) {
205: Log log = LogFactory.getLog(SCXMLTestHelper.class);
206: log.error(e.getMessage(), e);
207: Assert.fail(e.getMessage());
208: }
209: return exec.getCurrentStatus().getStates();
210: }
211:
212: public static Set fireEvent(SCXMLExecutor exec, TriggerEvent te) {
213: TriggerEvent[] evts = { te };
214: try {
215: exec.triggerEvents(evts);
216: } catch (Exception e) {
217: Log log = LogFactory.getLog(SCXMLTestHelper.class);
218: log.error(e.getMessage(), e);
219: Assert.fail(e.getMessage());
220: }
221: return exec.getCurrentStatus().getStates();
222: }
223:
224: public static Set fireEvents(SCXMLExecutor exec, TriggerEvent[] evts) {
225: try {
226: exec.triggerEvents(evts);
227: } catch (Exception e) {
228: Log log = LogFactory.getLog(SCXMLTestHelper.class);
229: log.error(e.getMessage(), e);
230: Assert.fail(e.getMessage());
231: }
232: return exec.getCurrentStatus().getStates();
233: }
234:
235: public static SCXML testModelSerializability(final SCXML scxml) {
236: File fileDir = new File(SERIALIZATION_DIR);
237: if (!fileDir.exists() && !fileDir.mkdir()) {
238: return null;
239: }
240: String filename = SERIALIZATION_FILE_PREFIX
241: + System.currentTimeMillis()
242: + SERIALIZATION_FILE_SUFFIX;
243: SCXML roundtrip = null;
244: try {
245: ObjectOutputStream out = new ObjectOutputStream(
246: new FileOutputStream(filename));
247: out.writeObject(scxml);
248: out.close();
249: ObjectInputStream in = new ObjectInputStream(
250: new FileInputStream(filename));
251: roundtrip = (SCXML) in.readObject();
252: in.close();
253: } catch (NotSerializableException nse) {
254: // <data> nodes failed serialization
255: System.err
256: .println("SERIALIZATION ERROR: The DOM implementation"
257: + " in use is not serializable");
258: return scxml;
259: } catch (IOException ex) {
260: ex.printStackTrace();
261: } catch (ClassNotFoundException ex) {
262: ex.printStackTrace();
263: }
264: return roundtrip;
265: }
266:
267: public static SCXMLExecutor testExecutorSerializability(
268: final SCXMLExecutor exec) {
269: File fileDir = new File(SERIALIZATION_DIR);
270: if (!fileDir.exists() && !fileDir.mkdir()) {
271: return null;
272: }
273: String filename = SERIALIZATION_FILE_PREFIX
274: + System.currentTimeMillis()
275: + SERIALIZATION_FILE_SUFFIX;
276: SCXMLExecutor roundtrip = null;
277: try {
278: ObjectOutputStream out = new ObjectOutputStream(
279: new FileOutputStream(filename));
280: out.writeObject(exec);
281: out.close();
282: ObjectInputStream in = new ObjectInputStream(
283: new FileInputStream(filename));
284: roundtrip = (SCXMLExecutor) in.readObject();
285: in.close();
286: } catch (NotSerializableException nse) {
287: // <data> nodes failed serialization, test cases do not add
288: // other non-serializable context data
289: System.err
290: .println("SERIALIZATION ERROR: The DOM implementation"
291: + " in use is not serializable");
292: return exec;
293: } catch (IOException ex) {
294: ex.printStackTrace();
295: } catch (ClassNotFoundException ex) {
296: ex.printStackTrace();
297: }
298: return roundtrip;
299: }
300:
301: /**
302: * Discourage instantiation.
303: */
304: private SCXMLTestHelper() {
305: super();
306: }
307:
308: }
|