View Javadoc
1 /*** 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Exoffice Technologies. For written permission, 18 * please contact tma@netspace.net.au. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Exoffice Technologies. Exolab is a registered 23 * trademark of Exoffice Technologies. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: CoverageReport.java,v 1.6 2004/02/03 21:52:08 tanderson Exp $ 44 */ 45 package org.exolab.jmscts.report; 46 47 import java.io.File; 48 import java.io.FileWriter; 49 import java.io.IOException; 50 51 import javax.xml.transform.TransformerException; 52 53 import org.apache.commons.cli.CommandLine; 54 import org.apache.commons.cli.CommandLineParser; 55 import org.apache.commons.cli.GnuParser; 56 import org.apache.commons.cli.Options; 57 import org.apache.log4j.Category; 58 59 import org.exolab.castor.xml.MarshalException; 60 import org.exolab.castor.xml.ValidationException; 61 62 import org.exolab.jmscts.core.TestCoverage; 63 import org.exolab.jmscts.provider.ProviderLoader; 64 65 66 /*** 67 * This class generates a requirements coverage report in HTML from a 68 * {@link TestCoverage} instance 69 * 70 * @version $Revision: 1.6 $ $Date: 2004/02/03 21:52:08 $ 71 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 72 * @see TestCoverage 73 */ 74 public class CoverageReport { 75 76 /*** 77 * The path to locate stylesheets 78 */ 79 private String _path; 80 81 /*** 82 * The test suite's coverage of requirements 83 */ 84 private TestCoverage _coverage; 85 86 /*** 87 * The provider configuration 88 */ 89 private ProviderLoader _provider; 90 91 /*** 92 * The logger 93 */ 94 private static final Category log = 95 Category.getInstance(CoverageReport.class); 96 97 /*** 98 * The file name of the raw report 99 */ 100 private static final String COVERAGE_XML = "coverage.xml"; 101 102 /*** 103 * The file name of the coverage stylesheet 104 */ 105 private static final String COVERAGE_STYLESHEET = "resources/coverage.xsl"; 106 107 /*** 108 * The file name of the xdoc report 109 */ 110 private static final String COVERAGE_XDOC = "coverage.xml"; 111 112 113 /*** 114 * Construct an instance to generate a report from an existing coverage.xml 115 * file 116 * 117 * @param path the path to locate stylesheets 118 */ 119 public CoverageReport(String path) { 120 if (path == null) { 121 throw new IllegalArgumentException("Argument 'path' is null"); 122 } 123 _path = path; 124 } 125 126 /*** 127 * Construct an instance with the test suite's coverage of requirements 128 * 129 * @param path the path to locate stylesheets 130 * @param coverage the test suite's coverage of requirements 131 * @param provider the provider loader 132 */ 133 public CoverageReport(String path, TestCoverage coverage, 134 ProviderLoader provider) { 135 if (path == null) { 136 throw new IllegalArgumentException("Argument 'path' is null"); 137 } 138 if (coverage == null) { 139 throw new IllegalArgumentException("Argument 'coverage' is null"); 140 } 141 if (provider == null) { 142 throw new IllegalArgumentException("Argument 'provider' is null"); 143 } 144 _path = path; 145 _coverage = coverage; 146 _provider = provider; 147 } 148 149 /*** 150 * Generate the coverage report 151 * 152 * @param dir the directory path to write the report to 153 * @throws IOException if the specified file is not found or if some other 154 * I/O error occurs 155 * @throws MarshalException if the coverage report cannot be written 156 * @throws ValidationException if the coverage report doesn't comply 157 * with the XML Schema definition 158 * @throws TransformerException if the transformation fails 159 */ 160 public void report(String dir) 161 throws IOException, MarshalException, TransformerException, 162 ValidationException { 163 164 if (_coverage != null) { 165 RequirementCoverage coverage = _coverage.getCoverage(); 166 coverage.setProvider(_provider.getName()); 167 String path = dir + "/" + COVERAGE_XML; 168 if (log.isDebugEnabled()) { 169 log.debug("Writing coverage report to " + path); 170 } 171 FileWriter stream = new FileWriter(path); 172 try { 173 coverage.marshal(stream); 174 } finally { 175 stream.close(); 176 } 177 } 178 transform(dir); 179 } 180 181 /*** 182 * Transforms all xdocs to HTML 183 * 184 * @param dir the base directory 185 * @throws IOException if an I/O error occurs 186 * @throws TransformerException if the transformation fails 187 */ 188 private void transform(String dir) 189 throws IOException, TransformerException { 190 191 File input = new File(dir, COVERAGE_XML); 192 File xdocs = new File(dir, "xdocs"); 193 if (!xdocs.exists() && !xdocs.mkdir()) { 194 throw new IOException("Directory " + xdocs 195 + " could not be created"); 196 } 197 198 File html = new File(dir, "html"); 199 if (!html.exists() && !html.mkdir()) { 200 throw new IOException("Directory " + html 201 + " could not be created"); 202 } 203 204 File coverageXdoc = new File(xdocs, COVERAGE_XDOC); 205 File stylesheet = new File(_path, COVERAGE_STYLESHEET); 206 207 TransformerHelper.transform(input, coverageXdoc, stylesheet); 208 TransformerHelper.transformAllXDoc2HTML(xdocs, html, new File(_path)); 209 } 210 211 /*** 212 * Main line 213 * 214 * @param args the command line arguments 215 * @throws Exception for any error 216 */ 217 public static void main(String[] args) throws Exception { 218 final String path = "path"; 219 220 Options options = new Options(); 221 options.addOption(path, true, "the output path"); 222 223 CommandLineParser parser = new GnuParser(); 224 CommandLine commands = parser.parse(options, args); 225 226 String dir = commands.getOptionValue(path); 227 if (dir == null) { 228 throw new Exception("-" + path + " <path> parameter not set"); 229 } 230 231 CoverageReport report = new CoverageReport("./"); 232 report.report(dir); 233 } 234 235 }

This page was automatically generated by Maven