    1   /*
    2    * Copyright 2005-2006 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.scxml;
    17
    18  import java.net.URL;
    19  import java.util.List;
    20  import java.util.Set;
    21
    22  import junit.framework.Assert;
    23
    24  import org.apache.commons.logging.Log;
    25  import org.apache.commons.logging.LogFactory;
    26
    27  import org.apache.commons.scxml.env.SimpleDispatcher;
    28  import org.apache.commons.scxml.env.Tracer;
    29  import org.apache.commons.scxml.env.jexl.JexlContext;
    30  import org.apache.commons.scxml.env.jexl.JexlEvaluator;
    31  import org.apache.commons.scxml.io.SCXMLDigester;
    32  import org.apache.commons.scxml.model.SCXML;
    33  import org.apache.commons.scxml.model.TransitionTarget;
    34
    35  import org.xml.sax.ErrorHandler;
    36  /***
    37   * Helper methods for running SCXML unit tests.
    38   */
    39  public class SCXMLTestHelper {
    40
    41      public static SCXML digest(final URL url) {
    42          return digest(url, null, null);
    43      }
    44
    45      public static SCXML digest(final URL url, final List customActions) {
    46          return digest(url, null, customActions);
    47      }
    48
    49      public static SCXML digest(final URL url, final ErrorHandler errHandler) {
    50          return digest(url, errHandler, null);
    51      }
    52
    53      public static SCXML digest(final URL url, final ErrorHandler errHandler,
    54              final List customActions) {
    55          Assert.assertNotNull(url);
    56          // SAX ErrorHandler may be null
    57          SCXML scxml = null;
    58          try {
    59              scxml = SCXMLDigester.digest(url, errHandler, customActions);
    60          } catch (Exception e) {
    61              Log log = LogFactory.getLog(SCXMLTestHelper.class);
    62              log.error(e.getMessage(), e);
    63              Assert.fail(e.getMessage());
    64          }
    65          Assert.assertNotNull(scxml);
    66          return scxml;
    67      }
    68
    69      public static SCXMLExecutor getExecutor(final URL url) {
    70          SCXML scxml = digest(url);
    71          Evaluator evaluator = new JexlEvaluator();
    72          return getExecutor(evaluator, scxml);
    73      }
    74
    75      public static SCXMLExecutor getExecutor(final URL url,
    76              final Evaluator evaluator) {
    77          SCXML scxml = digest(url);
    78          return getExecutor(evaluator, scxml);
    79      }
    80
    81      public static SCXMLExecutor getExecutor(final URL url,
    82              final ErrorHandler errHandler) {
    83          SCXML scxml = digest(url, errHandler);
    84          Evaluator evaluator = new JexlEvaluator();
    85          return getExecutor(evaluator, scxml);
    86      }
    87
    88      public static SCXMLExecutor getExecutor(SCXML scxml) {
    89          return getExecutor(scxml, null);
    90      }
    91
    92      public static SCXMLExecutor getExecutor(SCXML scxml,
    93              SCXMLSemantics semantics) {
    94          Context context = new JexlContext();
    95          Evaluator evaluator = new JexlEvaluator();
    96          EventDispatcher ed = new SimpleDispatcher();
    97          Tracer trc = new Tracer();
    98          return getExecutor(context, evaluator, scxml, ed, trc, semantics);
    99      }
    100
    101     public static SCXMLExecutor getExecutor(Evaluator evaluator, SCXML scxml) {
    102         EventDispatcher ed = new SimpleDispatcher();
    103         Tracer trc = new Tracer();
    104         Context context = new JexlContext();
    105         return getExecutor(context, evaluator, scxml, ed, trc);
    106     }
    107
    108     public static SCXMLExecutor getExecutor(final URL url, final Context ctx,
    109             final Evaluator evaluator) {
    110         SCXML scxml = digest(url);
    111         EventDispatcher ed = new SimpleDispatcher();
    112         Tracer trc = new Tracer();
    113         return getExecutor(ctx, evaluator, scxml, ed, trc);
    114     }
    115
    116     public static SCXMLExecutor getExecutor(final SCXML scxml,
    117             final Context ctx, final Evaluator evaluator) {
    118         EventDispatcher ed = new SimpleDispatcher();
    119         Tracer trc = new Tracer();
    120         return getExecutor(ctx, evaluator, scxml, ed, trc);
    121     }
    122
    123     public static SCXMLExecutor getExecutor(Context context,
    124             Evaluator evaluator, SCXML scxml, EventDispatcher ed, Tracer trc) {
    125         return getExecutor(context, evaluator, scxml, ed, trc, null);
    126     }
    127
    128     public static SCXMLExecutor getExecutor(Context context,
    129             Evaluator evaluator, SCXML scxml, EventDispatcher ed,
    130             Tracer trc, SCXMLSemantics semantics) {
    131         Assert.assertNotNull(evaluator);
    132         Assert.assertNotNull(context);
    133         Assert.assertNotNull(scxml);
    134         Assert.assertNotNull(ed);
    135         Assert.assertNotNull(trc);
    136         SCXMLExecutor exec = null;
    137         try {
    138             if (semantics == null) {
    139                 exec = new SCXMLExecutor(evaluator, ed, trc);
    140             } else {
    141                 exec = new SCXMLExecutor(evaluator, ed, trc, semantics);
    142             }
    143             exec.addListener(scxml, trc);
    144             exec.setRootContext(context);
    145             exec.setSuperStep(true);
    146             exec.setStateMachine(scxml);
    147             exec.go();
    148         } catch (Exception e) {
    149             Log log = LogFactory.getLog(SCXMLTestHelper.class);
    150             log.error(e.getMessage(), e);
    151             Assert.fail(e.getMessage());
    152         }
    153         Assert.assertNotNull(exec);
    154         return exec;
    155     }
    156
    157     public static TransitionTarget lookupTransitionTarget(SCXMLExecutor exec,
    158             String id) {
    159         return (TransitionTarget) exec.getStateMachine().getTargets().get(id);
    160     }
    161
    162     public static Context lookupContext(SCXMLExecutor exec,
    163             TransitionTarget tt) {
    164         return exec.getSCInstance().lookupContext(tt);
    165     }
    166
    167     public static Context lookupContext(SCXMLExecutor exec,
    168             String id) {
    169         TransitionTarget tt = lookupTransitionTarget(exec, id);
    170         if (tt == null) {
    171             return null;
    172         }
    173         return exec.getSCInstance().lookupContext(tt);
    174     }
    175
    176     public static Set fireEvent(SCXMLExecutor exec, String name) {
    177         TriggerEvent[] evts = {new TriggerEvent(name,
    178                 TriggerEvent.SIGNAL_EVENT, null)};
    179         try {
    180             exec.triggerEvents(evts);
    181         } catch (Exception e) {
    182             Log log = LogFactory.getLog(SCXMLTestHelper.class);
    183             log.error(e.getMessage(), e);
    184             Assert.fail(e.getMessage());
    185         }
    186         return exec.getCurrentStatus().getStates();
    187     }
    188
    189     public static Set fireEvent(SCXMLExecutor exec, TriggerEvent te) {
    190         TriggerEvent[] evts = { te };
    191         try {
    192             exec.triggerEvents(evts);
    193         } catch (Exception e) {
    194             Log log = LogFactory.getLog(SCXMLTestHelper.class);
    195             log.error(e.getMessage(), e);
    196             Assert.fail(e.getMessage());
    197         }
    198         return exec.getCurrentStatus().getStates();
    199     }
    200
    201     public static Set fireEvents(SCXMLExecutor exec, TriggerEvent[] evts) {
    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     /***
    213      * Discourage instantiation.
    214      */
    215     private SCXMLTestHelper() {
    216         super();
    217     }
    218
    219 }
    220