FileDocCategorySizeDatePackage
BuildFileTest.javaAPI DocApache Ant 1.7016190Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant

BuildFileTest

public abstract class BuildFileTest extends TestCase
A BuildFileTest is a TestCase which executes targets from an Ant buildfile for testing. This class provides a number of utility methods for particular build file tests which extend this class.

Fields Summary
protected Project
project
private StringBuffer
logBuffer
private StringBuffer
fullLogBuffer
private StringBuffer
outBuffer
private StringBuffer
errBuffer
private BuildException
buildException
Constructors Summary
public BuildFileTest()
Default constructor for the BuildFileTest object.

        super();
    
public BuildFileTest(String name)
Constructor for the BuildFileTest object.

param
name string to pass up to TestCase constructor

        super(name);
    
Methods Summary
public voidassertDebuglogContaining(java.lang.String substring)
Assert that the given substring is in the log messages.

        String realLog = getFullLog();
        assertTrue("expecting debug log to contain \"" + substring 
                   + "\" log was \""
                   + realLog + "\"",
                   realLog.indexOf(substring) >= 0);
    
public voidassertLogContaining(java.lang.String substring)
Assert that the given substring is in the log messages.

        String realLog = getLog();
        assertTrue("expecting log to contain \"" + substring + "\" log was \""
                   + realLog + "\"",
                   realLog.indexOf(substring) >= 0);
    
public voidassertOutputContaining(java.lang.String substring)
Assert that the given substring is in the output messages.

since
Ant1.7

        String realOutput = getOutput();
        assertTrue("expecting output to contain \"" + substring
                   + "\" output was \"" + realOutput + "\"",
                   realOutput.indexOf(substring) >= 0);
    
public voidassertPropertyEquals(java.lang.String property, java.lang.String value)
assert that a property equals a value; comparison is case sensitive.

param
property property name
param
value expected value

        String result = project.getProperty(property);
        assertEquals("property " + property,value,result);
    
public voidassertPropertySet(java.lang.String property)
assert that a property equals "true".

param
property property name

        assertPropertyEquals(property, "true");
    
public voidassertPropertyUnset(java.lang.String property)
assert that a property is null.

param
property property name

        assertPropertyEquals(property, null);
    
private java.lang.StringcleanBuffer(java.lang.StringBuffer buffer)

        StringBuffer cleanedBuffer = new StringBuffer();
        boolean cr = false;
        for (int i = 0; i < buffer.length(); i++) {
            char ch = buffer.charAt(i);
            if (ch == '\r") {
                cr = true;
                continue;
            }

            if (!cr) {
                cleanedBuffer.append(ch);
            } else {
                cleanedBuffer.append(ch);
            }
        }
        return cleanedBuffer.toString();
    
public voidconfigureProject(java.lang.String filename)
Sets up to run the named project

param
filename name of project file to run

        configureProject(filename, Project.MSG_DEBUG);
    
public voidconfigureProject(java.lang.String filename, int logLevel)
Sets up to run the named project

param
filename name of project file to run

        logBuffer = new StringBuffer();
        fullLogBuffer = new StringBuffer();
        project = new Project();
        project.init();
        File antFile = new File(System.getProperty("root"), filename);
        project.setUserProperty("ant.file" , antFile.getAbsolutePath());
        project.addBuildListener(new AntTestListener(logLevel));
        ProjectHelper.configureProject(project, antFile);
    
public voidexecuteTarget(java.lang.String targetName)
Executes a target we have set up

pre
configureProject has been called
param
targetName target to run

        PrintStream sysOut = System.out;
        PrintStream sysErr = System.err;
        try {
            sysOut.flush();
            sysErr.flush();
            outBuffer = new StringBuffer();
            PrintStream out = new PrintStream(new AntOutputStream(outBuffer));
            System.setOut(out);
            errBuffer = new StringBuffer();
            PrintStream err = new PrintStream(new AntOutputStream(errBuffer));
            System.setErr(err);
            logBuffer = new StringBuffer();
            fullLogBuffer = new StringBuffer();
            buildException = null;
            project.executeTarget(targetName);
        } finally {
            System.setOut(sysOut);
            System.setErr(sysErr);
        }

    
public voidexpectBuildException(java.lang.String target, java.lang.String cause)
run a target, expect for any build exception

param
target target to run
param
cause information string to reader of report

        expectSpecificBuildException(target, cause, null);
    
public voidexpectBuildExceptionContaining(java.lang.String target, java.lang.String cause, java.lang.String contains)
run a target, expect an exception string containing the substring we look for (case sensitive match)

param
target target to run
param
cause information string to reader of report
param
contains substring of the build exception to look for

        try {
            executeTarget(target);
        } catch (org.apache.tools.ant.BuildException ex) {
            buildException = ex;
            if ((null != contains) && (ex.getMessage().indexOf(contains) == -1)) {
                fail("Should throw BuildException because '" + cause + "' with message containing '" + contains + "' (actual message '" + ex.getMessage() + "' instead)");
            }
            return;
        }
        fail("Should throw BuildException because: " + cause);
    
public voidexpectDebuglog(java.lang.String target, java.lang.String log)
Assert that the given message has been logged with a priority >= VERBOSE when running the given target.

        executeTarget(target);
        String realLog = getFullLog();
        assertEquals(log, realLog);
    
public voidexpectLog(java.lang.String target, java.lang.String log)
Assert that only the given message has been logged with a priority <= INFO when running the given target.

        executeTarget(target);
        String realLog = getLog();
        assertEquals(log, realLog);
    
public voidexpectLogContaining(java.lang.String target, java.lang.String log)
Assert that the given message has been logged with a priority <= INFO when running the given target.

        executeTarget(target);
        assertLogContaining(log);
    
public voidexpectOutput(java.lang.String target, java.lang.String output)
execute the target, verify output matches expectations

param
target target to execute
param
output output to look for

        executeTarget(target);
        String realOutput = getOutput();
        assertEquals(output, realOutput.trim());
    
public voidexpectOutputAndError(java.lang.String target, java.lang.String output, java.lang.String error)
Executes the target, verify output matches expectations and that we got the named error at the end

param
target target to execute
param
output output to look for
param
error Description of Parameter

        executeTarget(target);
        String realOutput = getOutput();
        assertEquals(output, realOutput);
        String realError = getError();
        assertEquals(error, realError);
    
public voidexpectPropertySet(java.lang.String target, java.lang.String property, java.lang.String value)
call a target, verify property is as expected

param
target build file target
param
property property name
param
value expected value

        executeTarget(target);
        assertPropertyEquals(property, value);
    
public voidexpectPropertySet(java.lang.String target, java.lang.String property)
call a target, verify named property is "true".

param
target build file target
param
property property name

        expectPropertySet(target, property, "true");
    
public voidexpectPropertyUnset(java.lang.String target, java.lang.String property)
Call a target, verify property is null.

param
target build file target
param
property property name

        expectPropertySet(target, property, null);
    
public voidexpectSpecificBuildException(java.lang.String target, java.lang.String cause, java.lang.String msg)
Runs a target, wait for a build exception.

param
target target to run
param
cause information string to reader of report
param
msg the message value of the build exception we are waiting for set to null for any build exception to be valid

        try {
            executeTarget(target);
        } catch (org.apache.tools.ant.BuildException ex) {
            buildException = ex;
            if ((null != msg) && (!ex.getMessage().equals(msg))) {
                fail("Should throw BuildException because '" + cause
                        + "' with message '" + msg
                        + "' (actual message '" + ex.getMessage() + "' instead)");
            }
            return;
        }
        fail("Should throw BuildException because: " + cause);
    
public BuildExceptiongetBuildException()

        return buildException;
    
public java.lang.StringgetError()

        return cleanBuffer(errBuffer);
    
public java.lang.StringgetFullLog()
Gets the log the BuildFileTest object. Only valid if configureProject() has been called.

pre
fullLogBuffer!=null
return
The log value

        return fullLogBuffer.toString();
    
public java.lang.StringgetLog()
Gets the log the BuildFileTest object. Only valid if configureProject() has been called.

pre
logBuffer!=null
return
The log value

        return logBuffer.toString();
    
public java.lang.StringgetOutput()

        return cleanBuffer(outBuffer);
    
public ProjectgetProject()
Get the project which has been configured for a test.

return
the Project instance for this test.

        return project;
    
public java.io.FilegetProjectDir()
Gets the directory of the project.

return
the base dir of the project

        return project.getBaseDir();
    
public java.net.URLgetResource(java.lang.String resource)
Retrieve a resource from the caller classloader to avoid assuming a vm working directory. The resource path must be relative to the package name or absolute from the root path.

param
resource the resource to retrieve its url.
throws
junit.framework.AssertionFailedError if the resource is not found.

        URL url = getClass().getResource(resource);
        assertNotNull("Could not find resource :" + resource, url);
        return url;
    
protected voidtearDown()
Automatically calls the target called "tearDown" from the build file tested if it exits. This allows to use Ant tasks directly in the build file to clean up after each test. Note that no "setUp" target is automatically called, since it's trivial to have a test target depend on it.

        final String tearDown = "tearDown";
        if (project.getTargets().containsKey(tearDown)) {
            project.executeTarget(tearDown);
        }