commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ra...@apache.org
Subject svn commit: r381056 - in /jakarta/commons/sandbox/scxml/trunk/src: main/java/org/apache/commons/scxml/ main/java/org/apache/commons/scxml/io/ test/java/org/apache/commons/scxml/ test/java/org/apache/commons/scxml/model/
Date Sun, 26 Feb 2006 05:15:38 GMT
Author: rahul
Date: Sat Feb 25 21:15:33 2006
New Revision: 381056

URL: http://svn.apache.org/viewcvs?rev=381056&view=rev
Log:
Added easier support for custom actions. "Easier" implies that knowledge of Digester may not
be required for adding some flavors of custom actions to augment those already available in
the SCXML namespace.

As a demonstration, a custom action <my:hello name="..." /> registered to a fictitious
namespace bound to prefix "my" is used in one of the test cases (it merely logs a hello to
the value of the name attribute).

On a separate note, something that should have probably been done a long time ago -- added
the canonical "hello world" example for Commons SCXML.


Added:
    jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/custom-hello-world.xml
  (with props)
    jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/hello-world.xml
  (with props)
    jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/CustomActionTest.java
  (with props)
    jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/Hello.java
  (with props)
Modified:
    jakarta/commons/sandbox/scxml/trunk/src/main/java/org/apache/commons/scxml/SCXMLHelper.java
    jakarta/commons/sandbox/scxml/trunk/src/main/java/org/apache/commons/scxml/io/SCXMLDigester.java
    jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLDigesterTest.java
    jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLTestHelper.java
    jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/ModelTestSuite.java

Modified: jakarta/commons/sandbox/scxml/trunk/src/main/java/org/apache/commons/scxml/SCXMLHelper.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/main/java/org/apache/commons/scxml/SCXMLHelper.java?rev=381056&r1=381055&r2=381056&view=diff
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/main/java/org/apache/commons/scxml/SCXMLHelper.java
(original)
+++ jakarta/commons/sandbox/scxml/trunk/src/main/java/org/apache/commons/scxml/SCXMLHelper.java
Sat Feb 25 21:15:33 2006
@@ -289,6 +289,50 @@
     }
 
     /**
+     * Whether the first argument is a subtype of the second.
+     *
+     * @param child The candidate subtype
+     * @param parent The supertype
+     * @return true if child is subtype of parent, otherwise false
+     */
+    public static boolean subtypeOf(final Class child, final Class parent) {
+        if (child == null || parent == null) {
+            return false;
+        }
+        for (Class current = child; current != Object.class;
+                current = current.getSuperclass()) {
+            if (current == parent) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Whether the class implements the interface.
+     *
+     * @param clas The candidate class
+     * @param interfayce The interface
+     * @return true if clas implements interfayce, otherwise false
+     */
+    public static boolean implementationOf(final Class clas,
+            final Class interfayce) {
+        if (clas == null || interfayce == null || !interfayce.isInterface()) {
+            return false;
+        }
+        for (Class current = clas; current != Object.class;
+                current = current.getSuperclass()) {
+            Class[] implementedInterfaces = current.getInterfaces();
+            for (int i = 0; i < implementedInterfaces.length; i++) {
+                if (implementedInterfaces[i] == interfayce) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    /**
      * Discourage instantiation since this is a utility class.
      */
     private SCXMLHelper() {

Modified: jakarta/commons/sandbox/scxml/trunk/src/main/java/org/apache/commons/scxml/io/SCXMLDigester.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/main/java/org/apache/commons/scxml/io/SCXMLDigester.java?rev=381056&r1=381055&r2=381056&view=diff
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/main/java/org/apache/commons/scxml/io/SCXMLDigester.java
(original)
+++ jakarta/commons/sandbox/scxml/trunk/src/main/java/org/apache/commons/scxml/io/SCXMLDigester.java
Sat Feb 25 21:15:33 2006
@@ -80,6 +80,14 @@
  */
 public final class SCXMLDigester {
 
+    /**
+     * The SCXML namespace that this Digester is built for. Any document
+     * that is intended to be parsed by this digester <b>must</b>
+     * bind the SCXML elements to this namespace.
+     */
+    public static final String NAMESPACE_SCXML =
+        "http://www.w3.org/2005/07/scxml";
+
     //---------------------- PUBLIC METHODS ----------------------//
     /**
      * <p>API for standalone usage where the SCXML document is a URL.</p>
@@ -250,6 +258,7 @@
         digester.setNamespaceAware(true);
         //Uncomment next line after SCXML DTD is available
         //digester.setValidating(true);
+        digester.setRuleNamespaceURI(NAMESPACE_SCXML);
         digester.setRules(initRules(scxml, pr));
         return digester;
     }
@@ -284,6 +293,47 @@
         }
     }
 
+    /**
+     * <p>Add a custom namespaced action as part of SCXML executable
+     * content.</p>
+     *
+     * @param digester The SCXML digester, <b>must</b> be obtained by
+     *                 calling the
+     *     <code>SCXMLDigester#newInstance(SCXML,PathResolver)</code>
+     *                 method.
+     * @param namespaceURI The namespace URI for this custom action.
+     * @param localName The local name for this custom action.
+     * @param klass The class that will represent this custom action
+     *              in the Commons SCXML object model, <b>must</b> be
+     *              a subtype of
+     *              <code>org.apache.commons.scxml.model.Action</code>
+     */
+    public static void addCustomAction(final Digester digester,
+            final String namespaceURI, final String localName,
+            final Class klass) {
+        if (SCXMLHelper.isStringEmpty(namespaceURI)) {
+            throw new IllegalArgumentException(ERR_CUSTOM_ACTION_NO_NS);
+        }
+        if (namespaceURI.trim().equals(NAMESPACE_SCXML)) {
+            throw new IllegalArgumentException(ERR_CUSTOM_ACTION_RESERVED_NS);
+        }
+        if (SCXMLHelper.isStringEmpty(localName)) {
+            throw new IllegalArgumentException(ERR_CUSTOM_ACTION_NO_NAME);
+        }
+        if (klass == null || !SCXMLHelper.subtypeOf(klass, Action.class)) {
+            throw new IllegalArgumentException(ERR_CUSTOM_ACTION_TYPE);
+        }
+        String xpfLocalName = STR_SLASH + localName.trim();
+        digester.setRuleNamespaceURI(namespaceURI.trim());
+        ExtendedBaseRules scxmlRules = (ExtendedBaseRules) digester.
+            getRules();
+        if (SCXMLHelper.implementationOf(klass, ExternalContent.class)) {
+            addCustomActionRules(xpfLocalName, scxmlRules, klass, true);
+        } else {
+            addCustomActionRules(xpfLocalName, scxmlRules, klass, false);
+        }
+    }
+
     //---------------------- PRIVATE CONSTANTS ----------------------//
     //// Patterns to get the digestion going, prefixed by XP_
     /** Root &lt;scxml&gt; element. */
@@ -313,6 +363,16 @@
     /** &lt;if&gt; element. */
     private static final String XPU_IF = "!*/if";
 
+    // Executables, next three patterns useful when adding custom actions
+    /** &lt;onentry&gt; element. */
+    private static final String XPU_ONEN = "!*/onentry";
+
+    /** &lt;onexit&gt; element. */
+    private static final String XPU_ONEX = "!*/onexit";
+
+    /** &lt;transition&gt; element. */
+    private static final String XPU_TR = "!*/transition";
+
     //// Path Fragments, constants prefixed by XPF_
     // Onentries and Onexits
     /** &lt;onentry&gt; child element. */
@@ -414,6 +474,41 @@
     private static final String ERR_STATE_BAD_DEEP_HIST = "History state"
         + " for deep history is not descendant for \"{0}\"";
 
+    /**
+     * Error message while attempting to define a custom action without
+     * specifying a namespace.
+     */
+    private static final String ERR_CUSTOM_ACTION_NO_NS = "Custom action"
+        + " needs a namespace";
+
+    /**
+     * Error message while attempting to define a custom action in the
+     * SCXML namespace.
+     */
+    private static final String ERR_CUSTOM_ACTION_RESERVED_NS = "Cannot add"
+        + " custom action to SCXML namespace";
+
+    /**
+     * Error message while attempting to define a custom action in the
+     * SCXML namespace.
+     */
+    private static final String ERR_CUSTOM_ACTION_NO_NAME = "Cannot add"
+        + " custom action without a local name";
+
+    /**
+     * Error message while attempting to define a custom action which does
+     * not extend the Commons SCXML Action base class.
+     */
+    private static final String ERR_CUSTOM_ACTION_TYPE = "Custom action"
+        + " must be a subtype of org.apache.commons.scxml.model.Action";
+
+    // String constants
+    /** Slash. */
+    private static final String STR_SLASH = "/";
+
+    /** Prefix for universal digester patterns. */
+    private static final String STR_UNIVERSAL = "!*";
+
     //---------------------- PRIVATE UTILITY METHODS ----------------------//
     /*
      * Private utility functions for configuring digester rule base for SCXML.
@@ -676,6 +771,57 @@
         } catch (ParserConfigurationException pce) {
             log.error("Error parsing <send> element content",
                 pce);
+        }
+    }
+
+    /**
+     * Add Digester rules for a custom action with child nodes (in
+     * external namespaces).
+     *
+     * @param xpfLocalName The local name path fragment (trailing) for
+     *                       the custom action element
+     * @param scxmlRules The rule set to be used for digestion
+     * @param klass The <code>Action</code> class implementing the custom
+     *              action.
+     * @param bodyContent Whether the custom rule has body content
+     *              that should be parsed using
+     *              <code>NodeCreateRule</code>
+     */
+    private static void addCustomActionRules(final String xpfLocalName,
+            final ExtendedBaseRules scxmlRules, final Class klass,
+            final boolean bodyContent) {
+        addCustomActionRulesTuple(XPU_ONEN + xpfLocalName, scxmlRules,
+            klass, bodyContent);
+        addCustomActionRulesTuple(XPU_ONEX + xpfLocalName, scxmlRules,
+            klass, bodyContent);
+        addCustomActionRulesTuple(XPU_TR + xpfLocalName, scxmlRules,
+            klass, bodyContent);
+        addCustomActionRulesTuple(XPU_IF + xpfLocalName, scxmlRules,
+            klass, bodyContent);
+    }
+
+    /**
+     * Add Digester rules for a simple custom action (no body content).
+     *
+     * @param xp The path to the custom action element
+     * @param scxmlRules The rule set to be used for digestion
+     * @param klass The <code>Action</code> class implementing the custom
+     *              action.
+     * @param bodyContent Whether the custom rule has body content
+     *              that should be parsed using
+     *              <code>NodeCreateRule</code>
+     */
+    private static void addCustomActionRulesTuple(final String xp,
+            final ExtendedBaseRules scxmlRules, final Class klass,
+            final boolean bodyContent) {
+        addActionRulesTuple(xp, scxmlRules, klass);
+        if (bodyContent) {
+            try {
+                scxmlRules.add(xp, new ParseExternalContentRule());
+            } catch (ParserConfigurationException pce) {
+                log.error("Error instantiating body content parsing rule for"
+                    + " custom action", pce);
+            }
         }
     }
 

Modified: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLDigesterTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLDigesterTest.java?rev=381056&r1=381055&r2=381056&view=diff
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLDigesterTest.java
(original)
+++ jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLDigesterTest.java
Sat Feb 25 21:15:33 2006
@@ -23,7 +23,10 @@
 import junit.framework.TestSuite;
 import junit.textui.TestRunner;
 
+import org.apache.commons.digester.Digester;
+import org.apache.commons.scxml.io.SCXMLDigester;
 import org.apache.commons.scxml.io.SCXMLSerializer;
+import org.apache.commons.scxml.model.Hello;
 import org.apache.commons.scxml.model.SCXML;
 import org.apache.commons.scxml.model.Send;
 import org.apache.commons.scxml.model.State;
@@ -48,6 +51,7 @@
 
     // Test data
     private URL microwave01, microwave02, transitions01, send01;
+    private Digester digester;
     private SCXML scxml;
     private String scxmlAsString;
 
@@ -63,6 +67,7 @@
             getResource("org/apache/commons/scxml/transitions-01.xml");
         send01 = this.getClass().getClassLoader().
             getResource("org/apache/commons/scxml/send-01.xml");
+        digester = SCXMLDigester.newInstance(null, null);
     }
 
     /**
@@ -72,6 +77,7 @@
         microwave01 = microwave02 = transitions01 = send01 = null;
         scxml = null;
         scxmlAsString = null;
+        digester = null;
     }
 
     /**
@@ -114,6 +120,65 @@
             + " an example.</prompt></foo>";
         assertFalse(scxmlAsString.indexOf(expectedFoo2Serialization) == -1);
         */
+    }
+
+    public void testAddGoodCustomAction01() {
+        try {
+            SCXMLDigester.addCustomAction(digester,
+                "http://my.actions.domain/CUSTOM", "hello", Hello.class);
+        } catch (IllegalArgumentException iae) {
+            fail("Failed to add custom action &quot;Hello&quot;");
+        }
+    }
+
+    public void testAddBadCustomAction01() {
+        try {
+            SCXMLDigester.addCustomAction(digester,
+                null, "hello", Hello.class);
+            fail("Added custom action with illegal namespace");
+        } catch (IllegalArgumentException iae) {
+            // Expected
+        }
+    }
+
+    public void testAddBadCustomAction02() {
+        try {
+            SCXMLDigester.addCustomAction(digester,
+                "  ", "hello", Hello.class);
+            fail("Added custom action with illegal namespace");
+        } catch (IllegalArgumentException iae) {
+            // Expected
+        }
+    }
+
+    public void testAddBadCustomAction03() {
+        try {
+            SCXMLDigester.addCustomAction(digester,
+                "http://my.actions.domain/CUSTOM", "", Hello.class);
+            fail("Added custom action with illegal local name");
+        } catch (IllegalArgumentException iae) {
+            // Expected
+        }
+    }
+
+    public void testAddBadCustomAction04() {
+        try {
+            SCXMLDigester.addCustomAction(digester,
+                "http://my.actions.domain/CUSTOM", "  ", Hello.class);
+            fail("Added custom action with illegal local name");
+        } catch (IllegalArgumentException iae) {
+            // Expected
+        }
+    }
+
+    public void testAddBadCustomAction05() {
+        try {
+            SCXMLDigester.addCustomAction(digester,
+                "http://my.actions.domain/CUSTOM", "foo", this.getClass());
+            fail("Added custom action which is not an Action class subtype");
+        } catch (IllegalArgumentException iae) {
+            // Expected
+        }
     }
 
     private String serialize(final SCXML scxml) {

Modified: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLTestHelper.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLTestHelper.java?rev=381056&r1=381055&r2=381056&view=diff
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLTestHelper.java
(original)
+++ jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/SCXMLTestHelper.java
Sat Feb 25 21:15:33 2006
@@ -69,6 +69,19 @@
         return getExecutor(evaluator, scxml);
     }
 
+    public static SCXMLExecutor getExecutor(SCXML scxml) {
+        return getExecutor(scxml, null);
+    }
+
+    public static SCXMLExecutor getExecutor(SCXML scxml,
+            SCXMLSemantics semantics) {
+        Context context = new JexlContext();
+        Evaluator evaluator = new JexlEvaluator();
+        EventDispatcher ed = new SimpleDispatcher();
+        Tracer trc = new Tracer();
+        return getExecutor(context, evaluator, scxml, ed, trc, semantics);
+    }
+
     public static SCXMLExecutor getExecutor(Evaluator evaluator, SCXML scxml) {
         EventDispatcher ed = new SimpleDispatcher();
         Tracer trc = new Tracer();
@@ -86,6 +99,12 @@
 
     public static SCXMLExecutor getExecutor(Context context,
             Evaluator evaluator, SCXML scxml, EventDispatcher ed, Tracer trc) {
+        return getExecutor(context, evaluator, scxml, ed, trc, null);
+    }
+
+    public static SCXMLExecutor getExecutor(Context context,
+            Evaluator evaluator, SCXML scxml, EventDispatcher ed,
+            Tracer trc, SCXMLSemantics semantics) {
         Assert.assertNotNull(evaluator);
         Assert.assertNotNull(context);
         Assert.assertNotNull(scxml);
@@ -93,7 +112,11 @@
         Assert.assertNotNull(trc);
         SCXMLExecutor exec = null;
         try {
-            exec = new SCXMLExecutor(evaluator, ed, trc);
+            if (semantics == null) {
+                exec = new SCXMLExecutor(evaluator, ed, trc);
+            } else {
+                exec = new SCXMLExecutor(evaluator, ed, trc, semantics);
+            }
             exec.addListener(scxml, trc);
             exec.setRootContext(context);
             exec.setSuperStep(true);

Added: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/custom-hello-world.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/custom-hello-world.xml?rev=381056&view=auto
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/custom-hello-world.xml
(added)
+++ jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/custom-hello-world.xml
Sat Feb 25 21:15:33 2006
@@ -0,0 +1,31 @@
+<?xml version="1.0"?>
+<!--
+   Copyright 2006 The Apache Software Foundation
+
+   Licensed 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.
+-->
+<!-- Used for comparison with hello-world.xml by
+     CustomActionTest.java in model package -->
+<scxml xmlns="http://www.w3.org/2005/07/SCXML"
+       xmlns:my="http://my.custom-actions.domain/CUSTOM"
+       version="1.0"
+       initialstate="custom">
+
+    <state id="custom" final="true">
+        <onentry>
+            <my:hello name="world" />
+        </onentry>
+    </state>
+
+</scxml>
+

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/custom-hello-world.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/custom-hello-world.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/hello-world.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/hello-world.xml?rev=381056&view=auto
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/hello-world.xml
(added)
+++ jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/hello-world.xml
Sat Feb 25 21:15:33 2006
@@ -0,0 +1,30 @@
+<?xml version="1.0"?>
+<!--
+   Copyright 2006 The Apache Software Foundation
+
+   Licensed 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.
+-->
+<!-- Used for comparison with custom-hello-world.xml by
+     CustomActionTest.java in model package -->
+<scxml xmlns="http://www.w3.org/2005/07/SCXML"
+       version="1.0"
+       initialstate="hello">
+
+    <state id="hello" final="true">
+        <onentry>
+            <log expr="'hello world'" />
+        </onentry>
+    </state>
+
+</scxml>
+

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/hello-world.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/hello-world.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/CustomActionTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/CustomActionTest.java?rev=381056&view=auto
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/CustomActionTest.java
(added)
+++ jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/CustomActionTest.java
Sat Feb 25 21:15:33 2006
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.model;
+
+import java.net.URL;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.commons.digester.Digester;
+import org.apache.commons.scxml.SCXMLExecutor;
+import org.apache.commons.scxml.SCXMLTestHelper;
+import org.apache.commons.scxml.io.SCXMLDigester;
+import org.apache.commons.scxml.model.SCXML;
+import org.apache.commons.scxml.model.State;
+
+public class CustomActionTest extends TestCase {
+
+    public CustomActionTest(String testName) {
+        super(testName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(CustomActionTest.class);
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { CustomActionTest.class.getName()};
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    private URL hello01, custom01;
+    private Digester digester;
+    private SCXMLExecutor exec;
+
+    /**
+     * Set up instance variables required by this test case.
+     */
+    public void setUp() {
+        hello01 = this.getClass().getClassLoader().
+            getResource("org/apache/commons/scxml/hello-world.xml");
+        custom01 = this.getClass().getClassLoader().
+            getResource("org/apache/commons/scxml/custom-hello-world.xml");
+    }
+
+    /**
+     * Tear down instance variables required by this test case.
+     */
+    public void tearDown() {
+        hello01 = custom01 = null;
+        digester = null;
+        exec = null;
+    }
+
+    // Hello World example using the SCXML <log> action
+    public void testHelloWorld() {
+        // (1) Get a SCXMLExecutor
+        exec = SCXMLTestHelper.getExecutor(hello01);
+        // (2) Single, final state
+        assertEquals("hello", ((State) exec.getCurrentStatus().getStates().
+                iterator().next()).getId());
+        assertTrue(exec.getCurrentStatus().isFinal());
+    }
+
+    // Hello World example using a custom <hello> action
+    public void testCustomActionHelloWorld() {
+        // (1) Get Digester with "default" rules for parsing SCXML documents
+        digester = SCXMLDigester.newInstance(null, null);
+        // (2) Register the "custom" action(s)
+        SCXMLDigester.addCustomAction(digester,
+            "http://my.custom-actions.domain/CUSTOM", "hello", Hello.class);
+        // (3) Parse the SCXML document containing the custom action(s)
+        SCXML scxml = null;
+        try {
+            scxml = (SCXML) digester.parse(custom01.toString());
+        } catch (Exception e) {
+            fail(e.getMessage());
+        }
+        // (4) Wire up the object model for the SCXMLExecutor
+        SCXMLDigester.updateSCXML(scxml);
+        // (5) Get a SCXMLExecutor
+        exec = SCXMLTestHelper.getExecutor(scxml);
+        // (6) Fire events, proceed as usual
+        assertEquals("custom", ((State) exec.getCurrentStatus().getStates().
+                iterator().next()).getId());
+        assertTrue(exec.getCurrentStatus().isFinal());
+    }
+
+}
+

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/CustomActionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/CustomActionTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/Hello.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/Hello.java?rev=381056&view=auto
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/Hello.java
(added)
+++ jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/Hello.java
Sat Feb 25 21:15:33 2006
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.model;
+
+import java.util.Collection;
+
+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.SCXMLExpressionException;
+import org.apache.commons.scxml.model.Action;
+
+/**
+ * Our custom &quot;hello world&quot; action.
+ */
+public class Hello extends Action {
+
+    /** This is who we say hello to. */
+    private String name;
+
+    /** Public constructor is needed for the I in SCXML IO. */
+    public Hello() {
+        super();
+    }
+
+    /**
+     * Get the name.
+     *
+     * @return Returns the name.
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * Set the name.
+     *
+     * @param name The name to set.
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public void execute(final EventDispatcher evtDispatcher,
+            final ErrorReporter errRep, final SCInstance scInstance,
+            final Log appLog, final Collection derivedEvents)
+    throws ModelException, SCXMLExpressionException {
+        if (appLog.isInfoEnabled()) {
+            appLog.info("Hello " + name);
+        }
+    }
+}
+

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/Hello.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/Hello.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/ModelTestSuite.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/ModelTestSuite.java?rev=381056&r1=381055&r2=381056&view=diff
==============================================================================
--- jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/ModelTestSuite.java
(original)
+++ jakarta/commons/sandbox/scxml/trunk/src/test/java/org/apache/commons/scxml/model/ModelTestSuite.java
Sat Feb 25 21:15:33 2006
@@ -49,6 +49,7 @@
         suite.setName("Commons-SCXML Model Tests");
         suite.addTest(ActionsTest.suite());
         suite.addTest(ActionTest.suite());
+        suite.addTest(CustomActionTest.suite());
         suite.addTest(HistoryTest.suite());
         suite.addTest(PathTest.suite());
         suite.addTest(StateTest.suite());



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message