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