2004-12-03 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / nunit20 / util / XmlResultVisitor.cs
1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
3 '
4 ' Copyright © 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright © 2000-2003 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 © 2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright © 2000-2003 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.Util
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                 private TextWriter writer;
45                 private MemoryStream memoryStream;
46
47                 public XmlResultVisitor(string fileName, TestResult result)
48                 {
49                         xmlWriter = new XmlTextWriter( new StreamWriter(fileName, false, System.Text.Encoding.UTF8) );
50                         Initialize(result);
51                 }
52
53                 public XmlResultVisitor( TextWriter writer, TestResult result )
54                 {
55                         this.memoryStream = new MemoryStream();
56                         this.writer = writer;
57                         this.xmlWriter = new XmlTextWriter( new StreamWriter( memoryStream, System.Text.Encoding.UTF8 ) );
58                         Initialize( result );
59                 }
60
61                 private void Initialize(TestResult result) 
62                 {
63                         ResultSummarizer summaryResults = new ResultSummarizer(result);
64
65                         xmlWriter.Formatting = Formatting.Indented;
66                         xmlWriter.WriteStartDocument(false);
67                         xmlWriter.WriteComment("This file represents the results of running a test suite");
68
69                         xmlWriter.WriteStartElement("test-results");
70
71                         xmlWriter.WriteAttributeString("name", summaryResults.Name);
72                         xmlWriter.WriteAttributeString("total", summaryResults.ResultCount.ToString());
73                         xmlWriter.WriteAttributeString("failures", summaryResults.Failures.ToString());
74                         xmlWriter.WriteAttributeString("not-run", summaryResults.TestsNotRun.ToString());
75
76                         DateTime now = DateTime.Now;
77                         xmlWriter.WriteAttributeString("date", now.ToShortDateString());
78                         xmlWriter.WriteAttributeString("time", now.ToShortTimeString());
79                 }
80
81                 public void Visit(TestCaseResult caseResult) 
82                 {
83                         xmlWriter.WriteStartElement("test-case");
84                         xmlWriter.WriteAttributeString("name",caseResult.Name);
85
86                         if(caseResult.Description != null)
87                                 xmlWriter.WriteAttributeString("description", caseResult.Description);
88
89                         xmlWriter.WriteAttributeString("executed", caseResult.Executed.ToString());
90                         if(caseResult.Executed)
91                         {
92                                 xmlWriter.WriteAttributeString("success", caseResult.IsSuccess.ToString());
93
94                                 xmlWriter.WriteAttributeString("time", caseResult.Time.ToString("#####0.000", NumberFormatInfo.InvariantInfo));
95
96                                 xmlWriter.WriteAttributeString("asserts", caseResult.AssertCount.ToString() );
97                                 WriteCategories(caseResult);
98                                 if(caseResult.IsFailure)
99                                 {
100                                         if(caseResult.IsFailure)
101                                                 xmlWriter.WriteStartElement("failure");
102                                         else
103                                                 xmlWriter.WriteStartElement("error");
104                                 
105                                         xmlWriter.WriteStartElement("message");
106                                         xmlWriter.WriteString( caseResult.Message );
107                                         xmlWriter.WriteEndElement();
108                                 
109                                         xmlWriter.WriteStartElement("stack-trace");
110                                         if(caseResult.StackTrace != null)
111                                                 xmlWriter.WriteString( StackTraceFilter.Filter( caseResult.StackTrace ) );
112                                         xmlWriter.WriteEndElement();
113                                 
114                                         xmlWriter.WriteEndElement();
115                                 }
116                                 
117                         }
118                         else
119                         {
120                                 WriteCategories(caseResult);
121                                 xmlWriter.WriteStartElement("reason");
122                                 xmlWriter.WriteStartElement("message");
123                                 xmlWriter.WriteCData(caseResult.Message);
124                                 xmlWriter.WriteEndElement();
125                                 xmlWriter.WriteEndElement();
126                         }
127             
128                         xmlWriter.WriteEndElement();
129                 }
130
131                 private string EncodeCData( string text )
132                 {
133                         if ( text.IndexOf( "]]>" ) < 0 )
134                                 return text;
135
136                         return text.Replace( "]]>", "]]&gt;" );
137                 }
138
139                 public void WriteCategories(TestResult result)
140                 {
141                         if (result.Test.Categories != null && result.Test.Categories.Count > 0)
142                         {
143                                 xmlWriter.WriteStartElement("categories");
144                                 foreach (string category in result.Test.Categories)
145                                 {
146                                         xmlWriter.WriteStartElement("category");
147                                         xmlWriter.WriteAttributeString("name", category);
148                                         xmlWriter.WriteEndElement();
149                                 }
150                                 xmlWriter.WriteEndElement();
151                         }
152                 }
153
154                 public void Visit(TestSuiteResult suiteResult) 
155                 {
156                         xmlWriter.WriteStartElement("test-suite");
157                         xmlWriter.WriteAttributeString("name",suiteResult.Name);
158                         if(suiteResult.Description != null)
159                                 xmlWriter.WriteAttributeString("description", suiteResult.Description);
160
161                         xmlWriter.WriteAttributeString("success", suiteResult.IsSuccess.ToString());
162                         xmlWriter.WriteAttributeString("time", suiteResult.Time.ToString());
163                         xmlWriter.WriteAttributeString("asserts", suiteResult.AssertCount.ToString() );
164          
165                         WriteCategories(suiteResult);
166                         xmlWriter.WriteStartElement("results");                  
167                         foreach (TestResult result in suiteResult.Results)
168                         {
169                                 result.Accept(this);
170                         }
171                         xmlWriter.WriteEndElement();
172
173                         xmlWriter.WriteEndElement();
174                 }
175
176                 public void Write()
177                 {
178                         try 
179                         {
180                                 xmlWriter.WriteEndElement();
181                                 xmlWriter.WriteEndDocument();
182                                 xmlWriter.Flush();
183
184                                 if ( memoryStream != null && writer != null )
185                                 {
186                                         memoryStream.Position = 0;
187                                         using ( StreamReader rdr = new StreamReader( memoryStream ) )
188                                         {
189                                                 writer.Write( rdr.ReadToEnd() );
190                                         }
191                                 }
192
193                                 xmlWriter.Close();
194                         } 
195                         finally 
196                         {
197                                 //writer.Close();
198                         }
199                 }
200         }
201 }