Merge pull request #4169 from evincarofautumn/fix-xmm-scanning-mac-x86
[mono.git] / mcs / class / System / Test / System.Diagnostics / TraceListenerTest.cs
1 //
2 // TraceListenerTest.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
39 namespace MonoTests.System.Diagnostics
40 {
41         [TestFixture]
42         public class TraceListenerTest
43         {
44                 [Test]
45                 public void GetSupportedAttributes ()
46                 {
47                         MyTraceListener t = new MyTraceListener ();
48                         string [] attributes = t.SupportedAttributes;
49                         Assert.IsNull (attributes);
50                 }
51
52                 [Test]
53                 public void TraceEventAndTraceData ()
54                 {
55                         StringWriter sw = new StringWriter ();
56                         TextWriterTraceListener t = new TextWriterTraceListener (sw);
57                         t.TraceEvent (null, null, TraceEventType.Error, 0, null);
58                         t.TraceEvent (null, "bulldog", TraceEventType.Error, 0);
59                         TraceEventCache cc = new TraceEventCache ();
60                         t.TraceData (cc, null, TraceEventType.Error, 0);
61                         t.TraceData (cc, null, TraceEventType.Error, 0);
62                         t.TraceTransfer (null, "bulldog", 0, "hoge", Guid.Empty);
63                         t.Close ();
64                         string expected = @" Error: 0 : 
65 bulldog Error: 0 : 
66  Error: 0 : 
67  Error: 0 : 
68 bulldog Transfer: 0 : hoge, relatedActivityId=00000000-0000-0000-0000-000000000000
69 ";
70                         Assert.AreEqual (expected.Replace ("\r\n", "\n"), sw.ToString ().Replace ("\r\n", "\n"));
71                 }
72
73                 [Test]
74                 public void TraceOptionValues ()
75                 {
76                         Assert.AreEqual (TraceOptions.None,
77                                 new TextWriterTraceListener (TextWriter.Null).TraceOutputOptions, "#1");
78                         Assert.AreEqual (TraceOptions.None,
79                                 new XmlWriterTraceListener (TextWriter.Null).TraceOutputOptions, "#2");
80
81                         StringWriter sw = new StringWriter ();
82                         TextWriterTraceListener t = new TextWriterTraceListener (sw);
83                         Trace.CorrelationManager.StartLogicalOperation ("foo");
84                         Trace.CorrelationManager.StartLogicalOperation ("bar");
85                         t.TraceOutputOptions = TraceOptions.LogicalOperationStack | TraceOptions.DateTime | TraceOptions.Timestamp;// | TraceOptions.ProcessId | TraceOptions.ThreadId | TraceOptions.Callstack;
86                         t.TraceEvent (null, null, TraceEventType.Error, 0, null);
87                         t.TraceEvent (null, "bulldog", TraceEventType.Error, 0);
88                         TraceEventCache cc = new TraceEventCache ();
89                         DateTime date = cc.DateTime;
90                         long time = cc.Timestamp;
91                         t.TraceData (cc, null, TraceEventType.Error, 0);
92                         t.TraceData (cc, null, TraceEventType.Error, 0);
93                         t.TraceTransfer (null, "bulldog", 0, "hoge", Guid.Empty);
94                         Trace.CorrelationManager.StopLogicalOperation ();
95                         Trace.CorrelationManager.StopLogicalOperation ();
96                         t.Close ();
97                         string expected = String.Format (@" Error: 0 : 
98 bulldog Error: 0 : 
99  Error: 0 : 
100     LogicalOperationStack=bar, foo
101     DateTime={0}
102     Timestamp={1}" +
103 /*
104     Callstack=   at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
105    at System.Environment.get_StackTrace()
106    at System.Diagnostics.TraceEventCache.get_Callstack()
107    at System.Diagnostics.TraceListener.WriteFooter(TraceEventCache eventCache)
108    at System.Diagnostics.TraceListener.TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, Object[] data)
109    at MonoTests.System.Diagnostics.TraceListenerTest.TraceOptionValues()
110    ...
111 */
112                                 @"
113  Error: 0 : 
114     LogicalOperationStack=bar, foo
115     DateTime={0}
116     Timestamp={1}
117 bulldog Transfer: 0 : hoge, relatedActivityId=00000000-0000-0000-0000-000000000000
118 ", date.ToString ("o"), time); // date and time are in current culture
119                         Assert.AreEqual (expected.Replace ("\r\n", "\n"), sw.ToString ().Replace ("\r\n", "\n"));
120                 }
121
122                 class MyTraceListener : TraceListener
123                 {
124                         public string [] SupportedAttributes {
125                                 get { return base.GetSupportedAttributes (); }
126                         }
127
128                         public override void Write (string message)
129                         {
130                         }
131
132                         public override void WriteLine (string message)
133                         {
134                         }
135                 }
136         }
137 }
138
139 #endif