File: DiagnosticsConfigurationHandlerTest.cs

package info (click to toggle)
mono 1.2.2.1-1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 142,720 kB
  • ctags: 256,408
  • sloc: cs: 1,495,736; ansic: 249,442; sh: 18,327; xml: 12,463; makefile: 5,046; perl: 1,248; asm: 635; yacc: 285; sql: 7
file content (220 lines) | stat: -rw-r--r-- 6,115 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//
// DiagnosticsConfigurationHandlerTest.cs:
// 	NUnit Test Cases for System.Diagnostics.DiagnosticsConfigurationHandler
//
// Authors:
//   Jonathan Pryor (jonpryor@vt.edu)
//   Martin Willemoes Hansen (mwh@sysrq.dk)
//
// (C) Jonathan Pryor
// (C) 2003 Martin Willemoes Hansen
// 

using NUnit.Framework;
using System;
using System.Configuration;
using System.Diagnostics;
using System.Xml;

namespace MonoTests.System.Diagnostics {

	[TestFixture]
	public class DiagnosticsConfigurationHandlerTest : Assertion {
		
		private const string XmlFormat = 
			"{0}";
			/*
			"<system.diagnostics>" +
			"{0}" +
			"</system.diagnostics>";
			 */
    
		private DiagnosticsConfigurationHandler handler = new DiagnosticsConfigurationHandler ();

		[Test, Category ("NotDotNet")]
		public void SwitchesTag_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) {
				}
				catch (AssertionException) {
					// This is generated by the Assertion.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);
		}

		[Test, Category ("NotDotNet")]
		public void SwitchesTag_Elements ()
		{
			string[] badElements = {
				// not enough arguments
				"<add />",
				"<add name=\"a\"/>",
				"<add value=\"b\"/>",
				// non-integral value
				"<add name=\"string-value\" value=\"string-value\"/>",
				// 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=\"4\"/>",
				"<add name=\"a\" value=\"-2\"/>",
				"<remove name=\"a\"/>",
				"<clear/>"
			};
			ValidateSuccess ("#TST:IE:Good", "<switches>{0}</switches>", goodElements);
		}

		[Test, Category ("NotDotNet")]
		public void AssertTag ()
		{
			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);
		}

		[Test, Category ("NotDotNet")]
		public void TraceTag_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);
		}

		[Test, Category ("NotDotNet")]
		public void TraceTag_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);
		}

		[Test, Category ("NotDotNet")]
		public void TraceTag_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);
		}
	}
}