DiagnosticsConfigurationHandlerTest.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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, Ignore ("DiagnosticsConfigurationHandler is not meant to be used directly on Windows")]
  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, Ignore ("DiagnosticsConfigurationHandler is not meant to be used directly on Windows")]
  82. public void SwitchesTag_Elements ()
  83. {
  84. string[] badElements = {
  85. // not enough arguments
  86. "<add />",
  87. "<add value=\"b\"/>",
  88. // too many arguments
  89. "<add name=\"a\" value=\"b\" extra=\"c\"/>",
  90. // wrong casing
  91. "<add Name=\"a\" value=\"b\"/>",
  92. "<Add Name=\"a\" value=\"b\"/>",
  93. // missing args
  94. "<remove />",
  95. "<remove value=\"b\"/>",
  96. // too many args
  97. "<remove name=\"a\" value=\"b\"/>",
  98. "<clear name=\"a\"/>",
  99. // invalid element
  100. "<invalid element=\"a\" here=\"b\"/>"
  101. };
  102. ValidateExceptions ("#TST:IE:Bad", "<switches>{0}</switches>", badElements);
  103. string[] goodElements = {
  104. "<add name=\"a\" value=\"b\"/>",
  105. "<add name=\"a\"/>",
  106. "<remove name=\"a\"/>",
  107. "<clear/>"
  108. };
  109. ValidateSuccess ("#TST:IE:Good", "<switches>{0}</switches>", goodElements);
  110. }
  111. [Test, Ignore ("DiagnosticsConfigurationHandler is not meant to be used directly on Windows")]
  112. public void AssertTag ()
  113. {
  114. string[] goodAttributes = {
  115. "",
  116. "assertuienabled=\"true\"",
  117. "assertuienabled=\"false\" logfilename=\"some file name\"",
  118. "logfilename=\"some file name\""
  119. };
  120. ValidateSuccess ("#TAT:Good", "<assert {0}/>", goodAttributes);
  121. string[] badAttributes = {
  122. "AssertUiEnabled=\"true\"",
  123. "LogFileName=\"foo\"",
  124. "assertuienabled=\"\"",
  125. "assertuienabled=\"non-boolean-value\""
  126. };
  127. ValidateExceptions ("#TAT:BadAttrs", "<assert {0}/>", badAttributes);
  128. string[] badChildren = {
  129. "<any element=\"here\"/>"
  130. };
  131. ValidateExceptions ("#TAT:BadChildren", "<assert>{0}</assert>", badChildren);
  132. }
  133. [Test, Ignore ("DiagnosticsConfigurationHandler is not meant to be used directly on Windows")]
  134. public void TraceTag_Attributes ()
  135. {
  136. string[] good = {
  137. "",
  138. "autoflush=\"true\"",
  139. "indentsize=\"4\"",
  140. "autoflush=\"false\" indentsize=\"10\""
  141. };
  142. ValidateSuccess ("#TTT:A:Good", "<trace {0}/>", good);
  143. string[] bad = {
  144. "AutoFlush=\"true\"",
  145. "IndentSize=\"false\"",
  146. "autoflush=\"non-boolean-value\"",
  147. "autoflush=\"\"",
  148. "indentsize=\"non-integral-value\"",
  149. "indentsize=\"\"",
  150. "extra=\"invalid\""
  151. };
  152. ValidateExceptions ("#TTT:A:Bad", "<trace {0}/>", bad);
  153. }
  154. [Test, Ignore ("DiagnosticsConfigurationHandler is not meant to be used directly on Windows")]
  155. public void TraceTag_Children ()
  156. {
  157. string[] good = {
  158. // more about listeners in a different function...
  159. "<listeners />"
  160. };
  161. ValidateSuccess ("#TTT:C:Good", "<trace>{0}</trace>", good);
  162. string[] bad = {
  163. "<listeners with=\"attribute\"/>",
  164. "<invalid element=\"here\"/>"
  165. };
  166. ValidateExceptions ("#TTT:C:Bad", "<trace>{0}</trace>", bad);
  167. }
  168. [Test, Ignore ("DiagnosticsConfigurationHandler is not meant to be used directly on Windows")]
  169. public void TraceTag_Listeners ()
  170. {
  171. const string format = "<trace><listeners>{0}</listeners></trace>";
  172. string[] good = {
  173. "<clear/>",
  174. "<add name=\"foo\" " +
  175. "type=\"System.Diagnostics.TextWriterTraceListener, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" " +
  176. "initializeData=\"argument.txt\"/>",
  177. "<remove name=\"foo\"/>",
  178. "<add name=\"foo\"" +
  179. "type=\"System.Diagnostics.TextWriterTraceListener, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\" />",
  180. "<remove name=\"foo\"/>"
  181. };
  182. ValidateSuccess ("#TTT:L:Good", format, good);
  183. string[] bad = {
  184. "<invalid tag=\"here\"/>",
  185. "<clear with=\"args\"/>",
  186. "<remove/>",
  187. "<add/>",
  188. "<remove name=\"foo\" extra=\"arg\"/>",
  189. "<add name=\"foo\"/>",
  190. "<add type=\"foo\"/>",
  191. "<add name=\"foo\" type=\"invalid-type\"/>",
  192. };
  193. ValidateExceptions ("#TTT:L:Bad", format, bad);
  194. }
  195. }
  196. }