View Javadoc
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.scxml2.issues;
18  
19  import java.util.ArrayList;
20  import java.util.LinkedList;
21  import java.util.List;
22  import java.util.Queue;
23  
24  import org.apache.commons.scxml2.ActionExecutionContext;
25  import org.apache.commons.scxml2.SCXMLExecutor;
26  import org.apache.commons.scxml2.SCXMLExpressionException;
27  import org.apache.commons.scxml2.SCXMLTestHelper;
28  import org.apache.commons.scxml2.model.Action;
29  import org.apache.commons.scxml2.model.CustomAction;
30  import org.apache.commons.scxml2.model.ModelException;
31  import org.apache.commons.scxml2.model.SCXML;
32  import org.junit.After;
33  import org.junit.Assert;
34  import org.junit.Test;
35  
36  /**
37   * Test cases for issue 112.
38   * OPEN
39   */
40  public class Issue112Test {
41  
42      /**
43       * Tear down instance variables required by this test case.
44       */
45      @After
46      public void tearDown() {
47          Application.QUEUE.clear();
48      }
49  
50      // Example using a custom <my:enqueue> action that generates more events during event processing.
51      // An external event queue is used by <my:enqueue> instead of SCXMLExecutor#triggerEvent(TriggerEvent)    
52      @Test
53      public void test01issue112() throws Exception {
54  
55          CustomAction ca1 =
56              new CustomAction("http://my.custom-actions.domain/CUSTOM", "enqueue", Enqueue.class);
57          List<CustomAction> customActions = new ArrayList<CustomAction>();
58          customActions.add(ca1);
59  
60          SCXML scxml = SCXMLTestHelper.parse("org/apache/commons/scxml2/issues/queue-01.xml", customActions);
61          SCXMLExecutor exec = SCXMLTestHelper.getExecutor(scxml);
62          exec.go();
63          Assert.assertEquals("init", exec.getStatus().getStates().
64                  iterator().next().getId());
65  
66          // Add an event, other external events could be added to the queue at any time (this test only adds one).
67          Application.QUEUE.add("next");
68  
69          // Rest of the events in this test are added by custom action invocation during processing of the one above.
70          // Same concept applies to adding events in listeners, invokes and WRT AbstractStateMachine, state handlers.
71          while (!Application.QUEUE.isEmpty()) {
72              try {
73                  SCXMLTestHelper.fireEvent(exec, Application.QUEUE.remove());
74              } catch (Exception e) {
75                  e.printStackTrace();
76              }
77          }
78  
79          Assert.assertTrue(exec.getStatus().isFinal());
80          Assert.assertEquals("end", exec.getStatus().getStates().
81                  iterator().next().getId());
82  
83      }
84  
85      /**
86       * A custom action that generates external events.
87       */
88      public static class Enqueue extends Action {
89  
90          private static final long serialVersionUID = 1L;
91          private String event;
92  
93          public Enqueue() {
94              super();
95          }
96  
97          public String getEvent() {
98              return event;
99          }
100 
101         public void setEvent(String event) {
102             this.event = event;
103         }
104 
105         @Override
106         public void execute(ActionExecutionContext exctx) throws ModelException, SCXMLExpressionException {
107 
108             Application.QUEUE.add(event);
109 
110         }
111 
112     }
113 
114     // Test external event queue
115     private static final class Application {
116         private static final Queue<String> QUEUE = new LinkedList<String>();
117     }
118 
119 }
120