http://git-wip-us.apache.org/repos/asf/river-container/blob/3e29cd2d/river-hsm/src/test/java/org/apache/river/container/hsm/ReturnTypeTest.java ---------------------------------------------------------------------- diff --git a/river-hsm/src/test/java/org/apache/river/container/hsm/ReturnTypeTest.java b/river-hsm/src/test/java/org/apache/river/container/hsm/ReturnTypeTest.java new file mode 100644 index 0000000..82b29f2 --- /dev/null +++ b/river-hsm/src/test/java/org/apache/river/container/hsm/ReturnTypeTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2001-2005 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.river.container.hsm; + +import java.lang.reflect.Method; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; +/** + * + * @author trasukg + */ +public class ReturnTypeTest { + + public ReturnTypeTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Expectation is that if a method is declared to return 'void', then the + * 'Method.getReturnType()' value should be null; + */ + @Test + public void testVoidReturnType() throws Exception { + Method m=this.getClass().getMethod("testVoidReturnType", new Class[0]); + assertEquals("return type wasn't void", void.class, m.getReturnType()); + + } +}
http://git-wip-us.apache.org/repos/asf/river-container/blob/3e29cd2d/river-hsm/src/test/java/org/apache/river/container/hsm/StateMachineCompilerTest.java ---------------------------------------------------------------------- diff --git a/river-hsm/src/test/java/org/apache/river/container/hsm/StateMachineCompilerTest.java b/river-hsm/src/test/java/org/apache/river/container/hsm/StateMachineCompilerTest.java new file mode 100644 index 0000000..f6cbc0b --- /dev/null +++ b/river-hsm/src/test/java/org/apache/river/container/hsm/StateMachineCompilerTest.java @@ -0,0 +1,146 @@ +/* + * 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.river.container.hsm; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.List; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * Test the State Machine Compiler + * + */ +public class StateMachineCompilerTest { + + public StateMachineCompilerTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + StateMachineCompiler compiler = new StateMachineCompiler(); + + @Test + public void testCompileReturnsMetaState() throws Exception { + MetaState stateMachine = compiler.compile(TestSM.class); + assertTrue(stateMachine + " isn't a MetaState", stateMachine instanceof MetaState); + } + + @Test + public void testSampleActiveStates() throws Exception { + MetaState stateMachine = compiler.compile(TestSM.class); + List<Class> activeStates = stateMachine.getActiveStates(); + checkContains(activeStates, TestSM.class); + checkContains(activeStates, TestSM.A.class); + checkContains(activeStates, TestSM.A.A1.class); + } + + /** + * MetaState for TestSM should have event methods for sayHello and + * nullTransition, but nothing else. + * + * @throws Exception + */ + @Test + public void testEventMethods() throws Exception { + MetaState stateMachine = compiler.compile(TestSM.class); + Collection<Method> methods = stateMachine.eventMethods.keySet(); + Method sayHello = TestSMInterface.class.getMethod("sayHello"); + assertNotNull("Didn't find sayHello() method in interface", sayHello); + checkContains(methods, sayHello); + } + + /** + * A method annotated with + * + * @Guard should be reflected by a guarded transition operation. + * @throws Exception + */ + @Test + public void testGuardMethod() throws Exception { + MetaState rootState = compiler.compile(TestSM.class); + assertEquals("Number of guard methods on root metastate", 1, rootState.guardMethods.size()); + + } + + private void checkContains(Collection<?> collection, Object requiredObject) { + assertTrue(collection + " doesn't include " + requiredObject, collection.contains(requiredObject)); + } + + /** + * A method annotated with + * + * @Entry should be reflected by an invoke operation in the metastate. + * @throws Exception + */ + @Test + public void testEntryMethod() throws Exception { + MetaState rootState = compiler.compile(TestSM.class); + MetaState metaStateA = findMetaState(rootState, TestSM.A.class); + assertEquals("Count of onEntry methods for A", 1, metaStateA.entryMethods.size()); + } + + /** + * A method annotated with + * + * @Entry should be reflected by an invoke operation in the metastate. + * @throws Exception + */ + @Test + public void testExitMethod() throws Exception { + MetaState rootState = compiler.compile(TestSM.class); + MetaState metaStateA = findMetaState(rootState, TestSM.A.class); + assertEquals("Count of onExit methods for A", 1, metaStateA.exitMethods.size()); + } + + MetaState findMetaState(MetaState metaState, Class stateClass) { + for (SubstateInfo ssi : metaState.substates) { + for (MetaState ms : ssi.getPossibleMetaStates()) { + if (ms.stateClass == stateClass) { + return ms; + } + } + } + return null; + } + + @Test + public void testStructure() throws Exception { + MetaState rootState = compiler.compile(TestSM.class); + String expectedStructure = "TestSM(state(A(state(A1 ) B(state(B1 B2 B3 ) ) "; + String actualStructure = rootState.getStateStructure(); + } +} http://git-wip-us.apache.org/repos/asf/river-container/blob/3e29cd2d/river-hsm/src/test/java/org/apache/river/container/hsm/TestSM.java ---------------------------------------------------------------------- diff --git a/river-hsm/src/test/java/org/apache/river/container/hsm/TestSM.java b/river-hsm/src/test/java/org/apache/river/container/hsm/TestSM.java new file mode 100644 index 0000000..2bfeada --- /dev/null +++ b/river-hsm/src/test/java/org/apache/river/container/hsm/TestSM.java @@ -0,0 +1,150 @@ +/* + * 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.river.container.hsm; + +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author trasukg + */ +@RootState({TestSMInterface.class, TestSMSecondInterface.class}) +public class TestSM { + + @State({A.class, B.class, C.class}) + @Initial(A.class) + @Retained + public Object state; + + /* Really shouldn't need to be public - we'll fix shortly. */ + @Controller + public StateMachineInfo controller; + int nullTransitionEntryCount = 0; + int aEntryCount = 0, aExitCount = 0; + + public Object returnNull() { + return null; + } + + public List<Class> getActiveStates() { + try { + return controller.getActiveStates(); + } catch (IllegalArgumentException ex) { + Logger.getLogger(TestSM.class.getName()).log(Level.SEVERE, null, ex); + throw new RuntimeException(ex); + } + } + + @Transition(A.class) + public void gotoA() { + //controller.transition(A.class); + } + + @Transition(B.class) + public void gotoB() { + //controller.transition(B.class); + } + + @Transition(C.class) + public void doSecondInterfaceAction() { } + + public int getAEntryCount() { + return aEntryCount; + } + + public int getAExitCount() { + return aExitCount; + } + + public int getNullTransitionEntryCount() { + return nullTransitionEntryCount; + } + + public String sayConstantHello() { + return "Hello"; + } + + @Guard(A.class) + public boolean beFalse() { + return false; + } + + public class A { + + @State({A1.class}) + @Initial(A1.class) + public Object state; + + @Transition(B.class) + public String sayHello() { + //controller.transition(B.class); + return "Hello"; + } + + @Transition(A.class) + public void nullTransition() { + //controller.transition(A.class); + } + + @OnEntry + public void onEntry() { + aEntryCount++; + nullTransitionEntryCount++; + } + + @OnExit + public void onExit() { + aExitCount++; + } + + public class A1 {} + } + + public class B { + + @State({B1.class, B2.class, B3.class}) + @Initial(B1.class) + Object state; + + public String sayHello() { + return "There"; + } + } + + public class B1 { + + @Transition(B2.class) + public void moveSubstateOfB() { + + } + } + + public class B2 { + } + + public class B3 { + } + + public class C { + public String sayHello() { + return "HelloFromC"; + } + } +} http://git-wip-us.apache.org/repos/asf/river-container/blob/3e29cd2d/river-hsm/src/test/java/org/apache/river/container/hsm/TestSMInterface.java ---------------------------------------------------------------------- diff --git a/river-hsm/src/test/java/org/apache/river/container/hsm/TestSMInterface.java b/river-hsm/src/test/java/org/apache/river/container/hsm/TestSMInterface.java new file mode 100644 index 0000000..639a74e --- /dev/null +++ b/river-hsm/src/test/java/org/apache/river/container/hsm/TestSMInterface.java @@ -0,0 +1,50 @@ +/* + * 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.river.container.hsm; + +import java.util.List; + +/** + * + * @author trasukg + */ +public interface TestSMInterface { + + public String sayHello(); + + public String sayConstantHello(); + + public Object returnNull(); + + public int getNullTransitionEntryCount(); + + public void nullTransition(); + + public int getAEntryCount(); + + public int getAExitCount(); + + public void moveSubstateOfB(); + + public void gotoA(); + public void gotoB(); + + List<Class> getActiveStates(); + + public void unimplementedMethod(); +} http://git-wip-us.apache.org/repos/asf/river-container/blob/3e29cd2d/river-hsm/src/test/java/org/apache/river/container/hsm/TestSMSecondInterface.java ---------------------------------------------------------------------- diff --git a/river-hsm/src/test/java/org/apache/river/container/hsm/TestSMSecondInterface.java b/river-hsm/src/test/java/org/apache/river/container/hsm/TestSMSecondInterface.java new file mode 100644 index 0000000..8f82ae1 --- /dev/null +++ b/river-hsm/src/test/java/org/apache/river/container/hsm/TestSMSecondInterface.java @@ -0,0 +1,29 @@ +/* + * 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.river.container.hsm; + +import java.util.List; + +/** + * + * @author trasukg + */ +public interface TestSMSecondInterface { + + public void doSecondInterfaceAction(); +}
