1 19 20 package org.apache.geronimo.mavenplugins.testsuite; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 25 import java.util.ArrayList ; 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 47 public class SurefireXMLGeneratorMojo 48 extends MojoSupport 49 { 50 53 protected AntHelper ant; 54 55 59 private File currentBuildDirectory; 60 61 private File currentReportsDirectory; 62 63 67 private File currentBaseDirectory; 68 69 73 private File parentBuildDirectory; 74 75 private File parentReportsDirectory; 76 77 81 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 (currentBuildDirectory, "surefire-reports"); 101 parentReportsDirectory = new File (parentBuildDirectory, "surefire-reports"); 102 } 103 104 protected void doExecute() throws Exception { 105 106 if ( !currentReportsDirectory.exists() ) 107 { 108 log.info("No surefire-reports directory here"); 109 return; 110 } 111 112 String parent_tests = "0"; 113 String parent_skipped = "0"; 114 String parent_errors = "0"; 115 String parent_failures = "0"; 116 String parent_time = "0"; 117 118 String artifactName = FileUtils.filename(currentBaseDirectory.getAbsolutePath()); 119 if ( !parentReportsDirectory.exists() ) 120 { 121 parentReportsDirectory.mkdirs(); 122 } 123 File parentSurefireXMLFile = new File (parentReportsDirectory, "TEST-" + artifactName + ".xml"); 124 125 ArrayList xmlFiles = (ArrayList ) FileUtils.getFiles(currentReportsDirectory, "TEST*.xml", null); 126 for ( int i=0; i < xmlFiles.size(); i++ ) 127 { 128 File xmlFile = (File ) xmlFiles.get(i); 129 log.info("Loading surefire xml for xmlproperty: " + xmlFile.getAbsolutePath()); 130 131 String prefix = String.valueOf(System.currentTimeMillis()); 132 loadXMLProperty(xmlFile, prefix); 133 134 String tests = ant.getAnt().getProperty(prefix + ".testsuite.tests"); 135 String skipped = ant.getAnt().getProperty(prefix + ".testsuite.skipped"); 136 String errors = ant.getAnt().getProperty(prefix + ".testsuite.errors"); 137 String failures = ant.getAnt().getProperty(prefix + ".testsuite.failures"); 138 String 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 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 170 private void loadXMLProperty(File src, String 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 186 private void writeParentXML(int testsNum,int skippedNum,int errorsNum, 187 int failuresNum, float timeNum, 188 String artifactName, File parentSurefireXMLFile ) throws IOException { 189 190 final String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"; 191 String testSuite = "<testsuite errors=\"" + errorsNum + "\" skipped=\"" + skippedNum + "\" tests=\"" + testsNum + "\" time=\"" + timeNum + "\" failures=\"" + failuresNum + "\" name=\"" + artifactName + "#.\"/>"; 192 193 String 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 |