@Test
public void testBuild() throws CruiseControlException, IOException {

    final Date now = new Date();
    final MockModificationSet modSet = new MockModificationSet();
    modSet.setTimeOfCheck(now);
    final MockSchedule sched = new MockSchedule();
    projectConfig.add(sched);
    final Log log = new Log();
    final File logDir = new File(TEST_DIR + File.separator + "test-results");
    if (!logDir.exists()) {
        assertTrue(Util.doMkDirs(logDir));
    }
    filesToDelete.add(logDir);
    final String myProjectName = "myproject";
    log.setProjectName(myProjectName);
    filesToDelete.add(new File(TestUtil.getTargetDir(), myProjectName + ".ser"));
    log.setDir(logDir.getAbsolutePath());
    log.setEncoding("ISO-8859-1");
    log.validate();
    projectConfig.add(log);
    MergeLogger logger = new MergeLogger();
    logger.setFile(TEST_DIR + File.separator + "_auxLog1.xml");
    logger.validate();
    log.add(logger);
    logger = new MergeLogger();
    logger.setDir(TEST_DIR + File.separator + "_auxLogs");
    logger.validate();
    log.add(logger);
    projectConfig.add(modSet);
    project.setProjectConfig(projectConfig);
    project.setLabel("1.2.2");
    project.setName("myproject");
    project.setWasLastBuildSuccessful(false);
    project.setLastBuild(formatTime(now));
    project.setLastSuccessfulBuild(formatTime(now));
    writeFile(TEST_DIR + File.separator + "_auxLog1.xml", "<one/>");
    final File auxLogsDirectory = new File(TEST_DIR + File.separator + "_auxLogs");
    if (!auxLogsDirectory.exists()) {
        assertTrue(Util.doMkDirs(auxLogsDirectory));
    }
    filesToDelete.add(auxLogsDirectory);
    writeFile(TEST_DIR + File.separator + "_auxLogs/_auxLog2.xml",
            "<testsuite><properties><property/></properties><testcase/></testsuite>");
    writeFile(TEST_DIR + File.separator + "_auxLogs/_auxLog3.xml", "<testsuite/>");
    final List<BuildResultEvent> resultEvents = new ArrayList<BuildResultEvent>();
    project.addBuildResultListener(new BuildResultListener() {
        public void handleBuildResult(BuildResultEvent event) {
            resultEvents.add(event);
        }
    });
    final List<BuildProgressEvent> progressEvents = new ArrayList<BuildProgressEvent>();
    project.addBuildProgressListener(new BuildProgressListener() {
        public void handleBuildProgress(BuildProgressEvent event) {
            progressEvents.add(event);
        }
    });
    projectConfig.add(new DefaultLabelIncrementer());
    project.init();
    project.start();
    project.build();
    project.stop();
    final File expectedLogFile = new File(logDir, "log" + DateUtil.getFormattedTime(now) + "L1.2.2.xml");
    assertTrue(expectedLogFile.isFile());
    filesToDelete.add(expectedLogFile);
    assertTrue(project.isLastBuildSuccessful());
    final String expected =
            "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><cruisecontrol><modifications />"
            + "<info>"
            + "<property name=\"projectname\" value=\"myproject\" />"
            + "<property name=\"lastbuild\" value=\""
            + DateUtil.getFormattedTime(now)
            + "\" />"
            + "<property name=\"lastsuccessfulbuild\" value=\""
            + project.getLastSuccessfulBuild()
            + "\" />"
            + "<property name=\"builddate\" value=\""
            + DateUtil.formatIso8601(now)
            + "\" />"
            + "<property name=\"cctimestamp\" value=\""
            + DateUtil.getFormattedTime(now)
            + "\" />"
            + "<property name=\"label\" value=\"1.2.2\" />"
            + "<property name=\"interval\" value=\"300\" />"
            + "<property name=\"lastbuildsuccessful\" value=\"false\" />"
            + "<property name=\"logdir\" value=\""
            + logDir.getAbsolutePath()
            + "\" />"
            + "<property name=\"logfile\" value=\""
            + "log"
            + DateUtil.getFormattedTime(now)
            + "L1.2.2.xml\" />"
            + "</info><build /><one /><testsuite><testcase /></testsuite><testsuite /></cruisecontrol>";
    assertEquals(expected, Util.readFileToString(expectedLogFile));
    assertEquals("Didn't increment the label", "1.2.3", project.getLabel().intern());
    //look for sourcecontrol properties
    final Map<String, String> props = sched.getBuildProperties();
    assertNotNull("Build properties were null.", props);
    assertEquals("Build property count.", 10, props.size());
    assertTrue("projectname not found.", props.containsKey("projectname"));
    assertEquals("wrong projectname.", "myproject", props.get("projectname"));
    assertTrue("filemodified not found.", props.containsKey("filemodified"));
    assertTrue("fileremoved not found.", props.containsKey("fileremoved"));
    assertEquals(project.getLastSuccessfulBuild(), props.get("cclastgoodbuildtimestamp"));
    assertEquals(project.getLastBuild(), props.get("cclastbuildtimestamp"));
    assertTrue("cvstimestamp not passed.", props.containsKey("cvstimestamp"));
    // check that the proper events were fired
    assertEquals("Should be exactly one build result event", 1, resultEvents.size());
    final BuildResultEvent resultEvent = resultEvents.get(0);
    assertTrue("Should be successful build result event", resultEvent.isBuildSuccessful());
    assertTrue("Should have at least one of each project state except queued", progressEvents.size() >= 8);
}