EventInstanceTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //
  2. // EventInstanceTest.cs -
  3. // NUnit Test Cases for System.Diagnostics.EvenInstance
  4. //
  5. // Author:
  6. // Gert Driesen <[email protected]>
  7. //
  8. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if !MOBILE
  31. using System;
  32. using System.ComponentModel;
  33. using System.Diagnostics;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Diagnostics
  36. {
  37. [TestFixture]
  38. public class EventInstanceTest
  39. {
  40. [Test]
  41. public void Constructor1 ()
  42. {
  43. EventInstance ei = null;
  44. ei = new EventInstance (5, 10);
  45. Assert.AreEqual (10, ei.CategoryId, "#A1");
  46. Assert.AreEqual (5, ei.InstanceId, "#A2");
  47. Assert.AreEqual (EventLogEntryType.Information, ei.EntryType, "#A3");
  48. ei = new EventInstance (0, 0);
  49. Assert.AreEqual (0, ei.CategoryId, "#B1");
  50. Assert.AreEqual (0, ei.InstanceId, "#B2");
  51. Assert.AreEqual (EventLogEntryType.Information, ei.EntryType, "#B3");
  52. ei = new EventInstance (uint.MaxValue, ushort.MaxValue);
  53. Assert.AreEqual (ushort.MaxValue, ei.CategoryId, "#C1");
  54. Assert.AreEqual (uint.MaxValue, ei.InstanceId, "#C2");
  55. Assert.AreEqual (EventLogEntryType.Information, ei.EntryType, "#C3");
  56. }
  57. [Test]
  58. public void Constructor1_InstanceId_Invalid ()
  59. {
  60. try {
  61. new EventInstance (-1, 10);
  62. Assert.Fail ("#A1");
  63. } catch (ArgumentOutOfRangeException ex) {
  64. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  65. Assert.IsNotNull (ex.Message, "#A3");
  66. Assert.IsNull (ex.InnerException, "#A4");
  67. }
  68. try {
  69. new EventInstance ((long) uint.MaxValue + 1, 10);
  70. Assert.Fail ("#B1");
  71. } catch (ArgumentOutOfRangeException ex) {
  72. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  73. Assert.IsNotNull (ex.Message, "#B3");
  74. Assert.IsNull (ex.InnerException, "#B4");
  75. }
  76. }
  77. [Test]
  78. public void Constructor1_CategoryId_Invalid ()
  79. {
  80. try {
  81. new EventInstance (5, -1);
  82. Assert.Fail ("#A1");
  83. } catch (ArgumentOutOfRangeException ex) {
  84. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  85. Assert.IsNotNull (ex.Message, "#A3");
  86. Assert.IsNull (ex.InnerException, "#A4");
  87. }
  88. try {
  89. new EventInstance (5, ushort.MaxValue + 1);
  90. Assert.Fail ("#A1");
  91. } catch (ArgumentOutOfRangeException ex) {
  92. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  93. Assert.IsNotNull (ex.Message, "#A3");
  94. Assert.IsNull (ex.InnerException, "#A4");
  95. }
  96. }
  97. [Test]
  98. public void Constructor2 ()
  99. {
  100. EventInstance ei = null;
  101. ei = new EventInstance (5, 10, EventLogEntryType.Information);
  102. Assert.AreEqual (10, ei.CategoryId, "#A1");
  103. Assert.AreEqual (5, ei.InstanceId, "#A2");
  104. Assert.AreEqual (EventLogEntryType.Information, ei.EntryType, "#A3");
  105. ei = new EventInstance (0, 0, EventLogEntryType.Error);
  106. Assert.AreEqual (0, ei.CategoryId, "#B1");
  107. Assert.AreEqual (0, ei.InstanceId, "#B2");
  108. Assert.AreEqual (EventLogEntryType.Error, ei.EntryType, "#B3");
  109. ei = new EventInstance (uint.MaxValue, ushort.MaxValue,
  110. EventLogEntryType.Warning);
  111. Assert.AreEqual (ushort.MaxValue, ei.CategoryId, "#C1");
  112. Assert.AreEqual (uint.MaxValue, ei.InstanceId, "#C2");
  113. Assert.AreEqual (EventLogEntryType.Warning, ei.EntryType, "#C3");
  114. }
  115. [Test]
  116. public void Constructor2_InstanceId_Invalid ()
  117. {
  118. try {
  119. new EventInstance (-1, 10, EventLogEntryType.Error);
  120. Assert.Fail ("#A1");
  121. } catch (ArgumentOutOfRangeException ex) {
  122. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  123. Assert.IsNotNull (ex.Message, "#A3");
  124. Assert.IsNull (ex.InnerException, "#A4");
  125. }
  126. try {
  127. new EventInstance ((long) uint.MaxValue + 1, 10,
  128. EventLogEntryType.Error);
  129. Assert.Fail ("#B1");
  130. } catch (ArgumentOutOfRangeException ex) {
  131. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  132. Assert.IsNotNull (ex.Message, "#B3");
  133. Assert.IsNull (ex.InnerException, "#B4");
  134. }
  135. }
  136. [Test]
  137. public void Constructor2_CategoryId_Invalid ()
  138. {
  139. try {
  140. new EventInstance (5, -1, EventLogEntryType.Error);
  141. Assert.Fail ("#A1");
  142. } catch (ArgumentOutOfRangeException ex) {
  143. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  144. Assert.IsNotNull (ex.Message, "#A3");
  145. Assert.IsNull (ex.InnerException, "#A4");
  146. }
  147. try {
  148. new EventInstance (5, (int) ushort.MaxValue + 1, EventLogEntryType.Error);
  149. Assert.Fail ("#A1");
  150. } catch (ArgumentOutOfRangeException ex) {
  151. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  152. Assert.IsNotNull (ex.Message, "#A3");
  153. Assert.IsNull (ex.InnerException, "#A4");
  154. }
  155. }
  156. [Test]
  157. [ExpectedException (typeof (InvalidEnumArgumentException))] // Enum argument value 666 is not valid for type. type should be a value from EventLogEntryType.
  158. public void Constructor2_EntryType_Invalid ()
  159. {
  160. new EventInstance (5, 5, (EventLogEntryType) 666);
  161. }
  162. [Test]
  163. public void CategoryId ()
  164. {
  165. EventInstance ei = new EventInstance (5, 10);
  166. ei.CategoryId = 0;
  167. Assert.AreEqual (0, ei.CategoryId, "#1");
  168. ei.CategoryId = 5;
  169. Assert.AreEqual (5, ei.CategoryId, "#2");
  170. ei.CategoryId = ushort.MaxValue;
  171. Assert.AreEqual (ushort.MaxValue, ei.CategoryId, "#3");
  172. ei.CategoryId = 0;
  173. Assert.AreEqual (0, ei.CategoryId, "#4");
  174. Assert.AreEqual (5, ei.InstanceId, "#5");
  175. }
  176. [Test]
  177. public void CategoryId_Invalid ()
  178. {
  179. EventInstance ei = new EventInstance (5, 10, EventLogEntryType.Error);
  180. try {
  181. ei.CategoryId = -1;
  182. } catch (ArgumentOutOfRangeException ex) {
  183. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  184. Assert.IsNotNull (ex.Message, "#A3");
  185. Assert.IsNull (ex.InnerException, "#A4");
  186. }
  187. try {
  188. ei.CategoryId = (int) ushort.MaxValue + 1;
  189. } catch (ArgumentOutOfRangeException ex) {
  190. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  191. Assert.IsNotNull (ex.Message, "#A3");
  192. Assert.IsNull (ex.InnerException, "#A4");
  193. }
  194. Assert.AreEqual (10, ei.CategoryId, "#C1");
  195. Assert.AreEqual (EventLogEntryType.Error, ei.EntryType, "#C2");
  196. Assert.AreEqual (5, ei.InstanceId, "#C2");
  197. }
  198. [Test]
  199. public void InstanceId ()
  200. {
  201. EventInstance ei = new EventInstance (5, 10);
  202. ei.InstanceId = 0;
  203. Assert.AreEqual (0, ei.InstanceId, "#1");
  204. ei.InstanceId = 5;
  205. Assert.AreEqual (5, ei.InstanceId, "#2");
  206. ei.InstanceId = uint.MaxValue;
  207. Assert.AreEqual (uint.MaxValue, ei.InstanceId, "#3");
  208. ei.InstanceId = 0;
  209. Assert.AreEqual (0, ei.InstanceId, "#4");
  210. Assert.AreEqual (10, ei.CategoryId, "#5");
  211. }
  212. [Test]
  213. public void InstanceId_Invalid ()
  214. {
  215. EventInstance ei = new EventInstance (5, 10, EventLogEntryType.Error);
  216. try {
  217. ei.InstanceId = -1;
  218. Assert.Fail ("#A1");
  219. } catch (ArgumentOutOfRangeException ex) {
  220. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  221. Assert.IsNotNull (ex.Message, "#A3");
  222. Assert.IsNull (ex.InnerException, "#A4");
  223. }
  224. try {
  225. ei.InstanceId = (long) uint.MaxValue + 1;
  226. Assert.Fail ("#B1");
  227. } catch (ArgumentOutOfRangeException ex) {
  228. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  229. Assert.IsNotNull (ex.Message, "#B3");
  230. Assert.IsNull (ex.InnerException, "#B4");
  231. }
  232. Assert.AreEqual (10, ei.CategoryId, "#C1");
  233. Assert.AreEqual (EventLogEntryType.Error, ei.EntryType, "#C2");
  234. Assert.AreEqual (5, ei.InstanceId, "#C3");
  235. }
  236. [Test]
  237. public void EntryType ()
  238. {
  239. EventInstance ei = new EventInstance (5, 10, EventLogEntryType.Error);
  240. ei.EntryType = EventLogEntryType.Warning;
  241. Assert.AreEqual (EventLogEntryType.Warning, ei.EntryType, "#1");
  242. ei.EntryType = EventLogEntryType.FailureAudit;
  243. Assert.AreEqual (EventLogEntryType.FailureAudit, ei.EntryType, "#2");
  244. ei.EntryType = EventLogEntryType.SuccessAudit;
  245. Assert.AreEqual (EventLogEntryType.SuccessAudit, ei.EntryType, "#3");
  246. ei.EntryType = EventLogEntryType.Information;
  247. Assert.AreEqual (EventLogEntryType.Information, ei.EntryType, "#4");
  248. Assert.AreEqual (5, ei.InstanceId, "#5");
  249. Assert.AreEqual (10, ei.CategoryId, "#6");
  250. }
  251. [Test]
  252. public void EntryType_Invalid ()
  253. {
  254. EventInstance ei = new EventInstance (5, 10, EventLogEntryType.Error);
  255. try {
  256. ei.EntryType = (EventLogEntryType) 666;
  257. Assert.Fail ("#A1");
  258. } catch (InvalidEnumArgumentException ex) {
  259. // The value of argument 'value' (666) is invalid for Enum type
  260. // 'EventLogEntryType'
  261. Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#A2");
  262. Assert.IsNotNull (ex.Message, "#A3");
  263. Assert.IsTrue (ex.Message.IndexOf ("666") != -1, "#A4");
  264. Assert.IsTrue (ex.Message.IndexOf ("value") != -1, "#A5");
  265. Assert.IsTrue (ex.Message.IndexOf ("EventLogEntryType") != -1, "#A6");
  266. Assert.IsNull (ex.InnerException, "#A7");
  267. }
  268. try {
  269. ei.EntryType = new EventLogEntryType ();
  270. Assert.Fail ("#B1");
  271. } catch (InvalidEnumArgumentException ex) {
  272. // The value of argument 'value' (0) is invalid for Enum type
  273. // 'EventLogEntryType'
  274. Assert.AreEqual (typeof (InvalidEnumArgumentException), ex.GetType (), "#B2");
  275. Assert.IsNotNull (ex.Message, "#B3");
  276. Assert.IsTrue (ex.Message.IndexOf ("0") != -1, "#B4");
  277. Assert.IsTrue (ex.Message.IndexOf ("value") != -1, "#B5");
  278. Assert.IsTrue (ex.Message.IndexOf ("EventLogEntryType") != -1, "#B6");
  279. Assert.IsNull (ex.InnerException, "#B7");
  280. }
  281. Assert.AreEqual (10, ei.CategoryId, "#C1");
  282. Assert.AreEqual (EventLogEntryType.Error, ei.EntryType, "#C2");
  283. Assert.AreEqual (5, ei.InstanceId, "#C3");
  284. }
  285. }
  286. }
  287. #endif