Source Code Cross Referenced for SCXMLTestHelper.java in  » Library » Apache-commons-scxml-0.6-src » org » apache » commons » scxml » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons scxml 0.6 src » org.apache.commons.scxml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.