updating to the latest module.
[mono.git] / mcs / class / System / Test / System.Diagnostics / DiagnosticsConfigurationHandlerTest.cs
1 //
2 // DiagnosticsConfigurationHandlerTest.cs:
3 //      NUnit Test Cases for System.Diagnostics.DiagnosticsConfigurationHandler
4 //
5 // Authors:
6 //   Jonathan Pryor (jonpryor@vt.edu)
7 //   Martin Willemoes Hansen (mwh@sysrq.dk)
8 //
9 // (C) Jonathan Pryor
10 // (C) 2003 Martin Willemoes Hansen
11 // 
12
13 using NUnit.Framework;
14 using System;
15 using System.Configuration;
16 using System.Diagnostics;
17 using System.Xml;
18
19 namespace MonoTests.System.Diagnostics {
20
21         [TestFixture]
22         public class DiagnosticsConfigurationHandlerTest : Assertion {
23                 
24                 private const string XmlFormat = 
25                         "{0}";
26                         /*
27                         "<system.diagnostics>" +
28                         "{0}" +
29                         "</system.diagnostics>";
30                          */
31     
32                 private DiagnosticsConfigurationHandler handler = new DiagnosticsConfigurationHandler ();
33
34                 [Test, Category ("NotDotNet")]
35                 public void SwitchesTag_Attributes ()
36                 {
37                         string[] attrs = {"invalid=\"yes\""};
38                         ValidateExceptions ("#TST:A", "<switches {0}></switches>", attrs);
39                 }
40
41                 private void ValidateExceptions (string name, string format, string[] args)
42                 {
43                         foreach (string arg in args) {
44                                 string xml = string.Format (XmlFormat,
45                                                 string.Format (format, arg));
46                                 try {
47                                         CreateHandler (xml);
48                                         Fail (string.Format ("{0}:{1}: no exception generated", name, arg));
49                                 }
50                                 catch (ConfigurationException) {
51                                 }
52                                 catch (AssertionException) {
53                                         // This is generated by the Assertion.Fail() statement in the try block.
54                                         throw;
55                                 }
56                                 catch (Exception e) {
57                                         Fail (string.Format ("{0}:{1}: wrong exception generated: {2} ({3}).", 
58                                                                 // name, arg, e.Message, 
59                                                                 name, arg, e.ToString(), 
60                                                                 // e.InnerException == null ? "" : e.InnerException.Message));
61                                                                 e.InnerException == null ? "" : e.InnerException.ToString()));
62                                 }
63                         }
64                 }
65
66                 private void ValidateSuccess (string name, string format, string[] args)
67                 {
68                         foreach (string arg in args) {
69                                 string xml = string.Format (XmlFormat,
70                                                 string.Format (format, arg));
71                                 try {
72                                         CreateHandler (xml);
73                                 }
74                                 catch (Exception e) {
75                                         Fail (string.Format ("{0}:{1}: exception generated: {2} ({3}).", 
76                                                                 // name, arg, e.Message,
77                                                                 name, arg, e.ToString(),
78                                                                 // e.InnerException == null ? "" : e.InnerException.Message));
79                                                                 e.InnerException == null ? "" : e.InnerException.ToString()));
80                                 }
81                         }
82                 }
83
84                 private object CreateHandler (string xml)
85                 {
86                         XmlDocument d = new XmlDocument ();
87                         d.LoadXml (xml);
88                         return handler.Create (null, null, d);
89                 }
90
91                 [Test, Category ("NotDotNet")]
92                 public void SwitchesTag_Elements ()
93                 {
94                         string[] badElements = {
95                                 // not enough arguments
96                                 "<add />",
97                                 "<add name=\"a\"/>",
98                                 "<add value=\"b\"/>",
99                                 // non-integral value
100                                 "<add name=\"string-value\" value=\"string-value\"/>",
101                                 // too many arguments
102                                 "<add name=\"a\" value=\"b\" extra=\"c\"/>",
103                                 // wrong casing
104                                 "<add Name=\"a\" value=\"b\"/>",
105                                 "<Add Name=\"a\" value=\"b\"/>",
106                                 // missing args
107                                 "<remove />",
108                                 "<remove value=\"b\"/>",
109                                 // too many args
110                                 "<remove name=\"a\" value=\"b\"/>",
111                                 "<clear name=\"a\"/>",
112                                 // invalid element
113                                 "<invalid element=\"a\" here=\"b\"/>"
114                         };
115                         ValidateExceptions ("#TST:IE:Bad", "<switches>{0}</switches>", badElements);
116
117                         string[] goodElements = {
118                                 "<add name=\"a\" value=\"4\"/>",
119                                 "<add name=\"a\" value=\"-2\"/>",
120                                 "<remove name=\"a\"/>",
121                                 "<clear/>"
122                         };
123                         ValidateSuccess ("#TST:IE:Good", "<switches>{0}</switches>", goodElements);
124                 }
125
126                 [Test, Category ("NotDotNet")]
127                 public void AssertTag ()
128                 {
129                         string[] goodAttributes = {
130                                 "",
131                                 "assertuienabled=\"true\"",
132                                 "assertuienabled=\"false\" logfilename=\"some file name\"",
133                                 "logfilename=\"some file name\""
134                         };
135                         ValidateSuccess ("#TAT:Good", "<assert {0}/>", goodAttributes);
136
137                         string[] badAttributes = {
138                                 "AssertUiEnabled=\"true\"",
139                                 "LogFileName=\"foo\"",
140                                 "assertuienabled=\"\"",
141                                 "assertuienabled=\"non-boolean-value\""
142                         };
143                         ValidateExceptions ("#TAT:BadAttrs", "<assert {0}/>", badAttributes);
144
145                         string[] badChildren = {
146                                 "<any element=\"here\"/>"
147                         };
148                         ValidateExceptions ("#TAT:BadChildren", "<assert>{0}</assert>", badChildren);
149                 }
150
151                 [Test, Category ("NotDotNet")]
152                 public void TraceTag_Attributes ()
153                 {
154                         string[] good = {
155                                 "",
156                                 "autoflush=\"true\"",
157                                 "indentsize=\"4\"",
158                                 "autoflush=\"false\" indentsize=\"10\""
159                         };
160                         ValidateSuccess ("#TTT:A:Good", "<trace {0}/>", good);
161
162                         string[] bad = {
163                                 "AutoFlush=\"true\"",
164                                 "IndentSize=\"false\"",
165                                 "autoflush=\"non-boolean-value\"",
166                                 "autoflush=\"\"",
167                                 "indentsize=\"non-integral-value\"",
168                                 "indentsize=\"\"",
169                                 "extra=\"invalid\""
170                         };
171                         ValidateExceptions ("#TTT:A:Bad", "<trace {0}/>", bad);
172                 }
173
174                 [Test, Category ("NotDotNet")]
175                 public void TraceTag_Children ()
176                 {
177                         string[] good = {
178                                 // more about listeners in a different function...
179                                 "<listeners />"
180                         };
181                         ValidateSuccess ("#TTT:C:Good", "<trace>{0}</trace>", good);
182
183                         string[] bad = {
184                                 "<listeners with=\"attribute\"/>",
185                                 "<invalid element=\"here\"/>"
186                         };
187                         ValidateExceptions ("#TTT:C:Bad", "<trace>{0}</trace>", bad);
188                 }
189
190                 [Test, Category ("NotDotNet")]
191                 public void TraceTag_Listeners ()
192                 {
193                         const string format = "<trace><listeners>{0}</listeners></trace>";
194                         string[] good = {
195                                 "<clear/>",
196                                 "<add name=\"foo\" " +
197                                         "type=\"System.Diagnostics.TextWriterTraceListener, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" " +
198                                         "initializeData=\"argument.txt\"/>",
199                                 "<remove name=\"foo\"/>",
200                                 "<add name=\"foo\" " +
201                                         "type=\"System.Diagnostics.TextWriterTraceListener, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" />",
202                                 "<remove name=\"foo\"/>"
203                         };
204                         ValidateSuccess ("#TTT:L:Good", format, good);
205
206                         string[] bad = {
207                                 "<invalid tag=\"here\"/>",
208                                 "<clear with=\"args\"/>",
209                                 "<remove/>",
210                                 "<add/>",
211                                 "<remove name=\"foo\" extra=\"arg\"/>",
212                                 "<add name=\"foo\"/>",
213                                 "<add type=\"foo\"/>",
214                                 "<add name=\"foo\" type=\"invalid-type\"/>",
215                         };
216                         ValidateExceptions ("#TTT:L:Bad", format, bad);
217                 }
218         }
219 }
220