001 package nl.cwi.sen1.tunit; 002 003 import junit.framework.TestCase; 004 import nl.cwi.sen1.tunit.execution.DefaultTestToolExecutorFactory; 005 import toolbus.ToolBus; 006 import toolbus.adapter.AbstractTool; 007 import aterm.ATermFactory; 008 009 /** 010 * The TUnitTestCase class is a base class for testing toolbus scripts. This 011 * class can start / stop the toolbus and connect / disconnect stubs from the 012 * toolbus. 013 * 014 * @author Bas Basten & Anton Lycklama a Nijeholt 015 */ 016 public abstract class TUnitTestCase extends TestCase { 017 protected volatile ToolBus toolbus; 018 protected final ATermFactory factory; 019 protected final DefaultTestToolExecutorFactory defaultTestToolExecutorFactory; 020 021 private volatile boolean errorsOccured = false; 022 023 public TUnitTestCase() { 024 super(); 025 026 factory = AbstractTool.getFactory(); 027 defaultTestToolExecutorFactory = new DefaultTestToolExecutorFactory( 028 this); 029 } 030 031 /** 032 * Get the absolute top path of the current package. 033 */ 034 public String getTopSrcDir() { 035 // Get the environment variable BUILDFILE that is passed 036 // from the ant file that is generated from the build scripts. 037 String topSrcDir = System.getenv("BUILDFILE"); 038 039 if (topSrcDir != null) { 040 int pos = topSrcDir.indexOf("/meta-build.ant"); 041 if (pos != -1) { 042 return topSrcDir.substring(0, pos); 043 } 044 } 045 046 // When the unit test is not called from an ant script, we just 047 // return the current working directory. (For use in Eclipse) 048 return "."; 049 } 050 051 /** 052 * Starts the toolbus at a given port and executes a script. If a script 053 * uses include scripts the searchPath will be used to include the include 054 * scripts from. 055 * 056 * @param searchPath 057 * The directories to search for the included script files of the 058 * specified script. 059 * @param scriptName 060 * The script to be executed. 061 */ 062 protected void startToolbus(String searchPath, String scriptName){ 063 String[] args = { "-I" + searchPath, "-S" + scriptName }; 064 065 toolbus = new ToolBus(args); 066 toolbus.setToolExecutorFactory(defaultTestToolExecutorFactory); 067 068 try{ 069 toolbus.parsecup(); 070 toolbus.prepare(); 071 new Thread(new Runnable(){ 072 public void run(){ 073 toolbus.execute(); 074 } 075 }).start(); 076 }catch(Exception e){ 077 e.printStackTrace(); 078 fail(e.getMessage()); 079 } 080 } 081 082 /** 083 * Stops the toolbus. 084 */ 085 protected void stopToolbus() { 086 toolbus.shutdown(factory.makeList()); 087 } 088 089 public int getPort() { 090 return toolbus.getPort(); 091 } 092 093 public void connectToolComValidator(ToolComValidator toolComValidator) 094 throws Exception { 095 toolComValidator.connect(toolbus); 096 } 097 098 public void addComValidatorForExecutableTool(String name, 099 Class<ToolComValidator> toolStubClass) { 100 defaultTestToolExecutorFactory.addComValidatorForExecutableTool(name, 101 toolStubClass); 102 } 103 104 public void testFailed(String message) { 105 errorsOccured = true; 106 stopToolbus(); 107 108 fail(message); 109 } 110 111 public boolean hasFailed() { 112 return errorsOccured; 113 } 114 }