DiagnosticsConfigurationHandlerTest.cs 6.0 KB

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