2 // XmlWriterTraceListenerTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2007 Novell, Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using NUnit.Framework;
36 using System.Diagnostics;
37 using System.Threading;
40 namespace MonoTests.System.Diagnostics
43 public class XmlWriterTraceListenerTest
45 string sample1 = @"<E2ETraceEvent xmlns='http://schemas.microsoft.com/2004/06/E2ETraceEvent'><System xmlns='http://schemas.microsoft.com/2004/06/windows/eventlog/system'><EventID>0</EventID><Type>3</Type><SubType Name='Information'>0</SubType><Level>8</Level><TimeCreated SystemTime='2007-04-19T21:18:30.6250000+09:00' /><Source Name='Trace' /><Correlation ActivityID='{00000000-0000-0000-0000-000000000000}' /><Execution ProcessName='zzz' ProcessID='4776' ThreadID='1' /><Channel/><Computer>PC</Computer></System><ApplicationData>sample</ApplicationData></E2ETraceEvent>";
47 string sample2 = @"<E2ETraceEvent xmlns='http://schemas.microsoft.com/2004/06/E2ETraceEvent'><System xmlns='http://schemas.microsoft.com/2004/06/windows/eventlog/system'><EventID>4</EventID><Type>3</Type><SubType Name='Error'>0</SubType><Level>2</Level><TimeCreated SystemTime='2007-04-19T21:18:30.6250000+09:00' /><Source Name='' /><Correlation ActivityID='{00000000-0000-0000-0000-000000000000}' /><Execution ProcessName='zzz' ProcessID='4776' ThreadID='1' /><Channel/><Computer>PC</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent>";
49 string sample3 = @"<E2ETraceEvent xmlns='http://schemas.microsoft.com/2004/06/E2ETraceEvent'><System xmlns='http://schemas.microsoft.com/2004/06/windows/eventlog/system'><EventID>5</EventID><Type>3</Type><SubType Name='Error'>0</SubType><Level>2</Level><TimeCreated SystemTime='2007-04-19T21:18:30.6250000+09:00' /><Source Name='bulldog' /><Correlation ActivityID='{00000000-0000-0000-0000-000000000000}' /><Execution ProcessName='zzz' ProcessID='4776' ThreadID='1' /><Channel/><Computer>PC</Computer></System><ApplicationData></ApplicationData></E2ETraceEvent>";
51 string sample4 = @"<E2ETraceEvent xmlns='http://schemas.microsoft.com/2004/06/E2ETraceEvent'><System xmlns='http://schemas.microsoft.com/2004/06/windows/eventlog/system'><EventID>2</EventID><Type>3</Type><SubType Name='Error'>0</SubType><Level>2</Level><TimeCreated SystemTime='2007-04-19T12:18:30.6250000Z' /><Source Name='' /><Correlation ActivityID='{00000000-0000-0000-0000-000000000000}' /><Execution ProcessName='zzz' ProcessID='4776' ThreadID='1' /><Channel/><Computer>PC</Computer></System><ApplicationData><TraceData></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns='http://schemas.microsoft.com/2004/06/E2ETraceEvent'><System xmlns='http://schemas.microsoft.com/2004/06/windows/eventlog/system'><EventID>3</EventID><Type>3</Type><SubType Name='Error'>0</SubType><Level>2</Level><TimeCreated SystemTime='2007-04-19T12:18:30.6250000Z' /><Source Name='' /><Correlation ActivityID='{00000000-0000-0000-0000-000000000000}' /><Execution ProcessName='zzz' ProcessID='4776' ThreadID='1' /><Channel/><Computer>PC</Computer></System><ApplicationData><TraceData></TraceData></ApplicationData></E2ETraceEvent>";
53 string sample5 = @"<E2ETraceEvent xmlns='http://schemas.microsoft.com/2004/06/E2ETraceEvent'><System xmlns='http://schemas.microsoft.com/2004/06/windows/eventlog/system'><EventID>7</EventID><Type>3</Type><SubType Name='Error'>0</SubType><Level>2</Level><TimeCreated SystemTime='2007-04-19T12:18:30.6250000Z' /><Source Name='' /><Correlation ActivityID='{00000000-0000-0000-0000-000000000000}' /><Execution ProcessName='zzz' ProcessID='4776' ThreadID='1' /><Channel/><Computer>PC</Computer></System><ApplicationData><TraceData><DataItem>XYZ</DataItem></TraceData></ApplicationData></E2ETraceEvent><E2ETraceEvent xmlns='http://schemas.microsoft.com/2004/06/E2ETraceEvent'><System xmlns='http://schemas.microsoft.com/2004/06/windows/eventlog/system'><EventID>7</EventID><Type>3</Type><SubType Name='Error'>0</SubType><Level>2</Level><TimeCreated SystemTime='2007-04-19T12:18:30.6250000Z' /><Source Name='' /><Correlation ActivityID='{00000000-0000-0000-0000-000000000000}' /><Execution ProcessName='zzz' ProcessID='4776' ThreadID='1' /><Channel/><Computer>PC</Computer></System><ApplicationData><TraceData><DataItem>ABC</DataItem><DataItem>DEF</DataItem></TraceData></ApplicationData></E2ETraceEvent>";
55 string sample6 = "<E2ETraceEvent xmlns='http://schemas.microsoft.com/2004/06/E2ETraceEvent'><System xmlns='http://schemas.microsoft.com/2004/06/windows/eventlog/system'><EventID>0</EventID><Type>3</Type><SubType Name='Transfer'>0</SubType><Level>255</Level><TimeCreated SystemTime='2007-04-19T21:18:30.6250000+09:00' /><Source Name='bulldog' /><Correlation ActivityID='{00000000-0000-0000-0000-000000000000}' RelatedActivityID='{00000000-0000-0000-0000-000000000000}' /><Execution ProcessName='zzz' ProcessID='4776' ThreadID='1' /><Channel/><Computer>PC</Computer></System><ApplicationData>hoge</ApplicationData></E2ETraceEvent>";
57 string sample7 = "<E2ETraceEvent xmlns='http://schemas.microsoft.com/2004/06/E2ETraceEvent'><System xmlns='http://schemas.microsoft.com/2004/06/windows/eventlog/system'><EventID>0</EventID><Type>3</Type><SubType Name='Error'>0</SubType><Level>2</Level><TimeCreated SystemTime='2007-04-19T21:18:30.6250000+09:00' /><Source Name='Trace' /><Correlation ActivityID='{00000000-0000-0000-0000-000000000000}' /><Execution ProcessName='zzz' ProcessID='4776' ThreadID='1' /><Channel/><Computer>PC</Computer></System><ApplicationData>error summary error details</ApplicationData></E2ETraceEvent>";
60 [Ignore ("the test should be rewritten to not compare instance-specific items.")]
61 public void WriteLine1 ()
63 StringWriter sw = new StringWriter ();
64 XmlWriterTraceListener x = new XmlWriterTraceListener (sw);
65 x.WriteLine ("sample");
67 Assert.AreEqual (sample1.Replace ('\'', '"'), sw.ToString ());
71 [Ignore ("the test should be rewritten to not compare instance-specific items.")]
72 public void TraceEvent1 ()
74 StringWriter sw = new StringWriter ();
75 XmlWriterTraceListener x = new XmlWriterTraceListener (sw);
76 x.TraceEvent (null, null, TraceEventType.Error, 4, null);
78 Assert.AreEqual (sample2.Replace ('\'', '"'), sw.ToString ());
82 [Ignore ("the test should be rewritten to not compare instance-specific items.")]
83 public void TraceEvent2 ()
85 StringWriter sw = new StringWriter ();
86 XmlWriterTraceListener x = new XmlWriterTraceListener (sw);
87 x.TraceEvent (null, "bulldog", TraceEventType.Error, 5);
89 Assert.AreEqual (sample3.Replace ('\'', '"'), sw.ToString ());
93 [Ignore ("the test should be rewritten to not compare instance-specific items.")]
94 public void TraceDataWithCache1 ()
96 StringWriter sw = new StringWriter ();
97 XmlWriterTraceListener x = new XmlWriterTraceListener (sw);
98 TraceEventCache cc = new TraceEventCache ();
99 x.TraceData (cc, null, TraceEventType.Error, 2);
100 x.TraceData (cc, null, TraceEventType.Error, 3);
102 Assert.AreEqual (sample4.Replace ('\'', '"'), sw.ToString ());
106 [Ignore ("the test should be rewritten to not compare instance-specific items.")]
107 public void TraceDataWithCache2 ()
109 StringWriter sw = new StringWriter ();
110 XmlWriterTraceListener x = new XmlWriterTraceListener (sw);
111 TraceEventCache cc = new TraceEventCache ();
112 x.TraceData (cc, null, TraceEventType.Error, 7, "XYZ");
113 x.TraceData (cc, null, TraceEventType.Error, 7, "ABC", "DEF");
115 Assert.AreEqual (sample5.Replace ('\'', '"'), sw.ToString ());
119 [Ignore ("the test should be rewritten to not compare instance-specific items.")]
120 public void TraceTransfer1 ()
122 StringWriter sw = new StringWriter ();
123 XmlWriterTraceListener x = new XmlWriterTraceListener (sw);
124 x.TraceTransfer (null, "bulldog", 0, "hoge", Guid.Empty);
126 Assert.AreEqual (sample6.Replace ('\'', '"'), sw.ToString ());
130 [Ignore ("the test should be rewritten to not compare instance-specific items.")]
133 StringWriter sw = new StringWriter ();
134 XmlWriterTraceListener x = new XmlWriterTraceListener (sw);
135 TraceEventCache cc = new TraceEventCache ();
136 x.Fail ("error summary", "error details");
138 Assert.AreEqual (sample7.Replace ('\'', '"'), sw.ToString ());
142 public void XPathNavigatorAsData ()
144 // While XmlReader, XmlDocument and XDocument are not supported as direct xml content (i.e. to not get escaped), XPathNavigator is.
145 var sw = new StringWriter ();
146 var xl = new XmlWriterTraceListener (sw);
147 var doc = new XmlDocument ();
148 string xml = "<root><child xmlns=\"urn:foo\">text</child></root>";
150 xl.TraceData (null, "my source", TraceEventType.Information, 1, doc.CreateNavigator (), doc.CreateNavigator ());
151 // Note that it does not result in "<root xmlns=''>...".
152 // See XmlWriterTraceListener.TraceCore() for details.
153 Assert.IsTrue (sw.ToString ().IndexOf (xml) > 0, "#1");
154 Assert.IsTrue (sw.ToString ().IndexOf ("</DataItem><DataItem>") > 0, "#2");