1
package hudson.maven.reporters;
4
import hudson.maven.MojoInfo;
7
import java.util.Collection;
8
import java.util.Collections;
9
import java.util.Iterator;
11
import javax.annotation.CheckForNull;
13
import org.apache.maven.project.MavenProject;
14
import org.apache.tools.ant.types.FileSet;
15
import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
17
import com.google.common.base.Function;
18
import com.google.common.collect.Iterators;
21
* Description of a mojo which can run tests.
28
* Fallback to this if we have no exact match
30
FALLBACK("","","","reportsDirectory") {
32
protected boolean is(String artifactId, String groupId, String goal) {
33
// never match anything implicitly
38
MAVEN_SUREFIRE("org.apache.maven.plugins", "maven-surefire-plugin","test","reportsDirectory"),
39
MAVEN_FAILSAFE("org.apache.maven.plugins", "maven-failsafe-plugin", "integration-test","reportsDirectory"),
40
MAVEN_FAILSAFE_B("org.apache.maven.plugins", "maven-failsafe-plugin", "verify","reportsDirectory"),
42
MAVEN_JUNIT("com.sun.maven", "maven-junit-plugin", "test","reportsDirectory"),
43
FLEXMOJOS("org.sonatype.flexmojos", "flexmojos-maven-plugin", "test-run",null),
45
MAVEN_OSGI_TEST("org.sonatype.tycho", "maven-osgi-test-plugin", "test","reportsDirectory"),
46
TYCHO_SUREFIRE("org.eclipse.tycho", "tycho-surefire-plugin", "test","reportsDirectory"),
48
MAVEN_ANDROID_PLUGIN("com.jayway.maven.plugins.android.generation2", "maven-android-plugin",
49
"internal-integration-test",null,"3.0.0-alpha-6"),
50
ANDROID_MAVEN_PLUGIN("com.jayway.maven.plugins.android.generation2", "android-maven-plugin",
51
"internal-integration-test",null,"3.0.0-alpha-6"),
53
GWT_MAVEN_PLUGIN("org.codehaus.mojo", "gwt-maven-plugin", "test","reportsDirectory","1.2"),
55
MAVEN_SOAPUI_PLUGIN("eviware", "maven-soapui-plugin", "test", "outputFolder"),
56
MAVEN_SOAPUI_PRO_PLUGIN("eviware", "maven-soapui-pro-plugin", "test","outputFolder"),
58
JASMINE("com.github.searls","jasmine-maven-plugin","test",null) {
60
public Collection<File> getReportFiles(MavenProject pom,MojoInfo mojo)
61
throws ComponentConfigurationException {
62
// jasmine just creates a single JUnit result file
63
File reportsDir = mojo.getConfigurationValue("jasmineTargetDir", File.class);
64
String junitFileName = mojo.getConfigurationValue("junitXmlReportFileName", String.class);
66
if (reportsDir != null && junitFileName != null) {
67
return Collections.singleton(new File(reportsDir,junitFileName));
72
TOOLKIT_RESOLVER_PLUGIN("org.terracotta.maven.plugins", "toolkit-resolver-plugin", "toolkit-resolve-test","reportsDirectory");
74
private String reportDirectoryConfigKey;
76
private String minimalRequiredVersion;
78
private TestMojo(String artifactId, String groupId, String goal,
79
String reportDirectoryConfigKey) {
80
this.key = new Key(artifactId,groupId,goal);
81
this.reportDirectoryConfigKey = reportDirectoryConfigKey;
84
private TestMojo(String artifactId, String groupId, String goal,
85
String reportDirectoryConfigKey,String minimalRequiredVersion) {
86
this.key = new Key(artifactId,groupId,goal);
87
this.reportDirectoryConfigKey = reportDirectoryConfigKey;
88
this.minimalRequiredVersion = minimalRequiredVersion;
96
* Says if this mojo can run tests.
97
* Can e.g. return false if the version of the plugin is too old to create output in JUnit format.
99
public boolean canRunTests(MojoInfo mojo) {
100
if (this.minimalRequiredVersion == null) {
104
return mojo.pluginName.version.compareTo(this.minimalRequiredVersion) >= 0;
107
@CheckForNull public Iterable<File> getReportFiles(MavenProject pom, MojoInfo mojo) throws ComponentConfigurationException {
108
if (this.reportDirectoryConfigKey != null) {
109
File reportsDir = mojo.getConfigurationValue(this.reportDirectoryConfigKey, File.class);
110
if (reportsDir != null && reportsDir.exists()) {
111
return getReportFiles(reportsDir, getFileSet(reportsDir));
116
// some plugins just default to this:
117
File reportsDir = new File(pom.getBuild().getDirectory(), "surefire-reports");
118
if (reportsDir.exists()) {
119
return getReportFiles(reportsDir, getFileSet(reportsDir));
125
private Iterable<File> getReportFiles(final File baseDir, FileSet set) {
126
final String[] includedFiles = set.getDirectoryScanner().getIncludedFiles();
128
return new Iterable<File>() {
129
public Iterator<File> iterator() {
130
return Iterators.transform(
131
Iterators.forArray(includedFiles),
132
new Function<String, File>() {
134
public File apply(String file) {
135
return new File(baseDir,file);
143
* Returns the appropriate FileSet for the selected baseDir
147
private FileSet getFileSet(File baseDir) {
148
return Util.createFileSet(baseDir, "*.xml","testng-results.xml,testng-failed.xml");
151
protected boolean is(String artifactId, String groupId, String goal) {
152
return key.artifactId.equals(artifactId) && key.groupId.equals(groupId)
153
&& key.goal.equals(goal);
156
public static TestMojo lookup(String artifactId, String groupId, String goal) {
157
for (TestMojo mojo : values()) {
158
if (mojo.is(artifactId,groupId,goal)) {
163
if (goal.equals("test") || goal.equals("test-run") || goal.equals("integration-test")) {
170
public static TestMojo lookup(MojoInfo mojo) {
171
TestMojo testMojo = lookup(mojo.pluginName.groupId, mojo.pluginName.artifactId, mojo.getGoal());
172
if (testMojo != null && testMojo.canRunTests(mojo)) {
179
private String artifactId;
180
private String groupId;
183
public Key(String artifactId, String groupId, String goal) {
185
this.artifactId = artifactId;
186
this.groupId = groupId;
191
public int hashCode() {
192
final int prime = 31;
194
result = prime * result
195
+ ((artifactId == null) ? 0 : artifactId.hashCode());
196
result = prime * result + ((goal == null) ? 0 : goal.hashCode());
197
result = prime * result
198
+ ((groupId == null) ? 0 : groupId.hashCode());
202
public boolean equals(Object obj) {
207
if (getClass() != obj.getClass())
209
Key other = (Key) obj;
210
if (artifactId == null) {
211
if (other.artifactId != null)
213
} else if (!artifactId.equals(other.artifactId))
216
if (other.goal != null)
218
} else if (!goal.equals(other.goal))
220
if (groupId == null) {
221
if (other.groupId != null)
223
} else if (!groupId.equals(other.groupId))