RetryAnalyzerCount.java revision ad662c0a3971e0461a1aaa7580fd3c7e7a114a02
1package org.testng.util;
2
3import java.util.concurrent.atomic.AtomicInteger;
4
5import org.testng.IRetryAnalyzer;
6import org.testng.ITestResult;
7
8/**
9 * An implementation of IRetryAnalyzer that allows you to specify
10 * the maximum number of times you want your test to be retried.
11 *
12 * @author tocman@gmail.com (Jeremie Lenfant-Engelmann)
13 */
14public abstract class RetryAnalyzerCount implements IRetryAnalyzer {
15
16  // Default retry once.
17  AtomicInteger count = new AtomicInteger(1);
18
19  /**
20   * Set the max number of time the method needs to be retried.
21   */
22  protected void setCount(int count) {
23    this.count.set(count);
24  }
25
26  /**
27   * Return the current counter value
28   */
29  protected int getCount(){
30      return this.count.get();
31  }
32
33  /**
34   * Retries the test if count is not 0.
35   * @param result The result of the test.
36   */
37  @Override
38  public boolean retry(ITestResult result) {
39    if (count.getAndDecrement() > 0) {
40      return retryMethod(result);
41    }
42    return false;
43  }
44
45  /**
46   * The method implemented by the class that test if the test
47   * must be retried or not.
48   * @param result The result of the test.
49   * @return true if the test must be retried, false otherwise.
50   */
51  public abstract boolean retryMethod(ITestResult result);
52}
53