@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());
  }
  @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());
  }
  @LocalData
  public void testFailedSince() {
    Hudson hudson = Hudson.getInstance();
    List<Project> projects = hudson.getProjects();
    Run lastRun = null;
    for (Project project : projects) {
      if (project.getName().equalsIgnoreCase("failingtests")) {
        lastRun = project.getLastCompletedBuild();
      }
    }
    if (lastRun == null) fail("No build including Robot results was found");

    RobotBuildAction action = lastRun.getAction(RobotBuildAction.class);
    RobotResult result = action.getResult();
    RobotCaseResult firstFailed = result.getAllFailedCases().get(0);
    assertEquals(2, firstFailed.getFailedSince());
  }
  @LocalData
  public void testMissingReportFileWithOld() throws Exception {
    Hudson hudson = Hudson.getInstance();
    List<Project> projects = hudson.getProjects();
    Project testProject = null;
    for (Project project : projects) {
      if (project.getName().equals("oldrobotbuild")) testProject = project;
    }
    if (testProject == null) fail("Couldn't find example project");

    WebClient wc = getWebClient();

    File buildRoot = testProject.getLastBuild().getRootDir();
    File robotHtmlReport = new File(buildRoot, RobotPublisher.FILE_ARCHIVE_DIR + "/report.html");
    if (!robotHtmlReport.delete()) fail("Unable to delete report directory");

    HtmlPage page = wc.goTo("job/oldrobotbuild/robot/");
    WebAssert.assertTextPresent(page, "No Robot html report found!");

    page = wc.goTo("job/oldrobotbuild/1/robot/");
    WebAssert.assertTextPresent(page, "No Robot html report found!");
  }
  // Called for each page load of administration
  public void load() {

    /* In case plugin is uncheck */
    // List all jobs
    List<String> allJobsName = new ArrayList<String>();

    // List all the jobs that use the plugin (with at least one resource)
    List<String> allExclusionJobs = new ArrayList<String>();

    // Check all projects
    for (Project<?, ?> p : Hudson.getInstance().getProjects()) {

      // Add all jobs names to the list
      allJobsName.add(p.getName());
      // We want to retrieve all components BuildWrappers
      Map<Descriptor<BuildWrapper>, BuildWrapper> buildWrappers = p.getBuildWrappers();
      // For each of them
      for (Iterator i = buildWrappers.keySet().iterator(); i.hasNext(); ) {
        Descriptor<BuildWrapper> key = (Descriptor<BuildWrapper>) i.next();

        // We check if the descriptor is "org.jvnet.hudson.plugins.exclusion.IdAllocator $
        // DescriptorImpl"
        if (buildWrappers
            .get(key)
            .getDescriptor()
            .toString()
            .split("@")[0]
            .equals("org.jvnet.hudson.plugins.exclusion.IdAllocator$DescriptorImpl")) {
          // No duplicates
          if (!allExclusionJobs.contains(p.getName())) {
            allExclusionJobs.add(p.getName());
          }
        }
      }
    }

    // We delete each job that is in the global list and not in the list of exclusions
    for (String jobName : allJobsName) {
      if (!allExclusionJobs.contains(jobName)) {
        IdAllocator.deleteList(jobName);
      }
    }

    // Set all builds to false (build = currently used)
    for (RessourcesMonitor rm : listRessources) {
      rm.setBuild(false);
    }

    // For each resource Job, set build to true if a resource is used
    for (Iterator i = IdAllocationManager.ids.keySet().iterator(); i.hasNext(); ) {
      String resource = (String) i.next();
      IdAllocator.updateBuild(
          IdAllocationManager.ids.get(resource).getProject().getName(), resource, true);
    }

    list = new ArrayList<RessourcesMonitor>();
    // Local copy of the list
    for (RessourcesMonitor rm : listRessources) {
      list.add(new RessourcesMonitor(rm.getJobName(), rm.getRessource(), rm.getBuild()));
    }
  }
  @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>"));
  }