001    package nl.cwi.sen1.visbase.rstorecontainer;
002    
003    import nl.cwi.sen1.tunit.TUnitTestCase;
004    import nl.cwi.sen1.tunit.ToolComValidator;
005    import aterm.ATerm;
006    import aterm.ATermList;
007    import aterm.pure.PureFactory;
008    
009    
010    /**
011     * Unit test for testing the RStore Container Interface TB script.
012     *
013     * @author Arend van Beelen
014     * @date 06-03-2007
015     */
016    public class RCITBTest extends TUnitTestCase {
017    
018        /**
019         * The function that contains the actual test case.
020         */
021        public void testMessages() {
022            RStoreContainerTool rStoreContainerTool = new RStoreContainerTool(this, "rStoreContainer", true);
023            RCITool rciTool = new RCITool(this, "rciTest", true);
024            
025            Thread rStoreToolExecutor = new Thread(rStoreContainerTool);
026            rStoreToolExecutor.setDaemon(true);
027            rStoreToolExecutor.start();
028            Thread rciToolExecutor = new Thread(rciTool);
029            rciToolExecutor.setDaemon(true);
030            rciToolExecutor.start();
031            
032            toolbus.waitTillShutdown();
033            
034            if(hasFailed()) fail();
035            
036            System.out.println("Test Passed");
037        }
038        
039        private static class RStoreContainerTool extends ToolComValidator{
040            private final TUnitTestCase testCase;
041            
042            public RStoreContainerTool(TUnitTestCase testCase, String name, boolean verbose){
043                    super(testCase, name, verbose);
044                    
045                    this.testCase = testCase;
046            }
047            
048            public void run(){
049                    try{
050                            testCase.connectToolComValidator(this);
051                            
052                            PureFactory factory = getFactory();
053                            
054                            int rStoreId = 10;
055                    String rStoreFile = "awesome.rstore";
056                    ATerm rcLoadRStore = factory.make("rc-load-rstore(<str>)", rStoreFile);
057                    ATerm rcRStoreLoaded = factory.make("rc-rstore-loaded(<str>, <int>)", rStoreFile, new Integer(rStoreId));
058    
059                    registerForEval(rcLoadRStore, rcRStoreLoaded);
060                    expectAction();
061                    
062                    ATermList emptyList = factory.makeList();
063                    ATerm rcGetRStoreFacts = factory.make("rc-get-rstore-facts(<int>)", new Integer(rStoreId));
064                    ATerm rcRStoreFacts = factory.make("rc-rstore-facts(<list>)", emptyList);
065    
066                    registerForEval(rcGetRStoreFacts, rcRStoreFacts);
067                    expectAction();
068    
069                    int factId = 16;
070                    ATerm rcGetFactData = factory.make("rc-get-fact-data(<int>, <int>)", new Integer(rStoreId), new Integer(factId));
071                    ATerm rcFactData = factory.make("rc-fact-data(<term>)", emptyList);
072                    
073                    registerForEval(rcGetFactData, rcFactData);
074                    expectAction();
075                    }catch(Exception ex){
076                            ex.printStackTrace();
077                    }finally{
078                            disconnect();
079                    }
080            }
081        }
082        
083        private static class RCITool extends ToolComValidator{
084            private final TUnitTestCase testCase;
085            
086            public RCITool(TUnitTestCase testCase, String name, boolean verbose){
087                    super(testCase, name, verbose);
088                    
089                    this.testCase = testCase;
090            }
091            
092            public void run(){
093                    try{
094                            testCase.connectToolComValidator(this);
095                            
096                            PureFactory factory = getFactory();
097                            
098                    String rStoreFile = "awesome.rstore";
099                    ATerm rcLoadRStore = factory.make("rc-load-rstore(<str>)", rStoreFile);
100                            
101                    sendEvent(rcLoadRStore);
102    
103                    int rStoreId = 10;
104                    ATerm rcRStoreLoaded = factory.make("rc-rstore-loaded(<str>, <int>)", rStoreFile, new Integer(rStoreId));
105                    
106                    registerForDo(rcRStoreLoaded);
107                    expectAction();
108    
109                    ATerm rcGetRStoreFacts = factory.make("rc-get-rstore-facts(<int>)", new Integer(rStoreId));
110                    
111                    sendEvent(rcGetRStoreFacts);
112    
113                    ATermList emptyList = factory.makeList();
114                    ATerm rcGetRStoreFactsReply = factory.make("rc-get-rstore-facts(<int>, <list>)", new Integer(rStoreId), emptyList);
115                    
116                    registerForDo(rcGetRStoreFactsReply);
117                    expectAction();
118                    
119                    int factId = 16;
120                    ATerm rcGetFactData = factory.make("rc-get-fact-data(<int>, <int>)", new Integer(rStoreId), new Integer(factId));
121                    
122                    sendEvent(rcGetFactData);
123    
124                    ATerm rcGetFactDataReply = factory.make("rc-get-fact-data(<int>, <int>, <term>)", new Integer(rStoreId), new Integer(factId), emptyList);
125                    
126                    registerForDo(rcGetFactDataReply);
127                    expectAction();
128    
129                    waitForCompletion();   
130                    }catch(Exception ex){
131                            ex.printStackTrace();
132                    }finally{
133                            disconnect();
134                    }
135            }
136        }
137    
138        /**
139         * Initialize the RCI process.
140         */
141        protected void setUp(){
142            try{
143                startToolbus("./tbscript/", "./tbscript/init.tb");
144            }catch(Exception ex){
145                    ex.printStackTrace();
146                stopToolbus();
147                fail(ex.toString());
148            }
149        }
150    
151        /**
152         * Clean up the allocated resources at the end of the test.
153         */
154        protected void tearDown(){
155            stopToolbus();
156        }
157    }