FileDocCategorySizeDatePackage
ExecTaskTest.javaAPI DocApache Ant 1.7017681Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs

ExecTaskTest

public class ExecTaskTest extends BuildFileTest
Unit test for the <exec> task.

Fields Summary
private static final String
BUILD_PATH
private static final String
BUILD_FILE
private static final int
TIME_TO_WAIT
private static final int
MAX_BUILD_TIME
maximum time allowed for the build in milliseconds
private static final int
SECURITY_MARGIN
private static final org.apache.tools.ant.util.FileUtils
FILE_UTILS
Utilities used for file operations
private File
logFile
private MonitoredBuild
myBuild
private volatile boolean
buildFinished
Constructors Summary
public ExecTaskTest(String name)

       
        super(name);
    
Methods Summary
private java.lang.StringgetFileString(java.lang.String filename)

        String result = null;
        FileReader reader = null;
        try {
            reader = new FileReader(getProject().resolveFile(filename));
            result = FileUtils.readFully(reader);
        } catch (IOException eyeOhEx) {
        } finally {
            FileUtils.close(reader);
        }
        return result;
    
public voidsetUp()

        configureProject(BUILD_FILE);
    
public voidtearDown()

        if (logFile != null && logFile.exists()) {
            getProject().setProperty("logFile", logFile.getAbsolutePath());
        }
        executeTarget("cleanup");
    
public voidtestExecInconsistentSettings()

        executeTarget("testExecInconsistentSettings");
    
public voidtestExecOSFamily()

        executeTarget("testExecOSFamily");
    
public voidtestExecUnknownOS()

        executeTarget("testExecUnknownOS");
    
public voidtestNoRedirect()

        executeTarget("no-redirect");
        if (getProject().getProperty("test.can.run") == null) {
            return;
        }
        assertEquals("unexpected log content",
            getProject().getProperty("ant.file") + " out"
            + getProject().getProperty("ant.file") + " err", getLog());
    
public voidtestRedirect1()

        executeTarget("redirect1");
        if (getProject().getProperty("test.can.run") == null) {
            return;
        }
        String expectedOut = getProject().getProperty("ant.file") + " out\n"
            + getProject().getProperty("ant.file") + " err\n";

        assertEquals("unexpected output",
            expectedOut, getFileString("redirect.out"));
    
public voidtestRedirect2()

        executeTarget("redirect2");
        if (getProject().getProperty("test.can.run") == null) {
            return;
        }

        assertEquals("unexpected output",
            getProject().getProperty("ant.file") + " out\n",
            getFileString("redirect.out"));
        assertEquals("unexpected error output",
            getProject().getProperty("ant.file") + " err\n",
            getFileString("redirect.err"));
    
public voidtestRedirect3()

        executeTarget("redirect3");
        if (getProject().getProperty("test.can.run") == null) {
            return;
        }
        assertEquals("unexpected log content",
            getProject().getProperty("ant.file") + " err", getLog());
        String expectedOut = getProject().getProperty("ant.file") + " out\n";

        assertEquals("unexpected output",
            expectedOut, getFileString("redirect.out"));
        assertPropertyEquals("redirect.out", expectedOut.trim());
    
public voidtestRedirect4()

        executeTarget("redirect4");
        if (getProject().getProperty("test.can.run") == null) {
            return;
        }
        String expectedOut = getProject().getProperty("ant.file") + " out\n";
        String expectedErr = getProject().getProperty("ant.file") + " err\n";

        assertEquals("unexpected output",
            expectedOut, getFileString("redirect.out"));
        assertPropertyEquals("redirect.out", expectedOut.trim());
        assertEquals("unexpected error output",
            expectedErr, getFileString("redirect.err"));
        assertPropertyEquals("redirect.err", expectedErr.trim());
    
public voidtestRedirect5()

        testRedirect5or6("redirect5");
    
public voidtestRedirect5or6(java.lang.String target)

        executeTarget(target);
        if (getProject().getProperty("wc.can.run") == null) {
            return;
        }

        assertEquals("unexpected output", "3", getFileString("redirect.out").trim());
        assertEquals("property redirect.out", "3",
            getProject().getProperty("redirect.out").trim());
        assertNull("unexpected error output", getFileString("redirect.err"));
        assertPropertyEquals("redirect.err", "");
    
public voidtestRedirect6()

        testRedirect5or6("redirect6");
    
public voidtestRedirect7()

        executeTarget("redirect7");
        if (getProject().getProperty("wc.can.run") == null) {
            return;
        }

        assertEquals("unexpected output", "3", getFileString("redirect.out").trim());
        assertEquals("property redirect.out", "3",
            getProject().getProperty("redirect.out").trim());
        assertNull("unexpected error output", getFileString("redirect.err"));
    
public voidtestRedirector1()

        executeTarget("init");
        if (getProject().getProperty("test.can.run") == null) {
            return;
        }
        expectBuildException("redirector1", "cannot have > 1 nested <redirector>s");
    
public voidtestRedirector10()

        testRedirector9Thru12("redirector10");
    
public voidtestRedirector11()

        testRedirector9Thru12("redirector11");
    
public voidtestRedirector12()

        testRedirector9Thru12("redirector12");
    
public voidtestRedirector13()

        executeTarget("redirector13");
        if (getProject().getProperty("test.can.run") == null) {
            return;
        }
        String antfile = getProject().getProperty("ant.file");
        try {
            //no point in setting a message
            assertEquals(antfile + " OUTPUT???" + antfile + " ERROR!!!", getLog());
        } catch (ComparisonFailure cf) {
            assertEquals("unexpected log content",
                antfile + " ERROR!!!" + antfile + " OUTPUT???", getLog());
        }
    
public voidtestRedirector14()

        executeTarget("redirector14");
        if (getProject().getProperty("cat.can.run") == null) {
            return;
        }
        assertEquals("unexpected log output", "blah after blah", getLog());
    
public voidtestRedirector15()

        executeTarget("redirector15");
        if (getProject().getProperty("cat.can.run") == null) {
            return;
        }
        assertTrue("error with transcoding",
            FILE_UTILS.contentEquals(
            getProject().resolveFile("expected/utf-8"),
            getProject().resolveFile("redirector.out")));
    
public voidtestRedirector16()

        executeTarget("redirector16");
    
public voidtestRedirector17()

        executeTarget("redirector17");
    
public voidtestRedirector18()

        if (getProject().getProperty("test.can.run") == null) {
            return;
        }
        expectLog("redirector18", getProject().getProperty("ant.file")
            + " out" + getProject().getProperty("ant.file") + " err");
    
public voidtestRedirector2()

        executeTarget("redirector2");
        if (getProject().getProperty("test.can.run") == null) {
            return;
        }

        assertEquals("unexpected output",
            getProject().getProperty("ant.file") + " out\n"
            + getProject().getProperty("ant.file") + " err\n",
            getFileString("redirector.out"));
    
public voidtestRedirector3()

        executeTarget("redirector3");
        if (getProject().getProperty("test.can.run") == null) {
            return;
        }

        assertEquals("unexpected output",
            getProject().getProperty("ant.file") + " out\n",
            getFileString("redirector.out"));
        assertEquals("unexpected error output",
            getProject().getProperty("ant.file") + " err\n",
            getFileString("redirector.err"));
    
public voidtestRedirector4()

        executeTarget("redirector4");
        if (getProject().getProperty("test.can.run") == null) {
            return;
        }
        String expectedOut = getProject().getProperty("ant.file") + " out\n";

        assertEquals("unexpected log content",
            getProject().getProperty("ant.file") + " err", getLog());
        assertEquals("unexpected output", expectedOut,
            getFileString("redirector.out"));
        assertPropertyEquals("redirector.out", expectedOut.trim());
    
public voidtestRedirector5()

        testRedirector5or6("redirector5");
    
private voidtestRedirector5or6(java.lang.String target)

        executeTarget(target);
        if (getProject().getProperty("test.can.run") == null) {
            return;
        }
        String expectedOut = getProject().getProperty("ant.file") + " out\n";
        String expectedErr = getProject().getProperty("ant.file") + " err\n";

        assertEquals("unexpected output", expectedOut,
            getFileString("redirector.out"));
        assertPropertyEquals("redirector.out", expectedOut.trim());
        assertEquals("unexpected error output", expectedErr,
            getFileString("redirector.err"));
        assertPropertyEquals("redirector.err", expectedErr.trim());
    
public voidtestRedirector6()

        testRedirector5or6("redirector6");
    
public voidtestRedirector7()

        executeTarget("redirector7");
        if (getProject().getProperty("test.can.run") == null) {
            return;
        }
        String expectedOut = getProject().getProperty("ant.file") + " out\n";
        String expectedErr = getProject().getProperty("ant.file") + " ERROR!!!\n";

        assertEquals("unexpected output", expectedOut,
            getFileString("redirector.out"));
        assertPropertyEquals("redirector.out", expectedOut.trim());
        assertEquals("unexpected error output", expectedErr,
            getFileString("redirector.err"));
        assertPropertyEquals("redirector.err", expectedErr.trim());
    
public voidtestRedirector8()

        executeTarget("redirector8");
        if (getProject().getProperty("wc.can.run") == null) {
            return;
        }

        assertEquals("unexpected output", "3", getFileString("redirector.out").trim());
        assertEquals("property redirector.out", "3",
            getProject().getProperty("redirector.out").trim());
        assertNull("unexpected error output", getFileString("redirector.err"));
        assertPropertyEquals("redirector.err", "");
    
public voidtestRedirector9()

        testRedirector9Thru12("redirector9");
    
private voidtestRedirector9Thru12(java.lang.String target)

        executeTarget(target);
        if (getProject().getProperty("cat.can.run") == null) {
            return;
        }
        String expectedOut = "blah after blah";

        assertEquals("unexpected output",
            expectedOut, getFileString("redirector.out").trim());
        assertPropertyEquals("redirector.out", expectedOut.trim());
        assertNull("unexpected error output", getFileString("redirector.err"));
        assertPropertyEquals("redirector.err", "");
    
public voidtestspawn()

        project.executeTarget("init");
        if (project.getProperty("test.can.run") == null) {
            return;
        }
        myBuild = new MonitoredBuild(new File(System.getProperty("root"), BUILD_FILE), "spawn");
        logFile = FILE_UTILS.createTempFile("spawn","log", project.getBaseDir());
        // this is guaranteed by FileUtils#createTempFile
        assertTrue("log file not existing", !logFile.exists());
        // make the spawned process run 4 seconds
        myBuild.setTimeToWait(TIME_TO_WAIT);
        myBuild.setLogFile(logFile.getAbsolutePath());
        myBuild.addBuildListener(new MonitoredBuildListener());
        myBuild.start();
        GregorianCalendar startwait = new GregorianCalendar();
        // this loop runs parallel to the build
        while (!buildFinished) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                System.out.println("my sleep was interrupted");
            }
            GregorianCalendar now = new GregorianCalendar();
            // security
            if (now.getTime().getTime() - startwait.getTime().getTime() > MAX_BUILD_TIME) {
                System.out.println("aborting wait, too long " + (now.getTime().getTime() - startwait.getTime().getTime()) + "milliseconds");
                break;
            }
        }
        // now wait until the spawned process is finished
        try {
            Thread.sleep((TIME_TO_WAIT) * 1000 + SECURITY_MARGIN);
        } catch (InterruptedException e) {
            System.out.println("my sleep was interrupted");
        }
        // time of the build in milli seconds
        long elapsed = myBuild.getTimeElapsed();
        assertTrue("we waited more than the process lasted", TIME_TO_WAIT * 1000
                + SECURITY_MARGIN > elapsed);
        logFile = new File(logFile.getAbsolutePath());
        assertTrue("log file found after spawn", logFile.exists());