* roottypes.cs: Rename from tree.cs.
[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                         Assertion.AssertEquals ("#Tr01", value, buffer.ToString ());
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                         Trace.WriteLine ("Error 1: File not found");
83                         Trace.WriteLine ("Error 2: Directory not found");
84                         Trace.Unindent ();
85                         Trace.WriteLine ("End of list of errors");
86
87                         Assertion.AssertEquals ("#In01", value, buffer.ToString());
88                 }
89
90                 // Make sure that TraceListener properties (IndentLevel, IndentSize) are
91                 // modified when the corresponding Trace properties are changed.
92                 [Test]
93                 public void AddedTraceListenerProperties ()
94                 {
95                         TraceListener t1 = new TextWriterTraceListener (Console.Out);
96                         TraceListener t2 = new TextWriterTraceListener (Console.Error);
97                         Trace.Listeners.Add(t1);
98                         Trace.Listeners.Add(t2);
99
100                         const int ExpectedSize = 5;
101                         const int ExpectedLevel = 2;
102
103                         Trace.IndentSize = ExpectedSize;
104                         Trace.IndentLevel = ExpectedLevel;
105
106                         foreach (TraceListener t in Trace.Listeners) {
107                                 string ids = "#TATLP-S-" + t.Name;
108                                 string idl = "#TATLP-L-" + t.Name;
109                                 Assertion.AssertEquals (ids, ExpectedSize, t.IndentSize);
110                                 Assertion.AssertEquals (idl, ExpectedLevel, t.IndentLevel);
111                         }
112
113                         Trace.Listeners.Remove(t1);
114                         Trace.Listeners.Remove(t2);
115                 }
116
117                 // Make sure that the TraceListener properties (IndentLevel, IndentSize)
118                 // are properly modified when the TraceListener is added to the
119                 // collection.
120                 [Test]
121                 public void Listeners_Add_Values()
122                 {
123                         const int ExpectedLevel = 0;
124                         const int ExpectedSize = 4;
125                         Trace.IndentLevel = ExpectedLevel;
126                         Trace.IndentSize = ExpectedSize;
127                         TraceListener tl = new TextWriterTraceListener(Console.Out);
128
129                         tl.IndentLevel = 2*ExpectedLevel;
130                         tl.IndentSize = 2*ExpectedSize;
131
132                         Trace.Listeners.Add(tl);
133
134                         // Assertion.Assert that the listener we added has been set to the correct indent
135                         // level.
136                         Assertion.AssertEquals ("#LATL-L", ExpectedLevel, tl.IndentLevel);
137                         Assertion.AssertEquals ("#LATL-S", ExpectedSize, tl.IndentSize);
138
139                         // Assertion.Assert that all listeners in the collection have the same level.
140                         foreach (TraceListener t in Trace.Listeners)
141                         {
142                                 string idl = "#LATL-L:" + t.Name;
143                                 string ids = "#LATL-S:" + t.Name;
144                                 Assertion.AssertEquals(idl, ExpectedLevel, t.IndentLevel);
145                                 Assertion.AssertEquals(ids, ExpectedSize, t.IndentSize);
146                         }
147                 }
148
149                 // IndentSize, IndentLevel are thread-static
150
151                 class MyTraceListener : TraceListener
152                 {
153                         public int Writes;
154                         public int WriteLines;
155
156                         public MyTraceListener ()
157                                 : base ("mt-test")
158                         {
159                         }
160
161                         public override void Write (string msg)
162                         {
163                                 ++Writes;
164                         }
165
166                         public override void WriteLine (string msg)
167                         {
168                                 ++WriteLines;
169                         }
170                 }
171
172                 class MultiThreadModify
173                 {
174                         public MyTraceListener listener = new MyTraceListener ();
175
176                         public const int MaxIterations = 10000;
177
178                         public String Exception = null;
179
180                         public MultiThreadModify ()
181                         {
182                                 Trace.Listeners.Add (listener);
183                         }
184
185                         public void Write ()
186                         {
187                                 try {
188                                         for (int i = 0; i < MaxIterations; ++i)
189                                                 Trace.WriteLine ("message " + i + "... ");
190                                 }
191                                 catch (Exception e) {
192                                         Exception = string.Format (
193                                                         "#MTMW: Exception emitted from Trace.WriteLine: {0}", e);
194                                 }
195                         }
196
197                         public void Remove ()
198                         {
199                                 try {
200                                         Trace.Listeners.Remove (listener);
201                                 }
202                                 catch (Exception e) {
203                                         Exception = string.Format (
204                                                         "#MTMR: Exception emitted from Trace.Listeners.Remove: {0}", e);
205                                 }
206                         }
207                 }
208
209                 [Test]
210                 [Category ("NotWorking")]
211                 // Is this even valid !?!?!?!
212                 public void TestMultiThreadModify ()
213                 {
214                         MultiThreadModify m = new MultiThreadModify ();
215
216                         Thread t1 = new Thread (new ThreadStart (m.Write));
217                         Thread t2 = new Thread (new ThreadStart (m.Remove));
218
219                         t1.Start ();
220                         t2.Start ();
221
222                         t1.Join ();
223                         t2.Join ();
224
225                         Assert.IsTrue (m.Exception == null, m.Exception);
226                         Assert.AreEqual (MultiThreadModify.MaxIterations, m.listener.WriteLines,
227                                         "#tmtm: listener was removed before iterations were completed");
228                 }
229         }
230 }
231