aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/GenerateTestDescriptorMojo.java
blob: 73fe19844876ef243e5fe278f6a6f72b641260c4 (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
// Copyright Vespa.ai. 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.api.TestDescriptor;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * Generates a test descriptor file based on content of the compiled test classes
 *
 * @author bjorncs
 */
@Mojo(name = "generateTestDescriptor", threadSafe = true)
public class GenerateTestDescriptorMojo extends AbstractMojo {

    @Parameter(defaultValue = "${project}", readonly = true)
    protected MavenProject project;

    @Override
    public void execute() throws MojoExecutionException {
        TestAnnotationAnalyzer analyzer = new TestAnnotationAnalyzer();
        analyzeTestClasses(analyzer);
        TestDescriptor descriptor = TestDescriptor.from(
                TestDescriptor.CURRENT_VERSION,
                analyzer.systemTests(),
                analyzer.stagingTests(),
                analyzer.stagingSetupTests(),
                analyzer.productionTests());
        writeDescriptorFile(descriptor);
    }

    private void analyzeTestClasses(TestAnnotationAnalyzer analyzer) throws MojoExecutionException {
        if (! Files.exists(testClassesDirectory())) return;

        try (Stream<Path> files = Files.walk(testClassesDirectory())) {
            files
                    .filter(f -> f.toString().endsWith(".class"))
                    .forEach(analyzer::analyzeClass);
        } catch (Exception e) {
            throw new MojoExecutionException("Failed to analyze test classes: " + e.getMessage(), e);
        }
    }

    private void writeDescriptorFile(TestDescriptor descriptor) throws MojoExecutionException {
        try {
            Path descriptorFile = testClassesDirectory().resolve(TestDescriptor.DEFAULT_FILENAME);
            Files.createDirectories(descriptorFile.getParent());
            Files.write(descriptorFile, descriptor.toJson().getBytes(UTF_8));
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to write test descriptor file: " + e.getMessage(), e);
        }
    }

    private Path testClassesDirectory() { return Paths.get(project.getBuild().getTestOutputDirectory()); }
}