Skip to content

Instantly share code, notes, and snippets.

@CasperTDK
Created August 21, 2013 08:38
Show Gist options
  • Save CasperTDK/6291846 to your computer and use it in GitHub Desktop.
Save CasperTDK/6291846 to your computer and use it in GitHub Desktop.
Test datacontract on message dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using NUnit.Framework;
namespace PBAServer.Test
{
[TestFixture]
public class TestSerialization
{
[TestCase("PBAServer.Commands.Broadcast.DataTransferObjects.SimpleRegulationBidIntervalDto, PBAServer.Commands.Broadcast")]
public void Assert_DataContractAndEnumMemberAttribute_IsSetForEnums(string typeAssemblyQualifiedName)
{
var list = Assembly.GetAssembly(Type.GetType(typeAssemblyQualifiedName))
.GetTypes().Where(type => (type.IsEnum));
var errorList = new List<string>();
foreach (var clazz in list)
{
CheckClassAttributes(clazz, errorList);
CheckEnumAttributes(clazz, errorList);
}
Assert.AreEqual(0, errorList.Count);
}
[TestCase("PBAServer.Commands.Broadcast.DataTransferObjects.SimpleRegulationBidIntervalDto, PBAServer.Commands.Broadcast")]
public void Assert_DataContractAndDataMemberAttribute_IsSet(string typeAssemblyQualifiedName)
{
var falsePositives =
new List<Tuple<string, string>> //Class name, property name
{
};
var list = Assembly.GetAssembly(Type.GetType(typeAssemblyQualifiedName))
.GetTypes()
.Where(type => (type.IsClass && MustBeCheckedForSerializability(type)));
Assert.IsNotEmpty(list);
var errorList = new List<string>();
foreach (var clazz in list)
{
CheckClassAttributes(clazz, errorList);
CheckFieldAttributes(clazz, errorList);
CheckPropertyHasDataMemberAttributes(clazz, errorList, falsePositives);
CheckPropertyWithoutSetterIsNotDataMember(clazz, errorList, falsePositives);
}
Assert.IsEmpty(errorList);
}
private void CheckPropertyHasDataMemberAttributes(Type clazz, List<string> errorList, List<Tuple<string, string>> falsePositives)
{
var errors =
clazz.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly).
Where(prop => Attribute.GetCustomAttribute(prop, typeof(DataMemberAttribute)) == null).
Where(prop => prop.CanWrite).
Where(prop => NotInFalsePositives(clazz.Name, prop.Name, falsePositives)).
Select(prop => string.Format("Property {0} in class {1} does not have DataMember Attribute!", prop.Name, clazz.Name)).ToList();
errors.ForEach(Console.WriteLine);
errorList.AddRange(errors);
}
private bool NotInFalsePositives(string className, string propName, List<Tuple<string, string>> falsePositives)
{
return !falsePositives.Any(clsAndProp => clsAndProp.Item1 == className && clsAndProp.Item2 == propName);
}
private void CheckPropertyWithoutSetterIsNotDataMember(Type clazz, List<string> errorList, List<Tuple<string, string>> falsePositives)
{
var errors =
clazz.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly).
Where(prop => Attribute.GetCustomAttribute(prop, typeof(DataMemberAttribute)) != null).
Where(prop => !prop.CanWrite).
Select(prop => string.Format("No setter and marked with DataMember attribute. Remove attribute or create setter. Property {0} in class {1} !", prop.Name, clazz.Name)).ToList();
errors.ForEach(Console.WriteLine);
errorList.AddRange(errors);
}
private void CheckFieldAttributes(Type clazz, List<string> errorList)
{
var errors =
clazz.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly).
Where(field => Attribute.GetCustomAttribute(field, typeof(DataMemberAttribute)) == null).
Select(field => string.Format("Field {0} in class {1} does not have DataMember Attribute!", field.Name, clazz.Name)).ToList();
errors.ForEach(Console.WriteLine);
errorList.AddRange(errors);
}
bool MustBeCheckedForSerializability(Type type)
{
//anonymous type class
if (type.Name.Contains("__")) return false;
return true;
}
private static void CheckClassAttributes(Type clazz, List<string> errorList)
{
var attributes = Attribute.GetCustomAttributes(clazz);
var hasDataContractAttribute = attributes.OfType<DataContractAttribute>().Any();
if (!hasDataContractAttribute)
{
var error = string.Format("{0} does not have DataContract Attribute!", clazz.FullName);
Console.WriteLine(error);
errorList.Add(error);
}
}
private void CheckEnumAttributes(Type clazz, List<string> errorList)
{
var errors =
clazz.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly).
Where(field => Attribute.GetCustomAttribute(field, typeof(EnumMemberAttribute)) == null).
Select(field => string.Format("Enum value {0} in enum {1} does not have EnumMember Attribute!", field.Name, clazz.Name)).ToList();
errors.ForEach(Console.WriteLine);
errorList.AddRange(errors);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment