Skip to content

Commit

Permalink
feat: maven plugin to automatically generate a properties file (#674)
Browse files Browse the repository at this point in the history
* fix: clean the state and enable comment

* feat: new maven plugin to generate a properties file

* pom: remove old github repository

* install testrunner

* feat: the generator set up the filter

* ci: disable dspot-maven command line
  • Loading branch information
danglotb authored Jan 21, 2019
1 parent 19c0c5f commit 3fed66b
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ env:
- SCRIPT=travis-checkstyle.sh
- SCRIPT=travis-dhell.sh
- SCRIPT=travis-ci-xwiki.sh
- SCRIPT=travis-dspot-maven-cmd-line.sh
# - SCRIPT=travis-dspot-maven-cmd-line.sh

cache:
directories:
Expand Down
5 changes: 5 additions & 0 deletions .travis/travis-dspot-maven-cmd-line.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

DSPOT_VERSION=${1}

git clone https://github.com/STAMP-project/testrunner.git
cd testrunner
mvn install -DskipTests
cd ..

cd dspot-maven/src/test/resources/multi-module
mvn eu.stamp-project:dspot-maven:${DSPOT_VERSION}:amplify-unit-tests -Dverbose -Dpath-to-properties=multi-module.properties -Damplifiers=TestDataMutator -Diteration=1 -Dtest=example.TestSuiteExample -Dtest-criterion=JacocoCoverageSelector

Expand Down
2 changes: 0 additions & 2 deletions dspot-maven/src/main/java/eu/stamp_project/DSpotMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
@Mojo(name = "amplify-unit-tests", defaultPhase = LifecyclePhase.VERIFY, requiresDependencyResolution = ResolutionScope.TEST)
public class DSpotMojo extends AbstractMojo {

private static final Logger LOGGER = LoggerFactory.getLogger(DSpotMojo.class);

/**
* [optional] specify the path to the configuration file (format Java properties) of the target project (e.g. ./foo.properties).
*/
Expand Down
105 changes: 105 additions & 0 deletions dspot-maven/src/main/java/eu/stamp_project/GeneratePropertiesMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package eu.stamp_project;

import eu.stamp_project.utils.DSpotUtils;
import eu.stamp_project.utils.program.ConstantsProperties;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

/**
* created by Benjamin DANGLOT
* benjamin.danglot@inria.fr
* on 21/01/19
* <p>
* This maven plugin will generate a default properties with minimal values to use dspot
*/
@Mojo(name = "generate-properties")
public class GeneratePropertiesMojo extends AbstractMojo {

private final String COMMENT = "This properties file has been automatically generated using generate-properties of dspot-maven";

/**
* Configure the output path of the generated properties file.
*/
@Parameter(defaultValue = "dspot.properties", property = "output")
private String outputPath;

/**
* Maven project for which we want to produce a properties file.
*/
@Parameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject project;

private Properties properties;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
this.properties = new Properties();
final String rootPath = this.project.getBasedir().getAbsolutePath();
if (this.project.getParent() != null) {
final MavenProject topParent = getTopParent();
final String pathOfTopParent = topParent.getBasedir().getAbsolutePath();
this.properties.put(ConstantsProperties.PROJECT_ROOT_PATH.getName(), pathOfTopParent);
this.properties.put(ConstantsProperties.MODULE.getName(), rootPath);
} else {
this.properties.put(ConstantsProperties.PROJECT_ROOT_PATH.getName(), rootPath);
}
this.properties.put(ConstantsProperties.SRC_CODE.getName(), this.project.getCompileSourceRoots().get(0));
this.properties.put(ConstantsProperties.TEST_SRC_CODE.getName(), this.project.getTestCompileSourceRoots().get(0));
final File testSrcDirectory = new File(this.project.getCompileSourceRoots().get(0));
this.properties.put(ConstantsProperties.FILTER.getName(), buildFilter(testSrcDirectory));
this.output();
}

private String buildFilter(File testSrcDirectory) {
try {
final Path pathToTopPackage = Files.walk(Paths.get(testSrcDirectory.getAbsolutePath()))
.filter(path -> path.toFile().isDirectory())
.filter(path -> path.toFile().listFiles(pathname -> pathname.getAbsolutePath().endsWith(".java")).length > 0)
.findFirst()
.get();
return pathToTopPackage.toFile()
.getAbsolutePath()
.substring(DSpotUtils.shouldAddSeparator.apply(testSrcDirectory.getAbsolutePath()).length())
.replaceAll("/", ".") + "*";
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private MavenProject getTopParent() {
MavenProject parent = this.project.getParent();
while (parent.getParent() != null) {
parent = parent.getParent();
}
return parent;
}

private void output() {
try {
this.properties.store(new FileWriter(this.project.getBasedir().getAbsolutePath() + outputPath), COMMENT);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

// VISIBLE FOR TESTING
String getOutputPath() {
return this.outputPath;
}

MavenProject getProject() {
return this.project;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package eu.stamp_project;

import eu.stamp_project.utils.program.ConstantsProperties;
import org.apache.maven.plugin.testing.MojoRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.File;
import java.io.FileReader;
import java.util.Properties;

import static junit.framework.TestCase.assertTrue;
import static org.codehaus.plexus.PlexusTestCase.getBasedir;

/**
* created by Benjamin DANGLOT
* benjamin.danglot@inria.fr
* on 21/01/19
*/
public class GeneratePropertiesMojoTest {

@Rule
public MojoRule mojoRule = new MojoRule();

/*
The mojo under test
*/
private GeneratePropertiesMojo mojoUnderTest;

@Before
public void setUp() throws Exception {
File testPom = new File(getBasedir(), "src/test/resources/test-projects/");
mojoUnderTest = (GeneratePropertiesMojo) mojoRule.lookupConfiguredMojo(testPom , "generate-properties");
}


@Test
public void testOnTestProjects() throws Exception {
mojoUnderTest.execute();
assertTrue(mojoUnderTest.getProject().getBasedir().getAbsolutePath() + mojoUnderTest.getOutputPath() + " does not exist!",
new File(mojoUnderTest.getProject().getBasedir().getAbsolutePath() + mojoUnderTest.getOutputPath()).exists()
);
}


}
87 changes: 47 additions & 40 deletions dspot-maven/src/test/resources/test-projects/pom.xml
Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>example</groupId>
<artifactId>example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test-projects</name>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>example</groupId>
<artifactId>example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test-projects</name>

<properties>
<default.encoding>UTF-8</default.encoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<properties>
<default.encoding>UTF-8</default.encoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Minimal configuration: specify at least the path to the properties file. -->
<plugin>
<groupId>eu.stamp-project</groupId>
<artifactId>dspot-maven</artifactId>
<version>1.1.1-SNAPSHOT</version>
<configuration>
<pathToProperties>src/test/resources/test-projects/test-projects.properties</pathToProperties>
</configuration>
</plugin>
</plugins>
</build>
<plugin>
<groupId>eu.stamp-project</groupId>
<artifactId>dspot-maven</artifactId>
<version>1.2.2-SNAPSHOT</version>
<executions>
<execution>
<goals>
<goal>amplify-unit-tests</goal>
</goals>
<configuration>
<pathToProperties>src/test/resources/test-projects/test-projects.properties</pathToProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>
8 changes: 0 additions & 8 deletions dspot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
<name>DSpot</name>

<repositories>
<repository>
<id>stamp-maven-repository-mvn-repo</id>
<url>https://stamp-project.github.io/stamp-maven-repository/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
<repository>
<id>gradle-repo</id>
<name>Gradle Tooling API repository</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import eu.stamp_project.testrunner.runner.Failure;
import eu.stamp_project.utils.AmplificationHelper;
import eu.stamp_project.utils.program.InputConfiguration;
import org.junit.Before;
import org.junit.Test;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.declaration.CtClass;
Expand All @@ -25,6 +26,14 @@
*/
public class TestFrameworkTest extends AbstractTest {

@Override
@Before
public void setUp() throws Exception {
Utils.reset();
super.setUp();
InputConfiguration.get().setWithComment(true);
}

@Test
public void testGenerateExpectedExceptionsBlock() {

Expand Down
8 changes: 0 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@
</scm>

<repositories>
<repository>
<id>stamp-maven-repository-mvn-repo</id>
<url>https://stamp-project.github.io/stamp-maven-repository/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
<repository>
<id>gradle-repo</id>
<name>Gradle Tooling API repository</name>
Expand Down

0 comments on commit 3fed66b

Please sign in to comment.