    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.scxml.model;
    18
    19  import java.net.URL;
    20  import java.util.ArrayList;
    21  import java.util.List;
    22
    23  import junit.framework.Test;
    24  import junit.framework.TestCase;
    25  import junit.framework.TestSuite;
    26
    27  import org.apache.commons.scxml.SCXMLExecutor;
    28  import org.apache.commons.scxml.SCXMLTestHelper;
    29
    30  public class CustomActionTest extends TestCase {
    31
    32      public CustomActionTest(String testName) {
    33          super(testName);
    34      }
    35
    36      public static Test suite() {
    37          return new TestSuite(CustomActionTest.class);
    38      }
    39
    40      public static void main(String args[]) {
    41          String[] testCaseName = { CustomActionTest.class.getName()};
    42          junit.textui.TestRunner.main(testCaseName);
    43      }
    44
    45      private URL hello01, custom01, external01, override01;
    46      private SCXMLExecutor exec;
    47
    48      /***
    49       * Set up instance variables required by this test case.
    50       */
    51      public void setUp() {
    52          hello01 = this.getClass().getClassLoader().
    53              getResource("org/apache/commons/scxml/hello-world.xml");
    54          custom01 = this.getClass().getClassLoader().
    55              getResource("org/apache/commons/scxml/custom-hello-world-01.xml");
    56          external01 = this.getClass().getClassLoader().
    57              getResource("org/apache/commons/scxml/external-hello-world.xml");
    58          override01 = this.getClass().getClassLoader().
    59              getResource("org/apache/commons/scxml/custom-hello-world-03.xml");
    60      }
    61
    62      /***
    63       * Tear down instance variables required by this test case.
    64       */
    65      public void tearDown() {
    66          hello01 = custom01 = external01 = null;
    67          exec = null;
    68      }
    69
    70      public void testAddGoodCustomAction01() {
    71          try {
    72              new CustomAction("http://my.actions.domain/CUSTOM", "hello",
    73                  Hello.class);
    74          } catch (IllegalArgumentException iae) {
    75              fail("Failed to add custom action &quot;Hello&quot;");
    76          }
    77      }
    78
    79      public void testAddBadCustomAction01() {
    80          try {
    81              new CustomAction(null, "hello", Hello.class);
    82              fail("Added custom action with illegal namespace");
    83          } catch (IllegalArgumentException iae) {
    84              // Expected
    85          }
    86      }
    87
    88      public void testAddBadCustomAction02() {
    89          try {
    90              new CustomAction("  ", "hello", Hello.class);
    91              fail("Added custom action with illegal namespace");
    92          } catch (IllegalArgumentException iae) {
    93              // Expected
    94          }
    95      }
    96
    97      public void testAddBadCustomAction03() {
    98          try {
    99              new CustomAction("http://my.actions.domain/CUSTOM", "",
    100                 Hello.class);
    101             fail("Added custom action with illegal local name");
    102         } catch (IllegalArgumentException iae) {
    103             // Expected
    104         }
    105     }
    106
    107     public void testAddBadCustomAction04() {
    108         try {
    109             new CustomAction("http://my.actions.domain/CUSTOM", "  ",
    110                 Hello.class);
    111             fail("Added custom action with illegal local name");
    112         } catch (IllegalArgumentException iae) {
    113             // Expected
    114         }
    115     }
    116
    117     public void testAddBadCustomAction05() {
    118         try {
    119             new CustomAction("http://my.actions.domain/CUSTOM", "foo",
    120                 this.getClass());
    121             fail("Added custom action which is not an Action class subtype");
    122         } catch (IllegalArgumentException iae) {
    123             // Expected
    124         }
    125     }
    126
    127     public void testAddBadCustomAction06() {
    128         try {
    129             new CustomAction("http://www.w3.org/2005/07/scxml", "foo",
    130                 this.getClass());
    131             fail("Added custom action in the SCXML namespace");
    132         } catch (IllegalArgumentException iae) {
    133             // Expected
    134         }
    135     }
    136
    137     // Hello World example using the SCXML <log> action
    138     public void testHelloWorld() {
    139         // (1) Get a SCXMLExecutor
    140         exec = SCXMLTestHelper.getExecutor(hello01);
    141         // (2) Single, final state
    142         assertEquals("hello", ((State) exec.getCurrentStatus().getStates().
    143                 iterator().next()).getId());
    144         assertTrue(exec.getCurrentStatus().isFinal());
    145     }
    146
    147     // Hello World example using a custom <hello> action
    148     public void testCustomActionHelloWorld() {
    149         // (1) Form a list of custom actions defined in the SCXML
    150         //     document (and any included documents via "src" attributes)
    151         CustomAction ca1 =
    152             new CustomAction("http://my.custom-actions.domain/CUSTOM1",
    153                              "hello", Hello.class);
    154         // Register the same action under a different name, just to test
    155         // multiple custom actions
    156         CustomAction ca2 =
    157             new CustomAction("http://my.custom-actions.domain/CUSTOM2",
    158                              "bar", Hello.class);
    159         List customActions = new ArrayList();
    160         customActions.add(ca1);
    161         customActions.add(ca2);
    162         // (2) Parse the document with a custom digester.
    163         SCXML scxml = SCXMLTestHelper.digest(custom01, customActions);
    164         // (3) Get a SCXMLExecutor
    165         exec = SCXMLTestHelper.getExecutor(scxml);
    166         // (4) Single, final state
    167         assertEquals("custom", ((State) exec.getCurrentStatus().getStates().
    168                 iterator().next()).getId());
    169         assertTrue(exec.getCurrentStatus().isFinal());
    170     }
    171
    172     // Hello World example using custom <my:hello> action
    173     // as part of an external state source (src attribute)
    174     public void testCustomActionExternalSrcHelloWorld() {
    175         // (1) Form a list of custom actions defined in the SCXML
    176         //     document (and any included documents via "src" attributes)
    177         CustomAction ca =
    178             new CustomAction("http://my.custom-actions.domain/CUSTOM",
    179                              "hello", Hello.class);
    180         List customActions = new ArrayList();
    181         customActions.add(ca);
    182         // (2) Parse the document with a custom digester.
    183         SCXML scxml = SCXMLTestHelper.digest(external01, customActions);
    184         // (3) Get a SCXMLExecutor
    185         exec = SCXMLTestHelper.getExecutor(scxml);
    186         // (4) Single, final state
    187         assertEquals("custom", ((State) exec.getCurrentStatus().getStates().
    188             iterator().next()).getId());
    189     }
    190
    191     // Hello World example using custom <my:send> action
    192     // (overriding SCXML local name "send")
    193     public void testCustomActionOverrideLocalName() {
    194         // (1) List of custom actions, use same local name as SCXML action
    195         CustomAction ca =
    196             new CustomAction("http://my.custom-actions.domain/CUSTOM",
    197                              "send", Hello.class);
    198         List customActions = new ArrayList();
    199         customActions.add(ca);
    200         // (2) Parse the document with a custom digester.
    201         SCXML scxml = SCXMLTestHelper.digest(override01, customActions);
    202         // (3) Get a SCXMLExecutor
    203         exec = SCXMLTestHelper.getExecutor(scxml);
    204         // (4) Single, final state
    205         assertEquals("custom", ((State) exec.getCurrentStatus().getStates().
    206             iterator().next()).getId());
    207     }
    208
    209     // The custom action defined by Hello.class should be called
    210     // to execute() exactly 5 times upto this point
    211     public void testCustomActionCallbacks() {
    212         assertEquals(5, Hello.callbacks);
    213     }
    214
    215 }
    216