KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > japex > Japex


1 /*
2  * Japex ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * This Software is distributed under the following terms:
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, is permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistribution in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * Neither the name of Sun Microsystems, Inc., 'Java', 'Java'-based names,
19  * nor the names of contributors may be used to endorse or promote products
20  * derived from this Software without specific prior written permission.
21  *
22  * The Software is provided "AS IS," without a warranty of any kind. ALL
23  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
24  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25  * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS
26  * SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE
27  * AS A RESULT OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE
28  * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE
29  * LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
30  * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED
31  * AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
32  * INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGES.
34  *
35  * You acknowledge that the Software is not designed, licensed or intended
36  * for use in the design, construction, operation or maintenance of any
37  * nuclear facility.
38  */

39
40 package com.sun.japex;
41
42 import java.io.*;
43 import java.text.*;
44 import java.util.Date JavaDoc;
45 import java.net.URL JavaDoc;
46 import javax.xml.transform.*;
47 import javax.xml.transform.stream.*;
48
49 public class Japex {
50     
51     public static boolean HTML_OUTPUT = true;
52     public static Date JavaDoc TODAY = new Date JavaDoc();
53     
54     /** Creates a new instance of Japex */
55     public Japex() {
56     }
57     
58     /**
59      * @param args the command line arguments
60      */

61     public static void main(String JavaDoc[] args) {
62         if (args.length < 1 || args.length > 4) {
63             displayUsageAndExit();
64         }
65
66         // Parse command-line arguments
67
String JavaDoc configFile = null;
68         for (int i = 0; i < args.length; i++) {
69             if (args[i].equals("-nohtml")) {
70                 HTML_OUTPUT = false;
71             }
72             else if (args[i].equals("-cp") || args[i].equals("-classpath")) {
73                 i++; // Skip, already processed
74
}
75             else {
76                 configFile = args[i];
77             }
78         }
79         
80         if (configFile == null) {
81             displayUsageAndExit();
82         }
83
84         new Japex().run(configFile);
85     }
86
87     private static void displayUsageAndExit() {
88         System.err.println("Usage: japex [-cp <classpath>] [-nohtml] config.xml");
89         System.exit(1);
90     }
91     
92     public void run(String JavaDoc configFile) {
93         try {
94             System.out.println("Running ...");
95             
96             // Create testsuite object from configuration file
97
TestSuite testSuite = new Engine().start(configFile);
98             
99             // Create report directory
100
String JavaDoc fileSep = System.getProperty("file.separator");
101             DateFormat df = new SimpleDateFormat("yyyy_MM_dd_HH_mm");
102             String JavaDoc outputDir = testSuite.getParam(Constants.REPORTS_DIRECTORY)
103                 + fileSep + df.format(TODAY);
104             new File(outputDir).mkdirs();
105
106             // Generate report to string buffer
107
StringBuffer JavaDoc report = new StringBuffer JavaDoc();
108             testSuite.serialize(report);
109
110             // Output report to file
111
System.out.println("Generating reports ...");
112             System.out.println(" " +
113                 new File(outputDir + "/" + "report.xml").toURL());
114             OutputStreamWriter osr = new OutputStreamWriter(
115                 new FileOutputStream(
116                     new File(outputDir + fileSep + "report.xml")));
117             osr.write(report.toString());
118             osr.close();
119
120             // Return if no HTML needs to be output
121
if (!HTML_OUTPUT) return;
122             
123             // Generate charts
124
final String JavaDoc resultChart = "result.jpg";
125             testSuite.generateDriverChart(outputDir + fileSep
126                 + resultChart);
127             final String JavaDoc testCaseChartBase = "testcase";
128             int nOfCharts = testSuite.generateTestCaseCharts(outputDir
129                 + fileSep + testCaseChartBase, ".jpg");
130             
131             // Extend report with chart info
132
StringBuffer JavaDoc extendedReport = new StringBuffer JavaDoc();
133             extendedReport.append("<extendedTestSuiteReport " +
134                 "xmlns=\"http://www.sun.com/japex/extendedTestSuiteReport\">\n")
135                 .append(" <resultChart>" + resultChart + "</resultChart>\n");
136             for (int i = 0; i < nOfCharts; i++) {
137                 extendedReport.append(" <testCaseChart>" +
138                     testCaseChartBase + i + ".jpg" + "</testCaseChart>\n");
139             }
140             extendedReport.append(report);
141             extendedReport.append("</extendedTestSuiteReport>\n");
142
143             // Generate HTML report
144
TransformerFactory tf = TransformerFactory.newInstance();
145             URL JavaDoc stylesheet = getClass().getResource("/resources/report.xsl");
146             if (stylesheet != null) {
147                 Transformer transformer = tf.newTransformer(
148                     new StreamSource(stylesheet.toExternalForm()));
149
150                 System.out.println(" " +
151                     new File(outputDir + "/" + "report.html").toURL());
152                 transformer.transform(
153                     new StreamSource(new StringReader(extendedReport.toString())),
154                     new StreamResult(new FileOutputStream(
155                         new File(outputDir + fileSep + "report.html"))));
156                 
157                 // Copy CSS to the same directory
158
URL JavaDoc css = getClass().getResource("/resources/report.css");
159                 if (css != null) {
160                     InputStream is = css.openStream();
161                     FileOutputStream fos = new FileOutputStream(
162                         new File(outputDir + fileSep + "report.css"));
163                     
164                     int c;
165                     while ((c = is.read()) != -1) {
166                         fos.write(c);
167                     }
168                     is.close();
169                     fos.close();
170                 }
171             }
172         }
173         catch (RuntimeException JavaDoc e) {
174             throw e;
175         }
176         catch (Exception JavaDoc e) {
177             throw new RuntimeException JavaDoc(e);
178         }
179     }
180     
181 }
182
Popular Tags