DiagnosticsConfigurationHandlerTest.cs 6.0 KB

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