KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > mavenplugins > testsuite > SurefireXMLGeneratorMojo


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */

19
20 package org.apache.geronimo.mavenplugins.testsuite;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import java.util.ArrayList JavaDoc;
26
27 import org.apache.geronimo.genesis.MojoSupport;
28 import org.apache.geronimo.genesis.ant.AntHelper;
29
30 import org.apache.maven.project.MavenProject;
31
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.MojoFailureException;
34 import org.codehaus.plexus.util.FileUtils;
35
36 import org.apache.tools.ant.Project;
37 import org.apache.tools.ant.taskdefs.Property;
38 import org.apache.tools.ant.taskdefs.XmlProperty;
39
40 /**
41  * Create a surefire xml file by concatenating the surefire xmls of the child poms that are invoked using maven-maven-plugin:invoke
42  *
43  * @goal generate-surefire-xml
44  *
45  * @version $Rev: 477727 $ $Date: 2006-11-21 11:17:35 -0500 (Tue, 21 Nov 2006) $
46  */

47 public class SurefireXMLGeneratorMojo
48 extends MojoSupport
49 {
50     /**
51      * @component
52      */

53     protected AntHelper ant;
54
55     /**
56      * @parameter default-value="${project.build.directory}"
57      * @read-only
58      */

59     private File JavaDoc currentBuildDirectory;
60
61     private File JavaDoc currentReportsDirectory;
62
63     /**
64     * @parameter default-value="${project.basedir}"
65     * @read-only
66     */

67     private File JavaDoc currentBaseDirectory;
68
69     /**
70     * @parameter default-value="${project.parent.basedir}/target"
71     * @read-only
72     */

73     private File JavaDoc parentBuildDirectory;
74
75     private File JavaDoc parentReportsDirectory;
76
77     //
78
// MojoSupport Hooks
79
//
80

81     /**
82      * The maven project.
83      *
84      * @parameter expression="${project}"
85      * @required
86      * @readonly
87      */

88     protected MavenProject project = null;
89
90     protected MavenProject getProject()
91     {
92         return project;
93     }
94
95     protected void init() throws MojoExecutionException, MojoFailureException {
96         super.init();
97
98         ant.setProject(getProject());
99
100         currentReportsDirectory = new File JavaDoc(currentBuildDirectory, "surefire-reports");
101         parentReportsDirectory = new File JavaDoc(parentBuildDirectory, "surefire-reports");
102     }
103
104     protected void doExecute() throws Exception JavaDoc {
105
106         if ( !currentReportsDirectory.exists() )
107         {
108             log.info("No surefire-reports directory here");
109             return;
110         }
111
112         String JavaDoc parent_tests = "0";
113         String JavaDoc parent_skipped = "0";
114         String JavaDoc parent_errors = "0";
115         String JavaDoc parent_failures = "0";
116         String JavaDoc parent_time = "0";
117
118         String JavaDoc artifactName = FileUtils.filename(currentBaseDirectory.getAbsolutePath());
119         if ( !parentReportsDirectory.exists() )
120         {
121             parentReportsDirectory.mkdirs();
122         }
123         File JavaDoc parentSurefireXMLFile = new File JavaDoc(parentReportsDirectory, "TEST-" + artifactName + ".xml");
124
125         ArrayList JavaDoc xmlFiles = (ArrayList JavaDoc) FileUtils.getFiles(currentReportsDirectory, "TEST*.xml", null);
126         for ( int i=0; i < xmlFiles.size(); i++ )
127         {
128             File JavaDoc xmlFile = (File JavaDoc) xmlFiles.get(i);
129             log.info("Loading surefire xml for xmlproperty: " + xmlFile.getAbsolutePath());
130
131             String JavaDoc prefix = String.valueOf(System.currentTimeMillis());
132             loadXMLProperty(xmlFile, prefix);
133
134             String JavaDoc tests = ant.getAnt().getProperty(prefix + ".testsuite.tests");
135             String JavaDoc skipped = ant.getAnt().getProperty(prefix + ".testsuite.skipped");
136             String JavaDoc errors = ant.getAnt().getProperty(prefix + ".testsuite.errors");
137             String JavaDoc failures = ant.getAnt().getProperty(prefix + ".testsuite.failures");
138             String JavaDoc time = ant.getAnt().getProperty(prefix + ".testsuite.time");
139             log.debug("tests=" + tests + "; skipped=" + skipped + ", errors=" + errors + ", failures=" + failures + ", time=" + time);
140
141             if ( parentSurefireXMLFile.exists() )
142             {
143                 log.info("Loading parent surefire xml for xmlproperty");
144                 String JavaDoc parentPrefix = "parent" + prefix;
145                 loadXMLProperty(parentSurefireXMLFile, parentPrefix);
146
147                 parent_tests = ant.getAnt().getProperty(parentPrefix + ".testsuite.tests");
148                 parent_skipped = ant.getAnt().getProperty(parentPrefix + ".testsuite.skipped");
149                 parent_errors = ant.getAnt().getProperty(parentPrefix + ".testsuite.errors");
150                 parent_failures = ant.getAnt().getProperty(parentPrefix + ".testsuite.failures");
151                 parent_time = ant.getAnt().getProperty(parentPrefix + ".testsuite.time");
152                 log.debug("tests=" + parent_tests + "; skipped=" + parent_skipped + ", errors=" + parent_errors + ", failures=" + parent_failures + ", time=" + parent_time);
153             }
154
155             int testsNum = Integer.parseInt(parent_tests) + Integer.parseInt(tests);
156             int skippedNum = Integer.parseInt(parent_skipped) + Integer.parseInt(skipped);
157             int errorsNum = Integer.parseInt(parent_errors) + Integer.parseInt(errors);
158             int failuresNum = Integer.parseInt(parent_failures) + Integer.parseInt(failures);
159             float timeNum = Float.parseFloat(parent_time) + Float.parseFloat(time);
160
161             writeParentXML(testsNum,skippedNum,errorsNum,failuresNum,timeNum,artifactName,parentSurefireXMLFile);
162         }
163     }
164
165
166
167     /**
168      * http://ant.apache.org/manual/CoreTasks/xmlproperty.html
169      */

170     private void loadXMLProperty(File JavaDoc src, String JavaDoc prefix)
171     {
172         XmlProperty xmlProperty = (XmlProperty)ant.createTask("xmlproperty");
173         xmlProperty.setFile(src);
174         if ( prefix != null )
175         {
176             xmlProperty.setPrefix(prefix);
177         }
178         xmlProperty.setCollapseAttributes(true);
179         xmlProperty.execute();
180     }
181
182
183     /**
184      * (over)writes the surefire xml file in the parent's surefire-reports dir
185      */

186     private void writeParentXML(int testsNum,int skippedNum,int errorsNum,
187                                 int failuresNum, float timeNum,
188                                 String JavaDoc artifactName, File JavaDoc parentSurefireXMLFile ) throws IOException JavaDoc {
189
190         final String JavaDoc header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
191         String JavaDoc testSuite = "<testsuite errors=\"" + errorsNum + "\" skipped=\"" + skippedNum + "\" tests=\"" + testsNum + "\" time=\"" + timeNum + "\" failures=\"" + failuresNum + "\" name=\"" + artifactName + "#.\"/>";
192
193         String JavaDoc parentSurefireXMLFileName = parentSurefireXMLFile.getAbsolutePath();
194
195         log.debug(testSuite);
196
197         FileUtils.fileWrite(parentSurefireXMLFileName, header + "\n" + testSuite);
198
199         return;
200     }
201
202 }
203
Popular Tags