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
14 // We want tracing enabled, so...
17 using NUnit.Framework;
20 using System.Diagnostics;
21 using System.Threading;
23 namespace MonoTests.System.Diagnostics {
26 public class TraceTest {
28 private StringWriter buffer;
29 private TraceListener listener;
32 public void GetReady ()
34 // We don't want to deal with the default listener, which can send the
35 // output to various places (Debug stream, Console.Out, ...)
36 // Trace.Listeners.Remove ("Default");
38 buffer = new StringWriter ();
39 listener = new TextWriterTraceListener (buffer, "TestOutput");
40 Trace.Listeners.Clear ();
41 Trace.Listeners.Add (listener);
42 Trace.AutoFlush = true;
48 // Trace.Listeners.Add (new DefaultTraceListener ());
49 Trace.Listeners.Remove (listener);
52 // Make sure that when we get the output we expect....
54 public void Tracing ()
56 Trace.IndentLevel = 0;
60 "Entering Main" + Environment.NewLine +
61 "Exiting Main" + Environment.NewLine;
63 Trace.WriteLine ("Entering Main");
64 Trace.WriteLine ("Exiting Main");
66 Assert.AreEqual (value, buffer.ToString (), "#Tr01");
69 // Make sure we get the output we expect in the presence of indenting...
73 Trace.IndentLevel = 0;
77 "List of errors:" + Environment.NewLine +
78 " Error 1: File not found" + Environment.NewLine +
79 " Error 2: Directory not found" + Environment.NewLine +
80 "End of list of errors" + Environment.NewLine;
82 Trace.WriteLine ("List of errors:");
84 Assert.AreEqual (1, Trace.IndentLevel);
85 Trace.WriteLine ("Error 1: File not found");
86 Trace.WriteLine ("Error 2: Directory not found");
88 Assert.AreEqual (0, Trace.IndentLevel);
89 Trace.WriteLine ("End of list of errors");
91 Assert.AreEqual (value, buffer.ToString(), "#In01");
94 // Make sure that TraceListener properties (IndentLevel, IndentSize) are
95 // modified when the corresponding Trace properties are changed.
97 public void AddedTraceListenerProperties ()
99 TraceListener t1 = new TextWriterTraceListener (Console.Out);
100 TraceListener t2 = new TextWriterTraceListener (Console.Error);
101 Trace.Listeners.Add(t1);
102 Trace.Listeners.Add(t2);
104 const int ExpectedSize = 5;
105 const int ExpectedLevel = 2;
107 Trace.IndentSize = ExpectedSize;
108 Trace.IndentLevel = ExpectedLevel;
110 foreach (TraceListener t in Trace.Listeners) {
111 string ids = "#TATLP-S-" + t.Name;
112 string idl = "#TATLP-L-" + t.Name;
113 Assert.AreEqual (ExpectedSize, t.IndentSize, ids);
114 Assert.AreEqual (ExpectedLevel, t.IndentLevel, idl);
117 Trace.Listeners.Remove(t1);
118 Trace.Listeners.Remove(t2);
121 // Make sure that the TraceListener properties (IndentLevel, IndentSize)
122 // are properly modified when the TraceListener is added to the
125 public void Listeners_Add_Values()
127 const int ExpectedLevel = 0;
128 const int ExpectedSize = 4;
129 Trace.IndentLevel = ExpectedLevel;
130 Trace.IndentSize = ExpectedSize;
131 TraceListener tl = new TextWriterTraceListener(Console.Out);
133 tl.IndentLevel = 2*ExpectedLevel;
134 tl.IndentSize = 2*ExpectedSize;
136 Trace.Listeners.Add(tl);
138 // Assertion.Assert that the listener we added has been set to the correct indent
140 Assert.AreEqual (ExpectedLevel, tl.IndentLevel, "#LATL-L");
141 Assert.AreEqual (ExpectedSize, tl.IndentSize, "#LATL-S");
143 // Assertion.Assert that all listeners in the collection have the same level.
144 foreach (TraceListener t in Trace.Listeners)
146 string idl = "#LATL-L:" + t.Name;
147 string ids = "#LATL-S:" + t.Name;
148 Assert.AreEqual (ExpectedLevel, t.IndentLevel, idl);
149 Assert.AreEqual (ExpectedSize, t.IndentSize, ids);
153 // IndentSize, IndentLevel are thread-static
155 class MyTraceListener : TraceListener
158 public int WriteLines;
160 public MyTraceListener ()
165 public override void Write (string msg)
170 public override void WriteLine (string msg)
176 class MultiThreadModify
178 public MyTraceListener listener = new MyTraceListener ();
180 public const int MaxIterations = 10000;
182 public String Exception = null;
184 public MultiThreadModify ()
186 Trace.Listeners.Add (listener);
192 for (int i = 0; i < MaxIterations; ++i)
193 Trace.WriteLine ("message " + i + "... ");
195 catch (Exception e) {
196 Exception = string.Format (
197 "#MTMW: Exception emitted from Trace.WriteLine: {0}", e);
201 public void Remove ()
204 Trace.Listeners.Remove (listener);
206 catch (Exception e) {
207 Exception = string.Format (
208 "#MTMR: Exception emitted from Trace.Listeners.Remove: {0}", e);
214 [Category ("NotWorking")]
215 // Is this even valid !?!?!?!
216 public void TestMultiThreadModify ()
218 MultiThreadModify m = new MultiThreadModify ();
220 Thread t1 = new Thread (new ThreadStart (m.Write));
221 Thread t2 = new Thread (new ThreadStart (m.Remove));
229 Assert.IsTrue (m.Exception == null, m.Exception);
230 Assert.AreEqual (MultiThreadModify.MaxIterations, m.listener.WriteLines,
231 "#tmtm: listener was removed before iterations were completed");