aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/main/java/vid/automation/test/infra/SkipTestUntilTestngTransformer.java
blob: 2d2ce7cde19a3605192c6614159135edfadce95b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package vid.automation.test.infra;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.time.LocalDate;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;

/*
TestNg listener that skip tests that are annotated with SkipTestUntil annotation
Pay attention that this listener shall be configured in the testng.xml (or command line)
*/
public class SkipTestUntilTestngTransformer implements IAnnotationTransformer {

    @Override
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {

            if (testMethod!=null) {
                try {

                    if (!annotation.getEnabled()) {
                        return;
                    }

                    if (!testMethod.isAnnotationPresent(SkipTestUntil.class)) {
                        return;
                    }

                    String dateAsStr = testMethod.getAnnotation(SkipTestUntil.class).value();
                    if (shallDisableTest(dateAsStr)) {
                        disableTest(annotation, testMethod.getDeclaringClass().getName(), dateAsStr);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    }

    private boolean shallDisableTest(String dateAsStr) {
        try {
            return LocalDate.now().isBefore(LocalDate.parse(dateAsStr));
        }
        catch (RuntimeException exception) {
            System.out.println("Failure during processing of SkipTestUntil annotation value is " + dateAsStr);
            exception.printStackTrace();
            return false;
        }
    }

    private void disableTest(ITestAnnotation annotation, String name, String dateAsStr) {
        System.out.println("Ignore "+ name+" till "+dateAsStr);
        annotation.setEnabled(false);
    }

}