//
// TraceTest.cs - NUnit Test Cases for System.Diagnostics.Trace
//
-// Jonathan Pryor (jonpryor@vt.edu)
+// Authors:
+// Jonathan Pryor (jonpryor@vt.edu)
+// Martin Willemoes Hansen (mwh@sysrq.dk)
//
// (C) Jonathan Pryor
+// (C) 2003 Martin Willemoes Hansen
//
// We want tracing enabled, so...
using System;
using System.IO;
using System.Diagnostics;
+using System.Threading;
namespace MonoTests.System.Diagnostics {
- public class TraceTest : TestCase {
+ [TestFixture]
+ public class TraceTest {
private StringWriter buffer;
private TraceListener listener;
- public TraceTest ()
- : base ("System.Diagnostics.Trace testsuite")
- {
- }
-
- public TraceTest (string name)
- : base(name)
- {
- }
-
- protected override void SetUp ()
+ [SetUp]
+ public void GetReady ()
{
// We don't want to deal with the default listener, which can send the
// output to various places (Debug stream, Console.Out, ...)
// Trace.Listeners.Remove ("Default");
buffer = new StringWriter ();
-
listener = new TextWriterTraceListener (buffer, "TestOutput");
-
+ Trace.Listeners.Clear ();
Trace.Listeners.Add (listener);
-
Trace.AutoFlush = true;
-
}
- protected override void TearDown ()
+ [TearDown]
+ public void Clear ()
{
// Trace.Listeners.Add (new DefaultTraceListener ());
Trace.Listeners.Remove (listener);
}
- public static ITest Suite {
- get {
- return new TestSuite (typeof (TraceTest));
- }
- }
-
// Make sure that when we get the output we expect....
- public void TestTracing ()
+ [Test]
+ public void Tracing ()
{
+ Trace.IndentLevel = 0;
+ Trace.IndentSize = 4;
+
string value =
"Entering Main" + Environment.NewLine +
"Exiting Main" + Environment.NewLine;
Trace.WriteLine ("Entering Main");
Trace.WriteLine ("Exiting Main");
- AssertEquals ("#Tr01", value, buffer.ToString ());
+ Assertion.AssertEquals ("#Tr01", value, buffer.ToString ());
}
// Make sure we get the output we expect in the presence of indenting...
- public void TestIndent ()
+ [Test]
+ public void Indent ()
{
+ Trace.IndentLevel = 0;
+ Trace.IndentSize = 4;
+
string value =
"List of errors:" + Environment.NewLine +
" Error 1: File not found" + Environment.NewLine +
Trace.Unindent ();
Trace.WriteLine ("End of list of errors");
- AssertEquals ("#In01", value, buffer.ToString());
+ Assertion.AssertEquals ("#In01", value, buffer.ToString());
}
// Make sure that TraceListener properties (IndentLevel, IndentSize) are
// modified when the corresponding Trace properties are changed.
- public void TestAddedTraceListenerProperties ()
+ [Test]
+ public void AddedTraceListenerProperties ()
{
TraceListener t1 = new TextWriterTraceListener (Console.Out);
TraceListener t2 = new TextWriterTraceListener (Console.Error);
foreach (TraceListener t in Trace.Listeners) {
string ids = "#TATLP-S-" + t.Name;
string idl = "#TATLP-L-" + t.Name;
- AssertEquals (ids, ExpectedSize, t.IndentSize);
- AssertEquals (idl, ExpectedLevel, t.IndentLevel);
+ Assertion.AssertEquals (ids, ExpectedSize, t.IndentSize);
+ Assertion.AssertEquals (idl, ExpectedLevel, t.IndentLevel);
}
Trace.Listeners.Remove(t1);
// Make sure that the TraceListener properties (IndentLevel, IndentSize)
// are properly modified when the TraceListener is added to the
// collection.
- public void TestListeners_Add_Values()
+ [Test]
+ public void Listeners_Add_Values()
{
- const int ExpectedLevel = 5;
- const int ExpectedSize = 3;
+ const int ExpectedLevel = 0;
+ const int ExpectedSize = 4;
Trace.IndentLevel = ExpectedLevel;
Trace.IndentSize = ExpectedSize;
TraceListener tl = new TextWriterTraceListener(Console.Out);
Trace.Listeners.Add(tl);
- // Assert that the listener we added has been set to the correct indent
+ // Assertion.Assert that the listener we added has been set to the correct indent
// level.
- AssertEquals ("#LATL-L", ExpectedLevel, tl.IndentLevel);
- AssertEquals ("#LATL-S", ExpectedSize, tl.IndentSize);
+ Assertion.AssertEquals ("#LATL-L", ExpectedLevel, tl.IndentLevel);
+ Assertion.AssertEquals ("#LATL-S", ExpectedSize, tl.IndentSize);
- // Assert that all listeners in the collection have the same level.
+ // Assertion.Assert that all listeners in the collection have the same level.
foreach (TraceListener t in Trace.Listeners)
{
string idl = "#LATL-L:" + t.Name;
string ids = "#LATL-S:" + t.Name;
- AssertEquals(idl, ExpectedLevel, t.IndentLevel);
- AssertEquals(ids, ExpectedSize, t.IndentSize);
+ Assertion.AssertEquals(idl, ExpectedLevel, t.IndentLevel);
+ Assertion.AssertEquals(ids, ExpectedSize, t.IndentSize);
}
}
// IndentSize, IndentLevel are thread-static
+
+ class MyTraceListener : TraceListener
+ {
+ public int Writes;
+ public int WriteLines;
+
+ public MyTraceListener ()
+ : base ("mt-test")
+ {
+ }
+
+ public override void Write (string msg)
+ {
+ ++Writes;
+ }
+
+ public override void WriteLine (string msg)
+ {
+ ++WriteLines;
+ }
+ }
+
+ class MultiThreadModify
+ {
+ public MyTraceListener listener = new MyTraceListener ();
+
+ public const int MaxIterations = 10000;
+
+ public String Exception = null;
+
+ public MultiThreadModify ()
+ {
+ Trace.Listeners.Add (listener);
+ }
+
+ public void Write ()
+ {
+ try {
+ for (int i = 0; i < MaxIterations; ++i)
+ Trace.WriteLine ("message " + i + "... ");
+ }
+ catch (Exception e) {
+ Exception = string.Format (
+ "#MTMW: Exception emitted from Trace.WriteLine: {0}", e);
+ }
+ }
+
+ public void Remove ()
+ {
+ try {
+ Trace.Listeners.Remove (listener);
+ }
+ catch (Exception e) {
+ Exception = string.Format (
+ "#MTMR: Exception emitted from Trace.Listeners.Remove: {0}", e);
+ }
+ }
+ }
+
+ [Test]
+ public void TestMultiThreadModify ()
+ {
+ MultiThreadModify m = new MultiThreadModify ();
+
+ Thread t1 = new Thread (new ThreadStart (m.Write));
+ Thread t2 = new Thread (new ThreadStart (m.Remove));
+
+ t1.Start ();
+ t2.Start ();
+
+ t1.Join ();
+ t2.Join ();
+
+ Assert.IsTrue (m.Exception == null, m.Exception);
+ Assert.AreEqual (MultiThreadModify.MaxIterations, m.listener.WriteLines,
+ "#tmtm: listener was removed before iterations were completed");
+ }
}
}