KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > harness > GenerateReport


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

21
22 package org.apache.derbyTesting.functionTests.harness;
23
24 import java.util.Properties JavaDoc;
25 import java.util.Calendar JavaDoc;
26 import java.util.GregorianCalendar JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.FilenameFilter JavaDoc;
30 import java.io.FileWriter JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.BufferedReader JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34 import java.sql.Date JavaDoc;
35 import java.sql.Time JavaDoc;
36 import java.sql.Timestamp JavaDoc;
37
38 /**
39     Generate summary information from a RunSuite run.
40     Can be called separately, if given the suite name.
41     Will be called from RunSuite if System property genrep=true.
42
43     Condenses run information down, prints out result stats,
44     and shows details of failures (.diff files).
45
46     @author ames
47 **/

48 public class GenerateReport {
49
50     static void CollectProperties () {
51         Properties JavaDoc ps = System.getProperties();
52         char[] newline = {' '};
53         propFile.println(PropertyUtil.sortProperties(ps,newline));
54     }
55
56     static void CalculateRunLength () {
57     // read suite.sum for start/end timestamps,
58
// report start date and full duration of run
59
String JavaDoc odn = System.getProperty("outputdir");
60         if (odn==null) odn = System.getProperty("user.dir");
61
62         BufferedReader JavaDoc sumFile = null;
63         String JavaDoc firstLine = null;
64         String JavaDoc lastLine = null;
65         try {
66             sumFile = new BufferedReader JavaDoc(new FileReader JavaDoc(new File JavaDoc(new File JavaDoc(odn),SuiteName+".sum")));
67             firstLine = sumFile.readLine();
68             String JavaDoc aLine = firstLine;
69             while (aLine!=null) {
70                 lastLine = aLine;
71                 aLine = sumFile.readLine();
72             }
73             sumFile.close();
74         } catch (IOException JavaDoc ioe) {
75             ioe.printStackTrace(System.out);
76         }
77
78         // have firstLine and lastLine.
79
// format is:
80
// ******* Start Suite: <suite> <timestamp> *******
81
// ******* End Suite: <suite> <timestamp> *******
82
int tsStart = 22+SuiteName.length();
83         int tsEnd = firstLine.length()-8;
84         TestStart = Timestamp.valueOf(firstLine.substring(tsStart,tsEnd));
85         // last line is two shorter
86
tsStart-=2; tsEnd-=2;
87         Timestamp JavaDoc testEnd = Timestamp.valueOf(lastLine.substring(tsStart,tsEnd));
88
89         long testLen = testEnd.getTime() - TestStart.getTime();
90         // Time isn't really a duration, so we have to set the fields
91
int sec = (int) (testLen / 1000);
92         int min = sec / 60;
93         int hr = min / 60;
94         sec = sec - (min*60); // adjust for part removed
95
min = min - (hr*60); // adjust for part removed
96
Calendar JavaDoc cal = new GregorianCalendar JavaDoc();
97         cal.set(Calendar.HOUR_OF_DAY,hr);
98         cal.set(Calendar.MINUTE,min);
99         cal.set(Calendar.SECOND,sec);
100         TestDuration = new Time JavaDoc(cal.getTime().getTime());
101     }
102
103     static void CollectPassFailStats () {
104         // need to ensure outputdir is set...
105
String JavaDoc odn = System.getProperty("outputdir");
106         if (odn==null) odn = System.getProperty("user.dir");
107         CollectPassFailStats(new File JavaDoc(odn),"");
108     }
109
110     static void addLines(PrintWriter JavaDoc outFile, File JavaDoc inFile, String JavaDoc relativeName) {
111         BufferedReader JavaDoc readFile = null;
112         try {
113         readFile = new BufferedReader JavaDoc(new FileReader JavaDoc(inFile));
114         String JavaDoc aLine = readFile.readLine();
115         while (aLine!=null) {
116             outFile.print(relativeName);
117             outFile.print(":");
118             outFile.println(aLine);
119             aLine = readFile.readLine();
120         }
121         readFile.close();
122         } catch (IOException JavaDoc ioe) {
123             ioe.printStackTrace(System.out);
124         }
125
126     }
127
128     static void addDiff(PrintWriter JavaDoc outFile, File JavaDoc inFile, String JavaDoc relativeName) {
129         BufferedReader JavaDoc readFile = null;
130         try {
131         readFile = new BufferedReader JavaDoc(new FileReader JavaDoc(inFile));
132         outFile.print("********* Diff file ");
133         outFile.println(relativeName);
134         String JavaDoc aLine = readFile.readLine();
135         while (aLine!=null) {
136             outFile.println(aLine);
137             aLine = readFile.readLine();
138         }
139         readFile.close();
140         } catch (IOException JavaDoc ioe) {
141             ioe.printStackTrace(System.out);
142         }
143     }
144
145     static void CollectPassFailStats (File JavaDoc dir,String JavaDoc relativeName) {
146         // starting in specified dir,
147
String JavaDoc[] fileList = dir.list(fileFilter);
148         int l = fileList.length;
149         for (int i=0;i<l;i++) {
150             String JavaDoc fileName = fileList[i];
151             File JavaDoc file = new File JavaDoc(dir,fileName);
152
153             // collect all .pass files into suite_pass.txt (passFile)
154
if (fileName.endsWith(".pass")) {
155                 addLines(passFile,file,relativeName+"/"+fileName);
156             }
157             // collect all .fail files into suite_fail.txt (failFile)
158
else if (fileName.endsWith(".fail")) {
159                 addLines(failFile,file,relativeName+"/"+fileName);
160             }
161             // collect all .skip files into suite_skip.txt (skipFile)
162
else if (fileName.endsWith(".skip")) {
163                 addLines(skipFile,file,relativeName+"/"+fileName);
164             }
165             // collect all .diff files into suite_diff.txt (diffFile)
166
else if (fileName.endsWith(".diff")) {
167                 addDiff(diffFile,file,relativeName+"/"+fileName);
168             }
169
170             // recurse on all directories
171
else // it's a directory
172
{
173                 String JavaDoc newDir;
174                 if (relativeName.length()>0)
175                     newDir = relativeName+"/"+fileName;
176                 else newDir = fileName;
177                 CollectPassFailStats(file, newDir);
178             }
179         }
180     }
181
182     static void CalculatePassFailStats() {
183         // total tests run
184
// #, % failures
185
// #, % passed
186
NumPass = CountLines (passFileName);
187         NumFail = CountLines (failFileName);
188         NumRun = NumPass+NumFail;
189         NumSkip = CountLines (skipFileName);
190         PercentPass = (int)Math.floor(100* ((double)NumPass/(double)NumRun));
191         PercentFail = (int)Math.ceil(100* ((double)NumFail/(double)NumRun));
192     }
193
194     static int CountLines(String JavaDoc fileName) {
195         BufferedReader JavaDoc readFile = null;
196         int line = 0;
197         try {
198         readFile = new BufferedReader JavaDoc(new FileReader JavaDoc(fileName));
199         String JavaDoc aLine = readFile.readLine();
200         while (aLine!=null) {
201             line++;
202             aLine = readFile.readLine();
203         }
204         readFile.close();
205         } catch (IOException JavaDoc ioe) {
206             ioe.printStackTrace(System.out);
207         }
208         return line;
209     }
210
211     static void OutputFile(String JavaDoc fileName) {
212         BufferedReader JavaDoc readFile = null;
213         try {
214         readFile = new BufferedReader JavaDoc(new FileReader JavaDoc(fileName));
215         String JavaDoc aLine = readFile.readLine();
216         while (aLine != null) {
217             reportFile.println(aLine);
218             aLine = readFile.readLine();
219         }
220         readFile.close();
221         } catch (IOException JavaDoc ioe) {
222             ioe.printStackTrace(System.out);
223         }
224     }
225
226     static PrintWriter JavaDoc setupFile(String JavaDoc fn) {
227         File JavaDoc f = null;
228         PrintWriter JavaDoc pw = null;
229         try {
230         f = new File JavaDoc(fn);
231         if (f.exists()) {
232             System.out.println("WARNING: removing "+fn);
233             f.delete();
234         }
235         pw = new PrintWriter JavaDoc(new FileWriter JavaDoc(fn,true));
236         } catch (IOException JavaDoc ioe) {
237             ioe.printStackTrace(System.out);
238         }
239         return pw;
240     }
241
242     public static void main(String JavaDoc[] args) {
243         SuiteName = args[0];
244         String JavaDoc jvmName = args[1];
245         String JavaDoc javaCmd = args[2];
246         String JavaDoc classpath = args[3];
247         String JavaDoc framework = args[4];
248         String JavaDoc processexec = args[5];
249         boolean useprocess = true;
250         if ( (processexec.toLowerCase()).startsWith("false") )
251             useprocess = false;
252         String JavaDoc reportFileName = SuiteName+"_report.txt";
253         reportFile = setupFile(reportFileName);
254         reportFile.print("Generating report for RunSuite ");
255         for (int i=0;i<args.length;i++)
256             reportFile.print(args[i]+" ");
257         reportFile.println();
258         passFileName = SuiteName+"_pass.txt";
259         failFileName = SuiteName+"_fail.txt";
260         diffFileName = SuiteName+"_diff.txt";
261         skipFileName = SuiteName+"_skip.txt";
262         propFileName = SuiteName+"_prop.txt";
263         passFile = setupFile(passFileName);
264         failFile = setupFile(failFileName);
265         diffFile = setupFile(diffFileName);
266         skipFile = setupFile(skipFileName);
267         propFile = setupFile(propFileName);
268
269         // sysinfo printout
270
SysInfoLog sysLog = new SysInfoLog();
271         try
272         {
273             sysLog.exec(jvmName, javaCmd, classpath, framework, reportFile, useprocess);
274             //SysInfoMain.getMainInfo(reportFile,false,false);
275
}
276         catch (Exception JavaDoc e)
277         {
278             System.out.println("SysInfoLog Exception: " + e.getMessage());
279         }
280     
281         reportFile.println("Test environment information:");
282         reportFile.print("COMMAND LINE STYLE: ");
283         String JavaDoc jvm = System.getProperty("jvm");
284         if (jvm == null) jvm="jdk13";
285         reportFile.println(jvm);
286         reportFile.print("TEST CANONS: ");
287         String JavaDoc canondir = System.getProperty("canondir");
288         if (canondir == null) canondir = "master";
289         reportFile.println(canondir);
290         reportFile.println(DASHLINE);
291
292         reportFile.println(DASHLINE);
293         reportFile.println("Summary results:");
294         CalculateRunLength();
295         CollectPassFailStats();
296         CollectProperties();
297         passFile.close();
298         failFile.close();
299         skipFile.close();
300         diffFile.close();
301         propFile.close();
302         CalculatePassFailStats();
303         reportFile.println();
304         reportFile.println("Test Run Started: "+TestStart);
305         reportFile.println("Test Run Duration: "+TestDuration);
306         reportFile.println();
307         reportFile.println(NumRun+" Tests Run");
308         if (PercentPass<10) reportFile.print(" ");
309         reportFile.println(PercentPass+"% Pass ("+NumPass+" tests passed)");
310         if (PercentFail<10) reportFile.print(" ");
311         reportFile.println(PercentFail+"% Fail ("+NumFail+" tests failed)");
312         reportFile.println(NumSkip + " Suites skipped");
313         reportFile.println(DASHLINE);
314
315         if (NumFail>0) {
316             reportFile.println("Failed tests in: "+failFileName);
317             reportFile.println(DASHLINE);
318         }
319
320         if (NumPass>0) {
321             reportFile.println("Passed tests in: "+passFileName);
322             reportFile.println(DASHLINE);
323         }
324
325         if (NumSkip>0) {
326             reportFile.println("Skipped suites in: "+skipFileName);
327             reportFile.println(DASHLINE);
328         }
329
330         reportFile.println("System properties in: "+propFileName);
331         reportFile.println(DASHLINE);
332
333         reportFile.println(DASHLINE);
334         if (NumFail>0) {
335             reportFile.println("Failure Details:");
336             // cat each .diff file with full test name
337
OutputFile(diffFileName);
338         }
339         else reportFile.println("No Failures.");
340         reportFile.println(DASHLINE);
341         reportFile.close();
342
343         System.out.println("Generated report: "+reportFileName);
344     }
345     
346     static final String JavaDoc DASHLINE="------------------------------------------------------";
347     static String JavaDoc passFileName, failFileName, diffFileName, skipFileName, propFileName;
348     static PrintWriter JavaDoc passFile, failFile, diffFile, skipFile, propFile;
349     static PrintWriter JavaDoc reportFile;
350     static FilenameFilter JavaDoc fileFilter = new GRFileFilter();
351     static int NumPass, NumFail, NumRun, NumSkip;
352     static int PercentPass, PercentFail;
353     static Timestamp JavaDoc TestStart;
354     static Time JavaDoc TestDuration;
355     static String JavaDoc SuiteName;
356 }
357
Popular Tags