// Hello World example using a custom <hello> action @Test public void testCustomActionHelloWorld() throws Exception { // (1) Form a list of custom actions defined in the SCXML // document (and any included documents via "src" attributes) CustomAction ca1 = new CustomAction("http://my.custom-actions.domain/CUSTOM1", "hello", Hello.class); // Register the same action under a different name, just to test // multiple custom actions CustomAction ca2 = new CustomAction("http://my.custom-actions.domain/CUSTOM2", "bar", Hello.class); List<CustomAction> customActions = new ArrayList<CustomAction>(); customActions.add(ca1); customActions.add(ca2); // (2) Parse the document SCXML scxml = SCXMLTestHelper.parse("org/apache/commons/scxml2/custom-hello-world-01.xml", customActions); // (3) Get a SCXMLExecutor SCXMLExecutor exec = SCXMLTestHelper.getExecutor(scxml); exec.go(); // (4) Single, final state Assert.assertEquals("custom", (exec.getStatus().getStates().iterator().next()).getId()); Assert.assertTrue(exec.getStatus().isFinal()); // The custom action defined by Hello.class should be called // to execute() exactly twice at this point (one by <my:hello/> and the other by <foo:bar/>). Assert.assertEquals(2, Hello.callbacks); }
// Hello World example using the SCXML <log> action @Test public void testHelloWorld() throws Exception { // (1) Get a SCXMLExecutor SCXMLExecutor exec = SCXMLTestHelper.getExecutor("org/apache/commons/scxml2/hello-world.xml"); exec.go(); // (2) Single, final state Assert.assertEquals("hello", (exec.getStatus().getStates().iterator().next()).getId()); Assert.assertTrue(exec.getStatus().isFinal()); }
// Hello World example using custom <my:hello> action that generates an // event which has the payload examined with JEXL expressions @Test public void testCustomActionEventPayloadHelloWorldJexl() throws Exception { // (1) Form a list of custom actions defined in the SCXML // document (and any included documents via "src" attributes) CustomAction ca = new CustomAction("http://my.custom-actions.domain/CUSTOM", "hello", Hello.class); List<CustomAction> customActions = new ArrayList<CustomAction>(); customActions.add(ca); // (2) Parse the document SCXML scxml = SCXMLTestHelper.parse( "org/apache/commons/scxml2/custom-hello-world-04-jexl.xml", customActions); // (3) Get a SCXMLExecutor SCXMLExecutor exec = SCXMLTestHelper.getExecutor(scxml); exec.go(); // (4) Single, final state Assert.assertEquals( "Invalid intermediate state", "custom1", (exec.getStatus().getStates().iterator().next()).getId()); // (5) Verify datamodel variable is correct Assert.assertEquals( "Missing helloName1 in root context", "custom04a", exec.getGlobalContext().get("helloName1")); // The custom action defined by Hello.class should be called // to execute() exactly once at this point (by onentry in init state). Assert.assertEquals(1, Hello.callbacks); // (6) Check use of payload in non-initial state SCXMLTestHelper.fireEvent(exec, "custom.next"); // (7) Verify correct end state Assert.assertEquals( "Missing helloName1 in root context", "custom04b", exec.getGlobalContext().get("helloName1")); Assert.assertEquals( "Invalid final state", "end", (exec.getStatus().getStates().iterator().next()).getId()); Assert.assertTrue(exec.getStatus().isFinal()); // The custom action defined by Hello.class should be called // to execute() exactly two times at this point (by onentry in custom2 state). Assert.assertEquals(2, Hello.callbacks); }
// Hello World example using custom <my:send> action // (overriding SCXML local name "send") @Test public void testCustomActionOverrideLocalName() throws Exception { // (1) List of custom actions, use same local name as SCXML action CustomAction ca = new CustomAction("http://my.custom-actions.domain/CUSTOM", "send", Hello.class); List<CustomAction> customActions = new ArrayList<CustomAction>(); customActions.add(ca); // (2) Parse the document SCXML scxml = SCXMLTestHelper.parse("org/apache/commons/scxml2/custom-hello-world-03.xml", customActions); // (3) Get a SCXMLExecutor SCXMLExecutor exec = SCXMLTestHelper.getExecutor(scxml); exec.go(); // (4) Single, final state Assert.assertEquals("custom", (exec.getStatus().getStates().iterator().next()).getId()); // The custom action defined by Hello.class should be called // to execute() exactly once at this point (by <my:send/>). Assert.assertEquals(1, Hello.callbacks); }
// Hello World example using custom <my:hello> action // as part of an external state source (src attribute) @Test public void testCustomActionExternalSrcHelloWorld() throws Exception { // (1) Form a list of custom actions defined in the SCXML // document (and any included documents via "src" attributes) CustomAction ca = new CustomAction("http://my.custom-actions.domain/CUSTOM", "hello", Hello.class); List<CustomAction> customActions = new ArrayList<CustomAction>(); customActions.add(ca); // (2) Parse the document SCXML scxml = SCXMLTestHelper.parse("org/apache/commons/scxml2/external-hello-world.xml", customActions); // (3) Get a SCXMLExecutor SCXMLExecutor exec = SCXMLTestHelper.getExecutor(scxml); exec.go(); // (4) Single, final state Assert.assertEquals("custom", (exec.getStatus().getStates().iterator().next()).getId()); // The custom action defined by Hello.class should be called // to execute() exactly twice at this point (one by <my:hello/> and the other by <my:hello/> in // external). Assert.assertEquals(2, Hello.callbacks); }