Add Nunit2
[mono.git] / mcs / nunit20 / framework / XmlResultVisitor.cs
1 #region Copyright (c) 2002, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Philip A. Craig
2 /************************************************************************************
3 '
4 ' Copyright © 2002 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov
5 ' Copyright © 2000-2002 Philip A. Craig
6 '
7 ' This software is provided 'as-is', without any express or implied warranty. In no 
8 ' event will the authors be held liable for any damages arising from the use of this 
9 ' software.
10
11 ' Permission is granted to anyone to use this software for any purpose, including 
12 ' commercial applications, and to alter it and redistribute it freely, subject to the 
13 ' following restrictions:
14 '
15 ' 1. The origin of this software must not be misrepresented; you must not claim that 
16 ' you wrote the original software. If you use this software in a product, an 
17 ' acknowledgment (see the following) in the product documentation is required.
18 '
19 ' Portions Copyright © 2002 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov 
20 ' or Copyright © 2000-2002 Philip A. Craig
21 '
22 ' 2. Altered source versions must be plainly marked as such, and must not be 
23 ' misrepresented as being the original software.
24 '
25 ' 3. This notice may not be removed or altered from any source distribution.
26 '
27 '***********************************************************************************/
28 #endregion
29
30 namespace NUnit.Core
31 {
32         using System;
33         using System.Globalization;
34         using System.IO;
35         using System.Xml;
36         using NUnit.Core;
37
38         /// <summary>
39         /// Summary description for XmlResultVisitor.
40         /// </summary>
41         public class XmlResultVisitor : ResultVisitor
42         {
43                 private XmlTextWriter xmlWriter;
44
45                 public XmlResultVisitor(string fileName, TestResult result)
46                 {
47                         ResultSummarizer summaryResults = new ResultSummarizer(result);
48                         try
49                         {
50                                 xmlWriter = new XmlTextWriter (fileName, null);
51                         }
52                         catch(Exception e)
53                         {
54                                 Console.Error.WriteLine(e.StackTrace);
55                         }
56
57                         xmlWriter.Formatting = Formatting.Indented;
58                         xmlWriter.WriteStartDocument(false);
59                         xmlWriter.WriteComment("This file represents the results of running a test suite");
60
61                         xmlWriter.WriteStartElement("test-results");
62
63                         xmlWriter.WriteAttributeString("name", summaryResults.Name);
64                         xmlWriter.WriteAttributeString("total", summaryResults.ResultCount.ToString());
65                         xmlWriter.WriteAttributeString("failures", summaryResults.Failures.ToString());
66                         xmlWriter.WriteAttributeString("not-run", summaryResults.TestsNotRun.ToString());
67
68                         DateTime now = DateTime.Now;
69                         xmlWriter.WriteAttributeString("date", now.ToShortDateString());
70                         xmlWriter.WriteAttributeString("time", now.ToShortTimeString());
71
72                 }
73
74                 public void visit(TestCaseResult caseResult) 
75                 {
76                         xmlWriter.WriteStartElement("test-case");
77                         xmlWriter.WriteAttributeString("name",caseResult.Name);
78                         xmlWriter.WriteAttributeString("executed", caseResult.Executed.ToString());
79                         if(caseResult.Executed)
80                         {
81                                 xmlWriter.WriteAttributeString("success", caseResult.IsSuccess.ToString());
82
83                                 xmlWriter.WriteAttributeString("time", caseResult.Time.ToString("#####0.000", NumberFormatInfo.InvariantInfo));
84
85                                 if(caseResult.IsFailure)
86                                 {
87                                         if(caseResult.IsFailure)
88                                                 xmlWriter.WriteStartElement("failure");
89                                         else
90                                                 xmlWriter.WriteStartElement("error");
91                                 
92                                         xmlWriter.WriteStartElement("message");
93                                         xmlWriter.WriteCData(caseResult.Message);
94                                         xmlWriter.WriteEndElement();
95                                 
96                                         xmlWriter.WriteStartElement("stack-trace");
97                                         if(caseResult.StackTrace != null)
98                                                 xmlWriter.WriteCData(StackTraceFilter.Filter(caseResult.StackTrace));
99                                         xmlWriter.WriteEndElement();
100                                 
101                                         xmlWriter.WriteEndElement();
102                                 }
103                                 
104                         }
105                         else
106                         {
107                                 xmlWriter.WriteStartElement("reason");
108                                 xmlWriter.WriteStartElement("message");
109                                 xmlWriter.WriteCData(caseResult.Message);
110                                 xmlWriter.WriteEndElement();
111                                 xmlWriter.WriteEndElement();
112                         }
113             
114                         xmlWriter.WriteEndElement();
115                 }
116
117                 public void visit(TestSuiteResult suiteResult) 
118                 {
119                         xmlWriter.WriteStartElement("test-suite");
120                         xmlWriter.WriteAttributeString("name",suiteResult.Name);
121                         xmlWriter.WriteAttributeString("success", suiteResult.IsSuccess.ToString());
122                         xmlWriter.WriteAttributeString("time", suiteResult.Time.ToString());
123             
124                         xmlWriter.WriteStartElement("results");                  
125                         foreach (TestResult result in suiteResult.Results)
126                         {
127                                 result.Accept(this);
128                         }
129                         xmlWriter.WriteEndElement();
130
131                         xmlWriter.WriteEndElement();
132                 }
133
134                 public void Write()
135                 {
136                         xmlWriter.WriteEndElement();
137
138                         xmlWriter.WriteEndDocument();
139                         xmlWriter.Flush();
140                         xmlWriter.Close();
141                 }
142         }
143 }