DiagnosticsConfigurationHandlerTest.cs 6.3 KB

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