//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 NUnit extends ExecTask
{
private FileSet myFileSet;
private boolean isFailOnError = true;
private boolean isVerbose = false;
private String myUnitTesterFolder = null;
private String myAssembly = null;
private String myAssemblyFolder = null;
private String myAssemblyPath = null;
private String myNameSpace = null;
//protected...
protected String myUnitTesterPath;
//constants...
protected String cFailureMessage = "*** NUnit failed ***";
protected String cUnitTesterExe = "nunitconsole.exe";
protected String cProgIdSuffix = ".UnitTests.TestAll";
protected String cAssemblyFolderPropertyName = "BUILD_ROOT";
protected String cUnitTesterFolderPropertyName = "UNITTESTERFOLDER";
//----------------------------------------
public void execute() //throws BuildException
{
initialize();
if (myFileSet == null)
{
unittest(deriveProgId(".dll"), myAssemblyPath);
}
else
unittestFromFileSet();
}
//----------------------------------------
//-- the assembly name, eg. abc.dll
//-- required if fileset is not used
public void setAssembly(String theAssembly)
{
myAssembly = theAssembly;
}
//----------------------------------------
//-- where the assemblies are found
public void setAssemblyFolder(String theFolder)
{
myAssemblyFolder = theFolder;
}
//----------------------------------------
//-- where the assemblies are found
public void setNameSpace(String theNameSpace)
{
myNameSpace = theNameSpace;
}
//----------------------------------------
//-- where the unit tester executable can be found
//-- useful for testing new versions of the unit testers
public void setUnitTesterFolder(String theFolder)
{
myUnitTesterFolder = theFolder;
}
//----------------------------------------
//-- set to false if you want to continue after an error
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(deriveProgIdFrom(files[i], ".sln"), deriveAssembly(files[i]));
}
}
//----------------------------------------
private void initialize()
{
if (myFileSet == null && myAssembly == null)
throw new BuildException("You must specify an assembly name, e.g. abc.dll");
if (myFileSet != null && myAssembly != null)
log("Note: The 'assembly' attribute is ignored when using a fileset");
initializeUnitTesterFolder();
myUnitTesterPath = getFullPath(myUnitTesterFolder, cUnitTesterExe);
initializeAssemblyFolder();
myAssemblyPath = getFullPath(myAssemblyFolder, myAssembly);
}
//----------------------------------------
private String getFullPath(String theFolder, String theFile)
{
if (theFolder == null || theFolder == "") return theFile;
return theFolder + "/" + theFile;
}
//----------------------------------------
private void initializeAssemblyFolder()
{
//if user has set it, we're done
if (myAssemblyFolder != null) return;
//if there's a property for it, create the dir from that
if (getProject().getProperty(cAssemblyFolderPropertyName) != null)
{
setAssemblyFolder(getProject().getProperty(cAssemblyFolderPropertyName));
return;
}
//none of the above, use the current directory
setAssemblyFolder("./");
}
//----------------------------------------
private void initializeUnitTesterFolder()
{
//if user has set it, we're done
if (myUnitTesterFolder != null) return;
//if there's a property for it, create the dir from that
if (getProject().getProperty(cUnitTesterFolderPropertyName) != null)
{
setUnitTesterFolder(getProject().getProperty(cUnitTesterFolderPropertyName));
return;
}
//none of the above, use the current directory
setUnitTesterFolder("");
}
//----------------------------------------
private String deriveProgId(String extension)
{
if (myNameSpace != null) return myNameSpace + cProgIdSuffix;
return deriveProgIdFrom(myAssemblyPath, extension);
}
//----------------------------------------
private String deriveProgIdFrom(String file, String extension)
{
if (file == null) return "";
return capitalizeFirstCharacter(getFileNameFrom(file, extension)) + cProgIdSuffix;
}
//----------------------------------------
private String getFileNameFrom(String s, String extension)
{
int end = s.lastIndexOf(extension);
int begin = s.lastIndexOf("\\");
if (begin == -1) begin = s.lastIndexOf("/");
return s.substring(begin + 1, end);
}
//----------------------------------------
private String capitalizeFirstCharacter(String s)
{
StringBuffer s1 = new StringBuffer(s);
s1.setCharAt(0, Character.toUpperCase(s1.charAt(0)));
return s1.toString();
}
//----------------------------------------
private String deriveAssembly(String file)
{
if (file == null) return "";
int end = file.lastIndexOf(".sln");
int begin = file.lastIndexOf("\\");
if (begin == -1) begin = file.lastIndexOf("/");
StringBuffer f = new StringBuffer(file.substring(begin + 1, end));
f.append(".dll");
return getFullPath(myAssemblyFolder, f.toString());
}
//----------------------------------------
private void unittest(String theProgId, String theAssembly)
{
Commandline commandLine = buildCommandLine(theProgId, theAssembly);
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, String theAssemblyPath)
{
Commandline commandLine = new Commandline();
commandLine.setExecutable(myUnitTesterPath);
Path p = new Path(getProject());
p.setPath(theAssemblyPath);
String theLine = theProgId + "," + p.toString();
commandLine.createArgument().setLine(theLine);
return commandLine;
}
}
|