~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to tools/Report/.svn/text-base/Report.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***********************************************************************
2
 
 *  $RCSfile$
3
 
 *
4
 
 *  Copyright (C) 2004 Novell, Inc.
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or
7
 
 *  modify it under the terms of the GNU General Public
8
 
 *  License as published by the Free Software Foundation; either
9
 
 *  version 2 of the License, or (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 *  General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public
17
 
 *  License along with this program; if not, write to the Free
18
 
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
 
 *
20
 
 *  Author: Rob
21
 
 *
22
 
 ***********************************************************************/
23
 
 
24
 
using System;
25
 
using System.IO;
26
 
using System.Xml;
27
 
using System.Collections;
28
 
 
29
 
/// <summary>
30
 
/// Parses test result files and creates a summary report.
31
 
/// </summary>
32
 
public class Report 
33
 
{
34
 
        /// <summary>
35
 
        /// The test result files suffix.
36
 
        /// </summary>
37
 
        const string RESULTS_SUFFIX = "*.test.xml";
38
 
 
39
 
        /// <summary>
40
 
        /// The directory containing the test result files.
41
 
        /// </summary>
42
 
        string dir;
43
 
 
44
 
        /// <summary>
45
 
        /// The text writer for the summary report.
46
 
        /// </summary>
47
 
        TextWriter writer;
48
 
 
49
 
        /// <summary>
50
 
        /// The total number of test cases.
51
 
        /// </summary>
52
 
        int totalCases;
53
 
 
54
 
        /// <summary>
55
 
        /// The total number of test cases that ran.
56
 
        /// </summary>
57
 
        int totalRuns;
58
 
 
59
 
        /// <summary>
60
 
        /// The total number of test cases that passed.
61
 
        /// </summary>
62
 
        int totalPasses;
63
 
 
64
 
        /// <summary>
65
 
        /// The total time required to run the test cases.
66
 
        /// </summary>
67
 
        double totalTime;
68
 
 
69
 
        /// <summary>
70
 
        /// A string list of the number of fails in each test result file.
71
 
        /// </summary>
72
 
        ArrayList failList;
73
 
 
74
 
        /// <summary>
75
 
        /// Constructor
76
 
        /// </summary>
77
 
        /// <param name="dir">The directory with the test result files.</param>
78
 
        /// <param name="writer">The writer for the summary report.</param>
79
 
        public Report(string dir, TextWriter writer)
80
 
        {
81
 
                this.dir = dir;
82
 
                this.writer = writer;
83
 
                this.failList = new ArrayList();
84
 
        }
85
 
 
86
 
        /// <summary>
87
 
        /// Parse the test result files in the directory.
88
 
        /// </summary>
89
 
        void Parse()
90
 
        {
91
 
                writer.WriteLine("Parsing test result files...");
92
 
                writer.WriteLine();
93
 
 
94
 
                if (Directory.Exists(dir))
95
 
                {
96
 
                        String[] files = Directory.GetFiles(dir, RESULTS_SUFFIX);
97
 
 
98
 
                        foreach(string file in files)
99
 
                        {
100
 
                                writer.WriteLine("Parsing: {0}", Path.GetFileName(file));
101
 
 
102
 
                                ParseTestFile(file);
103
 
                        }
104
 
                }
105
 
        }
106
 
 
107
 
        /// <summary>
108
 
        /// Parse a test result file.
109
 
        /// </summary>
110
 
        /// <param name="file">The test result file.</param>
111
 
        void ParseTestFile(string file)
112
 
        {
113
 
                int cases = 0;
114
 
                int runs = 0;
115
 
                int passes = 0;
116
 
                int fails = 0;
117
 
 
118
 
                // find a component name
119
 
                string name = Path.GetFileNameWithoutExtension(file);
120
 
                name = Path.GetFileNameWithoutExtension(name);
121
 
 
122
 
                // parse the XML
123
 
                XmlDocument doc = new XmlDocument();
124
 
                doc.Load(file);
125
 
                
126
 
                foreach (XmlElement node in doc.GetElementsByTagName("test-case"))
127
 
                {
128
 
                        cases++;
129
 
        
130
 
                        if (bool.Parse(node.GetAttribute("executed")))
131
 
                        {
132
 
                                runs++;
133
 
 
134
 
                                if (bool.Parse(node.GetAttribute("success")))
135
 
                                {
136
 
                                        passes++;
137
 
                                }
138
 
                        }
139
 
 
140
 
                        // not all test-case elementes have a time
141
 
                        string time = node.GetAttribute("time"); 
142
 
                        if ((time != null) && (time.Length > 0))
143
 
                        {
144
 
                                totalTime += Double.Parse(time);
145
 
                        }
146
 
                }
147
 
                
148
 
                // fails
149
 
                if ((fails = runs - passes) > 0)
150
 
                {
151
 
                        failList.Add(String.Format("{0,4}: {1}", fails, name));
152
 
                }
153
 
 
154
 
                // sum up
155
 
                totalCases += cases;
156
 
                totalRuns += runs;
157
 
                totalPasses += passes;
158
 
        }
159
 
 
160
 
        /// <summary>
161
 
        /// Write the summary report.
162
 
        /// </summary>
163
 
        void WriteSummary()
164
 
        {
165
 
                const string HEADER = "------------ TEST REPORT ------------";
166
 
                
167
 
                // summary
168
 
                writer.WriteLine();
169
 
                writer.WriteLine(HEADER);
170
 
                writer.WriteLine();
171
 
                
172
 
                writer.WriteLine("TEST CASE RESULTS");
173
 
                writer.WriteLine();
174
 
                writer.WriteLine("  Total: {0}", totalCases);
175
 
                writer.WriteLine("    Ran: {0}", totalRuns);
176
 
                writer.WriteLine(" Passed: {0} [ {1:p1} ]", totalPasses,
177
 
                        (totalRuns > 0 ? (double)totalPasses/(double)totalRuns : 0));
178
 
                writer.WriteLine();
179
 
                writer.WriteLine("   Time: {0} seconds", totalTime);
180
 
                
181
 
                // failed cases
182
 
                writer.WriteLine();
183
 
                writer.WriteLine("FAILED TEST CASES");
184
 
                writer.WriteLine();
185
 
 
186
 
                foreach(string fail in failList)
187
 
                {
188
 
                        writer.WriteLine(fail);
189
 
                }
190
 
 
191
 
                writer.WriteLine();
192
 
                writer.WriteLine(HEADER);
193
 
                writer.WriteLine();
194
 
        }
195
 
 
196
 
        /// <summary>
197
 
        /// Main method.
198
 
        /// </summary>
199
 
        /// <param name="args">Command-line arguments.</param>
200
 
        /// <returns>The process result.</returns>
201
 
        [STAThread]
202
 
        static int Main(string[] args)
203
 
        {
204
 
                // check arguments
205
 
                if (args.Length != 1)
206
 
                {
207
 
                        Console.WriteLine("USAGE: Report.exe [directory]");
208
 
 
209
 
                        return 1;
210
 
                }
211
 
 
212
 
                // create the report object
213
 
                Report report = new Report(args[0], Console.Out);
214
 
                
215
 
                // parse the test result files
216
 
                report.Parse();
217
 
 
218
 
                // write the summary report
219
 
                report.WriteSummary();
220
 
 
221
 
                return 0;
222
 
        }
223
 
}
224
 
 
225