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.io;
17  
18  import java.net.URL;
19  import java.util.List;
20  
21  import junit.framework.Test;
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  import junit.textui.TestRunner;
25  
26  import org.apache.commons.digester.Digester;
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;
52      private Digester digester;
53      private SCXML scxml;
54      private String scxmlAsString;
55  
56      /***
57       * Set up instance variables required by this test case.
58       */
59      public void setUp() {
60          microwave01 = this.getClass().getClassLoader().
61              getResource("org/apache/commons/scxml/env/jsp/microwave-01.xml");
62          microwave02 = this.getClass().getClassLoader().
63              getResource("org/apache/commons/scxml/env/jsp/microwave-02.xml");
64          transitions01 = this.getClass().getClassLoader().
65              getResource("org/apache/commons/scxml/transitions-01.xml");
66          send01 = this.getClass().getClassLoader().
67              getResource("org/apache/commons/scxml/send-01.xml");
68          digester = SCXMLDigester.newInstance();
69      }
70  
71      /***
72       * Tear down instance variables required by this test case.
73       */
74      public void tearDown() {
75          microwave01 = microwave02 = transitions01 = send01 = null;
76          scxml = null;
77          scxmlAsString = null;
78          digester = null;
79      }
80  
81      /***
82       * Test the implementation
83       */
84      public void testSCXMLDigesterMicrowave01Sample() {
85          scxml = SCXMLTestHelper.digest(microwave01);
86          scxmlAsString = serialize(scxml);
87      }
88  
89      public void testSCXMLDigesterMicrowave02Sample() {
90          scxml = SCXMLTestHelper.digest(microwave02);
91          scxmlAsString = serialize(scxml);
92      }
93  
94      public void testSCXMLDigesterTransitions01Sample() {
95          scxml = SCXMLTestHelper.digest(transitions01);
96          scxmlAsString = serialize(scxml);
97      }
98  
99      public void testSCXMLDigesterSend01Sample() {
100         // Digest
101         scxml = SCXMLTestHelper.digest(send01);
102         State ten = scxml.getInitialState();
103         assertEquals("ten", ten.getId());
104         List ten_done = ten.getTransitionsList("ten.done");
105         assertEquals(1, ten_done.size());
106         Transition ten2twenty = (Transition) ten_done.get(0);
107         List actions = ten2twenty.getActions();
108         assertEquals(1, actions.size());
109         Send send = (Send) actions.get(0);
110         assertEquals("send1", send.getSendid());
111         /* Serialize
112         scxmlAsString = serialize(scxml);
113         assertNotNull(scxmlAsString);
114         String expectedFoo2Serialization =
115             "<foo xmlns=\"http://my.test.namespace\" id=\"foo2\">"
116             + "<prompt xmlns=\"http://foo.bar.com/vxml3\">This is just"
117             + " an example.</prompt></foo>";
118         assertFalse(scxmlAsString.indexOf(expectedFoo2Serialization) == -1);
119         */
120     }
121 
122     private String serialize(final SCXML scxml) {
123         scxmlAsString = SCXMLSerializer.serialize(scxml);
124         assertNotNull(scxmlAsString);
125         return scxmlAsString;
126     }
127 
128      public static void main(String args[]) {
129         TestRunner.run(suite());
130     }
131 }
132