2 // DiagnosticsConfigurationHandlerTest.cs:
3 // NUnit Test Cases for System.Diagnostics.DiagnosticsConfigurationHandler
6 // Jonathan Pryor (jonpryor@vt.edu)
7 // Martin Willemoes Hansen (mwh@sysrq.dk)
10 // (C) 2003 Martin Willemoes Hansen
15 using NUnit.Framework;
17 using System.Configuration;
18 using System.Diagnostics;
21 namespace MonoTests.System.Diagnostics
24 public class DiagnosticsConfigurationHandlerTest
26 private const string XmlFormat =
29 "<system.diagnostics>" +
31 "</system.diagnostics>";
34 private DiagnosticsConfigurationHandler handler = new DiagnosticsConfigurationHandler ();
37 [Category ("NotDotNet")]
38 public void SwitchesTag_Attributes ()
40 string[] attrs = {"invalid=\"yes\""};
41 ValidateExceptions ("#TST:A", "<switches {0}></switches>", attrs);
44 void ValidateExceptions (string name, string format, string[] args)
46 foreach (string arg in args) {
47 string xml = string.Format (XmlFormat,
48 string.Format (format, arg));
51 Assert.Fail (string.Format ("{0}:{1}: no exception generated", name, arg));
52 } catch (ConfigurationException) {
53 } catch (AssertionException) {
54 // This is generated by the Assertion.Fail() statement in the try block.
56 } catch (Exception e) {
57 Assert.Fail (string.Format ("{0}:{1}: wrong exception generated: {2} ({3}).",
58 name, arg, e.ToString(),
59 e.InnerException == null ? "" : e.InnerException.ToString()));
64 private void ValidateSuccess (string name, string format, string[] args)
66 foreach (string arg in args) {
67 string xml = string.Format (XmlFormat,
68 string.Format (format, arg));
71 } catch (Exception e) {
72 Assert.Fail (string.Format ("{0}:{1}: exception generated: {2} ({3}).",
73 name, arg, e.ToString(),
74 e.InnerException == null ? "" : e.InnerException.ToString()));
79 private object CreateHandler (string xml)
81 XmlDocument d = new XmlDocument ();
83 return handler.Create (null, null, d);
87 [Category ("NotDotNet")]
88 public void SwitchesTag_Elements ()
90 string[] badElements = {
91 // not enough arguments
96 // in the 2.0 profile, we currently allow non-integral
99 // MS actually introduced new configuration classes
100 // for the 2.0 profile and did not modify the original
103 // Once we add 2.0 configuration classes for
104 // system.diagnostics, the origina behavior should
105 // be restored and this test should be enabled again
106 // for the 2.0 profile
108 // non-integral value
109 "<add name=\"string-value\" value=\"string-value\"/>",
111 // too many arguments
112 "<add name=\"a\" value=\"b\" extra=\"c\"/>",
114 "<add Name=\"a\" value=\"b\"/>",
115 "<Add Name=\"a\" value=\"b\"/>",
118 "<remove value=\"b\"/>",
120 "<remove name=\"a\" value=\"b\"/>",
121 "<clear name=\"a\"/>",
123 "<invalid element=\"a\" here=\"b\"/>"
125 ValidateExceptions ("#TST:IE:Bad", "<switches>{0}</switches>", badElements);
127 string[] goodElements = {
128 "<add name=\"a\" value=\"4\"/>",
129 "<add name=\"a\" value=\"-2\"/>",
130 "<remove name=\"a\"/>",
133 ValidateSuccess ("#TST:IE:Good", "<switches>{0}</switches>", goodElements);
137 [Category ("NotDotNet")]
138 public void AssertTag ()
140 string[] goodAttributes = {
142 "assertuienabled=\"true\"",
143 "assertuienabled=\"false\" logfilename=\"some file name\"",
144 "logfilename=\"some file name\""
146 ValidateSuccess ("#TAT:Good", "<assert {0}/>", goodAttributes);
148 string[] badAttributes = {
149 "AssertUiEnabled=\"true\"",
150 "LogFileName=\"foo\"",
151 "assertuienabled=\"\"",
152 "assertuienabled=\"non-boolean-value\""
154 ValidateExceptions ("#TAT:BadAttrs", "<assert {0}/>", badAttributes);
156 string[] badChildren = {
157 "<any element=\"here\"/>"
159 ValidateExceptions ("#TAT:BadChildren", "<assert>{0}</assert>", badChildren);
163 [Category ("NotDotNet")]
164 public void TraceTag_Attributes ()
168 "autoflush=\"true\"",
170 "autoflush=\"false\" indentsize=\"10\""
172 ValidateSuccess ("#TTT:A:Good", "<trace {0}/>", good);
175 "AutoFlush=\"true\"",
176 "IndentSize=\"false\"",
177 "autoflush=\"non-boolean-value\"",
179 "indentsize=\"non-integral-value\"",
183 ValidateExceptions ("#TTT:A:Bad", "<trace {0}/>", bad);
187 [Category ("NotDotNet")]
188 public void TraceTag_Children ()
191 // more about listeners in a different function...
194 ValidateSuccess ("#TTT:C:Good", "<trace>{0}</trace>", good);
197 "<listeners with=\"attribute\"/>",
198 "<invalid element=\"here\"/>"
200 ValidateExceptions ("#TTT:C:Bad", "<trace>{0}</trace>", bad);
204 [Category ("NotDotNet")]
205 public void TraceTag_Listeners ()
207 const string format = "<trace><listeners>{0}</listeners></trace>";
210 "<add name=\"foo\" " +
211 "type=\"System.Diagnostics.TextWriterTraceListener, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" " +
212 "initializeData=\"argument.txt\"/>",
213 "<remove name=\"foo\"/>",
214 "<add name=\"foo\" " +
215 "type=\"System.Diagnostics.TextWriterTraceListener, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" />",
216 "<remove name=\"foo\"/>"
218 ValidateSuccess ("#TTT:L:Good", format, good);
221 "<invalid tag=\"here\"/>",
222 "<clear with=\"args\"/>",
225 "<remove name=\"foo\" extra=\"arg\"/>",
226 #if NET_2_0 // type is optional (it could indicate a named listener)
228 "<add name=\"foo\"/>",
230 "<add type=\"foo\"/>",
231 "<add name=\"foo\" type=\"invalid-type\"/>",
233 ValidateExceptions ("#TTT:L:Bad", format, bad);