File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\maven-plugin\src\main\java\hudson\maven\reporters\TestMojo.java.html

1 package hudson.maven.reporters;
2
3 import hudson.Util;
4 import hudson.maven.MojoInfo;
5
6 import java.io.File;
7 import java.util.Collection;
8 import java.util.Collections;
9 import java.util.Iterator;
10
11 import javax.annotation.CheckForNull;
12
13 import org.apache.maven.project.MavenProject;
14 import org.apache.tools.ant.types.FileSet;
15 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
16
17 import com.google.common.base.Function;
18 import com.google.common.collect.Iterators;
19
20 /**
21 * Description of a mojo which can run tests.
22 *
23 * @author kutzi
24 */
25 enum TestMojo {
26
27 /**
28 * Fallback to this if we have no exact match
29 */
30 FALLBACK("","","","reportsDirectory") {
31 @Override
32 protected boolean is(String artifactId, String groupId, String goal) {
33 // never match anything implicitly
34 return false;
35 }
36 },
37
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"),
41
42 MAVEN_JUNIT("com.sun.maven", "maven-junit-plugin", "test","reportsDirectory"),
43 FLEXMOJOS("org.sonatype.flexmojos", "flexmojos-maven-plugin", "test-run",null),
44
45 MAVEN_OSGI_TEST("org.sonatype.tycho", "maven-osgi-test-plugin", "test","reportsDirectory"),
46 TYCHO_SUREFIRE("org.eclipse.tycho", "tycho-surefire-plugin", "test","reportsDirectory"),
47
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"),
52
53 GWT_MAVEN_PLUGIN("org.codehaus.mojo", "gwt-maven-plugin", "test","reportsDirectory","1.2"),
54
55 MAVEN_SOAPUI_PLUGIN("eviware", "maven-soapui-plugin", "test", "outputFolder"),
56 MAVEN_SOAPUI_PRO_PLUGIN("eviware", "maven-soapui-pro-plugin", "test","outputFolder"),
57
58 JASMINE("com.github.searls","jasmine-maven-plugin","test",null) {
59 @Override
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);
65
66 if (reportsDir != null && junitFileName != null) {
67 return Collections.singleton(new File(reportsDir,junitFileName));
68 }
69 return null;
70 }
71 },
72 TOOLKIT_RESOLVER_PLUGIN("org.terracotta.maven.plugins", "toolkit-resolver-plugin", "toolkit-resolve-test","reportsDirectory");
73
74 private String reportDirectoryConfigKey;
75 private Key key;
76 private String minimalRequiredVersion;
77
78 private TestMojo(String artifactId, String groupId, String goal,
79 String reportDirectoryConfigKey) {
80 this.key = new Key(artifactId,groupId,goal);
81 this.reportDirectoryConfigKey = reportDirectoryConfigKey;
82 }
83
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;
89 }
90
91 public Key getKey() {
92 return this.key;
93 }
94
95 /**
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.
98 */
99 public boolean canRunTests(MojoInfo mojo) {
100 if (this.minimalRequiredVersion == null) {
101 return true;
102 }
103
104 return mojo.pluginName.version.compareTo(this.minimalRequiredVersion) >= 0;
105 }
106
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));
112 }
113
114 }
115
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));
120 }
121
122 return null;
123 }
124
125 private Iterable<File> getReportFiles(final File baseDir, FileSet set) {
126 final String[] includedFiles = set.getDirectoryScanner().getIncludedFiles();
127
128 return new Iterable<File>() {
129 public Iterator<File> iterator() {
130 return Iterators.transform(
131 Iterators.forArray(includedFiles),
132 new Function<String, File>() {
133 @Override
134 public File apply(String file) {
135 return new File(baseDir,file);
136 }
137 });
138 }
139 };
140 }
141
142 /**
143 * Returns the appropriate FileSet for the selected baseDir
144 * @param baseDir
145 * @return
146 */
147 private FileSet getFileSet(File baseDir) {
148 return Util.createFileSet(baseDir, "*.xml","testng-results.xml,testng-failed.xml");
149 }
150
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);
154 }
155
156 public static TestMojo lookup(String artifactId, String groupId, String goal) {
157 for (TestMojo mojo : values()) {
158 if (mojo.is(artifactId,groupId,goal)) {
159 return mojo;
160 }
161 }
162
163 if (goal.equals("test") || goal.equals("test-run") || goal.equals("integration-test")) {
164 return FALLBACK;
165 }
166
167 return null;
168 }
169
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)) {
173 return testMojo;
174 }
175 return null;
176 }
177
178 static class Key {
179 private String artifactId;
180 private String groupId;
181 private String goal;
182
183 public Key(String artifactId, String groupId, String goal) {
184 super();
185 this.artifactId = artifactId;
186 this.groupId = groupId;
187 this.goal = goal;
188 }
189
190 @Override
191 public int hashCode() {
192 final int prime = 31;
193 int result = 1;
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());
199 return result;
200 }
201 @Override
202 public boolean equals(Object obj) {
203 if (this == obj)
204 return true;
205 if (obj == null)
206 return false;
207 if (getClass() != obj.getClass())
208 return false;
209 Key other = (Key) obj;
210 if (artifactId == null) {
211 if (other.artifactId != null)
212 return false;
213 } else if (!artifactId.equals(other.artifactId))
214 return false;
215 if (goal == null) {
216 if (other.goal != null)
217 return false;
218 } else if (!goal.equals(other.goal))
219 return false;
220 if (groupId == null) {
221 if (other.groupId != null)
222 return false;
223 } else if (!groupId.equals(other.groupId))
224 return false;
225 return true;
226 }
227 }
228
229 }
230