1 //
2 // XmlWriterTraceListenerTest.cs
3 //
4 // Author:
5 //	Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2007 Novell, Inc.
8 //
9 
10 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
29 //
30 
31 #if !MOBILE
32 
33 using NUnit.Framework;
34 using System;
35 using System.IO;
36 using System.Diagnostics;
37 using System.Threading;
38 using System.Xml;
39 
40 namespace MonoTests.System.Diagnostics
41 {
42 	[TestFixture]
43 	public class XmlWriterTraceListenerTest
44 	{
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>";
46 
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>";
48 
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>";
50 
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>";
52 
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>";
54 
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>";
56 
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>";
58 
59 		[Test]
60 		[Ignore ("the test should be rewritten to not compare instance-specific items.")]
WriteLine1()61 		public void WriteLine1 ()
62 		{
63 			StringWriter sw = new StringWriter ();
64 			XmlWriterTraceListener x = new XmlWriterTraceListener (sw);
65 			x.WriteLine ("sample");
66 			x.Close ();
67 			Assert.AreEqual (sample1.Replace ('\'', '"'), sw.ToString ());
68 		}
69 
70 		[Test]
71 		[Ignore ("the test should be rewritten to not compare instance-specific items.")]
TraceEvent1()72 		public void TraceEvent1 ()
73 		{
74 			StringWriter sw = new StringWriter ();
75 			XmlWriterTraceListener x = new XmlWriterTraceListener (sw);
76 			x.TraceEvent (null, null, TraceEventType.Error, 4, null);
77 			x.Close ();
78 			Assert.AreEqual (sample2.Replace ('\'', '"'), sw.ToString ());
79 		}
80 
81 		[Test]
82 		[Ignore ("the test should be rewritten to not compare instance-specific items.")]
TraceEvent2()83 		public void TraceEvent2 ()
84 		{
85 			StringWriter sw = new StringWriter ();
86 			XmlWriterTraceListener x = new XmlWriterTraceListener (sw);
87 			x.TraceEvent (null, "bulldog", TraceEventType.Error, 5);
88 			x.Close ();
89 			Assert.AreEqual (sample3.Replace ('\'', '"'), sw.ToString ());
90 		}
91 
92 		[Test]
93 		[Ignore ("the test should be rewritten to not compare instance-specific items.")]
TraceDataWithCache1()94 		public void TraceDataWithCache1 ()
95 		{
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);
101 			x.Close ();
102 			Assert.AreEqual (sample4.Replace ('\'', '"'), sw.ToString ());
103 		}
104 
105 		[Test]
106 		[Ignore ("the test should be rewritten to not compare instance-specific items.")]
TraceDataWithCache2()107 		public void TraceDataWithCache2 ()
108 		{
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");
114 			x.Close ();
115 			Assert.AreEqual (sample5.Replace ('\'', '"'), sw.ToString ());
116 		}
117 
118 		[Test]
119 		[Ignore ("the test should be rewritten to not compare instance-specific items.")]
TraceTransfer1()120 		public void TraceTransfer1 ()
121 		{
122 			StringWriter sw = new StringWriter ();
123 			XmlWriterTraceListener x = new XmlWriterTraceListener (sw);
124 			x.TraceTransfer (null, "bulldog", 0, "hoge", Guid.Empty);
125 			x.Close ();
126 			Assert.AreEqual (sample6.Replace ('\'', '"'), sw.ToString ());
127 		}
128 
129 		[Test]
130 		[Ignore ("the test should be rewritten to not compare instance-specific items.")]
Fail1()131 		public void Fail1 ()
132 		{
133 			StringWriter sw = new StringWriter ();
134 			XmlWriterTraceListener x = new XmlWriterTraceListener (sw);
135 			TraceEventCache cc = new TraceEventCache ();
136 			x.Fail ("error summary", "error details");
137 			x.Close ();
138 			Assert.AreEqual (sample7.Replace ('\'', '"'), sw.ToString ());
139 		}
140 
141 		[Test]
XPathNavigatorAsData()142 		public void XPathNavigatorAsData ()
143 		{
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>";
149 			doc.LoadXml (xml);
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");
155 		}
156 	}
157 }
158 
159 #endif
160