//WARNING UNTESTED!!!
package com.arrizza.ant.taskdefs;
import java.io.*;
import org.apache.tools.ant.types.*;
import org.apache.tools.ant.taskdefs.*;
import org.apache.tools.ant.*;
public class VBUnit extends ExecTask
{
private FileSet myFileSet;
private boolean isFailOnError = true;
private boolean isVerbose = false;
private String myUnitTesterFolder = null;
//protected...
protected String myUnitTesterPath;
//constants...
protected String cFailureMessage = "*** VBUnit failed ***";
protected String cUnitTesterExe = "runvbunit.exe";
protected String cProgIdSuffix = ".TestSuite";
//----------------------------------------
public void execute() //throws BuildException
{
initialize();
if (myFileSet == null)
unittest(deriveProgId());
else
unittestFromFileSet();
}
//----------------------------------------
public void setUnitTesterFolder(String theFolder)
{
myUnitTesterFolder = theFolder;
}
//----------------------------------------
public void setFailonerror(boolean theFlag)
{
isFailOnError = theFlag;
}
//----------------------------------------
public void setVerbose(boolean theFlag)
{
isVerbose = theFlag;
}
//----------------------------------------
public void addTargets(FileSet set)
{
if (myFileSet != null) throw new BuildException("Only one fileset allowed");
myFileSet = set;
}
//----------------------------------------
//-- private from here on
//----------------------------------------
//----------------------------------------
private void unittestFromFileSet()
{
DirectoryScanner ds = myFileSet.getDirectoryScanner(getProject());
ds.scan();
unittest(ds.getIncludedFiles());
}
//----------------------------------------
private void unittest(String[] files)
{
if (files == null) return;
for (int i = 0; i < files.length;i++)
{
unittest(deriveProgId(files[i]));
}
}
//----------------------------------------
private void initialize()
{
initializeUnitTesterFolder();
myUnitTesterPath = getExePath(cUnitTesterExe);
}
//----------------------------------------
private String getExePath(String theExeFileName)
{
if (myUnitTesterFolder == null || myUnitTesterFolder == "") return theExeFileName;
return myUnitTesterFolder + "/" + theExeFileName;
}
//----------------------------------------
private void initializeUnitTesterFolder()
{
myUnitTesterFolder = "/vbunit3/bin";
}
//----------------------------------------
private String deriveProgId()
{
return getOwningTarget().toString() + cProgIdSuffix;
}
//----------------------------------------
private String deriveProgId(String file)
{
if (file == null) return "";
int index = file.lastIndexOf(".dll");
return file.substring(0, index) + cProgIdSuffix;
}
//----------------------------------------
private void unittest(String theProgId)
{
Commandline commandLine = buildCommandLine( theProgId);
PumpStreamHandler psh = createOutStream();
log("Command= " + commandLine.toString(), Project.MSG_INFO);
try
{
runCommand(commandLine, theProgId, psh);
}
catch (IOException e)
{
throw new BuildException("failed: " + e, e, getLocation());
}
}
//----------------------------------------
private PumpStreamHandler createOutStream()
{
return isVerbose ?
new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN)
:
new PumpStreamHandler(new ByteArrayOutputStream());
}
//----------------------------------------
private void runCommand(Commandline commandLine, String theProgId, PumpStreamHandler theStream) throws IOException
{
int errorVal = executeCommand(commandLine, theStream);
if (errorVal == 0) return;
String errorMessage = cFailureMessage + " Error= " + errorVal + " Target='" + theProgId + "'";
if (isFailOnError)
throw new BuildException(errorMessage, getLocation());
else
log(errorMessage, Project.MSG_INFO);
}
//----------------------------------------
private int executeCommand(Commandline commandLine, PumpStreamHandler theStream) throws IOException
{
Execute exe = new Execute(theStream);
exe.setCommandline(commandLine.getCommandline());
return exe.execute();
}
//----------------------------------------
//-- create the command line
private Commandline buildCommandLine(String theProgId)
{
Commandline commandLine = new Commandline();
commandLine.setExecutable(myUnitTesterPath);
commandLine.createArgument().setValue(theProgId);
return commandLine;
}
}
|