Skip to content

Commit 3fed66b

Browse files
authored
feat: maven plugin to automatically generate a properties file (#674)
* 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
1 parent 19c0c5f commit 3fed66b

File tree

9 files changed

+214
-59
lines changed

9 files changed

+214
-59
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ env:
1717
- SCRIPT=travis-checkstyle.sh
1818
- SCRIPT=travis-dhell.sh
1919
- SCRIPT=travis-ci-xwiki.sh
20-
- SCRIPT=travis-dspot-maven-cmd-line.sh
20+
# - SCRIPT=travis-dspot-maven-cmd-line.sh
2121

2222
cache:
2323
directories:

.travis/travis-dspot-maven-cmd-line.sh

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
DSPOT_VERSION=${1}
44

5+
git clone https://github.com/STAMP-project/testrunner.git
6+
cd testrunner
7+
mvn install -DskipTests
8+
cd ..
9+
510
cd dspot-maven/src/test/resources/multi-module
611
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
712

dspot-maven/src/main/java/eu/stamp_project/DSpotMojo.java

-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
@Mojo(name = "amplify-unit-tests", defaultPhase = LifecyclePhase.VERIFY, requiresDependencyResolution = ResolutionScope.TEST)
3232
public class DSpotMojo extends AbstractMojo {
3333

34-
private static final Logger LOGGER = LoggerFactory.getLogger(DSpotMojo.class);
35-
3634
/**
3735
* [optional] specify the path to the configuration file (format Java properties) of the target project (e.g. ./foo.properties).
3836
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package eu.stamp_project;
2+
3+
import eu.stamp_project.utils.DSpotUtils;
4+
import eu.stamp_project.utils.program.ConstantsProperties;
5+
import org.apache.maven.plugin.AbstractMojo;
6+
import org.apache.maven.plugin.MojoExecutionException;
7+
import org.apache.maven.plugin.MojoFailureException;
8+
import org.apache.maven.plugins.annotations.Mojo;
9+
import org.apache.maven.plugins.annotations.Parameter;
10+
import org.apache.maven.project.MavenProject;
11+
12+
import java.io.File;
13+
import java.io.FileWriter;
14+
import java.io.IOException;
15+
import java.nio.file.Files;
16+
import java.nio.file.Path;
17+
import java.nio.file.Paths;
18+
import java.util.Properties;
19+
20+
/**
21+
* created by Benjamin DANGLOT
22+
* benjamin.danglot@inria.fr
23+
* on 21/01/19
24+
* <p>
25+
* This maven plugin will generate a default properties with minimal values to use dspot
26+
*/
27+
@Mojo(name = "generate-properties")
28+
public class GeneratePropertiesMojo extends AbstractMojo {
29+
30+
private final String COMMENT = "This properties file has been automatically generated using generate-properties of dspot-maven";
31+
32+
/**
33+
* Configure the output path of the generated properties file.
34+
*/
35+
@Parameter(defaultValue = "dspot.properties", property = "output")
36+
private String outputPath;
37+
38+
/**
39+
* Maven project for which we want to produce a properties file.
40+
*/
41+
@Parameter(defaultValue = "${project}", required = true, readonly = true)
42+
private MavenProject project;
43+
44+
private Properties properties;
45+
46+
@Override
47+
public void execute() throws MojoExecutionException, MojoFailureException {
48+
this.properties = new Properties();
49+
final String rootPath = this.project.getBasedir().getAbsolutePath();
50+
if (this.project.getParent() != null) {
51+
final MavenProject topParent = getTopParent();
52+
final String pathOfTopParent = topParent.getBasedir().getAbsolutePath();
53+
this.properties.put(ConstantsProperties.PROJECT_ROOT_PATH.getName(), pathOfTopParent);
54+
this.properties.put(ConstantsProperties.MODULE.getName(), rootPath);
55+
} else {
56+
this.properties.put(ConstantsProperties.PROJECT_ROOT_PATH.getName(), rootPath);
57+
}
58+
this.properties.put(ConstantsProperties.SRC_CODE.getName(), this.project.getCompileSourceRoots().get(0));
59+
this.properties.put(ConstantsProperties.TEST_SRC_CODE.getName(), this.project.getTestCompileSourceRoots().get(0));
60+
final File testSrcDirectory = new File(this.project.getCompileSourceRoots().get(0));
61+
this.properties.put(ConstantsProperties.FILTER.getName(), buildFilter(testSrcDirectory));
62+
this.output();
63+
}
64+
65+
private String buildFilter(File testSrcDirectory) {
66+
try {
67+
final Path pathToTopPackage = Files.walk(Paths.get(testSrcDirectory.getAbsolutePath()))
68+
.filter(path -> path.toFile().isDirectory())
69+
.filter(path -> path.toFile().listFiles(pathname -> pathname.getAbsolutePath().endsWith(".java")).length > 0)
70+
.findFirst()
71+
.get();
72+
return pathToTopPackage.toFile()
73+
.getAbsolutePath()
74+
.substring(DSpotUtils.shouldAddSeparator.apply(testSrcDirectory.getAbsolutePath()).length())
75+
.replaceAll("/", ".") + "*";
76+
} catch (Exception e) {
77+
throw new RuntimeException(e);
78+
}
79+
}
80+
81+
private MavenProject getTopParent() {
82+
MavenProject parent = this.project.getParent();
83+
while (parent.getParent() != null) {
84+
parent = parent.getParent();
85+
}
86+
return parent;
87+
}
88+
89+
private void output() {
90+
try {
91+
this.properties.store(new FileWriter(this.project.getBasedir().getAbsolutePath() + outputPath), COMMENT);
92+
} catch (IOException e) {
93+
throw new RuntimeException(e);
94+
}
95+
}
96+
97+
// VISIBLE FOR TESTING
98+
String getOutputPath() {
99+
return this.outputPath;
100+
}
101+
102+
MavenProject getProject() {
103+
return this.project;
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package eu.stamp_project;
2+
3+
import eu.stamp_project.utils.program.ConstantsProperties;
4+
import org.apache.maven.plugin.testing.MojoRule;
5+
import org.junit.Before;
6+
import org.junit.Rule;
7+
import org.junit.Test;
8+
9+
import java.io.File;
10+
import java.io.FileReader;
11+
import java.util.Properties;
12+
13+
import static junit.framework.TestCase.assertTrue;
14+
import static org.codehaus.plexus.PlexusTestCase.getBasedir;
15+
16+
/**
17+
* created by Benjamin DANGLOT
18+
* benjamin.danglot@inria.fr
19+
* on 21/01/19
20+
*/
21+
public class GeneratePropertiesMojoTest {
22+
23+
@Rule
24+
public MojoRule mojoRule = new MojoRule();
25+
26+
/*
27+
The mojo under test
28+
*/
29+
private GeneratePropertiesMojo mojoUnderTest;
30+
31+
@Before
32+
public void setUp() throws Exception {
33+
File testPom = new File(getBasedir(), "src/test/resources/test-projects/");
34+
mojoUnderTest = (GeneratePropertiesMojo) mojoRule.lookupConfiguredMojo(testPom , "generate-properties");
35+
}
36+
37+
38+
@Test
39+
public void testOnTestProjects() throws Exception {
40+
mojoUnderTest.execute();
41+
assertTrue(mojoUnderTest.getProject().getBasedir().getAbsolutePath() + mojoUnderTest.getOutputPath() + " does not exist!",
42+
new File(mojoUnderTest.getProject().getBasedir().getAbsolutePath() + mojoUnderTest.getOutputPath()).exists()
43+
);
44+
}
45+
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,53 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0"
2-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
5-
<groupId>example</groupId>
6-
<artifactId>example</artifactId>
7-
<version>0.0.1-SNAPSHOT</version>
8-
<name>test-projects</name>
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>example</groupId>
6+
<artifactId>example</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>test-projects</name>
99

10-
<properties>
11-
<default.encoding>UTF-8</default.encoding>
12-
<maven.compiler.source>1.7</maven.compiler.source>
13-
<maven.compiler.target>1.7</maven.compiler.target>
14-
</properties>
10+
<properties>
11+
<default.encoding>UTF-8</default.encoding>
12+
<maven.compiler.source>1.7</maven.compiler.source>
13+
<maven.compiler.target>1.7</maven.compiler.target>
14+
</properties>
1515

16-
<build>
17-
<plugins>
18-
<plugin>
19-
<groupId>org.apache.maven.plugins</groupId>
20-
<artifactId>maven-compiler-plugin</artifactId>
21-
<version>3.7.0</version>
22-
<configuration>
23-
<source>1.8</source>
24-
<target>1.8</target>
25-
</configuration>
26-
</plugin>
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-compiler-plugin</artifactId>
21+
<version>3.7.0</version>
22+
<configuration>
23+
<source>1.8</source>
24+
<target>1.8</target>
25+
</configuration>
26+
</plugin>
2727
<!-- Minimal configuration: specify at least the path to the properties file. -->
28-
<plugin>
29-
<groupId>eu.stamp-project</groupId>
30-
<artifactId>dspot-maven</artifactId>
31-
<version>1.1.1-SNAPSHOT</version>
32-
<configuration>
33-
<pathToProperties>src/test/resources/test-projects/test-projects.properties</pathToProperties>
34-
</configuration>
35-
</plugin>
36-
</plugins>
37-
</build>
28+
<plugin>
29+
<groupId>eu.stamp-project</groupId>
30+
<artifactId>dspot-maven</artifactId>
31+
<version>1.2.2-SNAPSHOT</version>
32+
<executions>
33+
<execution>
34+
<goals>
35+
<goal>amplify-unit-tests</goal>
36+
</goals>
37+
<configuration>
38+
<pathToProperties>src/test/resources/test-projects/test-projects.properties</pathToProperties>
39+
</configuration>
40+
</execution>
41+
</executions>
42+
</plugin>
43+
</plugins>
44+
</build>
3845

39-
<dependencies>
40-
<dependency>
41-
<groupId>junit</groupId>
42-
<artifactId>junit</artifactId>
43-
<version>4.11</version>
44-
</dependency>
45-
</dependencies>
46+
<dependencies>
47+
<dependency>
48+
<groupId>junit</groupId>
49+
<artifactId>junit</artifactId>
50+
<version>4.11</version>
51+
</dependency>
52+
</dependencies>
4653
</project>

dspot/pom.xml

-8
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@
1414
<name>DSpot</name>
1515

1616
<repositories>
17-
<repository>
18-
<id>stamp-maven-repository-mvn-repo</id>
19-
<url>https://stamp-project.github.io/stamp-maven-repository/</url>
20-
<snapshots>
21-
<enabled>true</enabled>
22-
<updatePolicy>always</updatePolicy>
23-
</snapshots>
24-
</repository>
2517
<repository>
2618
<id>gradle-repo</id>
2719
<name>Gradle Tooling API repository</name>

dspot/src/test/java/eu/stamp_project/test_framework/TestFrameworkTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import eu.stamp_project.testrunner.runner.Failure;
77
import eu.stamp_project.utils.AmplificationHelper;
88
import eu.stamp_project.utils.program.InputConfiguration;
9+
import org.junit.Before;
910
import org.junit.Test;
1011
import spoon.reflect.code.CtInvocation;
1112
import spoon.reflect.declaration.CtClass;
@@ -25,6 +26,14 @@
2526
*/
2627
public class TestFrameworkTest extends AbstractTest {
2728

29+
@Override
30+
@Before
31+
public void setUp() throws Exception {
32+
Utils.reset();
33+
super.setUp();
34+
InputConfiguration.get().setWithComment(true);
35+
}
36+
2837
@Test
2938
public void testGenerateExpectedExceptionsBlock() {
3039

pom.xml

-8
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,6 @@
6262
</scm>
6363

6464
<repositories>
65-
<repository>
66-
<id>stamp-maven-repository-mvn-repo</id>
67-
<url>https://stamp-project.github.io/stamp-maven-repository/</url>
68-
<snapshots>
69-
<enabled>true</enabled>
70-
<updatePolicy>always</updatePolicy>
71-
</snapshots>
72-
</repository>
7365
<repository>
7466
<id>gradle-repo</id>
7567
<name>Gradle Tooling API repository</name>

0 commit comments

Comments
 (0)