001    package nl.cwi.sen1.tunit.example;
002    import nl.cwi.sen1.tunit.TUnitTestCase;
003    import nl.cwi.sen1.tunit.ToolComValidator;
004    import aterm.ATerm;
005    import aterm.pure.PureFactory;
006    
007    /**
008     * An example test case to demonstrate the toolbus testing framework.
009     *
010     * @author Bas Basten & Anton Lycklama a Nijeholt
011     */
012    public class ExampleTestCase extends TUnitTestCase{
013            
014            public ExampleTestCase(){
015                    super();
016            }
017            
018        /**
019         * The function that contains the actual test case.
020         */
021        public void testExample(){
022            defaultTestToolExecutorFactory.addComValidatorForExecutableTool("executedTool", ExecutedTestTool.class);
023            
024            Tool1 tool1 = new Tool1(this, "tool1", true);
025            Tool2 tool2 = new Tool2(this, "tool2", true);
026            
027            Thread tool1Executor = new Thread(tool1);
028            tool1Executor.setDaemon(true);
029            tool1Executor.start();
030            Thread tool2Executor = new Thread(tool2);
031            tool2Executor.setDaemon(true);
032            tool2Executor.start();
033            
034            toolbus.waitTillShutdown();
035            
036            if(hasFailed()) fail();
037            
038            System.out.println("Test Passed");
039        }
040        
041        protected void setUp(){
042            try{
043                    startToolbus("./src/nl/cwi/sen1/tunit/example/", "./src/nl/cwi/sen1/tunit/example/init.tb");
044            }catch(Exception ex){
045                    ex.printStackTrace();
046                    stopToolbus();
047                    fail(ex.toString());
048            }
049        }
050        
051        protected void tearDown(){
052            stopToolbus();
053        }
054        
055        public static class Tool1 extends ToolComValidator{
056            private final TUnitTestCase testCase;
057            
058            public Tool1(TUnitTestCase testCase, String name, boolean verbose){
059                    super(testCase, name, verbose);
060                    
061                    this.testCase = testCase;
062            }
063            
064            public void run(){
065                    try{
066                            testCase.connectToolComValidator(this);
067                            
068                            PureFactory factory = getFactory();
069                            ATerm sendValue = factory.make("sendValue(<int>)", new Integer(10));
070                            ATerm showValue = factory.make("showValue(<int>)", new Integer(10));
071                            
072                            sendEvent(sendValue);
073                            
074                            registerForDo(showValue);
075                            expectAction();
076                            
077                            waitForCompletion();
078                    }catch(Exception ex){
079                            ex.printStackTrace();
080                    }finally{
081                            disconnect();
082                    }
083            }
084        }
085        
086        public static class Tool2 extends ToolComValidator{
087            private final TUnitTestCase testCase;
088            
089            public Tool2(TUnitTestCase testCase, String name, boolean verbose){
090                    super(testCase, name, verbose);
091                    
092                    this.testCase = testCase;
093            }
094            
095            public void run(){
096                    try{
097                            testCase.connectToolComValidator(this);
098                            
099                            PureFactory factory = getFactory();
100                            ATerm sendValue = factory.make("sendValue(<int>)", new Integer(10));
101                            ATerm showValue = factory.make("showValue(<int>)", new Integer(10));
102                            
103                            registerForEval(showValue, showValue);
104                            expectAction();
105                            
106                            sendEvent(sendValue);
107                            
108                            waitForCompletion();
109                    }catch(Exception ex){
110                            ex.printStackTrace();
111                    }finally{
112                            disconnect();
113                    }
114            }
115        }
116        
117        public static class ExecutedTestTool extends ToolComValidator{
118            
119            public ExecutedTestTool(TUnitTestCase testCase, String name, int id, boolean verbose){
120                    super(testCase, name, id, verbose);
121            }
122            
123            public void run(){
124                    try{
125                            registerForDo(getFactory().make("\"executed\""));
126                        expectAction();
127                        
128                        waitForCompletion();
129                    }catch(Exception ex){
130                            ex.printStackTrace();
131                    }finally{
132                            disconnect();
133                    }
134            }
135        }
136    }