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