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

org.honton.chas.testpojo.TestPojoMojo Maven / Gradle / Ivy

The newest version!
package org.honton.chas.testpojo;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * test pojo setters, getters, equals, hashCode
 */
@Mojo(name = "test", defaultPhase = LifecyclePhase.TEST, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
public class TestPojoMojo extends AbstractMojo {

    @Parameter(property = "project.runtimeClasspathElements", readonly = true)
    private List runtimeScope;

    @Parameter(property = "project.compileClasspathElements", readonly = true)
    private List compileScope;

    @Parameter(property = "project.build.outputDirectory", readonly = true)
    private String outputDirectory;

    @Parameter(property = "project.build.directory", readonly = true)
    private String buildDirectory;

    @Parameter(property = "project.model.properties", readonly = true)
    private Map properties;

    /**
     * Skip executing this plugin
     *
     * @since 0.0.9
     */
    @Parameter(defaultValue = "${skipTests}", property = "testpojo.skip")
    private boolean skip;

    @Override
    public void execute() throws MojoExecutionException {
        if(skip) {
            getLog().info("skipping testpojo execution");
            return;
        }
        if(!new File(buildDirectory).isDirectory()) {
            getLog().info("No classes, skipping");
            return;
        }
        String argLine = properties.get("argLine");
        if (argLine == null) {
            getLog().info("No argLine specifying javaagent - jacoco coverage may not be effective");
        }
        List arguments = replaceProperties(argLine);

        try {
            File jarFile = new File(buildDirectory, "testPojo.jar");
            new BuildExecJar(jarFile, Main.class.getCanonicalName())
                .buildJar(getClassPath());

            JavaProcess proc = new JavaProcess(getLog());
            arguments.add("-jar");
            arguments.add(jarFile.getAbsolutePath());
            proc.setJavaArgs(arguments);
            proc.setCmdArgs(Arrays.asList(outputDirectory, createDependencyFile()));

            int errors = proc.execute();
            if (errors > 0) {
                getLog().info(errors + " pojos had errors");
            }
        } catch (MojoExecutionException ex) {
            throw ex;
        }
        catch(Exception ex) {
            throw new MojoExecutionException(ex.getMessage(), ex);
        }
    }

    private String createDependencyFile() throws IOException {
        // This temporary file makes the plugin not thread-safe
        File dependencyFile = new File(buildDirectory, "testPojo.dependencies");
        BufferedWriter dependencies = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dependencyFile), StandardCharsets.UTF_8));
        try {
            write(dependencies, runtimeScope);
            write(dependencies, compileScope);
        }
        finally {
            dependencies.close();
        }
        return dependencyFile.getAbsolutePath();
    }

    private static void write(BufferedWriter dependencies, List dependencyScope) throws IOException {
        for(String dependency : dependencyScope) {
            dependencies.write(dependency);
            dependencies.newLine();
        }
    }

    private Set getClassPath() throws URISyntaxException {
        Set classPath = new HashSet<>();
        URLClassLoader ucl = (URLClassLoader) getClass().getClassLoader();
        for(URL url : ucl.getURLs()) {
            classPath.add(url.toURI().getPath());
        }
        return classPath;
    }

    private static final Pattern ARG_PATTERN = Pattern.compile("@\\{([^}]+)\\}");

    private List replaceProperties(String argLine) {
        List params = new ArrayList<>();
        if (argLine != null) {
            for (String param : argLine.split(" ")) {
                String property = replaceProperty(param);
                if (!property.isEmpty()) {
                    params.add(property);
                }
            }
        }
        return params;
    }

    private String replaceProperty(String argLine) {
        StringBuilder sb = new StringBuilder();
        int start = 0;
        for (Matcher m = ARG_PATTERN.matcher(argLine); m.find();) {
            String value = properties.get(m.group(1));
            if (value != null) {
                sb.append(argLine, start, m.start());
                sb.append(value);
                start = m.end();
            }
        }
        if (start != 0) {
            return sb.append(argLine.substring(start)).toString();
        }
        return argLine;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy