001    package nl.cwi.sen1.tunit;
002    
003    import java.net.InetAddress;
004    
005    import toolbus.ToolBus;
006    import toolbus.adapter.AbstractTool;
007    import aterm.ATerm;
008    
009    public abstract class ToolComValidator extends AbstractTool implements Runnable{
010            private volatile TestToolBridge testToolBridge;
011            private final TUnitTestCase testCase;
012            
013            private final String name;
014            private final int id;
015            
016            private final boolean verbose;
017            
018            public ToolComValidator(TUnitTestCase testCase, String name, boolean verbose){
019                    super();
020                    
021                    this.testCase = testCase;
022                    
023                    this.name = name;
024                    this.id = -1;
025                    
026                    this.verbose = verbose;
027            }
028            
029            public ToolComValidator(TUnitTestCase testCase, String name, int id, boolean verbose){
030                    super();
031                    
032                    this.testCase = testCase;
033                    
034                    this.name = name;
035                    this.id = id;
036                    
037                    this.verbose = verbose;
038            }
039            
040            public void connect(ToolBus toolbus) throws Exception{
041                    connectDirectly(toolbus, Thread.currentThread().getContextClassLoader(), name, id);
042            }
043            
044            /**
045             * @see toolbus.adapter.AbstractTool#connect(String[])
046             */
047            public void connect(String[] args) throws Exception{
048                    String toolName = null;
049                    int toolID = -1;
050    
051                    InetAddress host = null;
052                    int port = -1;
053    
054                    for(int i = 0; i < args.length; i++){
055                            String arg = args[i];
056                            if(arg.equals("-TB_TOOL_NAME")){
057                                    toolName = args[++i];
058                            }else if(arg.equals("-TB_TOOL_ID")){
059                                    toolID = Integer.parseInt(args[++i]);
060                            }else if(arg.equals("-TB_HOST")){
061                                    host = InetAddress.getByName(args[++i]);
062                            }else if(arg.equals("-TB_PORT")){
063                                    port = Integer.parseInt(args[++i]);
064                            }
065                    }
066    
067                    if(toolName == null) throw new RuntimeException("Missing tool identification.");
068    
069                    testToolBridge = new TestToolBridge(testCase, termFactory, this, toolName, toolID, host, port, verbose);
070                    toolBridge = testToolBridge;
071                    toolBridge.run();
072            }
073            
074            public void connectDirectly(ToolBus toolbus, ClassLoader toolClassLoader, String toolName, int toolID) throws Exception{
075                    if(toolName == null) throw new RuntimeException("Missing tool identification.");
076    
077                    testToolBridge = new TestToolBridge(testCase, termFactory, this, toolName, toolID, toolClassLoader, toolbus, verbose);
078                    toolBridge = testToolBridge;
079                    toolBridge.run();
080            }
081            
082            public void sendEvent(ATerm event){
083                    sendEvent(event, TestToolBridge.DEFAULTTIMEOUT);
084            }
085            
086            public void sendEvent(ATerm event, long timeout){
087                    testToolBridge.sendEvent(event, timeout);
088            }
089            
090            public ATerm sendRequest(ATerm request, ATerm expectedResponse){
091                    return sendRequest(request, expectedResponse, TestToolBridge.DEFAULTTIMEOUT);
092            }
093            
094            public ATerm sendRequest(ATerm request, ATerm expectedResponse, long timeout){
095                    return testToolBridge.sendRequest(request, expectedResponse, timeout);
096            }
097            
098            public void registerForDo(ATerm action){
099                    testToolBridge.registerForDo(action);
100            }
101            
102            public void registerForEval(ATerm action, ATerm expectedResult){
103                    testToolBridge.registerForEval(action, expectedResult);
104            }
105            
106            public void expectAction(){
107                    expectAction(TestToolBridge.DEFAULTTIMEOUT);
108            }
109            
110            public void expectAction(long timeout){
111                    testToolBridge.expectAction(timeout);
112            }
113            
114            public void waitForCompletion(){
115                    testToolBridge.waitForCompletion(TestToolBridge.DEFAULTTIMEOUT);
116            }
117            
118            public void waitForCompletion(long timeout){
119                    testToolBridge.waitForCompletion(timeout);
120            }
121            
122            public void disconnect(){
123                    disconnect(termFactory.makeList());
124            }
125            
126            public void receiveAckEvent(ATerm aTerm){
127                    // Ignore
128            }
129            
130            public void receiveTerminate(ATerm aTerm){
131                    // Ignore
132            }
133    }