All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codacy.scoobydoo.Retry Maven / Gradle / Ivy

There is a newer version: 3.30.0
Show newest version
package com.codacy.scoobydoo;

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;


public class Retry implements IRetryAnalyzer {

    private final int maxTry;
    private int count = 0;

    public Retry() {
        String retryEnvValue = System.getenv("RETRY_NUMBER");
        if (retryEnvValue != null && !retryEnvValue.isBlank()) {
            try {
                maxTry = Integer.parseInt(retryEnvValue);

            } catch (NumberFormatException e) {
                final String errorMessage =
                        "Value [" + retryEnvValue + "] for key 'RETRY_NUMBER' is not valid. It as to be an integer.";
                LoggingHelper.error(errorMessage, e);
                throw e;
            }

        } else {
            maxTry = 0;
        }
    }

    @Override
    public boolean retry(ITestResult result) {

        if(result.isSuccess()) {
            result.setStatus(ITestResult.SUCCESS);
            return false;

        } else if (hasShouldNotBeRetriedAnnotation(result)) {
            return false;

        } else {
            if (count < maxTry) {
                count++;
                return true;
            } else {
                return false;
            }
        }
    }

    private boolean hasShouldNotBeRetriedAnnotation(ITestResult result) {
        return result.getMethod().getConstructorOrMethod().getMethod().isAnnotationPresent(ShouldNotBeRetried.class);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy