@LocalData
  public void testDontCopyExcessFilesWhenOtherFilesEmpty() throws Exception {
    Hudson hudson = Hudson.getInstance();
    List<Project> projects = hudson.getProjects();
    Project testProject = null;
    for (Project project : projects) {
      if (project.getName().equals("dont-copy")) testProject = project;
    }
    if (testProject == null) fail("Couldn't find example project");
    Future<Run> run = testProject.scheduleBuild2(0);

    while (!run.isDone()) {
      Thread.sleep(5);
    }

    Run lastBuild = testProject.getLastBuild();
    assertTrue("Build wasn't a success", lastBuild.getResult() == Result.SUCCESS);

    File storedOutput =
        new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/output.xml");
    File storedSplitOutput =
        new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/output-001.xml");
    File storedDummy =
        new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/dummy.file");

    assertTrue("output.xml was not stored", storedOutput.exists());
    assertTrue("output-001.xml was not stored", storedSplitOutput.exists());
    assertFalse("dummy.file was copied", storedDummy.exists());
  }
  public void test() throws Exception {
    Project<?, ?> projectA = createFreeStyleProject("projectA");
    List<AbstractBuildParameters> buildParameters = new ArrayList<AbstractBuildParameters>();
    buildParameters.add(new CurrentBuildParameters());
    BlockingBehaviour neverFail = new BlockingBehaviour("never", "never", "never");
    BlockableBuildTriggerConfig config =
        new BlockableBuildTriggerConfig("projectB", neverFail, buildParameters);
    projectA.getBuildersList().add(new TriggerBuilder(config));

    CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
    projectA.getBuildersList().add(builder);

    Project projectB = createFreeStyleProject("projectB");
    projectB.setQuietPeriod(0);
    hudson.rebuildDependencyGraph();

    // Just to make sure they differ from projectA's build numbers.
    projectB.updateNextBuildNumber(3);

    int expectedBuildNumber = projectB.getNextBuildNumber();
    projectA.scheduleBuild2(0, new UserCause()).get();

    EnvVars envVars = builder.getEnvVars();
    assertThat(envVars, notNullValue());
    assertThat(envVars, hasEntry("LAST_TRIGGERED_JOB_NAME", "projectB"));
    assertThat(
        envVars,
        hasEntry("TRIGGERED_BUILD_NUMBER_projectB", Integer.toString(expectedBuildNumber)));

    // The below test for expectedBuildNumber is meaningless if the
    // value doesn't update, though it should always update.
    assertThat(projectB.getNextBuildNumber(), is(not(expectedBuildNumber)));

    expectedBuildNumber = projectB.getNextBuildNumber();
    projectA.scheduleBuild2(0, new UserCause()).get();
    envVars = builder.getEnvVars();

    assertThat(envVars, notNullValue());
    assertThat(envVars, hasEntry("LAST_TRIGGERED_JOB_NAME", "projectB"));
    assertThat(
        envVars,
        hasEntry("TRIGGERED_BUILD_NUMBER_projectB", Integer.toString(expectedBuildNumber)));
  }
  @LocalData
  public void testPublish() throws Exception {
    Hudson hudson = Hudson.getInstance();
    List<Project> projects = hudson.getProjects();
    Project testProject = null;
    for (Project project : projects) {
      if (project.getName().equals("robot")) testProject = project;
    }
    if (testProject == null) fail("Couldn't find example project");
    Future<Run> run = testProject.scheduleBuild2(0);

    while (!run.isDone()) {
      Thread.sleep(5);
    }

    Run lastBuild = testProject.getLastBuild();
    assertTrue("Build wasn't a success", lastBuild.getResult() == Result.SUCCESS);

    File storedOutput =
        new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/output.xml");
    File storedSplitOutput =
        new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/output-001.xml");
    File storedReport =
        new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/report.html");
    File storedSplitReport =
        new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/report.html");
    File storedLog =
        new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/log.html");
    File storedSplitLog =
        new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/log-001.html");
    File storedJs = new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/log.js");
    File storedSplitJs1 =
        new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/log-001.js");
    File storedImage1 =
        new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/screenshot.png");
    File storedImage2 =
        new File(
            lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "/subfolder/screenshot2.png");
    File storedDummy =
        new File(lastBuild.getRootDir(), RobotPublisher.FILE_ARCHIVE_DIR + "dummy.file");

    assertTrue("output.xml was not stored", storedOutput.exists());
    assertTrue("output-001.xml was not stored", storedSplitOutput.exists());
    assertTrue("report.html was not stored", storedReport.exists());
    assertTrue("report-001.html was not stored", storedSplitReport.exists());
    assertTrue("log.html was not stored", storedLog.exists());
    assertTrue("log-001.html was not stored", storedSplitLog.exists());
    assertTrue("log.js was not stored", storedJs.exists());
    assertTrue("log-001.js was not stored", storedSplitJs1.exists());
    assertTrue("screenshot.png was not stored", storedImage1.exists());
    assertTrue("screenshot2.png was not stored", storedImage2.exists());
    assertFalse("dummy.file was copied", storedDummy.exists());
  }
 /**
  * Tests with no extensions.
  *
  * @throws IOException IOException
  * @throws InterruptedException InterruptedException
  * @throws ExecutionException ExecutionException
  */
 public void testNoRebuildValidatorExtension()
     throws IOException, InterruptedException, ExecutionException {
   Project projectA = createFreeStyleProject("testFreeStyleA");
   Build buildA =
       (Build)
           projectA
               .scheduleBuild2(
                   0,
                   new Cause.UserIdCause(),
                   new ParametersAction(new StringParameterValue("party", "megaparty")))
               .get();
   assertNotNull(buildA.getAction(RebuildAction.class));
 }
 /**
  * Tests with an extension returning isApplicable false.
  *
  * @throws IOException IOException
  * @throws InterruptedException InterruptedException
  * @throws ExecutionException ExecutionException
  */
 public void testRebuildValidatorExtensionIsApplicableFalse()
     throws IOException, InterruptedException, ExecutionException {
   hudson.getExtensionList(RebuildValidator.class).add(0, new ValidatorNeverApplicable());
   Project projectA = createFreeStyleProject("testFreeStyleC");
   Build buildA =
       (Build)
           projectA
               .scheduleBuild2(
                   0,
                   new Cause.UserIdCause(),
                   new ParametersAction(new StringParameterValue("party", "megaparty")))
               .get();
   assertNotNull(buildA.getAction(RebuildAction.class));
 }
  @Before
  public void setUp() throws Exception {
    java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit")
        .setLevel(java.util.logging.Level.SEVERE);

    j.jenkins.setAuthorizationStrategy(new FullControlOnceLoggedInAuthorizationStrategy());
    j.jenkins.setSecurityRealm(j.createDummySecurityRealm());

    createKnowledgeBase();
    project = j.createFreeStyleProject("x");
    project.getBuildersList().add(new FailureBuilder());
    project.getPublishersList().add(new ClaimPublisher());
    build = project.scheduleBuild2(0).get();
  }
  @LocalData
  public void testReportPage() throws Exception {
    Hudson hudson = Hudson.getInstance();
    List<Project> projects = hudson.getProjects();
    Project testProject = null;
    for (Project project : projects) {
      if (project.getName().equals("robot")) testProject = project;
    }
    if (testProject == null) fail("Couldn't find example project");
    Future<Run> run = testProject.scheduleBuild2(0);

    while (!run.isDone()) {
      Thread.sleep(5);
    }
    Run lastBuild = testProject.getLastBuild();
    assertTrue("Build wasn't a success", lastBuild.getResult() == Result.SUCCESS);

    WebClient wc = getWebClient();
    HtmlPage page = wc.goTo("job/robot/robot/");
    WebAssert.assertTextPresent(page, "Robot Framework test results");
    WebAssert.assertTextPresent(page, "4 failed tests, 4 critical");
    WebAssert.assertTextPresent(page, "Tests took 0:00:00.009 (+0:00:00.009)");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='Testcases%20&%20Othercases/Testcases/Not%20equal' and contains(.,'Testcases & Othercases.Testcases.Not equal')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='Testcases%20&%20Othercases/Othercases' and contains(.,'Testcases & Othercases.Othercases')]");

    page = wc.goTo("job/robot/1/robot/report/");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//a[@href='output.xml' and contains(.,'output.xml')]");

    page = wc.goTo("job/robot/1/robot/Testcases%20&%20Othercases");
    WebAssert.assertTextPresent(page, "4 failed tests, 4 critical");
    WebAssert.assertTextPresent(page, "Tests took 0:00:00.009 (+0:00:00.009)");
    WebAssert.assertTextNotPresent(page, "All Testcases");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='Testcases/Not%20equal' and contains(.,'Testcases.Not equal')]");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//a[@href='Othercases' and contains(.,'Othercases')]");

    page = wc.goTo("job/robot/1/robot/Testcases%20&%20Othercases/Othercases");
    WebAssert.assertTextPresent(page, "2 failed tests, 2 critical");
    WebAssert.assertTextPresent(page, "Tests took 0:00:00.005 (+0:00:00.005)");
    WebAssert.assertTextPresent(page, "All Testcases");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//a[@href='Not%20equal' and contains(.,'Not equal')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='Contains%20string' and contains(.,'Contains string')]");

    page = wc.goTo("job/robot/1/robot/Testcases%20&%20Othercases/Othercases/Not%20equal");
    WebAssert.assertTextPresent(page, "Critical test case: \"Not equal\"");
    WebAssert.assertTextPresent(page, "Failed!");
    WebAssert.assertTextPresent(page, "Error message:");
    WebAssert.assertTextPresent(page, "Hello, world! != Good bye, world!");
    WebAssert.assertTextPresent(page, "Test took 0:00:00.001 (+0:00:00.001)");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//img[@src='durationGraph']");

    page = wc.goTo("job/robot/1/robot/Testcases%20&%20Othercases/Othercases/Contains%20string");
    WebAssert.assertTextPresent(page, "Passed!");
    WebAssert.assertTextNotPresent(page, "Error message:");
  }
  @LocalData
  public void testSummariesWithData() throws Exception {
    Hudson hudson = Hudson.getInstance();
    List<Project> projects = hudson.getProjects();
    Project testProject = null;
    for (Project project : projects) {
      if (project.getName().equals("robot")) testProject = project;
    }
    if (testProject == null) fail("Couldn't find example project");
    Future<Run> run = testProject.scheduleBuild2(0);

    while (!run.isDone()) {
      Thread.sleep(5);
    }

    Run lastBuild = testProject.getLastBuild();
    assertTrue("Build wasn't a success", lastBuild.getResult() == Result.SUCCESS);

    WebClient wc = getWebClient();

    HtmlPage page = wc.goTo("job/robot/");
    WebAssert.assertElementPresentByXPath(
        page, "//div[@id='navigation']//a[@href='/job/robot/robot']");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//h4[contains(.,'Latest Robot Results:')]");
    WebAssert.assertElementPresentByXPath(page, "//td[@id='main-panel']//img[@src='robot/graph']");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='/job/robot/1/robot' and contains(text(),'Browse results')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='/job/robot/1/robot/report/report.html' and contains(text(), 'Open report.html')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='/job/robot/1/robot/report/log.html' and contains(text(), 'Open log.html')]");

    HtmlTable table = page.getHtmlElementById("robot-summary-table");
    Assert.assertTrue(
        table
            .asXml()
            .replaceAll("\\s", "")
            .contains(
                "<tableclass=\"table\"id=\"robot-summary-table\"><tbodyalign=\"left\"><tr><th/><th>Total</th><th>Failed</th><th>Passed</th><th>Pass%</th></tr><tr><th>Criticaltests</th><tdclass=\"table-upper-row\"style=\"border-left:0px;\">8</td><tdclass=\"table-upper-row\"><spanclass=\"fail\">4</span></td><tdclass=\"table-upper-row\">4</td><tdclass=\"table-upper-row\">50.0</td></tr><tr><th>Alltests</th><tdstyle=\"border-left:0px;\">8</td><td><spanclass=\"fail\">4</span></td><td>4</td><td>50.0</td></tr></tbody></table>"));

    page = wc.goTo("job/robot/1/");
    WebAssert.assertElementPresentByXPath(
        page, "//div[@id='navigation']//a[@href='/job/robot/1/robot']");
    WebAssert.assertElementPresentByXPath(
        page, "//td[@id='main-panel']//h4[contains(.,'Robot Test Summary:')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='/job/robot/1/robot' and contains(text(),'Browse results')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='/job/robot/1/robot/report/report.html' and contains(text(), 'Open report.html')]");
    WebAssert.assertElementPresentByXPath(
        page,
        "//td[@id='main-panel']//a[@href='/job/robot/1/robot/report/log.html' and contains(text(), 'Open log.html')]");
    table = page.getHtmlElementById("robot-summary-table");
    Assert.assertTrue(
        table
            .asXml()
            .replaceAll("\\s", "")
            .contains(
                "<tableclass=\"table\"id=\"robot-summary-table\"><tbodyalign=\"left\"><tr><th/><th>Total</th><th>Failed</th><th>Passed</th><th>Pass%</th></tr><tr><th>Criticaltests</th><tdclass=\"table-upper-row\"style=\"border-left:0px;\">8</td><tdclass=\"table-upper-row\"><spanclass=\"fail\">4</span></td><tdclass=\"table-upper-row\">4</td><tdclass=\"table-upper-row\">50.0</td></tr><tr><th>Alltests</th><tdstyle=\"border-left:0px;\">8</td><td><spanclass=\"fail\">4</span></td><td>4</td><td>50.0</td></tr></tbody></table>"));
  }