Created
May 27, 2014 16:32
-
-
Save danbarua/81cfb92c4a931c9e8ba1 to your computer and use it in GitHub Desktop.
Courtesey of @the.fridge.ninja
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestFixture] | |
public class MessagingAcceptanceTests | |
{ | |
[Test] | |
public void All_commands_are_decorated_with_data_contract() | |
{ | |
var messageTypes = typeof(Command).Assembly.GetTypes() | |
.Where(typeof(Command).IsAssignableFrom) | |
.Where(type => type.IsClass && false == type.IsAbstract); | |
var badMessageTypes = (from messageType in messageTypes | |
where false == messageType.GetCustomAttributes(false).OfType<DataContractAttribute>().Any() | |
select messageType).ToArray(); | |
var errorMessage = badMessageTypes | |
.Aggregate( | |
new StringBuilder().AppendLine("The following types were not decorated with DataContract:"), | |
(sb, type) => sb.AppendLine(type.FullName)).ToString(); | |
Assert.IsEmpty(badMessageTypes, errorMessage); | |
} | |
[Test] | |
public void All_messages_have_a_private_parameterless_constructor() | |
{ | |
var messageTypes = typeof (Event).Assembly.GetTypes() | |
.Where(typeof (Message).IsAssignableFrom) | |
.Where(type => type.IsClass && false == type.IsAbstract); | |
var badMessageTypes = (from messageType in messageTypes | |
where messageType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null) == null | |
select messageType).ToArray(); | |
var errorMessage = badMessageTypes | |
.Aggregate( | |
new StringBuilder().AppendLine("The following types did not have a private parameterless constructor:"), | |
(sb, type) => sb.AppendLine(type.FullName)).ToString(); | |
Assert.IsEmpty(badMessageTypes, errorMessage); | |
} | |
[Test] | |
public void All_messages_override_to_string() | |
{ | |
var messageTypes = typeof(Event).Assembly.GetTypes() | |
.Where(typeof(Message).IsAssignableFrom) | |
.Where(type => type.IsClass && false == type.IsAbstract); | |
var badMessageTypes = (from messageType in messageTypes | |
where messageType.GetMethod("ToString").DeclaringType != messageType | |
select messageType).ToArray(); | |
var errorMessage = badMessageTypes | |
.Aggregate( | |
new StringBuilder().AppendLine("The following types did not override ToString:"), | |
(sb, type) => sb.AppendLine(type.FullName)).ToString(); | |
Assert.IsEmpty(badMessageTypes, errorMessage); | |
} | |
[Test] | |
public void All_messages_can_be_serialized_by_the_net_data_contract_formatter() | |
{ | |
TestFormatter(new NetDataContractMessageFormatter(new NetDataContractSerializer())); | |
} | |
[Test] | |
public void All_messages_can_be_serialized_by_the_protobuf_formatter() | |
{ | |
TestFormatter(new ProtobufMessageFormatter()); | |
} | |
[Test] | |
public void All_messages_can_be_serialized_by_the_microsoft_queue_formatter() | |
{ | |
TestFormatter(new MicrosoftQueueFormatter(new ProtobufMessageFormatter(), msg => msg.GetType().ToPartiallyQualifiedName(), Type.GetType)); | |
} | |
private void TestFormatter(IMessageFormatter formatter) | |
{ | |
var messageTypes = typeof (Event).Assembly.GetTypes() | |
.Where(typeof (Message).IsAssignableFrom) | |
.Where(type => type.IsClass && false == type.IsAbstract); | |
Func<object, object> safeSerialization = input => | |
{ | |
object output = null; | |
try | |
{ | |
output = formatter.Deserialize(formatter.Serialize(input), input.GetType()); | |
} | |
catch | |
{ | |
} | |
return output; | |
}; | |
var badMessageTypes = (from messageType in messageTypes | |
where safeSerialization(Activator.CreateInstance(messageType, true)) == null | |
select messageType).ToArray(); | |
var errorMessage = badMessageTypes | |
.Aggregate( | |
new StringBuilder().Append("The following types could not be serialized by the ") | |
.Append(formatter.GetType().FullName) | |
.Append(" formatter:"), | |
(sb, type) => sb.AppendLine(type.FullName)).ToString(); | |
Assert.IsEmpty(badMessageTypes, errorMessage); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment