Merge pull request #1225 from strawd/bug22307
[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 #if !MOBILE
14
15 using NUnit.Framework;
16 using System;
17 using System.Configuration;
18 using System.Diagnostics;
19 using System.Xml;
20
21 namespace MonoTests.System.Diagnostics
22 {
23         [TestFixture]
24         public class DiagnosticsConfigurationHandlerTest
25         {
26                 private const string XmlFormat = 
27                         "{0}";
28                         /*
29                         "<system.diagnostics>" +
30                         "{0}" +
31                         "</system.diagnostics>";
32                          */
33
34                 private DiagnosticsConfigurationHandler handler = new DiagnosticsConfigurationHandler ();
35
36                 [Test]
37                 [Category ("NotDotNet")]
38                 public void SwitchesTag_Attributes ()
39                 {
40                         string[] attrs = {"invalid=\"yes\""};
41                         ValidateExceptions ("#TST:A", "<switches {0}></switches>", attrs);
42                 }
43
44                 void ValidateExceptions (string name, string format, string[] args)
45                 {
46                         foreach (string arg in args) {
47                                 string xml = string.Format (XmlFormat,
48                                                 string.Format (format, arg));
49                                 try {
50                                         CreateHandler (xml);
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.
55                                         throw;
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()));
60                                 }
61                         }
62                 }
63
64                 private void ValidateSuccess (string name, string format, string[] args)
65                 {
66                         foreach (string arg in args) {
67                                 string xml = string.Format (XmlFormat,
68                                                 string.Format (format, arg));
69                                 try {
70                                         CreateHandler (xml);
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()));
75                                 }
76                         }
77                 }
78
79                 private object CreateHandler (string xml)
80                 {
81                         XmlDocument d = new XmlDocument ();
82                         d.LoadXml (xml);
83                         return handler.Create (null, null, d);
84                 }
85
86                 [Test]
87                 [Category ("NotDotNet")]
88                 public void SwitchesTag_Elements ()
89                 {
90                         string[] badElements = {
91                                 // not enough arguments
92                                 "<add />",
93                                 "<add name=\"a\"/>",
94                                 "<add value=\"b\"/>",
95                                 // too many arguments
96                                 "<add name=\"a\" value=\"b\" extra=\"c\"/>",
97                                 // wrong casing
98                                 "<add Name=\"a\" value=\"b\"/>",
99                                 "<Add Name=\"a\" value=\"b\"/>",
100                                 // missing args
101                                 "<remove />",
102                                 "<remove value=\"b\"/>",
103                                 // too many args
104                                 "<remove name=\"a\" value=\"b\"/>",
105                                 "<clear name=\"a\"/>",
106                                 // invalid element
107                                 "<invalid element=\"a\" here=\"b\"/>"
108                         };
109                         ValidateExceptions ("#TST:IE:Bad", "<switches>{0}</switches>", badElements);
110
111                         string[] goodElements = {
112                                 "<add name=\"a\" value=\"4\"/>",
113                                 "<add name=\"a\" value=\"-2\"/>",
114                                 "<remove name=\"a\"/>",
115                                 "<clear/>"
116                         };
117                         ValidateSuccess ("#TST:IE:Good", "<switches>{0}</switches>", goodElements);
118                 }
119
120                 [Test]
121                 [Category ("NotDotNet")]
122                 public void AssertTag ()
123                 {
124                         string[] goodAttributes = {
125                                 "",
126                                 "assertuienabled=\"true\"",
127                                 "assertuienabled=\"false\" logfilename=\"some file name\"",
128                                 "logfilename=\"some file name\""
129                         };
130                         ValidateSuccess ("#TAT:Good", "<assert {0}/>", goodAttributes);
131
132                         string[] badAttributes = {
133                                 "AssertUiEnabled=\"true\"",
134                                 "LogFileName=\"foo\"",
135                                 "assertuienabled=\"\"",
136                                 "assertuienabled=\"non-boolean-value\""
137                         };
138                         ValidateExceptions ("#TAT:BadAttrs", "<assert {0}/>", badAttributes);
139
140                         string[] badChildren = {
141                                 "<any element=\"here\"/>"
142                         };
143                         ValidateExceptions ("#TAT:BadChildren", "<assert>{0}</assert>", badChildren);
144                 }
145
146                 [Test]
147                 [Category ("NotDotNet")]
148                 public void PerformanceCountersTag ()
149                 {
150                         string[] goodAttributes = {
151                                 "",
152                                 "filemappingsize=\"1048576\"",
153                                 "filemappingsize=\"0\""
154                         };
155                         ValidateSuccess ("#PCT:Good", "<performanceCounters {0}/>", goodAttributes);
156
157                         string[] badAttributes = {
158                                 "FileMappingSize=\"1048576\"",
159                                 "filemappingsize=\"\"",
160                                 "filemappingsize=\"non-int-value\""
161                         };
162                         ValidateExceptions ("#PCT:BadAttrs", "<performanceCounters {0}/>", badAttributes);
163
164                         string[] badChildren = {
165                                 "<any element=\"here\"/>"
166                         };
167                         ValidateExceptions ("#PCT:BadChildren", "<performanceCounters>{0}</performanceCounters>", badChildren);
168                 }
169
170                 [Test]
171                 [Category ("NotDotNet")]
172                 public void TraceTag_Attributes ()
173                 {
174                         string[] good = {
175                                 "",
176                                 "autoflush=\"true\"",
177                                 "indentsize=\"4\"",
178                                 "autoflush=\"false\" indentsize=\"10\""
179                         };
180                         ValidateSuccess ("#TTT:A:Good", "<trace {0}/>", good);
181
182                         string[] bad = {
183                                 "AutoFlush=\"true\"",
184                                 "IndentSize=\"false\"",
185                                 "autoflush=\"non-boolean-value\"",
186                                 "autoflush=\"\"",
187                                 "indentsize=\"non-integral-value\"",
188                                 "indentsize=\"\"",
189                                 "extra=\"invalid\""
190                         };
191                         ValidateExceptions ("#TTT:A:Bad", "<trace {0}/>", bad);
192                 }
193
194                 [Test]
195                 [Category ("NotDotNet")]
196                 public void TraceTag_Children ()
197                 {
198                         string[] good = {
199                                 // more about listeners in a different function...
200                                 "<listeners />"
201                         };
202                         ValidateSuccess ("#TTT:C:Good", "<trace>{0}</trace>", good);
203
204                         string[] bad = {
205                                 "<listeners with=\"attribute\"/>",
206                                 "<invalid element=\"here\"/>"
207                         };
208                         ValidateExceptions ("#TTT:C:Bad", "<trace>{0}</trace>", bad);
209                 }
210
211                 [Test]
212                 [Category ("NotDotNet")]
213                 public void TraceTag_Listeners ()
214                 {
215                         const string format = "<trace><listeners>{0}</listeners></trace>";
216                         string[] good = {
217                                 "<clear/>",
218                                 "<add name=\"foo\" " +
219                                         "type=\"System.Diagnostics.TextWriterTraceListener, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" " +
220                                         "initializeData=\"argument.txt\"/>",
221                                 "<remove name=\"foo\"/>",
222                                 "<add name=\"foo\" " +
223                                         "type=\"System.Diagnostics.TextWriterTraceListener, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" />",
224                                 "<remove name=\"foo\"/>"
225                         };
226                         ValidateSuccess ("#TTT:L:Good", format, good);
227
228                         string[] bad = {
229                                 "<invalid tag=\"here\"/>",
230                                 "<clear with=\"args\"/>",
231                                 "<remove/>",
232                                 "<add/>",
233                                 "<remove name=\"foo\" extra=\"arg\"/>",
234                                 "<add type=\"foo\"/>",
235                                 "<add name=\"foo\" type=\"invalid-type\"/>",
236                         };
237                         ValidateExceptions ("#TTT:L:Bad", format, bad);
238                 }
239         }
240 }
241
242 #endif