[ 
https://issues.apache.org/jira/browse/SCXML-47?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12507958
 ] 

Michael Heuer commented on SCXML-47:
------------------------------------

I have discovered a problem with StateMachineSupport as designed, due to its 
static reference to stateMachine.

The following test case will fail:

    public void testMoreThanOneScxmlDocument() throws Exception {
        URL fooScxmlDocument = getClass().getResource("foo.xml");
        URL barScxmlDocument = getClass().getResource("bar.xml");

        Foo foo = new Foo(fooScxmlDocument);
        Bar bar = new Bar(barScxmlDocument);

        assertTrue(fooCalled);
        // bar's initialstate "bar" never called, since bar's 
StateMachineSupport has
        //    static reference to stateMachine for foo.xml
        assertTrue(barCalled);
    }

    private class Foo {

        private StateMachineSupport delegate;

        public Foo(final URL scxmlDocument) {
            delegate = new StateMachineSupport(this, scxmlDocument);
        }

        public void foo() {
            fooCalled = true;
        }
    }

    private class Bar {

        private StateMachineSupport delegate;

        public Bar(final URL scxmlDocument) {
            delegate = new StateMachineSupport(this, scxmlDocument);
        }

        public void bar() {
            barCalled = true;
        }
    }

with simple SCXML files

foo.xml:
<scxml xmlns="http://www.w3.org/2005/07/scxml"; version="1.0" initialstate="foo">
  <state id="foo"/>
</scxml>

bar.xml:
<scxml xmlns="http://www.w3.org/2005/07/scxml"; version="1.0" initialstate="bar">
  <state id="bar"/>
</scxml>

java.lang.NoSuchMethodException: 
org.apache.commons.scxml.env.StateMachineSupportTest$Bar.foo()
        at java.lang.Class.getDeclaredMethod(Class.java:1937)
        at 
org.apache.commons.scxml.env.StateMachineSupport.invoke(StateMachineSupport.java:249)


I believe the way to make this work is to have StateMachineSupport accept a 
reference to an instance of SCXML in its constructor or otherwise and to not 
reuse StateMachineSupport in AbstractStateMachine.

Sorry for not providing better unit tests in the first place.

> Provide a state machine support class to allow for delegation.
> --------------------------------------------------------------
>
>                 Key: SCXML-47
>                 URL: https://issues.apache.org/jira/browse/SCXML-47
>             Project: Commons SCXML
>          Issue Type: Improvement
>    Affects Versions: 0.6
>            Reporter: Michael Heuer
>            Priority: Minor
>             Fix For: 0.7
>
>         Attachments: additional-tests.tar.gz, state-machine-support-src.tar.gz
>
>
> This is not completely thought out yet, but if folks like the idea I might 
> persue it further.
> I would like to use AbstractStateMachine but cannot extend from it:
> class B extends A /*, AbstractStateMachine */ {
>   // copy source from AbstractStateMachine here
> }
> I propose here a StateMachineSupport class that provides for use by 
> subclassing and for use by delegation.  The constructors are a little messy, 
> but in the end I have
> public class StateMachineSupport {
>   // use by subclassing
>   protected StateMachineSupport(...) {
>   }
>   // use by delegation
>   public StateMachineSupport(Object delegator, ...) {
>   }
>   // ... methods from AbstractStateMachine
> }
> public abstract class AbstractStateMachine extends StateMachineSupport {
>   protected AbstractStateMachine(...) {
>     super(...);
>   }
> }
> // use by subclassing
> class ConcreteStateMachine extends AbstractStateMachine {
>   ConcreteStateMachine() {
>     super(..."stopwatch.xml");
>   }
>   public void reset() { ... }
>   public void running() { ... }
>   public void paused() { ... }
>   public void stopped() { ... }
> }
> // use by delegation
> class DelegatingStateMachine extends SomethingElse {
>   StateMachineSupport delegate;
>   DelegatingStateMachine() {
>     delegate = new StateMachineSupport(this, ..."stopwatch.xml");
>   }
>   public void reset() { ... }
>   public void running() { ... }
>   public void paused() { ... }
>   public void stopped() { ... }
> }
> StateMachineSupport.java, AbstractStateMachine2.java, 
> StateMachineSupportTest.java, and AbstractStateMachine2Test.java attached.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to