Browse Source

New/Updated test cases. Fixed TraceTest so that it isn't causing test
failures anymore. Added SwitchesTest (to test switches) and
DiagnosticsConfigurationHandler to test .config file handling.

Issues: while the DiagnosticsConfigurationHandlerTest is reasonably
self-contained, SwitchesTest is not. To properly test switches, we need a
.exe and a .exe.config file. The needed .exe.config file that SwitchesTest
depends on is present in `test-config-file', but I'm unsure where to place it
(much less what to name it).

Additionally, SwitchesTest generates test failures at this time, principally
because of the lack of .config file support in the test system.

Finally, makefile.gnu & system_linux_test.args was changed to use the newly
added tests and to reference System.Xml.dll, which is used by
DiagnosticsConfigurationHandlerTest.

svn path=/trunk/mcs/; revision=9807

Jonathan Pryor 23 years ago
parent
commit
2571837c88

+ 10 - 0
mcs/class/System/Test/ChangeLog

@@ -1,3 +1,13 @@
+2002-12-20 Jonathan Pryor <[email protected]>
+
+	* makefile.gnu: Add System.Xml.dll to list of referenced assemblies
+	* system_linux_test.args: Add System.Xml, new test cases
+	* test-config-file: new file; sample .config file that
+	  System.Diagnostics/SwitchesTest.cs would use to test .config file
+		operations.
+		I'm not sure what this file should be named, much less it's proper
+		location.  In CVS so it's stored in a "safe" location.
+
 2002-12-08 Jackson Harper <[email protected]>
 
 	* system_linux_test.args: Remove Compiler tests

+ 2 - 0
mcs/class/System/Test/System.Diagnostics/AllTests.cs

@@ -21,6 +21,8 @@ namespace MonoTests.System.Diagnostics {
 			get {
 				TestSuite suite = new TestSuite();
 				suite.AddTest (MonoTests.System.Diagnostics.TraceTest.Suite);
+				suite.AddTest (MonoTests.System.Diagnostics.SwitchesTest.Suite);
+				suite.AddTest (MonoTests.System.Diagnostics.DiagnosticsConfigurationHandlerTest.Suite);
 				return suite;
 			}
 		}

+ 9 - 0
mcs/class/System/Test/System.Diagnostics/ChangeLog

@@ -1,3 +1,12 @@
+2002-12-20  Jonathan Pryor <[email protected]>
+
+	* AllTests.cs: Add new tests
+	* SwitchesTest.cs: new file to test switches
+	* DiagnosticsConfigurationHandlerTest.cs: new file to test
+	  DiagnosticsConfigurationHandler
+	* TraceTest.cs: Make sure that IndentLevel and IndentSize are set
+    appropriately before testing output.
+
 2002-09-23  Nick Drochak  <[email protected]>
 
 	* TraceTest.cs: Default level and size are 0 & 4, respectively.

+ 231 - 0
mcs/class/System/Test/System.Diagnostics/DiagnosticsConfigurationHandlerTest.cs

@@ -0,0 +1,231 @@
+//
+// DiagnosticsConfigurationHandlerTest.cs:
+// 	NUnit Test Cases for System.Diagnostics.DiagnosticsConfigurationHandler
+//
+// Jonathan Pryor ([email protected])
+//
+// (C) Jonathan Pryor
+// 
+
+using NUnit.Framework;
+using System;
+using System.Configuration;
+using System.Diagnostics;
+using System.Xml;
+
+namespace MonoTests.System.Diagnostics {
+
+	public class DiagnosticsConfigurationHandlerTest : TestCase {
+		
+		private const string XmlFormat = 
+			"{0}";
+			/*
+			"<system.diagnostics>" +
+			"{0}" +
+			"</system.diagnostics>";
+			 */
+    
+		private DiagnosticsConfigurationHandler handler = new DiagnosticsConfigurationHandler ();
+
+		public DiagnosticsConfigurationHandlerTest () 
+			: base ("System.Diagnostics.DiagnosticsConfigurationHandler testsuite")
+		{
+		}
+
+		public DiagnosticsConfigurationHandlerTest (string name)
+			: base(name)
+		{
+		}
+
+		protected override void SetUp ()
+		{
+		}
+
+		protected override void TearDown ()
+		{
+		}
+
+		public static ITest Suite {
+ 			get { 
+				return new TestSuite (typeof (DiagnosticsConfigurationHandlerTest)); 
+			}
+		}
+
+		public void TestSwitchesTag_Attributes ()
+		{
+			string[] attrs = {"invalid=\"yes\""};
+			ValidateExceptions ("#TST:A", "<switches {0}></switches>", attrs);
+		}
+
+		private void ValidateExceptions (string name, string format, string[] args)
+		{
+			foreach (string arg in args) {
+				string xml = string.Format (XmlFormat,
+						string.Format (format, arg));
+				try {
+					CreateHandler (xml);
+					Fail (string.Format ("{0}:{1}: no exception generated", name, arg));
+				}
+				catch (ConfigurationException ce) {
+				}
+				catch (AssertionFailedError afe) {
+					// This is generated by the Fail() statement in the try block.
+					throw;
+				}
+				catch (Exception e) {
+					Fail (string.Format ("{0}:{1}: wrong exception generated: {2} ({3}).", 
+								// name, arg, e.Message, 
+								name, arg, e.ToString(), 
+								// e.InnerException == null ? "" : e.InnerException.Message));
+								e.InnerException == null ? "" : e.InnerException.ToString()));
+				}
+			}
+		}
+
+		private void ValidateSuccess (string name, string format, string[] args)
+		{
+			foreach (string arg in args) {
+				string xml = string.Format (XmlFormat,
+						string.Format (format, arg));
+				try {
+					CreateHandler (xml);
+				}
+				catch (Exception e) {
+					Fail (string.Format ("{0}:{1}: exception generated: {2} ({3}).", 
+								// name, arg, e.Message,
+								name, arg, e.ToString(),
+								// e.InnerException == null ? "" : e.InnerException.Message));
+								e.InnerException == null ? "" : e.InnerException.ToString()));
+				}
+			}
+		}
+
+		private object CreateHandler (string xml)
+		{
+			XmlDocument d = new XmlDocument ();
+			d.LoadXml (xml);
+			return handler.Create (null, null, d);
+		}
+
+		public void TestSwitchesTag_Elements ()
+		{
+			string[] badElements = {
+				// not enough arguments
+				"<add />",
+				"<add value=\"b\"/>",
+				// too many arguments
+				"<add name=\"a\" value=\"b\" extra=\"c\"/>",
+				// wrong casing
+				"<add Name=\"a\" value=\"b\"/>",
+				"<Add Name=\"a\" value=\"b\"/>",
+				// missing args
+				"<remove />",
+				"<remove value=\"b\"/>",
+				// too many args
+				"<remove name=\"a\" value=\"b\"/>",
+				"<clear name=\"a\"/>",
+				// invalid element
+				"<invalid element=\"a\" here=\"b\"/>"
+			};
+			ValidateExceptions ("#TST:IE:Bad", "<switches>{0}</switches>", badElements);
+
+			string[] goodElements = {
+				"<add name=\"a\" value=\"b\"/>",
+				"<add name=\"a\"/>",
+				"<remove name=\"a\"/>",
+				"<clear/>"
+			};
+			ValidateSuccess ("#TST:IE:Good", "<switches>{0}</switches>", goodElements);
+		}
+
+		public void TestAssertTag ()
+		{
+			string[] goodAttributes = {
+				"",
+				"assertuienabled=\"true\"",
+				"assertuienabled=\"false\" logfilename=\"some file name\"",
+				"logfilename=\"some file name\""
+			};
+			ValidateSuccess ("#TAT:Good", "<assert {0}/>", goodAttributes);
+
+			string[] badAttributes = {
+				"AssertUiEnabled=\"true\"",
+				"LogFileName=\"foo\"",
+				"assertuienabled=\"\"",
+				"assertuienabled=\"non-boolean-value\""
+			};
+			ValidateExceptions ("#TAT:BadAttrs", "<assert {0}/>", badAttributes);
+
+			string[] badChildren = {
+				"<any element=\"here\"/>"
+			};
+			ValidateExceptions ("#TAT:BadChildren", "<assert>{0}</assert>", badChildren);
+		}
+
+		public void TestTraceTag_Attributes ()
+		{
+			string[] good = {
+				"",
+				"autoflush=\"true\"",
+				"indentsize=\"4\"",
+				"autoflush=\"false\" indentsize=\"10\""
+			};
+			ValidateSuccess ("#TTT:A:Good", "<trace {0}/>", good);
+
+			string[] bad = {
+				"AutoFlush=\"true\"",
+				"IndentSize=\"false\"",
+				"autoflush=\"non-boolean-value\"",
+				"autoflush=\"\"",
+				"indentsize=\"non-integral-value\"",
+				"indentsize=\"\"",
+				"extra=\"invalid\""
+			};
+			ValidateExceptions ("#TTT:A:Bad", "<trace {0}/>", bad);
+		}
+
+		public void TestTraceTag_Children ()
+		{
+			string[] good = {
+				// more about listeners in a different function...
+				"<listeners />"
+			};
+			ValidateSuccess ("#TTT:C:Good", "<trace>{0}</trace>", good);
+
+			string[] bad = {
+				"<listeners with=\"attribute\"/>",
+				"<invalid element=\"here\"/>"
+			};
+			ValidateExceptions ("#TTT:C:Bad", "<trace>{0}</trace>", bad);
+		}
+
+		public void TestTraceTag_Listeners ()
+		{
+			const string format = "<trace><listeners>{0}</listeners></trace>";
+			string[] good = {
+				"<clear/>",
+				"<add name=\"foo\" " +
+					"type=\"System.Diagnostics.TextWriterTraceListener, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" " +
+					"initializeData=\"argument.txt\"/>",
+				"<remove name=\"foo\"/>",
+				"<add name=\"foo\"" +
+					"type=\"System.Diagnostics.TextWriterTraceListener, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" />",
+				"<remove name=\"foo\"/>"
+			};
+			ValidateSuccess ("#TTT:L:Good", format, good);
+
+			string[] bad = {
+				"<invalid tag=\"here\"/>",
+				"<clear with=\"args\"/>",
+				"<remove/>",
+				"<add/>",
+				"<remove name=\"foo\" extra=\"arg\"/>",
+				"<add name=\"foo\"/>",
+				"<add type=\"foo\"/>",
+				"<add name=\"foo\" type=\"invalid-type\"/>",
+			};
+			ValidateExceptions ("#TTT:L:Bad", format, bad);
+		}
+	}
+}
+

+ 161 - 0
mcs/class/System/Test/System.Diagnostics/SwitchesTest.cs

@@ -0,0 +1,161 @@
+//
+// SwitchesTest.cs:
+// 		NUnit Test Cases for System.Diagnostics.BooleanSwitch and
+// 		System.Diagnostics.TraceSwitch
+//
+// Jonathan Pryor ([email protected])
+//
+// (C) 2002 Jonathan Pryor
+//
+// README:
+// Tests in this file are expected to fail until a decent strategy to test
+// .config files is found.  See the mono-list archives for more information
+// (assuming anybody ever answers the initial requests for help...).
+// 
+
+using NUnit.Framework;
+using System;
+using System.Text;
+using System.Collections;
+using System.Configuration;
+using System.Diagnostics;
+
+namespace MonoTests.System.Diagnostics {
+
+	class TestNewSwitch : Switch {
+		private string v;
+		private StringBuilder ops = new StringBuilder ();
+		private const string expected = 
+			".ctor\n" +
+			"get_Value\n" +
+			"OnSwitchSettingChanged\n" +
+			"GetSetting";
+
+		public TestNewSwitch (string name, string desc)
+			: base (name, desc)
+		{
+			ops.Append (".ctor\n");
+		}
+
+		public string Value {
+			get {
+				ops.Append ("get_Value\n");
+				// ensure that the .config file is read in
+				int n = base.SwitchSetting;
+				return v;
+			}
+		}
+
+		public bool Validate ()
+		{
+			return expected == ops.ToString();
+		}
+
+		private void GetSetting ()
+		{
+			ops.Append ("GetSetting\n");
+			IDictionary d = (IDictionary) ConfigurationSettings.GetConfig ("system.diagnostics");
+			if (d != null) {
+				d = (IDictionary) d ["switches"];
+				if (d != null) {
+					v = d [DisplayName].ToString();
+				}
+			}
+		}
+
+		protected override void OnSwitchSettingChanged ()
+		{
+			ops.Append ("OnSwitchSettingChanged\n");
+
+			GetSetting ();
+		}
+	}
+
+	public class SwitchesTest : TestCase {
+    
+		private static BooleanSwitch bon = new BooleanSwitch ("bool-true", "");
+		private static BooleanSwitch bon2 = new BooleanSwitch ("bool-true-2", "");
+		private static BooleanSwitch bon3 = new BooleanSwitch ("bool-true-3", "");
+		private static BooleanSwitch boff = new BooleanSwitch ("bool-false", "");
+		private static BooleanSwitch boff2 = new BooleanSwitch ("bool-default", "");
+
+		private static TraceSwitch toff = new TraceSwitch ("trace-off", "");
+		private static TraceSwitch terror = new TraceSwitch ("trace-error", "");
+		private static TraceSwitch twarning = new TraceSwitch ("trace-warning", "");
+		private static TraceSwitch tinfo = new TraceSwitch ("trace-info", "");
+		private static TraceSwitch tverbose = new TraceSwitch ("trace-verbose", "");
+		private static TraceSwitch tdefault = new TraceSwitch ("no-value", "");
+		private static TraceSwitch tsv = new TraceSwitch ("string-value", "");
+		private static TraceSwitch tnegative = new TraceSwitch ("trace-negative", "");
+
+		private static TestNewSwitch tns = new TestNewSwitch ("string-value", "");
+
+		public SwitchesTest () 
+			: base ("System.Diagnostics.Trace testsuite")
+		{
+		}
+
+		public SwitchesTest (string name)
+			: base(name)
+		{
+		}
+
+		protected override void SetUp ()
+		{
+		}
+
+		protected override void TearDown ()
+		{
+		}
+
+		public static ITest Suite {
+ 			get { 
+				return new TestSuite (typeof (SwitchesTest)); 
+			}
+		}
+
+		public void TestBooleanSwitches ()
+		{
+			AssertEquals ("#BS:T:1", true, bon.Enabled);
+			AssertEquals ("#BS:T:2", true, bon2.Enabled);
+			AssertEquals ("#BS:T:3", true, bon3.Enabled);
+			AssertEquals ("#BS:F:1", false, boff.Enabled);
+			AssertEquals ("#BS:F:2", false, boff2.Enabled);
+		}
+
+		public void TestTraceSwitches ()
+		{
+			// The levels 0..4:
+			CheckTraceSwitch (toff,      false, false, false, false);
+			CheckTraceSwitch (terror,    true,  false, false, false);
+			CheckTraceSwitch (twarning,  true,  true,  false, false);
+			CheckTraceSwitch (tinfo,     true,  true,  true,  false);
+			CheckTraceSwitch (tverbose,  true,  true,  true,  true);
+
+			// Default value is 0
+			CheckTraceSwitch (tdefault,  false, false, false, false);
+
+			// string value can't be converted to int, so default is 0
+			CheckTraceSwitch (tsv,       false, false, false, false);
+
+			// negative number is < 0, so all off
+			CheckTraceSwitch (tnegative, false, false, false, false);
+		}
+
+		private void CheckTraceSwitch (TraceSwitch ts, bool te, bool tw, bool ti, bool tv)
+		{
+			string desc = string.Format ("#TS:{0}", ts.DisplayName);
+			AssertEquals (desc + ":TraceError",   te, ts.TraceError);
+			AssertEquals (desc + ":TraceWarning", tw, ts.TraceWarning);
+			AssertEquals (desc + ":TraceInfo",    ti, ts.TraceInfo);
+			AssertEquals (desc + ":TraceVerbose", tv, ts.TraceVerbose);
+		}
+
+		public void TestNewSwitch ()
+		{
+			AssertEquals ("#NS:Validate", true, tns.Validate());
+			AssertEquals ("#NS:Value", "string-value", tns.Value);
+		}
+	}
+}
+

+ 6 - 1
mcs/class/System/Test/System.Diagnostics/TraceTest.cs

@@ -42,7 +42,6 @@ namespace MonoTests.System.Diagnostics {
 			Trace.Listeners.Clear ();
 			Trace.Listeners.Add (listener);
 			Trace.AutoFlush = true;
-
 		}
 
 		protected override void TearDown ()
@@ -60,6 +59,9 @@ namespace MonoTests.System.Diagnostics {
 		// Make sure that when we get the output we expect....
 		public void TestTracing ()
 		{
+			Trace.IndentLevel = 0;
+			Trace.IndentSize = 4;
+
 			string value =  
 				"Entering Main" + Environment.NewLine +
 				"Exiting Main" + Environment.NewLine;
@@ -73,6 +75,9 @@ namespace MonoTests.System.Diagnostics {
 		// Make sure we get the output we expect in the presence of indenting...
 		public void TestIndent ()
 		{
+			Trace.IndentLevel = 0;
+			Trace.IndentSize = 4;
+
 			string value =  
 				"List of errors:" + Environment.NewLine +
 				"    Error 1: File not found" + Environment.NewLine +

+ 1 - 0
mcs/class/System/Test/makefile.gnu

@@ -6,6 +6,7 @@ LIB_LIST = system_linux_test.args
 LIB_FLAGS =	\
 		-r $(topdir)/class/lib/corlib.dll \
 		-r $(topdir)/class/lib/System.dll \
+		-r $(topdir)/class/lib/System.Xml.dll \
 	    -r $(topdir)/class/lib/NUnitCore_mono.dll
 
 SOURCES_INCLUDE=*.cs

+ 3 - 0
mcs/class/System/Test/system_linux_test.args

@@ -3,6 +3,7 @@
 --noconfig
 -r ../../lib/corlib.dll
 -r ../../lib/System.dll
+-r ../../lib/System.Xml.dll
 -r ../../../nunit/NUnitCore_mono.dll
 AllTests.cs
 BasicOperationsTest.cs
@@ -18,6 +19,8 @@ System.ComponentModel/AllTests.cs
 System.ComponentModel/EventHandlerListTests.cs
 System.Diagnostics/AllTests.cs
 System.Diagnostics/TraceTest.cs
+System.Diagnostics/SwitchesTest.cs
+System.Diagnostics/DiagnosticsConfigurationHandlerTest.cs
 System.Net/AllTests.cs
 System.Net/CookieCollectionTest.cs
 System.Net/CookieTest.cs

+ 30 - 0
mcs/class/System/Test/test-config-file

@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<configuration>
+  <system.diagnostics>
+    <switches>
+      <!-- Boolean switches: any non-zero value is true -->
+      <add name="bool-true" value="1"/>
+      <add name="bool-true-2" value="4"/>
+      <add name="bool-true-3" value="-2"/>
+      <add name="bool-false" value="0"/>
+      <add name="bool-default"/>
+
+      <!-- trace switches: -->
+      <add name="trace-off" value="0"/>
+      <add name="trace-error" value="1"/>
+      <add name="trace-warning" value="2"/>
+      <add name="trace-info" value="3"/>
+      <add name="trace-verbose" value="4"/>
+
+      <!-- this should have a default value of 0 -->
+      <add name="no-value"/>
+
+      <!-- this should be read in as 0 for Switches -->
+      <add name="string-value" value="string-value"/>
+
+      <!-- this causes problems on .NET -->
+      <add name="trace-negative" value="-1"/>
+    </switches>
+  </system.diagnostics>
+</configuration>
+