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