Example #1
0
  @Override
  public Statement apply(Statement statement, Description description) {
    RetryOnFailure retryOnFailure = description.getAnnotation(RetryOnFailure.class);
    RetryOnException retryOnException = description.getAnnotation(RetryOnException.class);

    // sanity check that we don't use expected exceptions with the RetryOnX annotations
    if (retryOnFailure != null || retryOnException != null) {
      Test test = description.getAnnotation(Test.class);
      if (test.expected() != Test.None.class) {
        throw new IllegalArgumentException(
            "You cannot combine the RetryOnFailure "
                + "annotation with the Test(expected) annotation.");
      }
    }

    // sanity check that we don't use both annotations
    if (retryOnFailure != null && retryOnException != null) {
      throw new IllegalArgumentException(
          "You cannot combine the RetryOnFailure and RetryOnException annotations.");
    }

    if (retryOnFailure != null) {
      return new RetryOnFailureStatement(retryOnFailure.times(), statement);
    } else if (retryOnException != null) {
      return new RetryOnExceptionStatement(
          retryOnException.times(), retryOnException.exception(), statement);
    } else {
      return statement;
    }
  }