PageRenderTime 147ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/SharedLibs/Sources/managedesent-61618/EsentInteropTests/MiscTests.cs

https://github.com/jalchr/ravendb
C# | 230 lines | 154 code | 16 blank | 60 comment | 13 complexity | 067506753d370cc25721e5fec3039edc MD5 | raw file
  1. //-----------------------------------------------------------------------
  2. // <copyright file="MiscTests.cs" company="Microsoft Corporation">
  3. // Copyright (c) Microsoft Corporation.
  4. // </copyright>
  5. //-----------------------------------------------------------------------
  6. namespace InteropApiTests
  7. {
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Globalization;
  11. using System.Linq;
  12. using System.Reflection;
  13. using Microsoft.Isam.Esent.Interop;
  14. using Microsoft.VisualStudio.TestTools.UnitTesting;
  15. /// <summary>
  16. /// Miscellaneous tests.
  17. /// </summary>
  18. [TestClass]
  19. public class MiscTests
  20. {
  21. /// <summary>
  22. /// Verify the default value in a ColumnInfo structure is read-only.
  23. /// </summary>
  24. [TestMethod]
  25. [Priority(0)]
  26. [Description("Verify the default value in a ColumnInfo structure is read-only")]
  27. [ExpectedException(typeof(NotSupportedException))]
  28. public void VerifyColumnInfoDefaultValueIsReadOnly()
  29. {
  30. var columnInfo = new ColumnInfo(
  31. "column",
  32. JET_COLUMNID.Nil,
  33. JET_coltyp.LongText,
  34. JET_CP.Unicode,
  35. 8,
  36. Any.BytesOfLength(8),
  37. ColumndefGrbit.ColumnFixed);
  38. columnInfo.DefaultValue[0] = 0x1;
  39. }
  40. /// <summary>
  41. /// Verify that the index segments in an IndexInfo are read-only.
  42. /// </summary>
  43. [TestMethod]
  44. [Description("Verify that the index segments in an IndexInfo are read-only")]
  45. [Priority(0)]
  46. [ExpectedException(typeof(NotSupportedException))]
  47. public void VerifyIndexInfoSegmentsAreReadOnly()
  48. {
  49. var segments = new[] { new IndexSegment("column1", JET_coltyp.LongText, true, false) };
  50. var info = new IndexInfo(
  51. "index", CultureInfo.InvariantCulture, CompareOptions.None, segments, CreateIndexGrbit.None, 0, 0, 0);
  52. info.IndexSegments[0] = new IndexSegment("column2", JET_coltyp.Short, false, false);
  53. }
  54. /// <summary>
  55. /// NATIVE_RSTINFO.SizeOfRstinfo should not be 0.
  56. /// </summary>
  57. [TestMethod]
  58. [Description("Verify NATIVE_RSTINFO.SizeOfIndexId is not 0")]
  59. [Priority(0)]
  60. public void VerifySizeOfRstinfoIsNotZero()
  61. {
  62. Assert.AreNotEqual(0, NATIVE_RSTINFO.SizeOfRstinfo);
  63. }
  64. /// <summary>
  65. /// Test calling JetFreeBuffer on a null buffer.
  66. /// </summary>
  67. [TestMethod]
  68. [Description("Test calling JetFreeBuffer on a null buffer")]
  69. [Priority(0)]
  70. public void FreeNullBuffer()
  71. {
  72. Api.JetFreeBuffer(IntPtr.Zero);
  73. }
  74. /// <summary>
  75. /// Verify that all TestMethods in this assembly have priorities.
  76. /// </summary>
  77. [TestMethod]
  78. [Description("Verify that all TestMethods in this assembly have priorities")]
  79. [Priority(1)]
  80. public void VerifyAllTestMethodsHavePriorities()
  81. {
  82. Assembly assembly = this.GetType().Assembly;
  83. VerifyAllTestMethodsHaveAttribute<PriorityAttribute>(assembly);
  84. }
  85. /// <summary>
  86. /// Verify that all TestMethods in this assembly have descriptions.
  87. /// </summary>
  88. [TestMethod]
  89. [Description("Verify that all TestMethods in this assembly have descriptions")]
  90. [Priority(1)]
  91. public void VerifyAllTestMethodsHaveDescriptions()
  92. {
  93. Assembly assembly = this.GetType().Assembly;
  94. VerifyAllTestMethodsHaveAttribute<DescriptionAttribute>(assembly);
  95. }
  96. /// <summary>
  97. /// Verify that all public methods on public types in the assembly
  98. /// have the [TestMethod] attribute.
  99. /// </summary>
  100. [TestMethod]
  101. [Description("Verify that all public methods on public types in the assembly have the [TestMethod] attribute")]
  102. [Priority(1)]
  103. public void VerifyAllPublicMethodsAreTests()
  104. {
  105. Assembly assembly = this.GetType().Assembly;
  106. var methods = FindPublicNonTestMethods(assembly);
  107. if (methods.Length > 0)
  108. {
  109. Console.WriteLine("{0} public methods have no [TestMethod] attribute", methods.Length);
  110. foreach (string m in methods)
  111. {
  112. Console.WriteLine("\t{0}", m);
  113. }
  114. Assert.Fail("A public method is not a test. Missing a [TestMethod] attribute?");
  115. }
  116. }
  117. /// <summary>
  118. /// Verify that all public methods on public classes and structs in the assembly
  119. /// override ToString().
  120. /// </summary>
  121. [TestMethod]
  122. [Description("Verify that all public classes have a ToString() method")]
  123. [Priority(0)]
  124. public void VerifyAllPublicClassesHaveToString()
  125. {
  126. Assembly assembly = typeof(Api).Assembly;
  127. var classes = FindPublicClassesWithoutToString(assembly);
  128. int classesWithoutToString = 0;
  129. foreach (Type @class in classes)
  130. {
  131. Console.WriteLine("{0} does not override Object.ToString", @class);
  132. ++classesWithoutToString;
  133. }
  134. Assert.AreEqual(0, classesWithoutToString, "Some classes do not override Object.ToString()");
  135. }
  136. /// <summary>
  137. /// Verify that all TestMethods in an assembly have a specific attribute.
  138. /// If not all methods have the attribute then the names of the methods
  139. /// are printed and the test fails.
  140. /// </summary>
  141. /// <typeparam name="T">The required attribute.</typeparam>
  142. /// <param name="assembly">The assembly to look in.</param>
  143. private static void VerifyAllTestMethodsHaveAttribute<T>(Assembly assembly) where T : Attribute
  144. {
  145. var methods = FindTestMethodsWithoutAttribute<T>(assembly);
  146. if (methods.Length > 0)
  147. {
  148. Console.WriteLine("{0} methods have no {1} attribute", methods.Length, typeof(T).Name);
  149. foreach (string m in methods)
  150. {
  151. Console.WriteLine("\t{0}", m);
  152. }
  153. Assert.Fail("A test method does not have a required attribute");
  154. }
  155. }
  156. /// <summary>
  157. /// Return an array of all test methods in the given assembly that do not have
  158. /// a specific attribute.
  159. /// </summary>
  160. /// <typeparam name="T">The required attribute.</typeparam>
  161. /// <param name="assembly">The assembly to look in.</param>
  162. /// <returns>An array of method names for test methods without the attribute.</returns>
  163. private static string[] FindTestMethodsWithoutAttribute<T>(Assembly assembly) where T : Attribute
  164. {
  165. return assembly.GetTypes()
  166. .SelectMany(
  167. t => from method in t.GetMethods()
  168. where method.GetCustomAttributes(true).Any(attribute => attribute is TestMethodAttribute)
  169. && !method.GetCustomAttributes(true).Any(attribute => attribute is T)
  170. select String.Format("{0}.{1}", method.DeclaringType, method.Name))
  171. .OrderBy(x => x)
  172. .ToArray();
  173. }
  174. /// <summary>
  175. /// Return an array of all public methods on [TestClass] types in the given assembly that do not have
  176. /// the [TestMethod] attribute. These are probably meant to be tests.
  177. /// </summary>
  178. /// <param name="assembly">The assembly to look in.</param>
  179. /// <returns>An array of method names for methods without the TestMethod attribute.</returns>
  180. private static IEnumerable<Type> FindPublicClassesWithoutToString(Assembly assembly)
  181. {
  182. return assembly.GetTypes()
  183. .Where(type =>
  184. type.IsPublic
  185. && !type.IsAbstract
  186. && !type.IsSubclassOf(typeof(Delegate))
  187. && type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
  188. .Any(method =>
  189. method.Name == "ToString"
  190. && method.DeclaringType == typeof(object)));
  191. }
  192. /// <summary>
  193. /// Return an array of all public methods on [TestClass] types in the given assembly that do not have
  194. /// the [TestMethod] attribute. These are probably meant to be tests.
  195. /// </summary>
  196. /// <param name="assembly">The assembly to look in.</param>
  197. /// <returns>An array of method names for methods without the TestMethod attribute.</returns>
  198. private static string[] FindPublicNonTestMethods(Assembly assembly)
  199. {
  200. return assembly.GetTypes()
  201. .Where(type => type.IsPublic)
  202. .SelectMany(
  203. type => from method in type.GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static)
  204. where !method.GetCustomAttributes(true).Any(attribute => attribute is ClassInitializeAttribute)
  205. && !method.GetCustomAttributes(true).Any(attribute => attribute is ClassCleanupAttribute)
  206. && !method.GetCustomAttributes(true).Any(attribute => attribute is TestInitializeAttribute)
  207. && !method.GetCustomAttributes(true).Any(attribute => attribute is TestCleanupAttribute)
  208. && !method.GetCustomAttributes(true).Any(attribute => attribute is TestMethodAttribute)
  209. select String.Format("{0}.{1}", method.DeclaringType, method.Name))
  210. .OrderBy(x => x)
  211. .ToArray();
  212. }
  213. }
  214. }