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.model;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.scxml2.SCXMLExecutor;
25  import org.apache.commons.scxml2.SCXMLIOProcessor;
26  import org.apache.commons.scxml2.SCXMLTestHelper;
27  import org.apache.commons.scxml2.TriggerEvent;
28  import org.apache.commons.scxml2.env.SimpleDispatcher;
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  public class SendTest {
33  
34      @Test
35      public void testNamelistOrderPreserved() throws Exception {
36          final List<Object> payloads = new ArrayList<Object>();
37          final SCXML scxml = SCXMLTestHelper.parse("org/apache/commons/scxml2/model/send-test-01.xml");
38          final SCXMLExecutor exec = SCXMLTestHelper.getExecutor(scxml, null, new SimpleDispatcher() {
39              @Override
40              public void send(final Map<String, SCXMLIOProcessor> ioProcessors, final String id, final String target,
41                      final String type, final String event, final Object data, final Object hints, final long delay) {
42                  payloads.add(data);
43                  super.send(ioProcessors, id, target, type, event, data, hints, delay);
44              }
45          });
46          exec.go();
47          TriggerEvent te = new TriggerEvent("event.foo", TriggerEvent.SIGNAL_EVENT, new Integer(3));
48          SCXMLTestHelper.fireEvent(exec, te);
49  
50          Assert.assertFalse("Payloads empty.", payloads.isEmpty());
51          Assert.assertTrue("Payload is not a map.", payloads.get(0) instanceof Map);
52          Map<String, Object> firstPayload = (Map<String, Object>) payloads.get(0);
53          Assert.assertEquals("Only two in the namelist data expected.", 2, firstPayload.size());
54  
55          Assert.assertEquals("Unexpected value for 'one'.", 1.0, firstPayload.get("one"));
56          Assert.assertEquals("Unexpected value for 'two'.", 2.0, firstPayload.get("two"));
57  
58          // Note: the standard allows specifying the value of the namelist attribute of the <send> element
59          // as space-separated list of values, which implies an ordered sequence of items.
60          final Iterator<String> it = firstPayload.keySet().iterator();
61          Assert.assertEquals("The first one in the namelist must be 'one'.", "one", it.next());
62          Assert.assertEquals("The first one in the namelist must be 'two'.", "two", it.next());
63      }
64  }