aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/TestAnnotationAnalyzer.java
blob: 5eb49b3781388e61117ebf9681154cb8e14943a7 (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.hosted.plugin;


import ai.vespa.hosted.cd.ProductionTest;
import ai.vespa.hosted.cd.StagingSetup;
import ai.vespa.hosted.cd.StagingTest;
import ai.vespa.hosted.cd.SystemTest;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

/**
 * Analyzes test classes and tracks all classes containing hosted Vespa test annotations ({@link ai.vespa.hosted.cd}).
 *
 * @author bjorncs
 */
class TestAnnotationAnalyzer {

    private final List<String> systemTests = new ArrayList<>();
    private final List<String> stagingTests = new ArrayList<>();
    private final List<String> stagingSetupTests = new ArrayList<>();
    private final List<String> productionTests = new ArrayList<>();

    List<String> systemTests() { return systemTests; }
    List<String> stagingTests() { return stagingTests; }
    List<String> stagingSetupTests() { return stagingSetupTests; }
    List<String> productionTests() { return productionTests; }

    void analyzeClass(Path classFile) {
        try (InputStream in = Files.newInputStream(classFile)) {
            new ClassReader(in).accept(new AsmClassVisitor(), ClassReader.SKIP_DEBUG);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    private class AsmClassVisitor extends ClassVisitor {

        private String className;

        AsmClassVisitor() { super(Opcodes.ASM7); }

        @Override
        public void visit(
                int version, int access, String name, String signature, String superName, String[] interfaces) {
            Type type = Type.getObjectType(name);
            if (type.getSort() == Type.OBJECT) {
                this.className = type.getClassName();
                super.visit(version, access, name, signature, superName, interfaces);
            }
        }

        @Override
        public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
            String annotationClassName = Type.getType(descriptor).getClassName();
            if (ProductionTest.class.getName().equals(annotationClassName)) {
                productionTests.add(className);
            } else if (StagingTest.class.getName().equals(annotationClassName)) {
                stagingTests.add(className);
            } else if (StagingSetup.class.getName().equals(annotationClassName)) {
                stagingSetupTests.add(className);
            } else if (SystemTest.class.getName().equals(annotationClassName)) {
                systemTests.add(className);
            }
            return null;
        }
    }
}