Author: rahul Date: Fri Aug 7 04:28:13 2009 New Revision: 801878 URL: http://svn.apache.org/viewvc?rev=801878&view=rev Log: Add a test case to demonstrate the use of an external event queue for an executor (where new external events generated during event processing are queued). SCXML-112
Added: commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/Issue112Test.java (with props) commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/queue-01.xml (with props) Modified: commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/IssuesTestSuite.java Added: commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/Issue112Test.java URL: http://svn.apache.org/viewvc/commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/Issue112Test.java?rev=801878&view=auto ============================================================================== --- commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/Issue112Test.java (added) +++ commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/Issue112Test.java Fri Aug 7 04:28:13 2009 @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.scxml.issues; + +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.apache.commons.logging.Log; +import org.apache.commons.scxml.ErrorReporter; +import org.apache.commons.scxml.EventDispatcher; +import org.apache.commons.scxml.SCInstance; +import org.apache.commons.scxml.SCXMLExecutor; +import org.apache.commons.scxml.SCXMLExpressionException; +import org.apache.commons.scxml.SCXMLTestHelper; +import org.apache.commons.scxml.TriggerEvent; +import org.apache.commons.scxml.model.Action; +import org.apache.commons.scxml.model.CustomAction; +import org.apache.commons.scxml.model.ModelException; +import org.apache.commons.scxml.model.SCXML; +import org.apache.commons.scxml.model.State; + +/** + * Test cases for issue 112. + * OPEN + */ +public class Issue112Test extends TestCase { + + public Issue112Test(String testName) { + super(testName); + } + + public static Test suite() { + return new TestSuite(Issue112Test.class); + } + + public static void main(String args[]) { + String[] testCaseName = { Issue112Test.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + private URL queue01; + private SCXMLExecutor exec; + + /** + * Set up instance variables required by this test case. + */ + @Override + public void setUp() { + queue01 = this.getClass().getClassLoader(). + getResource("org/apache/commons/scxml/issues/queue-01.xml"); + } + + /** + * Tear down instance variables required by this test case. + */ + @Override + public void tearDown() { + queue01 = null; + exec = null; + Application.QUEUE.clear(); + } + + // Example using a custom <my:enqueue> action that generates more events during event processing. + // An external event queue is used by <my:enqueue> instead of SCXMLExecutor#triggerEvent(TriggerEvent) + public void test01issue112() throws Exception { + + CustomAction ca1 = + new CustomAction("http://my.custom-actions.domain/CUSTOM", + "enqueue", Enqueue.class); + List<CustomAction> customActions = new ArrayList<CustomAction>(); + customActions.add(ca1); + + SCXML scxml = SCXMLTestHelper.parse(queue01, customActions); + + exec = SCXMLTestHelper.getExecutor(scxml); + assertEquals("init", ((State) exec.getCurrentStatus().getStates(). + iterator().next()).getId()); + + // Add an event, other external events could be added to the queue at any time (this test only adds one). + Application.QUEUE.add("next"); + + // Rest of the events in this test are added by custom action invocation during processing of the one above. + // Same concept applies to adding events in listeners, invokes and WRT AbstractStateMachine, state handlers. + while (!Application.QUEUE.isEmpty()) { + try { + SCXMLTestHelper.fireEvent(exec, Application.QUEUE.remove()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + assertTrue(exec.getCurrentStatus().isFinal()); + assertEquals("end", ((State) exec.getCurrentStatus().getStates(). + iterator().next()).getId()); + + } + + /** + * A custom action that generates external events. + */ + public static class Enqueue extends Action { + + private static final long serialVersionUID = 1L; + private String event; + + public Enqueue() { + super(); + } + + public String getEvent() { + return event; + } + + public void setEvent(String event) { + this.event = event; + } + + @Override + public void execute(EventDispatcher evtDispatcher, + ErrorReporter errRep, SCInstance scInstance, Log appLog, + Collection<TriggerEvent> derivedEvents) + throws ModelException, SCXMLExpressionException { + + Application.QUEUE.add(event); + + } + + } + + // Test external event queue + private static final class Application { + private static final Queue<String> QUEUE = new LinkedList<String>(); + } + +} + Propchange: commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/Issue112Test.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/Issue112Test.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Modified: commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/IssuesTestSuite.java URL: http://svn.apache.org/viewvc/commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/IssuesTestSuite.java?rev=801878&r1=801877&r2=801878&view=diff ============================================================================== --- commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/IssuesTestSuite.java (original) +++ commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/IssuesTestSuite.java Fri Aug 7 04:28:13 2009 @@ -49,6 +49,7 @@ suite.setName("Commons SCXML Issues Tests"); suite.addTest(Issue62Test.suite()); suite.addTest(Issue64Test.suite()); + suite.addTest(Issue112Test.suite()); return suite; } } Added: commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/queue-01.xml URL: http://svn.apache.org/viewvc/commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/queue-01.xml?rev=801878&view=auto ============================================================================== --- commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/queue-01.xml (added) +++ commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/queue-01.xml Fri Aug 7 04:28:13 2009 @@ -0,0 +1,50 @@ +<?xml version="1.0"?> +<!-- + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +--> +<!-- Correct SCXML document --> +<scxml xmlns="http://www.w3.org/2005/07/scxml" + xmlns:my="http://my.custom-actions.domain/CUSTOM" + version="1.0" + initial="init"> + + <state id="init"> + <transition event="next" target="triggers"/> + </state> + + <state id="triggers"> + <onentry> + <my:enqueue event="foo.bar"/> + <my:enqueue event="foo.baz"/> + </onentry> + <transition event="foo.bar" target="trigger"/> + </state> + + <state id="trigger"> + <transition event="foo.baz" target="wait"/> + <onexit> + <my:enqueue event="done"/> + </onexit> + </state> + + <state id="wait"> + <transition event="done" target="end"/> + </state> + + <final id="end"/> + +</scxml> + Propchange: commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/queue-01.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/issues/queue-01.xml ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL