Guest User

Untitled

a guest
Jun 25th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using XYZ.Core;
  6. using XYZ.Entities;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using System.Reflection;
  9.  
  10. namespace XYZ.Tests.Entities
  11. {
  12. /// <summary>
  13. /// Summary description for GenericEntityTest
  14. /// </summary>
  15. [TestClass]
  16. public class GenericEntityTest
  17. {
  18.  
  19. private const string ENTITY_NAMESPACE = "XYZ.Entities";
  20.  
  21. [TestMethod]
  22. public void AllEntitiesMustBeSerializable()
  23. {
  24. IterateAllIEntites(
  25. (type, notSerializableTypes) =>
  26. {
  27. var serializableAtt = type.GetCustomAttributes(typeof(SerializableAttribute), false);
  28. if (serializableAtt.Length != 1)
  29. notSerializableTypes.Add(type);
  30. }
  31. , "The following Types are not serializable :");
  32. }
  33.  
  34. [TestMethod]
  35. public void AllEntitiesMustBeInTheEntityNamespace()
  36. {
  37. IterateAllIEntites(
  38. (type, badNameSpaceTypes) =>
  39. {
  40. var nameSpace = type.Namespace;
  41. if (!nameSpace.StartsWith(ENTITY_NAMESPACE))
  42. badNameSpaceTypes.Add(type);
  43. }
  44. , "The following Types are in an incorrect namespace :");
  45. }
  46. [TestMethod]
  47. public void AllEntitiesMustHaveAllMethodsVirtual()
  48. {
  49. IterateAllIEntites(
  50. (type, invalidTypes) =>
  51. {
  52. var methods = type.GetMethods(BindingFlags.DeclaredOnly);
  53. foreach (var method in methods)
  54. {
  55. if (!method.IsVirtual)
  56. {
  57. invalidTypes.Add(type);
  58. break;
  59. }
  60. }
  61. }
  62. , "The following Types have non virtual methods :");
  63. }
  64. [TestMethod]
  65. public void AllEntitiesMustHaveAllPropertiesVirtual()
  66. {
  67. IterateAllIEntites(
  68. (type, invalidTypes) =>
  69. {
  70. var properties = type.GetProperties();
  71. foreach (var property in properties)
  72. {
  73. var setter = property.GetSetMethod(true);
  74. if (setter != null && !setter.IsVirtual)
  75. {
  76. invalidTypes.Add(type);
  77. break;
  78. }
  79. var getter = property.GetGetMethod(true);
  80. if (getter != null && !getter.IsVirtual)
  81. {
  82. invalidTypes.Add(type);
  83. break;
  84. }
  85. }
  86. }
  87. , "The following Types have non virtual properties :");
  88. }
  89.  
  90. [TestMethod]
  91. public void AllEntitiesMustHaveADefaultConstructor()
  92. {
  93. IterateAllIEntites(
  94. (type, invalidTypes) =>
  95. {
  96. var ctorInfo = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[0], null);
  97. if (ctorInfo == null || ctorInfo.IsPrivate)
  98. invalidTypes.Add(type);
  99. }
  100. , "The following Types require a public or protected default constructor :");
  101. }
  102.  
  103. [TestMethod]
  104. public void AllTypesInTheEntitiesNamespaceMustImplementIEntity()
  105. {
  106. var invalidTypes = new List<Type>();
  107. Type[] types = typeof(Ccn).Assembly.GetTypes();
  108. var typesInNamespace = from t in types
  109. where t.Namespace.StartsWith(ENTITY_NAMESPACE)
  110. select t;
  111. foreach (var typeInNamespace in typesInNamespace)
  112. {
  113. var implementIEntity = false;
  114. Type[] interfaceTypes = typeInNamespace.GetInterfaces();
  115. foreach (Type interfaceType in interfaceTypes)
  116. {
  117. // if you check for interfaceType == typeof(IEntity<>) now
  118. // you won't find it
  119. if (interfaceType.IsGenericType &&
  120. interfaceType.GetGenericTypeDefinition() == typeof (IEntity<>))
  121. implementIEntity = true;
  122. }
  123. if(!implementIEntity)
  124. invalidTypes.Add(typeInNamespace);
  125. }
  126. Assert.AreEqual(0, invalidTypes.Count(), "The Following types are in the Entities namepsace but do not implement IEntity" + ListTypes(invalidTypes));
  127. }
  128.  
  129.  
  130. private static void IterateAllIEntites(Action<Type, List<Type>> action, String errorPrefix)
  131. {
  132. //get all the entities
  133. Type[] types = typeof(Ccn).Assembly.GetTypes();
  134. var invalidTypes = new List<Type>();
  135. foreach (var type in types)
  136. {
  137. Type[] interfaceTypes = type.GetInterfaces();
  138. foreach (Type interfaceType in interfaceTypes)
  139. {
  140. // if you check for interfaceType == typeof(IEntity<>) now
  141. // you won't find it
  142. if (interfaceType.IsGenericType &&
  143. interfaceType.GetGenericTypeDefinition() == typeof(IEntity<>))
  144. // now you have found an interface that was constructed
  145. // from IEntity<T>
  146. {
  147. action(type, invalidTypes);
  148. }
  149. }
  150. }
  151. Assert.AreEqual(0, invalidTypes.Count(), errorPrefix + ListTypes(invalidTypes));
  152.  
  153. }
  154.  
  155. private static string ListTypes(IEnumerable<Type> unpersitableTypes)
  156. {
  157. var sb = new StringBuilder();
  158. sb.AppendLine();
  159. foreach (var type in unpersitableTypes)
  160. {
  161. sb.AppendLine(type.Name);
  162. }
  163. return sb.ToString();
  164. }
  165. }
  166. }
Add Comment
Please, Sign In to add comment