001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.scxml2.model;
018
019import java.util.ArrayList;
020import java.util.Iterator;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.commons.scxml2.SCXMLExecutor;
025import org.apache.commons.scxml2.SCXMLIOProcessor;
026import org.apache.commons.scxml2.SCXMLTestHelper;
027import org.apache.commons.scxml2.TriggerEvent;
028import org.apache.commons.scxml2.env.SimpleDispatcher;
029import org.junit.Assert;
030import org.junit.Test;
031
032public class SendTest {
033
034    @Test
035    public void testNamelistOrderPreserved() throws Exception {
036        final List<Object> payloads = new ArrayList<Object>();
037        final SCXML scxml = SCXMLTestHelper.parse("org/apache/commons/scxml2/model/send-test-01.xml");
038        final SCXMLExecutor exec = SCXMLTestHelper.getExecutor(scxml, null, new SimpleDispatcher() {
039            @Override
040            public void send(final Map<String, SCXMLIOProcessor> ioProcessors, final String id, final String target,
041                    final String type, final String event, final Object data, final Object hints, final long delay) {
042                payloads.add(data);
043                super.send(ioProcessors, id, target, type, event, data, hints, delay);
044            }
045        });
046        exec.go();
047        TriggerEvent te = new TriggerEvent("event.foo", TriggerEvent.SIGNAL_EVENT, new Integer(3));
048        SCXMLTestHelper.fireEvent(exec, te);
049
050        Assert.assertFalse("Payloads empty.", payloads.isEmpty());
051        Assert.assertTrue("Payload is not a map.", payloads.get(0) instanceof Map);
052        Map<String, Object> firstPayload = (Map<String, Object>) payloads.get(0);
053        Assert.assertEquals("Only two in the namelist data expected.", 2, firstPayload.size());
054
055        Assert.assertEquals("Unexpected value for 'one'.", 1.0, firstPayload.get("one"));
056        Assert.assertEquals("Unexpected value for 'two'.", 2.0, firstPayload.get("two"));
057
058        // Note: the standard allows specifying the value of the namelist attribute of the <send> element
059        // as space-separated list of values, which implies an ordered sequence of items.
060        final Iterator<String> it = firstPayload.keySet().iterator();
061        Assert.assertEquals("The first one in the namelist must be 'one'.", "one", it.next());
062        Assert.assertEquals("The first one in the namelist must be 'two'.", "two", it.next());
063    }
064}