2009-06-26 Robert Jordan <robertj@gmx.net>
[mono.git] / mcs / class / System / Test / System.Diagnostics / TraceTest.cs
1 //
2 // TraceTest.cs - NUnit Test Cases for System.Diagnostics.Trace
3 //
4 // Authors:
5 //   Jonathan Pryor (jonpryor@vt.edu)
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)
7 //
8 // (C) Jonathan Pryor
9 // (C) 2003 Martin Willemoes Hansen
10 // 
11
12 // We want tracing enabled, so...
13 #define TRACE
14
15 using NUnit.Framework;
16 using System;
17 using System.IO;
18 using System.Diagnostics;
19 using System.Threading;
20
21 namespace MonoTests.System.Diagnostics {
22
23         [TestFixture]
24         public class TraceTest {
25     
26                 private StringWriter buffer;
27                 private TraceListener listener;
28
29                 [SetUp]
30                 public void GetReady ()
31                 {
32                         // We don't want to deal with the default listener, which can send the
33                         // output to various places (Debug stream, Console.Out, ...)
34                         // Trace.Listeners.Remove ("Default");
35
36                         buffer = new StringWriter ();
37                         listener = new TextWriterTraceListener (buffer, "TestOutput");
38                         Trace.Listeners.Clear ();
39                         Trace.Listeners.Add (listener);
40                         Trace.AutoFlush = true;
41                 }
42
43                 [TearDown]
44                 public void Clear ()
45                 {
46                         // Trace.Listeners.Add (new DefaultTraceListener ());
47                         Trace.Listeners.Remove (listener);
48                 }
49
50                 // Make sure that when we get the output we expect....
51                 [Test]
52                 public void Tracing ()
53                 {
54                         Trace.IndentLevel = 0;
55                         Trace.IndentSize = 4;
56
57                         string value =  
58                                 "Entering Main" + Environment.NewLine +
59                                 "Exiting Main" + Environment.NewLine;
60
61                         Trace.WriteLine ("Entering Main");
62                         Trace.WriteLine ("Exiting Main");
63
64                         Assert.AreEqual (value, buffer.ToString (), "#Tr01");
65                 }
66
67                 // Make sure we get the output we expect in the presence of indenting...
68                 [Test]
69                 public void Indent ()
70                 {
71                         Trace.IndentLevel = 0;
72                         Trace.IndentSize = 4;
73
74                         string value =  
75                                 "List of errors:" + Environment.NewLine +
76                                 "    Error 1: File not found" + Environment.NewLine +
77                                 "    Error 2: Directory not found" + Environment.NewLine +
78                                 "End of list of errors" + Environment.NewLine;
79
80                         Trace.WriteLine ("List of errors:");
81                         Trace.Indent ();
82                         Assert.AreEqual (1, Trace.IndentLevel);
83                         Trace.WriteLine ("Error 1: File not found");
84                         Trace.WriteLine ("Error 2: Directory not found");
85                         Trace.Unindent ();
86                         Assert.AreEqual (0, Trace.IndentLevel);
87                         Trace.WriteLine ("End of list of errors");
88
89                         Assert.AreEqual (value, buffer.ToString(), "#In01");
90                 }
91
92                 // Make sure that TraceListener properties (IndentLevel, IndentSize) are
93                 // modified when the corresponding Trace properties are changed.
94                 [Test]
95                 public void AddedTraceListenerProperties ()
96                 {
97                         TraceListener t1 = new TextWriterTraceListener (Console.Out);
98                         TraceListener t2 = new TextWriterTraceListener (Console.Error);
99                         Trace.Listeners.Add(t1);
100                         Trace.Listeners.Add(t2);
101
102                         const int ExpectedSize = 5;
103                         const int ExpectedLevel = 2;
104
105                         Trace.IndentSize = ExpectedSize;
106                         Trace.IndentLevel = ExpectedLevel;
107
108                         foreach (TraceListener t in Trace.Listeners) {
109                                 string ids = "#TATLP-S-" + t.Name;
110                                 string idl = "#TATLP-L-" + t.Name;
111                                 Assert.AreEqual (ExpectedSize, t.IndentSize, ids);
112                                 Assert.AreEqual (ExpectedLevel, t.IndentLevel, idl);
113                         }
114
115                         Trace.Listeners.Remove(t1);
116                         Trace.Listeners.Remove(t2);
117                 }
118
119                 // Make sure that the TraceListener properties (IndentLevel, IndentSize)
120                 // are properly modified when the TraceListener is added to the
121                 // collection.
122                 [Test]
123                 public void Listeners_Add_Values()
124                 {
125                         const int ExpectedLevel = 0;
126                         const int ExpectedSize = 4;
127                         Trace.IndentLevel = ExpectedLevel;
128                         Trace.IndentSize = ExpectedSize;
129                         TraceListener tl = new TextWriterTraceListener(Console.Out);
130
131                         tl.IndentLevel = 2*ExpectedLevel;
132                         tl.IndentSize = 2*ExpectedSize;
133
134                         Trace.Listeners.Add(tl);
135
136                         // Assertion.Assert that the listener we added has been set to the correct indent
137                         // level.
138                         Assert.AreEqual (ExpectedLevel, tl.IndentLevel, "#LATL-L");
139                         Assert.AreEqual (ExpectedSize, tl.IndentSize, "#LATL-S");
140
141                         // Assertion.Assert that all listeners in the collection have the same level.
142                         foreach (TraceListener t in Trace.Listeners)
143                         {
144                                 string idl = "#LATL-L:" + t.Name;
145                                 string ids = "#LATL-S:" + t.Name;
146                                 Assert.AreEqual (ExpectedLevel, t.IndentLevel, idl);
147                                 Assert.AreEqual (ExpectedSize, t.IndentSize, ids);
148                         }
149                 }
150
151                 // IndentSize, IndentLevel are thread-static
152
153                 class MyTraceListener : TraceListener
154                 {
155                         public int Writes;
156                         public int WriteLines;
157
158                         public MyTraceListener ()
159                                 : base ("mt-test")
160                         {
161                         }
162
163                         public override void Write (string msg)
164                         {
165                                 ++Writes;
166                         }
167
168                         public override void WriteLine (string msg)
169                         {
170                                 ++WriteLines;
171                         }
172                 }
173
174                 class MultiThreadModify
175                 {
176                         public MyTraceListener listener = new MyTraceListener ();
177
178                         public const int MaxIterations = 10000;
179
180                         public String Exception = null;
181
182                         public MultiThreadModify ()
183                         {
184                                 Trace.Listeners.Add (listener);
185                         }
186
187                         public void Write ()
188                         {
189                                 try {
190                                         for (int i = 0; i < MaxIterations; ++i)
191                                                 Trace.WriteLine ("message " + i + "... ");
192                                 }
193                                 catch (Exception e) {
194                                         Exception = string.Format (
195                                                         "#MTMW: Exception emitted from Trace.WriteLine: {0}", e);
196                                 }
197                         }
198
199                         public void Remove ()
200                         {
201                                 try {
202                                         Trace.Listeners.Remove (listener);
203                                 }
204                                 catch (Exception e) {
205                                         Exception = string.Format (
206                                                         "#MTMR: Exception emitted from Trace.Listeners.Remove: {0}", e);
207                                 }
208                         }
209                 }
210
211                 [Test]
212                 [Category ("NotWorking")]
213                 // Is this even valid !?!?!?!
214                 public void TestMultiThreadModify ()
215                 {
216                         MultiThreadModify m = new MultiThreadModify ();
217
218                         Thread t1 = new Thread (new ThreadStart (m.Write));
219                         Thread t2 = new Thread (new ThreadStart (m.Remove));
220
221                         t1.Start ();
222                         t2.Start ();
223
224                         t1.Join ();
225                         t2.Join ();
226
227                         Assert.IsTrue (m.Exception == null, m.Exception);
228                         Assert.AreEqual (MultiThreadModify.MaxIterations, m.listener.WriteLines,
229                                         "#tmtm: listener was removed before iterations were completed");
230                 }
231         }
232 }
233