/**
   * Returns the future object for a newly created project.
   *
   * @param blockingJobName the name for the project
   * @param shell the shell command task to add
   * @param label the label to bind to master or slave
   * @return the future object for a newly created project
   * @throws IOException
   */
  private Future<FreeStyleBuild> createBlockingProject(
      String blockingJobName, Shell shell, Label label) throws IOException {
    FreeStyleProject blockingProject = this.createFreeStyleProject(blockingJobName);
    blockingProject.setAssignedLabel(label);

    blockingProject.getBuildersList().add(shell);
    Future<FreeStyleBuild> future = blockingProject.scheduleBuild2(0);

    while (!blockingProject.isBuilding()) {
      // wait until job is started
    }

    return future;
  }
  /**
   * Creates a new freestyle project and rebuild. Check that the RebuildCause has been set to the
   * new build. Check also that a UserIdCause is added.
   *
   * @throws Exception Exception
   */
  public void testWhenProjectWithCauseThenCauseIsCopiedAndUserCauseAdded() throws Exception {
    FreeStyleProject project = createFreeStyleProject();

    // Build (#1)
    project
        .scheduleBuild2(
            0,
            new Cause.RemoteCause("host", "note"),
            new ParametersAction(new StringParameterValue("name", "test")))
        .get();
    HtmlPage rebuildConfigPage = createWebClient().getPage(project, "1/rebuild");
    // Rebuild (#2)
    submit(rebuildConfigPage.getFormByName("config"));

    createWebClient().getPage(project).getAnchorByText("Rebuild Last").click();

    while (project.isBuilding()) {
      Thread.sleep(100);
    }
    List<Action> actions = project.getLastCompletedBuild().getActions();
    boolean hasRebuildCause = false;
    boolean hasRemoteCause = false;
    boolean hasUserIdCause = false;
    for (Action action : actions) {
      if (action instanceof CauseAction) {
        CauseAction causeAction = (CauseAction) action;
        if (causeAction.getCauses().get(0).getClass().equals(RebuildCause.class)) {
          hasRebuildCause = true;
        }
        if (causeAction.getCauses().get(0).getClass().equals(Cause.RemoteCause.class)) {
          hasRemoteCause = true;
        }
        if (causeAction.getCauses().get(0).getClass().equals(Cause.UserIdCause.class)) {
          hasUserIdCause = true;
        }
      }
    }
    assertTrue(
        "Build should have user, remote and rebuild causes",
        hasRebuildCause && hasRemoteCause && hasUserIdCause);
  }