Author: rahul Date: Tue Jun 26 13:58:10 2007 New Revision: 550948 URL: http://svn.apache.org/viewvc?view=rev&rev=550948 Log: SCXML-48 Broken subclassing for AbstractStateMachine.
Unrelated changes: - Two new constructors to avoid recurring parsing cost - Some cosmetic changes so the class Javadoc renders in a readable manner. Thanks to Michael Heuer <heuermh AT acm DOT org> for the AbstractStateMachine tests (which now pass). Added: jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/AbstractStateMachineTest.java (with props) jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/bar.xml (with props) jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/foo.xml (with props) Modified: jakarta/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml/env/AbstractStateMachine.java jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/EnvTestSuite.java Modified: jakarta/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml/env/AbstractStateMachine.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml/env/AbstractStateMachine.java?view=diff&rev=550948&r1=550947&r2=550948 ============================================================================== --- jakarta/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml/env/AbstractStateMachine.java (original) +++ jakarta/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml/env/AbstractStateMachine.java Tue Jun 26 13:58:10 2007 @@ -39,27 +39,27 @@ import org.xml.sax.SAXException; /** - * This class demonstrates one approach for providing the base + * <p>This class demonstrates one approach for providing the base * functionality needed by classes representing stateful entities, - * whose behaviors are defined via SCXML documents. + * whose behaviors are defined via SCXML documents.</p> * - * SCXML documents (more generically, UML state chart diagrams) can be + * <p>SCXML documents (more generically, UML state chart diagrams) can be * used to define stateful behavior of objects, and Commons SCXML enables * developers to use this model directly into the corresponding code * artifacts. The resulting artifacts tend to be much simpler, embody * a useful separation of concerns and are easier to understand and * maintain. As the size of the modeled entity grows, these benefits - * become more apparent. + * become more apparent.</p> * - * This approach functions by registering an SCXMLListener that gets + * <p>This approach functions by registering an SCXMLListener that gets * notified onentry, and calls the namesake method for each state that - * has been entered. + * has been entered.</p> * - * This class swallows all exceptions only to log them. Developers of + * <p>This class swallows all exceptions only to log them. Developers of * subclasses should think of themselves as "component developers" * catering to other end users, and therefore ensure that the subclasses * are free of <code>ModelException</code>s and the like. Most methods - * are <code>protected</code> for ease of subclassing. + * are <code>protected</code> for ease of subclassing.</p> * */ public abstract class AbstractStateMachine { @@ -67,7 +67,7 @@ /** * The state machine that will drive the instances of this class. */ - private static SCXML stateMachine; + private SCXML stateMachine; /** * The instance specific SCXML engine. @@ -92,7 +92,7 @@ private static final Object[] PARAMETERS = new Object[0]; /** - * Convenience constructor. + * Convenience constructor, object instantiation incurs parsing cost. * * @param scxmlDocument The URL pointing to the SCXML document that * describes the "lifecycle" of the @@ -104,7 +104,7 @@ } /** - * Primary constructor. + * Primary constructor, object instantiation incurs parsing cost. * * @param scxmlDocument The URL pointing to the SCXML document that * describes the "lifecycle" of the @@ -118,20 +118,58 @@ public AbstractStateMachine(final URL scxmlDocument, final Context rootCtx, final Evaluator evaluator) { log = LogFactory.getLog(this.getClass()); - if (stateMachine == null) { - // parse only once per subclass - ErrorHandler errHandler = new SimpleErrorHandler(); - try { - stateMachine = SCXMLDigester.digest(scxmlDocument, - errHandler); - } catch (IOException ioe) { - logError(ioe); - } catch (SAXException sae) { - logError(sae); - } catch (ModelException me) { - logError(me); - } + ErrorHandler errHandler = new SimpleErrorHandler(); + try { + stateMachine = SCXMLDigester.digest(scxmlDocument, + errHandler); + } catch (IOException ioe) { + logError(ioe); + } catch (SAXException sae) { + logError(sae); + } catch (ModelException me) { + logError(me); } + initialize(stateMachine, rootCtx, evaluator); + } + + /** + * Convenience constructor. + * + * @param stateMachine The parsed SCXML instance that + * describes the "lifecycle" of the + * instances of this class. + */ + public AbstractStateMachine(final SCXML stateMachine) { + // default is JEXL + this(stateMachine, new JexlContext(), new JexlEvaluator()); + } + + /** + * Primary constructor. + * + * @param stateMachine The parsed SCXML instance that + * describes the "lifecycle" of the + * instances of this class. + * @param rootCtx The root context for this instance. + * @param evaluator The expression evaluator for this instance. + * + * @see Context + * @see Evaluator + */ + public AbstractStateMachine(final SCXML stateMachine, + final Context rootCtx, final Evaluator evaluator) { + initialize(stateMachine, rootCtx, evaluator); + } + + /** + * Instantiate and initialize the underlying executor instance. + * + * @param stateMachine The state machine + * @param rootCtx The root context + * @param evaluator The expression evaluator + */ + private void initialize(final SCXML stateMachine, + final Context rootCtx, final Evaluator evaluator) { engine = new SCXMLExecutor(evaluator, new SimpleDispatcher(), new SimpleErrorReporter()); engine.setStateMachine(stateMachine); @@ -167,9 +205,10 @@ * Get the SCXML object representing this state machine. * * @return Returns the stateMachine. + * @deprecated Returns null, use getEngine().getStateMachine() instead */ public static SCXML getStateMachine() { - return stateMachine; + return null; } /** Added: jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/AbstractStateMachineTest.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/AbstractStateMachineTest.java?view=auto&rev=550948 ============================================================================== --- jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/AbstractStateMachineTest.java (added) +++ jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/AbstractStateMachineTest.java Tue Jun 26 13:58:10 2007 @@ -0,0 +1,92 @@ +/* + * 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.env; + +import java.net.URL; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import junit.textui.TestRunner; + +/** + * Unit tests [EMAIL PROTECTED] org.apache.commons.scxml.env.AbstractStateMachine}. + */ +public class AbstractStateMachineTest extends TestCase { + + /** + * Construct a new instance of AbstractStateMachineTest with the specified name + */ + public AbstractStateMachineTest(String name) { + super(name); + } + + // Test data + private boolean fooCalled; + private boolean barCalled; + + /** + * Set up instance variables required by this test case. + */ + public void setUp() { + fooCalled = false; + barCalled = false; + } + + public void testMoreThanOneScxmlDocument() throws Exception { + URL fooScxmlDocument = getClass().getResource("foo.xml"); + URL barScxmlDocument = getClass().getResource("bar.xml"); + + new Foo(fooScxmlDocument); + new Bar(barScxmlDocument); + + assertTrue(fooCalled); + assertTrue(barCalled); + } + + private class Foo extends AbstractStateMachine { + + public Foo(final URL scxmlDocument) { + super(scxmlDocument); + } + + public void foo() { + fooCalled = true; + } + } + + private class Bar extends AbstractStateMachine { + + public Bar(final URL scxmlDocument) { + super(scxmlDocument); + } + + public void bar() { + barCalled = true; + } + } + + public static Test suite() { + TestSuite suite = new TestSuite(AbstractStateMachineTest.class); + suite.setName("AbstractStateMachine Tests"); + return suite; + } + + public static void main(String args[]) { + TestRunner.run(suite()); + } +} Propchange: jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/AbstractStateMachineTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/AbstractStateMachineTest.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Modified: jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/EnvTestSuite.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/EnvTestSuite.java?view=diff&rev=550948&r1=550947&r2=550948 ============================================================================== --- jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/EnvTestSuite.java (original) +++ jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/EnvTestSuite.java Tue Jun 26 13:58:10 2007 @@ -48,6 +48,7 @@ public static Test suite() { TestSuite suite = new TestSuite(); suite.setName("Commons-SCXML Environments Tests"); + suite.addTest(AbstractStateMachineTest.suite()); suite.addTest(AbstractSCXMLListenerTest.suite()); suite.addTest(LogUtilsTest.suite()); suite.addTest(SimpleContextTest.suite()); Added: jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/bar.xml URL: http://svn.apache.org/viewvc/jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/bar.xml?view=auto&rev=550948 ============================================================================== --- jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/bar.xml (added) +++ jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/bar.xml Tue Jun 26 13:58:10 2007 @@ -0,0 +1,25 @@ +<?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. +--> +<scxml xmlns="http://www.w3.org/2005/07/scxml" + version="1.0" + initialstate="bar"> + + <state id="bar"/> + +</scxml> + Propchange: jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/bar.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/bar.xml ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Added: jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/foo.xml URL: http://svn.apache.org/viewvc/jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/foo.xml?view=auto&rev=550948 ============================================================================== --- jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/foo.xml (added) +++ jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/foo.xml Tue Jun 26 13:58:10 2007 @@ -0,0 +1,25 @@ +<?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. +--> +<scxml xmlns="http://www.w3.org/2005/07/scxml" + version="1.0" + initialstate="foo"> + + <state id="foo"/> + +</scxml> + Propchange: jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/foo.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/env/foo.xml ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]