TraceTest.cs 5.7 KB

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