package test.retryAnalyzer;

import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.TestNG;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.util.RetryAnalyzerCount;

/**
 * retryAnalyzer parameter unit tests.
 * @author tocman@gmail.com (Jeremie Lenfant-Engelmann)
 *
 */
public final class RetryAnalyzerTest extends RetryAnalyzerCount {

  private static int r = 1;
  private static int r2 = 1;
  private static int r3 = 1;
  private static int value = 42;

  public RetryAnalyzerTest() {
    setCount(2);
  }

  @Test(retryAnalyzer=RetryAnalyzerTest.class)
  public void testAnnotation() {
    Assert.assertTrue(true);
  }

  @Test(retryAnalyzer=RetryAnalyzerTest.class)
  public void testAnnotationWithOneRetry() {
    if (r == 1) {
      r--;
      Assert.assertTrue(false);
    }
    if (r == 0) {
      Assert.assertTrue(true);
    }
  }

  @DataProvider(name="dataProvider")
  private Object[][] dataProvider() {
    return new Object[][] { { 1, false }, { 0, true }, { 0, true },
        { 1, false } };
  }

  @DataProvider(name="dataProvider2")
  private Object[][] dataProvider2() {
    value = 42;

    return new Object[][] { { true }, { true } };
  }

  @Test(retryAnalyzer=RetryAnalyzerTest.class, dataProvider="dataProvider")
  public void testAnnotationWithDataProvider(int paf, boolean test) {
    if (paf == 1 && test == false) {
      if (r2 >= 1) {
        r2--;
        Assert.assertTrue(false);
      } else if (r2 == 0) {
        Assert.assertTrue(true);
      }
    }
    else if (paf == 0 || test == true) {
      Assert.assertTrue(true);
    }
  }

  @Test(retryAnalyzer=RetryAnalyzerTest.class, dataProvider="dataProvider2")
  public void testAnnotationWithDataProviderAndRecreateParameters(boolean dummy) {
    if (r3 == 1) {
      this.value = 0;
      r3--;
      Assert.assertTrue(false);
    } else if (r3 == 0) {
      Assert.assertEquals(this.value, 42);
    }
  }

  @Test
  public void withFactory() {
    TestNG tng = new TestNG();
    tng.setVerbose(0);
    tng.setTestClasses(new Class[] { MyFactory.class});
    FactoryTest.m_count = 0;
    tng.run();

    Assert.assertEquals(FactoryTest.m_count, 4);
  }

  @Override
  public boolean retryMethod(ITestResult result) {
    return true;
  }
}

@@ -536,24 +536,60 @@
 value = 42;%0D
+%0A  private int executionNumber = 0;%0D
 %0A%0D%0A  public 
@@ -1115,25 +1115,25 @@
 , true %7D, %7B 
-0
+1
 , true %7D,%0D%0A 
@@ -1463,24 +1463,48 @@
 an test) %7B%0D%0A
+    executionNumber++;%0D%0A
     if (paf 
@@ -1678,21 +1678,16 @@
 %7D%0D%0A    %7D
-%0D%0A   
  else if
@@ -1701,10 +1701,10 @@
 = 0 
-%7C%7C
+&&
  tes
@@ -1721,44 +1721,247 @@
  %7B%0D%0A
-      Assert.assertTrue(true);%0D%0A    
+%09%09%09Assert.assertTrue(true);%0D%0A%09%09%09Assert.assertEquals(executionNumber, 3);%0D%0A%09%09%7D else if (paf == 1 && test == true) %7B%0D%0A%09%09%09Assert.assertTrue(true);%0D%0A%09%09%09// Enforce no duplicate correct executions%0D%0A%09%09%09Assert.assertEquals(executionNumber, 4);%0D%0A%09%09
 %7D%0D%0A 

Update RetryAnalyzerTest.java