Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Collections.Generic;
- using System.Linq;
- using XYZ.Core;
- using XYZ.Entities;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using System.Reflection;
- namespace XYZ.Tests.Entities
- {
- /// <summary>
- /// Summary description for GenericEntityTest
- /// </summary>
- [TestClass]
- public class GenericEntityTest
- {
- private const string ENTITY_NAMESPACE = "XYZ.Entities";
- [TestMethod]
- public void AllEntitiesMustBeSerializable()
- {
- IterateAllIEntites(
- (type, notSerializableTypes) =>
- {
- var serializableAtt = type.GetCustomAttributes(typeof(SerializableAttribute), false);
- if (serializableAtt.Length != 1)
- notSerializableTypes.Add(type);
- }
- , "The following Types are not serializable :");
- }
- [TestMethod]
- public void AllEntitiesMustBeInTheEntityNamespace()
- {
- IterateAllIEntites(
- (type, badNameSpaceTypes) =>
- {
- var nameSpace = type.Namespace;
- if (!nameSpace.StartsWith(ENTITY_NAMESPACE))
- badNameSpaceTypes.Add(type);
- }
- , "The following Types are in an incorrect namespace :");
- }
- [TestMethod]
- public void AllEntitiesMustHaveAllMethodsVirtual()
- {
- IterateAllIEntites(
- (type, invalidTypes) =>
- {
- var methods = type.GetMethods(BindingFlags.DeclaredOnly);
- foreach (var method in methods)
- {
- if (!method.IsVirtual)
- {
- invalidTypes.Add(type);
- break;
- }
- }
- }
- , "The following Types have non virtual methods :");
- }
- [TestMethod]
- public void AllEntitiesMustHaveAllPropertiesVirtual()
- {
- IterateAllIEntites(
- (type, invalidTypes) =>
- {
- var properties = type.GetProperties();
- foreach (var property in properties)
- {
- var setter = property.GetSetMethod(true);
- if (setter != null && !setter.IsVirtual)
- {
- invalidTypes.Add(type);
- break;
- }
- var getter = property.GetGetMethod(true);
- if (getter != null && !getter.IsVirtual)
- {
- invalidTypes.Add(type);
- break;
- }
- }
- }
- , "The following Types have non virtual properties :");
- }
- [TestMethod]
- public void AllEntitiesMustHaveADefaultConstructor()
- {
- IterateAllIEntites(
- (type, invalidTypes) =>
- {
- var ctorInfo = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[0], null);
- if (ctorInfo == null || ctorInfo.IsPrivate)
- invalidTypes.Add(type);
- }
- , "The following Types require a public or protected default constructor :");
- }
- [TestMethod]
- public void AllTypesInTheEntitiesNamespaceMustImplementIEntity()
- {
- var invalidTypes = new List<Type>();
- Type[] types = typeof(Ccn).Assembly.GetTypes();
- var typesInNamespace = from t in types
- where t.Namespace.StartsWith(ENTITY_NAMESPACE)
- select t;
- foreach (var typeInNamespace in typesInNamespace)
- {
- var implementIEntity = false;
- Type[] interfaceTypes = typeInNamespace.GetInterfaces();
- foreach (Type interfaceType in interfaceTypes)
- {
- // if you check for interfaceType == typeof(IEntity<>) now
- // you won't find it
- if (interfaceType.IsGenericType &&
- interfaceType.GetGenericTypeDefinition() == typeof (IEntity<>))
- implementIEntity = true;
- }
- if(!implementIEntity)
- invalidTypes.Add(typeInNamespace);
- }
- Assert.AreEqual(0, invalidTypes.Count(), "The Following types are in the Entities namepsace but do not implement IEntity" + ListTypes(invalidTypes));
- }
- private static void IterateAllIEntites(Action<Type, List<Type>> action, String errorPrefix)
- {
- //get all the entities
- Type[] types = typeof(Ccn).Assembly.GetTypes();
- var invalidTypes = new List<Type>();
- foreach (var type in types)
- {
- Type[] interfaceTypes = type.GetInterfaces();
- foreach (Type interfaceType in interfaceTypes)
- {
- // if you check for interfaceType == typeof(IEntity<>) now
- // you won't find it
- if (interfaceType.IsGenericType &&
- interfaceType.GetGenericTypeDefinition() == typeof(IEntity<>))
- // now you have found an interface that was constructed
- // from IEntity<T>
- {
- action(type, invalidTypes);
- }
- }
- }
- Assert.AreEqual(0, invalidTypes.Count(), errorPrefix + ListTypes(invalidTypes));
- }
- private static string ListTypes(IEnumerable<Type> unpersitableTypes)
- {
- var sb = new StringBuilder();
- sb.AppendLine();
- foreach (var type in unpersitableTypes)
- {
- sb.AppendLine(type.Name);
- }
- return sb.ToString();
- }
- }
- }
Add Comment
Please, Sign In to add comment