1 18 package org.apache.tools.ant.taskdefs.optional.junit; 19 20 import java.io.BufferedOutputStream ; 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.io.OutputStreamWriter ; 26 import java.io.PrintWriter ; 27 import java.util.Enumeration ; 28 import java.util.Vector ; 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.DirectoryScanner; 33 import org.apache.tools.ant.Project; 34 import org.apache.tools.ant.Task; 35 import org.apache.tools.ant.types.FileSet; 36 import org.apache.tools.ant.util.DOMElementWriter; 37 import org.apache.tools.ant.util.FileUtils; 38 import org.apache.tools.ant.util.StringUtils; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 import org.xml.sax.SAXException ; 42 43 44 58 public class XMLResultAggregator extends Task implements XMLConstants { 59 60 62 protected Vector filesets = new Vector (); 63 64 65 protected String toFile; 66 67 68 protected File toDir; 69 70 protected Vector transformers = new Vector (); 71 72 73 public static final String DEFAULT_DIR = "."; 74 75 76 public static final String DEFAULT_FILENAME = "TESTS-TestSuites.xml"; 77 78 79 protected int generatedId = 0; 80 81 84 static final String WARNING_IS_POSSIBLY_CORRUPTED 85 = " is not a valid XML document. It is possibly corrupted."; 86 89 static final String WARNING_INVALID_ROOT_ELEMENT 90 = " is not a valid testsuite XML document"; 91 94 static final String WARNING_EMPTY_FILE 95 = " is empty.\nThis can be caused by the test JVM exiting unexpectedly"; 96 98 102 public AggregateTransformer createReport() { 103 AggregateTransformer transformer = new AggregateTransformer(this); 104 transformers.addElement(transformer); 105 return transformer; 106 } 107 108 114 public void setTofile(String value) { 115 toFile = value; 116 } 117 118 125 public void setTodir(File value) { 126 toDir = value; 127 } 128 129 133 public void addFileSet(FileSet fs) { 134 filesets.addElement(fs); 135 } 136 137 143 public void execute() throws BuildException { 144 Element rootElement = createDocument(); 145 File destFile = getDestinationFile(); 146 try { 148 writeDOMTree(rootElement.getOwnerDocument(), destFile); 149 } catch (IOException e) { 150 throw new BuildException("Unable to write test aggregate to '" + destFile + "'", e); 151 } 152 Enumeration e = transformers.elements(); 154 while (e.hasMoreElements()) { 155 AggregateTransformer transformer = 156 (AggregateTransformer) e.nextElement(); 157 transformer.setXmlDocument(rootElement.getOwnerDocument()); 158 transformer.transform(); 159 } 160 } 161 162 167 public File getDestinationFile() { 168 if (toFile == null) { 169 toFile = DEFAULT_FILENAME; 170 } 171 if (toDir == null) { 172 toDir = getProject().resolveFile(DEFAULT_DIR); 173 } 174 return new File (toDir, toFile); 175 } 176 177 182 protected File [] getFiles() { 183 Vector v = new Vector (); 184 final int size = filesets.size(); 185 for (int i = 0; i < size; i++) { 186 FileSet fs = (FileSet) filesets.elementAt(i); 187 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); 188 ds.scan(); 189 String [] f = ds.getIncludedFiles(); 190 for (int j = 0; j < f.length; j++) { 191 String pathname = f[j]; 192 if (pathname.endsWith(".xml")) { 193 File file = new File (ds.getBasedir(), pathname); 194 file = getProject().resolveFile(file.getPath()); 195 v.addElement(file); 196 } 197 } 198 } 199 200 File [] files = new File [v.size()]; 201 v.copyInto(files); 202 return files; 203 } 204 205 207 213 protected void writeDOMTree(Document doc, File file) throws IOException { 214 OutputStream out = null; 215 PrintWriter wri = null; 216 try { 217 out = new BufferedOutputStream (new FileOutputStream (file)); 218 wri = new PrintWriter (new OutputStreamWriter (out, "UTF8")); 219 wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); 220 (new DOMElementWriter()).write(doc.getDocumentElement(), wri, 0, " "); 221 wri.flush(); 222 if (wri.checkError()) { 224 throw new IOException ("Error while writing DOM content"); 225 } 226 } finally { 227 FileUtils.close(wri); 228 FileUtils.close(out); 229 } 230 } 231 232 238 protected Element createDocument() { 239 DocumentBuilder builder = getDocumentBuilder(); 241 Document doc = builder.newDocument(); 242 Element rootElement = doc.createElement(TESTSUITES); 243 doc.appendChild(rootElement); 244 245 generatedId = 0; 246 247 File [] files = getFiles(); 249 for (int i = 0; i < files.length; i++) { 250 File file = files[i]; 251 try { 252 log("Parsing file: '" + file + "'", Project.MSG_VERBOSE); 253 if (file.length() > 0) { 254 Document testsuiteDoc 255 = builder.parse( 256 FileUtils.getFileUtils().toURI(files[i].getAbsolutePath())); 257 Element elem = testsuiteDoc.getDocumentElement(); 258 if (TESTSUITE.equals(elem.getNodeName())) { 260 addTestSuite(rootElement, elem); 261 generatedId++; 262 } else { 263 log("the file " + file 266 + WARNING_INVALID_ROOT_ELEMENT, 267 Project.MSG_WARN); 268 } 269 } else { 270 log("the file " + file 271 + WARNING_EMPTY_FILE, 272 Project.MSG_WARN); 273 } 274 } catch (SAXException e) { 275 log("The file " + file + WARNING_IS_POSSIBLY_CORRUPTED, Project.MSG_WARN); 278 log(StringUtils.getStackTrace(e), Project.MSG_DEBUG); 279 } catch (IOException e) { 280 log("Error while accessing file " + file + ": " 281 + e.getMessage(), Project.MSG_ERR); 282 } 283 } 284 return rootElement; 285 } 286 287 299 protected void addTestSuite(Element root, Element testsuite) { 300 String fullclassname = testsuite.getAttribute(ATTR_NAME); 301 int pos = fullclassname.lastIndexOf('.'); 302 303 String pkgName = (pos == -1) ? "" : fullclassname.substring(0, pos); 305 String classname = (pos == -1) ? fullclassname : fullclassname.substring(pos + 1); 306 Element copy = (Element ) DOMUtil.importNode(root, testsuite); 307 308 copy.setAttribute(ATTR_NAME, classname); 310 copy.setAttribute(ATTR_PACKAGE, pkgName); 311 copy.setAttribute(ATTR_ID, Integer.toString(generatedId)); 312 } 313 314 320 private static DocumentBuilder getDocumentBuilder() { 321 try { 322 return DocumentBuilderFactory.newInstance().newDocumentBuilder(); 323 } catch (Exception exc) { 324 throw new ExceptionInInitializerError (exc); 325 } 326 } 327 328 } 329 | Popular Tags |