2 // TraceTest.cs - NUnit Test Cases for System.Diagnostics.Trace
5 // Jonathan Pryor (jonpryor@vt.edu)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
9 // (C) 2003 Martin Willemoes Hansen
12 // We want tracing enabled, so...
15 using NUnit.Framework;
18 using System.Diagnostics;
19 using System.Threading;
21 namespace MonoTests.System.Diagnostics {
24 public class TraceTest {
26 private StringWriter buffer;
27 private TraceListener listener;
30 public void GetReady ()
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");
36 buffer = new StringWriter ();
37 listener = new TextWriterTraceListener (buffer, "TestOutput");
38 Trace.Listeners.Clear ();
39 Trace.Listeners.Add (listener);
40 Trace.AutoFlush = true;
46 // Trace.Listeners.Add (new DefaultTraceListener ());
47 Trace.Listeners.Remove (listener);
50 // Make sure that when we get the output we expect....
52 public void Tracing ()
54 Trace.IndentLevel = 0;
58 "Entering Main" + Environment.NewLine +
59 "Exiting Main" + Environment.NewLine;
61 Trace.WriteLine ("Entering Main");
62 Trace.WriteLine ("Exiting Main");
64 Assertion.AssertEquals ("#Tr01", value, buffer.ToString ());
67 // Make sure we get the output we expect in the presence of indenting...
71 Trace.IndentLevel = 0;
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;
80 Trace.WriteLine ("List of errors:");
82 Trace.WriteLine ("Error 1: File not found");
83 Trace.WriteLine ("Error 2: Directory not found");
85 Trace.WriteLine ("End of list of errors");
87 Assertion.AssertEquals ("#In01", value, buffer.ToString());
90 // Make sure that TraceListener properties (IndentLevel, IndentSize) are
91 // modified when the corresponding Trace properties are changed.
93 public void AddedTraceListenerProperties ()
95 TraceListener t1 = new TextWriterTraceListener (Console.Out);
96 TraceListener t2 = new TextWriterTraceListener (Console.Error);
97 Trace.Listeners.Add(t1);
98 Trace.Listeners.Add(t2);
100 const int ExpectedSize = 5;
101 const int ExpectedLevel = 2;
103 Trace.IndentSize = ExpectedSize;
104 Trace.IndentLevel = ExpectedLevel;
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);
113 Trace.Listeners.Remove(t1);
114 Trace.Listeners.Remove(t2);
117 // Make sure that the TraceListener properties (IndentLevel, IndentSize)
118 // are properly modified when the TraceListener is added to the
121 public void Listeners_Add_Values()
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);
129 tl.IndentLevel = 2*ExpectedLevel;
130 tl.IndentSize = 2*ExpectedSize;
132 Trace.Listeners.Add(tl);
134 // Assertion.Assert that the listener we added has been set to the correct indent
136 Assertion.AssertEquals ("#LATL-L", ExpectedLevel, tl.IndentLevel);
137 Assertion.AssertEquals ("#LATL-S", ExpectedSize, tl.IndentSize);
139 // Assertion.Assert that all listeners in the collection have the same level.
140 foreach (TraceListener t in Trace.Listeners)
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);
149 // IndentSize, IndentLevel are thread-static
151 class MyTraceListener : TraceListener
154 public int WriteLines;
156 public MyTraceListener ()
161 public override void Write (string msg)
166 public override void WriteLine (string msg)
172 class MultiThreadModify
174 public MyTraceListener listener = new MyTraceListener ();
176 public const int MaxIterations = 10000;
178 public String Exception = null;
180 public MultiThreadModify ()
182 Trace.Listeners.Add (listener);
188 for (int i = 0; i < MaxIterations; ++i)
189 Trace.WriteLine ("message " + i + "... ");
191 catch (Exception e) {
192 Exception = string.Format (
193 "#MTMW: Exception emitted from Trace.WriteLine: {0}", e);
197 public void Remove ()
200 Trace.Listeners.Remove (listener);
202 catch (Exception e) {
203 Exception = string.Format (
204 "#MTMR: Exception emitted from Trace.Listeners.Remove: {0}", e);
210 [Category ("NotWorking")]
211 // Is this even valid !?!?!?!
212 public void TestMultiThreadModify ()
214 MultiThreadModify m = new MultiThreadModify ();
216 Thread t1 = new Thread (new ThreadStart (m.Write));
217 Thread t2 = new Thread (new ThreadStart (m.Remove));
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");