|
| 1 | +package integration; |
| 2 | +import org.junit.Before; |
| 3 | +import org.junit.BeforeClass; |
| 4 | +import org.junit.Test; |
| 5 | +import org.xml.sax.SAXException; |
| 6 | +import stateMachine.AbstractStateMachine; |
| 7 | +import stateMachine.GStateMachine; |
| 8 | +import stateMachine.SCXMLToJava; |
| 9 | +import stateMachine.State; |
| 10 | + |
| 11 | +import javax.tools.JavaCompiler; |
| 12 | +import javax.tools.ToolProvider; |
| 13 | +import javax.xml.parsers.ParserConfigurationException; |
| 14 | +import java.io.File; |
| 15 | +import java.io.IOException; |
| 16 | +import java.net.URL; |
| 17 | +import java.net.URLClassLoader; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | + |
| 23 | +/** |
| 24 | + * Created by Rami on 08/04/2017. |
| 25 | + */ |
| 26 | +public class SimpleStateMachineTest { |
| 27 | + |
| 28 | + static String ressourceDir = "src\\main\\resources\\"; |
| 29 | + static String integrationTestDir = "integrationTests\\"; |
| 30 | + static AbstractStateMachine stateMachine; |
| 31 | + |
| 32 | + @BeforeClass |
| 33 | + public static void prepareEnv(){ |
| 34 | + File scxml = new File(ressourceDir + integrationTestDir +"test1.scxml"); |
| 35 | + try { |
| 36 | + SCXMLToJava builder = new SCXMLToJava(scxml); |
| 37 | + builder.generateJavaCode(); |
| 38 | + File javaCode = builder.getJavaCodeFile(); |
| 39 | + File root = javaCode.getAbsoluteFile().getParentFile(); |
| 40 | + System.out.println(root); |
| 41 | + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
| 42 | + compiler.run(null, System.out, System.err, javaCode.getPath()); |
| 43 | + URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{root.toURI().toURL()}); |
| 44 | + Class<?> cls = Class.forName("GStateMachine", true, classLoader); |
| 45 | + stateMachine = (AbstractStateMachine) cls.newInstance(); |
| 46 | + } catch (ParserConfigurationException e) { |
| 47 | + e.printStackTrace(); |
| 48 | + } catch (IOException e) { |
| 49 | + e.printStackTrace(); |
| 50 | + } catch (SAXException e) { |
| 51 | + e.printStackTrace(); |
| 52 | + } catch (IllegalAccessException e) { |
| 53 | + e.printStackTrace(); |
| 54 | + } catch (InstantiationException e) { |
| 55 | + e.printStackTrace(); |
| 56 | + } catch (ClassNotFoundException e) { |
| 57 | + e.printStackTrace(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testInitState(){ |
| 64 | + //Test if the proper state has been set to initial (State1 without prior indication) |
| 65 | + assertEquals(this.stateMachine.getInitState().getId(), "State1"); |
| 66 | + |
| 67 | + //Test if initial state is current state if we do not start the machine |
| 68 | + assertEquals(this.stateMachine.getInitState(), this.stateMachine.getCurrentState()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testNbOfStates(){ |
| 73 | + assertEquals(this.stateMachine.getStateList().size(), 2); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testStatesNames(){ |
| 78 | + List<State> states = this.stateMachine.getStateList(); |
| 79 | + List<State> expected = new ArrayList<State>(); |
| 80 | + expected.add(new State("State1")); |
| 81 | + expected.add(new State("State2")); |
| 82 | + for(int i = 0; i < states.size(); i++){ |
| 83 | + assertEquals(states.get(i).getId(), expected.get(i).getId()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testNBOfTransitions(){ |
| 89 | + List<State> states = this.stateMachine.getStateList(); |
| 90 | + for(State state: states){ |
| 91 | + assertEquals(state.getTransitions().size(), 1); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testTransition(){ |
| 97 | + List<State> states = this.stateMachine.getStateList(); |
| 98 | + System.out.println(states.get(0).getTransitions().get(0)); |
| 99 | + assertEquals(states.get(0).getTransitions().get(0).from(), states.get(0)); |
| 100 | + assertEquals(states.get(0).getTransitions().get(0).to(), states.get(1)); |
| 101 | + |
| 102 | + assertEquals(states.get(1).getTransitions().get(0).from(), states.get(1)); |
| 103 | + assertEquals(states.get(1).getTransitions().get(0).to(), states.get(0)); |
| 104 | + |
| 105 | + } |
| 106 | +} |
0 commit comments