Home | History | Annotate | Download | only in retryAnalyzer
      1 package test.retryAnalyzer;
      2 
      3 import static org.testng.Assert.assertEquals;
      4 import static org.testng.Assert.fail;
      5 
      6 import java.util.concurrent.atomic.AtomicInteger;
      7 
      8 import org.testng.IRetryAnalyzer;
      9 import org.testng.ITestResult;
     10 import org.testng.TestNG;
     11 import org.testng.annotations.DataProvider;
     12 import org.testng.annotations.Test;
     13 
     14 import com.google.common.collect.ConcurrentHashMultiset;
     15 import com.google.common.collect.Multiset;
     16 
     17 /**
     18  * retryAnalyzer parameter unit tests.
     19  * @author tocman (at) gmail.com (Jeremie Lenfant-Engelmann)
     20  *
     21  */
     22 public final class InvocationCountTest implements IRetryAnalyzer {
     23   static final Multiset<String> invocations = ConcurrentHashMultiset.create();
     24   private static final AtomicInteger retriesRemaining = new AtomicInteger(100);
     25 
     26   private int r1 = 0;
     27   private int r2 = 0;
     28   private int r3 = 0;
     29   private int r7 = 0;
     30   private static int value = 42;
     31 
     32   @Test(retryAnalyzer = InvocationCountTest.class)
     33   public void testAnnotationWithNoRetries() {
     34   }
     35 
     36   @Test(retryAnalyzer = InvocationCountTest.class)
     37   public void testAnnotationWithOneRetry() {
     38     if (r1++ < 1) {
     39       fail();
     40     }
     41   }
     42 
     43   @Test(retryAnalyzer = InvocationCountTest.class)
     44   public void testAnnotationWithSevenRetries() {
     45     if (r7++ < 7) {
     46       fail();
     47     }
     48   }
     49 
     50   @Test(retryAnalyzer = ThreeRetries.class, successPercentage = 0)
     51   public void failAfterThreeRetries() {
     52     fail();
     53   }
     54 
     55   @Test(dependsOnMethods = {
     56       "testAnnotationWithNoRetries",
     57       "testAnnotationWithOneRetry",
     58       "testAnnotationWithSevenRetries",
     59       "failAfterThreeRetries"
     60   }, alwaysRun = true)
     61   public void checkInvocationCounts() {
     62     assertEquals(invocations.count("testAnnotationWithNoRetries"), 0);
     63     assertEquals(invocations.count("testAnnotationWithOneRetry"), 1);
     64     assertEquals(invocations.count("testAnnotationWithSevenRetries"), 7);
     65     assertEquals(invocations.count("failAfterThreeRetries"), 4);
     66   }
     67 
     68   @DataProvider(name="dataProvider")
     69   private Object[][] dataProvider() {
     70     return new Object[][] { { 1, false }, { 0, true }, { 0, true },
     71         { 1, false } };
     72   }
     73 
     74   @DataProvider(name="dataProvider2")
     75   private Object[][] dataProvider2() {
     76     value = 42;
     77 
     78     return new Object[][] { { true }, { true } };
     79   }
     80 
     81   @Test(retryAnalyzer = InvocationCountTest.class, dataProvider = "dataProvider")
     82   public void testAnnotationWithDataProvider(int paf, boolean test) {
     83     if (paf == 1 && test == false) {
     84       if (r2 >= 1) {
     85         r2--;
     86         fail();
     87       }
     88     }
     89   }
     90 
     91   @Test(retryAnalyzer = InvocationCountTest.class, dataProvider = "dataProvider2")
     92   public void testAnnotationWithDataProviderAndRecreateParameters(boolean dummy) {
     93     if (r3 == 1) {
     94       this.value = 0;
     95       r3--;
     96       fail();
     97     } else if (r3 == 0) {
     98       assertEquals(this.value, 42);
     99     }
    100   }
    101 
    102   @Test
    103   public void withFactory() {
    104     TestNG tng = new TestNG();
    105     tng.setVerbose(0);
    106     tng.setTestClasses(new Class[] { MyFactory.class});
    107     FactoryTest.m_count = 0;
    108 
    109     tng.run();
    110 
    111     assertEquals(FactoryTest.m_count, 4);
    112   }
    113 
    114   @Override
    115   public boolean retry(ITestResult result) {
    116     invocations.add(result.getName());
    117     return retriesRemaining.getAndDecrement() >= 0;
    118   }
    119 
    120   public static class ThreeRetries implements IRetryAnalyzer {
    121     private final AtomicInteger remainingRetries = new AtomicInteger(3);
    122 
    123     @Override
    124     public boolean retry(ITestResult result) {
    125       invocations.add(result.getName());
    126       return remainingRetries.getAndDecrement() > 0;
    127     }
    128   }
    129 }
    130