001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.geronimo.mavenplugins.testsuite;
019:
020: import java.io.File;
021: import java.io.IOException;
022:
023: import java.util.ArrayList;
024:
025: import org.codehaus.mojo.pluginsupport.MojoSupport;
026: import org.codehaus.mojo.pluginsupport.ant.AntHelper;
027:
028: import org.apache.maven.project.MavenProject;
029:
030: import org.apache.maven.plugin.MojoExecutionException;
031: import org.apache.maven.plugin.MojoFailureException;
032: import org.codehaus.plexus.util.FileUtils;
033:
034: import org.apache.tools.ant.Project;
035: import org.apache.tools.ant.taskdefs.Property;
036: import org.apache.tools.ant.taskdefs.XmlProperty;
037:
038: /**
039: * Create a surefire xml file by concatenating the surefire xmls of the child poms that are invoked using maven-maven-plugin:invoke
040: *
041: * @goal generate-surefire-xml
042: *
043: * @version $Rev: 582367 $ $Date: 2007-10-05 11:11:01 -0700 (Fri, 05 Oct 2007) $
044: */
045: public class SurefireXMLGeneratorMojo extends MojoSupport {
046: /**
047: * @component
048: */
049: protected AntHelper ant;
050:
051: /**
052: * @parameter default-value="${project.build.directory}"
053: * @read-only
054: */
055: private File currentBuildDirectory;
056:
057: private File currentReportsDirectory;
058:
059: /**
060: * @parameter default-value="${project.basedir}"
061: * @read-only
062: */
063: private File currentBaseDirectory;
064:
065: /**
066: * @parameter default-value="${project.parent.basedir}/target"
067: * @read-only
068: */
069: private File parentBuildDirectory;
070:
071: private File parentReportsDirectory;
072:
073: /**
074: * Sometimes it is necessary to generate the surefire xml in a grand parent project because
075: * the current pom was not invoked by the maven-maven-plugin by a parent project.
076: * Such a parent project whose packaging is set to pom will not transfer the surefire data to it's parent.
077: * So we will directly write to the grandparent's surefire-reports dir.
078: *
079: * @parameter default-value="false"
080: */
081: private boolean grandParent;
082:
083: //
084: // MojoSupport Hooks
085: //
086:
087: /**
088: * The maven project.
089: *
090: * @parameter expression="${project}"
091: * @required
092: * @readonly
093: */
094: protected MavenProject project = null;
095:
096: protected MavenProject getProject() {
097: return project;
098: }
099:
100: protected void init() throws MojoExecutionException,
101: MojoFailureException {
102: super .init();
103:
104: ant.setProject(getProject());
105:
106: currentReportsDirectory = new File(currentBuildDirectory,
107: "surefire-reports");
108: if (grandParent) {
109: log.debug(getProject().getParent().getParent()
110: .getArtifactId()
111: + " -- "
112: + getProject().getParent().getParent().getBasedir()
113: .getAbsolutePath());
114: parentBuildDirectory = new File(getProject().getParent()
115: .getParent().getBasedir(), "target");
116: }
117: parentReportsDirectory = new File(parentBuildDirectory,
118: "surefire-reports");
119: log.info("Updating directory: "
120: + parentReportsDirectory.getAbsolutePath());
121: }
122:
123: protected void doExecute() throws Exception {
124: if (!currentReportsDirectory.exists()) {
125: log.info("No surefire-reports directory here");
126: return;
127: }
128:
129: String parent_tests = "0";
130: String parent_skipped = "0";
131: String parent_errors = "0";
132: String parent_failures = "0";
133: String parent_time = "0";
134:
135: String artifactName = FileUtils.filename(currentBaseDirectory
136: .getAbsolutePath());
137: if (!parentReportsDirectory.exists()) {
138: parentReportsDirectory.mkdirs();
139: }
140: File parentSurefireXMLFile = new File(parentReportsDirectory,
141: "TEST-" + artifactName + ".xml");
142: if (parentSurefireXMLFile.exists()) {
143: parentSurefireXMLFile.delete();
144: }
145:
146: if (grandParent) {
147: artifactName = project.getParent().getBasedir().getName()
148: + "@" + artifactName;
149: }
150:
151: ArrayList xmlFiles = (ArrayList) FileUtils.getFiles(
152: currentReportsDirectory, "TEST*.xml", null);
153: for (int i = 0; i < xmlFiles.size(); i++) {
154: File xmlFile = (File) xmlFiles.get(i);
155: log.info("Loading surefire xml for xmlproperty: "
156: + xmlFile.getAbsolutePath());
157:
158: String prefix = String.valueOf(System.currentTimeMillis());
159: loadXMLProperty(xmlFile, prefix);
160:
161: String tests = ant.getAnt().getProperty(
162: prefix + ".testsuite.tests");
163: String skipped = ant.getAnt().getProperty(
164: prefix + ".testsuite.skipped");
165: String errors = ant.getAnt().getProperty(
166: prefix + ".testsuite.errors");
167: String failures = ant.getAnt().getProperty(
168: prefix + ".testsuite.failures");
169: String time = ant.getAnt().getProperty(
170: prefix + ".testsuite.time");
171: log.debug("tests=" + tests + "; skipped=" + skipped
172: + ", errors=" + errors + ", failures=" + failures
173: + ", time=" + time);
174:
175: if (parentSurefireXMLFile.exists()) {
176: log
177: .info("Loading parent surefire xml for xmlproperty: "
178: + parentSurefireXMLFile
179: .getAbsolutePath());
180: String parentPrefix = "parent" + prefix;
181: loadXMLProperty(parentSurefireXMLFile, parentPrefix);
182:
183: parent_tests = ant.getAnt().getProperty(
184: parentPrefix + ".testsuite.tests");
185: parent_skipped = ant.getAnt().getProperty(
186: parentPrefix + ".testsuite.skipped");
187: parent_errors = ant.getAnt().getProperty(
188: parentPrefix + ".testsuite.errors");
189: parent_failures = ant.getAnt().getProperty(
190: parentPrefix + ".testsuite.failures");
191: parent_time = ant.getAnt().getProperty(
192: parentPrefix + ".testsuite.time");
193: log.debug("tests=" + parent_tests + "; skipped="
194: + parent_skipped + ", errors=" + parent_errors
195: + ", failures=" + parent_failures + ", time="
196: + parent_time);
197: }
198:
199: int testsNum = Integer.parseInt(parent_tests)
200: + Integer.parseInt(tests);
201: int skippedNum = Integer.parseInt(parent_skipped)
202: + Integer.parseInt(skipped);
203: int errorsNum = Integer.parseInt(parent_errors)
204: + Integer.parseInt(errors);
205: int failuresNum = Integer.parseInt(parent_failures)
206: + Integer.parseInt(failures);
207: float timeNum = getTime(parent_time) + getTime(time);
208:
209: writeParentXML(testsNum, skippedNum, errorsNum,
210: failuresNum, timeNum, artifactName,
211: parentSurefireXMLFile);
212: }
213: }
214:
215: private float getTime(String time) {
216: time = time.replaceAll(",", "");
217: return Float.parseFloat(time);
218: }
219:
220: /**
221: * http://ant.apache.org/manual/CoreTasks/xmlproperty.html
222: */
223: private void loadXMLProperty(File src, String prefix) {
224: XmlProperty xmlProperty = (XmlProperty) ant
225: .createTask("xmlproperty");
226: xmlProperty.setFile(src);
227: if (prefix != null) {
228: xmlProperty.setPrefix(prefix);
229: }
230: xmlProperty.setCollapseAttributes(true);
231: xmlProperty.execute();
232: }
233:
234: /**
235: * (over)writes the surefire xml file in the parent's surefire-reports dir
236: */
237: private void writeParentXML(int testsNum, int skippedNum,
238: int errorsNum, int failuresNum, float timeNum,
239: String artifactName, File parentSurefireXMLFile)
240: throws IOException {
241:
242: final String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
243: String testSuite = "<testsuite errors=\"" + errorsNum
244: + "\" skipped=\"" + skippedNum + "\" tests=\""
245: + testsNum + "\" time=\"" + timeNum + "\" failures=\""
246: + failuresNum + "\" name=\"" + artifactName + "#.\"/>";
247:
248: String parentSurefireXMLFileName = parentSurefireXMLFile
249: .getAbsolutePath();
250:
251: log.debug(testSuite);
252:
253: FileUtils.fileWrite(parentSurefireXMLFileName, header + "\n"
254: + testSuite);
255:
256: return;
257: }
258: }
|