    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.io;
    18
    19  import java.net.URL;
    20  import java.util.List;
    21
    22  import junit.framework.Test;
    23  import junit.framework.TestCase;
    24  import junit.framework.TestSuite;
    25  import junit.textui.TestRunner;
    26
    27  import org.apache.commons.scxml.SCXMLTestHelper;
    28  import org.apache.commons.scxml.model.SCXML;
    29  import org.apache.commons.scxml.model.Send;
    30  import org.apache.commons.scxml.model.State;
    31  import org.apache.commons.scxml.model.Transition;
    32  /***
    33   * Unit tests {@link org.apache.commons.scxml.SCXMLDigester}.
    34   */
    35  public class SCXMLDigesterTest extends TestCase {
    36      /***
    37       * Construct a new instance of SCXMLDigesterTest with
    38       * the specified name
    39       */
    40      public SCXMLDigesterTest(String name) {
    41          super(name);
    42      }
    43
    44      public static Test suite() {
    45          TestSuite suite = new TestSuite(SCXMLDigesterTest.class);
    46          suite.setName("SCXML Digester Tests");
    47          return suite;
    48      }
    49
    50      // Test data
    51      private URL microwave01, microwave02, transitions01, send01, prefix01;
    52      private SCXML scxml;
    53      private String scxmlAsString;
    54
    55      /***
    56       * Set up instance variables required by this test case.
    57       */
    58      public void setUp() {
    59          microwave01 = this.getClass().getClassLoader().
    60              getResource("org/apache/commons/scxml/env/jsp/microwave-01.xml");
    61          microwave02 = this.getClass().getClassLoader().
    62              getResource("org/apache/commons/scxml/env/jsp/microwave-02.xml");
    63          transitions01 = this.getClass().getClassLoader().
    64              getResource("org/apache/commons/scxml/transitions-01.xml");
    65          send01 = this.getClass().getClassLoader().
    66              getResource("org/apache/commons/scxml/send-01.xml");
    67          prefix01 = this.getClass().getClassLoader().
    68              getResource("org/apache/commons/scxml/prefix-01.xml");
    69      }
    70
    71      /***
    72       * Tear down instance variables required by this test case.
    73       */
    74      public void tearDown() {
    75          microwave01 = microwave02 = transitions01 = prefix01 = send01 = null;
    76          scxml = null;
    77          scxmlAsString = null;
    78      }
    79
    80      /***
    81       * Test the implementation
    82       */
    83      public void testSCXMLDigesterMicrowave01Sample() {
    84          scxml = SCXMLTestHelper.digest(microwave01);
    85          assertNotNull(scxml);
    86          scxmlAsString = serialize(scxml);
    87          assertNotNull(scxmlAsString);
    88      }
    89
    90      public void testSCXMLDigesterMicrowave02Sample() {
    91          scxml = SCXMLTestHelper.digest(microwave02);
    92          assertNotNull(scxml);
    93          scxmlAsString = serialize(scxml);
    94          assertNotNull(scxmlAsString);
    95      }
    96
    97      public void testSCXMLDigesterTransitions01Sample() {
    98          scxml = SCXMLTestHelper.digest(transitions01);
    99          assertNotNull(scxml);
    100         scxmlAsString = serialize(scxml);
    101         assertNotNull(scxmlAsString);
    102     }
    103
    104     public void testSCXMLDigesterPrefix01Sample() {
    105         scxml = SCXMLTestHelper.digest(prefix01);
    106         assertNotNull(scxml);
    107         scxmlAsString = serialize(scxml);
    108         assertNotNull(scxmlAsString);
    109     }
    110
    111     public void testSCXMLDigesterSend01Sample() {
    112         // Digest
    113         scxml = SCXMLTestHelper.digest(send01);
    114         State ten = scxml.getInitialState();
    115         assertEquals("ten", ten.getId());
    116         List ten_done = ten.getTransitionsList("ten.done");
    117         assertEquals(1, ten_done.size());
    118         Transition ten2twenty = (Transition) ten_done.get(0);
    119         List actions = ten2twenty.getActions();
    120         assertEquals(1, actions.size());
    121         Send send = (Send) actions.get(0);
    122         assertEquals("send1", send.getSendid());
    123         /* Serialize
    124         scxmlAsString = serialize(scxml);
    125         assertNotNull(scxmlAsString);
    126         String expectedFoo2Serialization =
    127             "<foo xmlns=\"http://my.test.namespace\" id=\"foo2\">"
    128             + "<prompt xmlns=\"http://foo.bar.com/vxml3\">This is just"
    129             + " an example.</prompt></foo>";
    130         assertFalse(scxmlAsString.indexOf(expectedFoo2Serialization) == -1);
    131         */
    132     }
    133
    134     private String serialize(final SCXML scxml) {
    135         scxmlAsString = SCXMLSerializer.serialize(scxml);
    136         assertNotNull(scxmlAsString);
    137         return scxmlAsString;
    138     }
    139
    140      public static void main(String args[]) {
    141         TestRunner.run(suite());
    142     }
    143 }
    144