public void Attributes_on_properties_should_be_mapped() { var mapper = new MessageMapper(); mapper.Initialize(new[]{typeof(InterfaceWithPropertiesAndAttributes)}); Assert.IsTrue(PropertyContainsAttribute("SomeProperty",typeof(SomeAttribute),mapper.CreateInstance(typeof(InterfaceWithPropertiesAndAttributes)))); // Doesn't affect properties without attributes Assert.IsFalse(PropertyContainsAttribute("SomeOtherProperty", typeof(SomeAttribute), mapper.CreateInstance(typeof(InterfaceWithPropertiesAndAttributes)))); }
public void ShouldAllowMultipleMapperInstancesPerAppDomain() { Parallel.For(0, 10, i => { var mapper = new MessageMapper(); mapper.CreateInstance <SampleMessageClass>(); mapper.CreateInstance <ISampleMessageInterface>(); mapper.CreateInstance <ClassImplementingIEnumerable <string> >(); }); }
public void CreateInstance_WhenMessageNotInitialized_ShouldBeThreadsafe() { var mapper = new MessageMapper(); Parallel.For(0, 10, i => { mapper.CreateInstance <SampleMessageClass>(); mapper.CreateInstance <ISampleMessageInterface>(); mapper.CreateInstance <ClassImplementingIEnumerable <string> >(); }); }
public void TestMultipleInterfacesDuplicatedProperty() { var mapper = new MessageMapper(); var serializer = SerializerFactory.Create <IThird>(mapper); var msgBeforeSerialization = mapper.CreateInstance <IThird>(x => x.FirstName = "Danny"); var count = 0; using (var stream = new MemoryStream()) { serializer.Serialize(msgBeforeSerialization, stream); stream.Position = 0; var reader = XmlReader.Create(stream); while (reader.Read()) { if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "FirstName")) { count++; } } } Assert.AreEqual(count, 1); }
public void TestInterfaces() { var output = new MemoryStream(); var obj = messageMapper.CreateInstance <IA>( x => { x.S = "kalle"; x.I = 42; x.Data = new byte[23]; x.B = new B { BString = "BOO", C = new C { Cstr = "COO" } }; } ); new Random().NextBytes(obj.Data); messageMapper = new MessageMapper(); messageMapper.Initialize(new[] { typeof(IA), typeof(IAImpl) }); serializer = new JsonMessageSerializer(messageMapper, null, null, null, null); serializer.Serialize(obj, output); output.Position = 0; var filename = $"{GetType().Name}.{MethodBase.GetCurrentMethod().Name}.txt"; File.WriteAllBytes(filename, output.ToArray()); output.Position = 0; var result = serializer.Deserialize(output, new[] { typeof(IAImpl) }); Assert.DoesNotThrow(() => output.Position = 0, "Stream should still be open"); Assert.IsNotEmpty(result); Assert.That(result, Has.Length.EqualTo(1)); Assert.That(result[0], Is.AssignableTo(typeof(IA))); var a = (IA)result[0]; Assert.AreEqual(a.Data, obj.Data); Assert.AreEqual(42, a.I); Assert.AreEqual("kalle", a.S); Assert.IsNotNull(a.B); Assert.AreEqual("BOO", a.B.BString); Assert.AreEqual("COO", ((C)a.B.C).Cstr); }
public void Deserialize_message_without_concrete_implementation() { var messageMapper = new MessageMapper(); messageMapper.Initialize(new[] { typeof(ISuperMessageWithoutConcreteImpl) }); var serializer = new JsonMessageSerializer(messageMapper); using (var stream = new MemoryStream()) { var msg = messageMapper.CreateInstance <ISuperMessageWithoutConcreteImpl>(); msg.SomeProperty = "test"; serializer.Serialize(msg, stream); stream.Position = 0; var result = (ISuperMessageWithoutConcreteImpl)serializer.Deserialize(stream, new[] { typeof(ISuperMessageWithoutConcreteImpl) })[0]; Assert.AreEqual("test", result.SomeProperty); } }
public void Deserialize() { var messageMapper = new MessageMapper(); var messageTypes = new[] { typeof(IWithoutConcrete) }; messageMapper.Initialize(messageTypes); var serializer = new JsonMessageSerializer(messageMapper, null, null, null, null); var message = messageMapper.CreateInstance <IWithoutConcrete>(); message.SomeProperty = "test"; using (var stream = new MemoryStream()) { serializer.Serialize(message, stream); stream.Position = 0; var result = (IWithoutConcrete)serializer.Deserialize(stream, messageTypes)[0]; Assert.AreEqual("test", result.SomeProperty); } }
public void Send <T>(Action <T> messageConstructor, string destination) { if (string.IsNullOrWhiteSpace(destination)) { throw new ArgumentException("destination required", "destination"); } SendInternal(MessageMapper.CreateInstance(messageConstructor), new[] { destination }); }
public void Generated_type_should_preserve_namespace_to_make_it_easier_for_users_to_define_custom_conventions() { var mapper = new MessageMapper(); mapper.Initialize(new[] { typeof(IInterfaceToGenerate) }); Assert.AreEqual(typeof(IInterfaceToGenerate).Namespace, mapper.CreateInstance(typeof(IInterfaceToGenerate)).GetType().Namespace); }
public void Should_handle_messages_with_nullable_reference_types() { var mapper = new MessageMapper(); // Type defined in separate assembly as a workaround // because we can't use nullable refeference types yet mapper.CreateInstance <WithDodgyNullable.IMyMessage>(); }
public void Should_handle_interfaces_that_have_attributes_with_nullable_properties() { var mapper = new MessageMapper(); var messageInstance = mapper.CreateInstance <IMessageInterfaceWithNullablePropertyAttribute>(); Assert.IsNotNull(messageInstance); }
public static T CreateMessage <T>(Action <T> a2) where T : IMessage { IMessageMapper mapper = new MessageMapper(); mapper.Initialize(new[] { typeof(T) }); return(mapper.CreateInstance(a2)); }
public void Should_create_instance_of_concrete_type_with_illegal_interface_property() { var mapper = new MessageMapper(); mapper.Initialize(new[] { typeof(ConcreteMessageWithIllegalInterfaceProperty) }); mapper.CreateInstance <ConcreteMessageWithIllegalInterfaceProperty>(); }
public void Accept_Attributes_with_no_default_ctor_as_long_as_the_parameter_in_constructor_has_the_same_name_as_the_property() { var mapper = new MessageMapper(); mapper.Initialize(new[] { typeof(InterfaceWithCustomAttributeThatHasNoDefaultConstructor) }); var instance = mapper.CreateInstance(typeof (InterfaceWithCustomAttributeThatHasNoDefaultConstructor)); var attributes = instance.GetType().GetProperty("SomeProperty").GetCustomAttributes(typeof(CustomAttributeWithNoDefaultConstructor),true); var attr = (CustomAttributeWithNoDefaultConstructor)attributes[0]; Assert.AreEqual(attr.Name, "Blah"); }
public void CreateInstance_should_initialize_interface_message_type_on_demand() { var mapper = new MessageMapper(); var messageInstance = mapper.CreateInstance <ISampleMessageInterface>(); Assert.IsNotNull(messageInstance); Assert.IsInstanceOf <ISampleMessageInterface>(messageInstance); }
public void Should_create_a_new_saga_if_no_existing_instance_is_found_for_interface_based_messages() { RegisterSaga <MySaga>(); RegisterMessageType <StartMessageThatIsAnInterface>(); ReceiveMessage(MessageMapper.CreateInstance <StartMessageThatIsAnInterface>()); Assert.AreEqual(1, persister.CurrentSagaEntities.Keys.Count()); }
public void Interface_should_be_created() { var mapper = new MessageMapper(); mapper.Initialize(new[] { typeof(Interface) }); var result = mapper.CreateInstance<Interface>(null); Assert.IsNotNull(result); }
public void Should_create_structs() { var mapper = new MessageMapper(); var messageInstance = mapper.CreateInstance <SampleMessageStruct>(); Assert.IsNotNull(messageInstance); Assert.AreEqual(typeof(SampleMessageStruct), messageInstance.GetType()); }
// this is not desired behavior and just documents current behavior public void CreateInstance_should_not_initialize_message_type_implementing_IEnumerable() { var mapper = new MessageMapper(); var messageInstance = mapper.CreateInstance <ClassImplementingIEnumerable <string> >(); Assert.IsNotNull(messageInstance); Assert.IsFalse(messageInstance.CtorInvoked); }
public void Interface_should_be_created() { var mapper = new MessageMapper(); mapper.Initialize(new[] { typeof(IInterface) }); var result = mapper.CreateInstance <IInterface>(null); Assert.IsNotNull(result); }
public void CreateInstance_should_initialize_message_type_on_demand() { var mapper = new MessageMapper(); var messageInstance = mapper.CreateInstance <SampleMessageClass>(); Assert.IsNotNull(messageInstance); Assert.AreEqual(typeof(SampleMessageClass), messageInstance.GetType()); Assert.IsTrue(messageInstance.CtorInvoked); }
public T CreateInstance <T>(Action <T> action) { var instance = MessageMapper.CreateInstance <T>(); if (action != null) { action(instance); } return(instance); }
public void Accept_Attributes_with_no_default_ctor_as_long_as_the_parameter_in_constructor_has_the_same_name_as_the_property() { var mapper = new MessageMapper(); mapper.Initialize(new[] { typeof(IInterfaceWithCustomAttributeThatHasNoDefaultConstructor) }); var instance = mapper.CreateInstance(typeof(IInterfaceWithCustomAttributeThatHasNoDefaultConstructor)); var attributes = instance.GetType().GetProperty("SomeProperty").GetCustomAttributes(typeof(CustomAttributeWithNoDefaultConstructor), true); var attr = (CustomAttributeWithNoDefaultConstructor)attributes[0]; Assert.AreEqual(attr.Name, "Blah"); }
public void Accept_Attributes_with_no_default_ctor_while_ctor_parameters_are_different_than_properties_of_custom_attribute() { var mapper = new MessageMapper(); mapper.Initialize(new[] { typeof(IInterfaceWithCustomAttributeThatHasNoDefaultConstructorAndNoMatchingParameters) }); var instance = mapper.CreateInstance(typeof(IInterfaceWithCustomAttributeThatHasNoDefaultConstructorAndNoMatchingParameters)); var attributes = instance.GetType().GetProperty("SomeProperty").GetCustomAttributes(typeof(CustomAttributeWithNoDefaultConstructorAndNoMatchingParameters), true); var attr = (CustomAttributeWithNoDefaultConstructorAndNoMatchingParameters)attributes[0]; Assert.AreEqual(attr.Name, "Blah"); }
public void Accept_attributes_with_value_attribute() { var mapper = new MessageMapper(); mapper.Initialize(new[] { typeof(IMyEventWithAttributeWithBoolProperty) }); var instance = mapper.CreateInstance(typeof(IMyEventWithAttributeWithBoolProperty)); var attributes = instance.GetType().GetProperty("EventId").GetCustomAttributes(typeof(CustomAttributeWithValueProperties), true); var attr = attributes[0] as CustomAttributeWithValueProperties; Assert.AreEqual(attr != null && attr.FlagIsSet, true); if (attr != null) { Assert.AreEqual(attr.MyAge, 21); } }
public void Updating_the_message_to_a_new_type_should_update_the_MessageType() { var mapper = new MessageMapper(); var message = mapper.CreateInstance <IMyMessage>(m => m.Id = Guid.NewGuid()); var context = new OutgoingLogicalMessageContext("message1234", new Dictionary <string, string>(), new OutgoingLogicalMessage(typeof(IMyMessage), message), null, null); var differentMessage = new MyDifferentMessage { Id = Guid.NewGuid() }; context.UpdateMessage(differentMessage); Assert.AreEqual(typeof(MyDifferentMessage), context.Message.MessageType); }
private object GetObjectOfTypeFromNode(Type t, XmlNode node) { if (t.IsSimpleType() || t == typeof(Uri)) { return(GetPropertyValue(t, node)); } if (typeof(IEnumerable).IsAssignableFrom(t)) { return(GetPropertyValue(t, node)); } object result = MessageMapper.CreateInstance(t); foreach (XmlNode n in node.ChildNodes) { Type type = null; if (n.Name.Contains(":")) { type = Type.GetType("System." + n.Name.Substring(0, n.Name.IndexOf(":")), false, true); } var prop = GetProperty(t, n.Name); if (prop != null) { var val = GetPropertyValue(type ?? prop.PropertyType, n); if (val != null) { propertyInfoToLateBoundPropertySet[prop].Invoke(result, val); continue; } } var field = GetField(t, n.Name); if (field != null) { object val = GetPropertyValue(type ?? field.FieldType, n); if (val != null) { fieldInfoToLateBoundFieldSet[field].Invoke(result, val); continue; } } } return(result); }
public void Serialize_message_without_concrete_implementation() { MessageMapper = new MessageMapper(); MessageMapper.Initialize(new[] { typeof(ISuperMessageWithoutConcreteImpl) }); Serializer = new JsonMessageSerializer(MessageMapper); using (var stream = new MemoryStream()) { Serializer.Serialize(MessageMapper.CreateInstance <ISuperMessageWithoutConcreteImpl>(), stream); stream.Position = 0; var result = new StreamReader(stream).ReadToEnd(); Assert.That(!result.Contains("$type"), result); Assert.That(result.Contains("SomeProperty"), result); } }
public void SerializeEmptyLists() { IMessageMapper mapper = new MessageMapper(); var serializer = SerializerFactory.Create <MessageWithList>(); var msg = mapper.CreateInstance <MessageWithList>(); msg.Items = new List <MessageWithListItem>(); using (var stream = new MemoryStream()) { serializer.Serialize(msg, stream); stream.Position = 0; var msgArray = serializer.Deserialize(stream); var m = (MessageWithList)msgArray[0]; Assert.IsEmpty(m.Items); } }
public void Serialize_message_without_concrete_implementation() { var mapper = new MessageMapper(); mapper.Initialize(new [] { typeof(ISuperMessageWithoutConcreteImpl) }); using (var stream = new MemoryStream()) { Serializer.SkipArrayWrappingForSingleMessages = true; Serializer.Serialize(new object[] { mapper.CreateInstance <ISuperMessageWithoutConcreteImpl>() }, stream); stream.Position = 0; var result = new StreamReader(stream).ReadToEnd(); Assert.That(!result.Contains("$type"), result); Assert.That(result.Contains("SomeProperty"), result); } }
public void Deserialize_message_without_concrete_implementation() { var mapper = new MessageMapper(); mapper.Initialize(new[] { typeof(ISuperMessageWithoutConcreteImpl) }); using (var stream = new MemoryStream()) { Serializer.SkipArrayWrappingForSingleMessages = true; var msg = mapper.CreateInstance<ISuperMessageWithoutConcreteImpl>(); msg.SomeProperty = "test"; Serializer.Serialize(new object[] { msg }, stream); stream.Position = 0; var result = (ISuperMessageWithoutConcreteImpl)Serializer.Deserialize(stream, new[] { typeof(ISuperMessageWithoutConcreteImpl) })[0]; Assert.AreEqual("test", result.SomeProperty); } }
public void SerializeClosedGenericListsInSameNamespace() { IMessageMapper mapper = new MessageMapper(); var serializer = SerializerFactory.Create <MessageWithClosedList>(); var msg = mapper.CreateInstance <MessageWithClosedList>(); msg.Items = new ItemList { new MessageWithListItem { Data = "Hello" } }; using (var stream = new MemoryStream()) { serializer.Serialize(msg, stream); stream.Position = 0; var msgArray = serializer.Deserialize(stream); var m = (MessageWithClosedList)msgArray[0]; Assert.AreEqual("Hello", m.Items.First().Data); } }
public void SerializeClosedGenericListsInAlternateNamespaceMultipleIListImplementations() { IMessageMapper mapper = new MessageMapper(); var serializer = SerializerFactory.Create <MessageWithClosedListInAlternateNamespaceMultipleIListImplementations>(); var msg = mapper.CreateInstance <MessageWithClosedListInAlternateNamespaceMultipleIListImplementations>(); msg.Items = new AlternateNamespace.AlternateItemListMultipleIListImplementations { new AlternateNamespace.MessageWithListItemAlternate { Data = "Hello" } }; using (var stream = new MemoryStream()) { serializer.Serialize(msg, stream); stream.Position = 0; var msgArray = serializer.Deserialize(stream); var m = (MessageWithClosedListInAlternateNamespaceMultipleIListImplementations)msgArray[0]; Assert.AreEqual("Hello", m.Items.First <AlternateNamespace.MessageWithListItemAlternate>().Data); } }
public void SerializeLists() { IMessageMapper mapper = new MessageMapper(); var serializer = SerializerFactory.Create <MessageWithList>(); var msg = mapper.CreateInstance <MessageWithList>(); msg.Items = new List <MessageWithListItem> { new MessageWithListItem { Data = "Hello" } }; using (var stream = new MemoryStream()) { serializer.Serialize(new[] { msg }, stream); stream.Position = 0; var msgArray = serializer.Deserialize(stream); var m = msgArray[0] as MessageWithList; Assert.AreEqual("Hello", m.Items.First().Data); } }
public void Generated_type_should_preserve_namespace_to_make_it_easier_for_users_to_define_custom_conventions() { var mapper = new MessageMapper(); mapper.Initialize(new[] { typeof(InterfaceToGenerate) }); Assert.AreEqual(typeof(InterfaceToGenerate).Namespace, mapper.CreateInstance(typeof(InterfaceToGenerate)).GetType().Namespace); }
public void Accept_Attributes_with_no_default_ctor_while_ctor_parameters_are_different_than_properties_of_custom_attribute() { var mapper = new MessageMapper(); mapper.Initialize(new[] { typeof(InterfaceWithCustomAttributeThatHasNoDefaultConstructorAndNoMatchingParameters) }); var instance = mapper.CreateInstance(typeof(InterfaceWithCustomAttributeThatHasNoDefaultConstructorAndNoMatchingParameters)); var attributes = instance.GetType().GetProperty("SomeProperty").GetCustomAttributes(typeof(CustomAttributeWithNoDefaultConstructorAndNoMatchingParameters), true); var attr = (CustomAttributeWithNoDefaultConstructorAndNoMatchingParameters)attributes[0]; Assert.AreEqual(attr.Name, "Blah"); }
private void messageTypeList_SelectedIndexChanged(object sender, EventArgs e) { var messageType = messageTypeList.SelectedItem as Type; if (messageType == null) return; var mapper = new MessageMapper(); mapper.Initialize(new[] { messageType }); _serializer.MessageMapper = mapper; _serializer.InitType(messageType); var message = mapper.CreateInstance(messageType); InitializeProperties(message); propertyGrid1.SelectedObject = message; bodyText.Text = SerializeMessage(message); }
public void Serialize_message_without_concrete_implementation() { var mapper = new MessageMapper(); mapper.Initialize(new []{ typeof(ISuperMessageWithoutConcreteImpl)}); using (var stream = new MemoryStream()) { Serializer.SkipArrayWrappingForSingleMessages = true; Serializer.Serialize(new object[] { mapper.CreateInstance<ISuperMessageWithoutConcreteImpl>() }, stream); stream.Position = 0; var result = new StreamReader(stream).ReadToEnd(); Assert.That(!result.Contains("$type"), result); Assert.That(result.Contains("SomeProperty"), result); } }
public void Accept_attributes_with_value_attribute() { var mapper = new MessageMapper(); mapper.Initialize(new[] { typeof(IMyEventWithAttributeWithBoolProperty) }); var instance = mapper.CreateInstance(typeof(IMyEventWithAttributeWithBoolProperty)); var attributes = instance.GetType().GetProperty("EventId").GetCustomAttributes(typeof(CustomAttributeWithValueProperties), true); var attr = attributes[0] as CustomAttributeWithValueProperties; Assert.AreEqual(attr != null && attr.FlagIsSet, true); if (attr != null) Assert.AreEqual(attr.MyAge, 21); }
public void Serialize_message_without_concrete_implementation() { var messageMapper = new MessageMapper(); messageMapper.Initialize(new[] { typeof(ISuperMessageWithoutConcreteImpl) }); var serializer = new JsonMessageSerializer(messageMapper); using (var stream = new MemoryStream()) { serializer.Serialize(messageMapper.CreateInstance<ISuperMessageWithoutConcreteImpl>(), stream); stream.Position = 0; var result = new StreamReader(stream).ReadToEnd(); Assert.That(!result.Contains("$type"), result); Assert.That(result.Contains("SomeProperty"), result); } }
public void Deserialize_message_without_concrete_implementation() { var messageMapper = new MessageMapper(); messageMapper.Initialize(new[] { typeof(ISuperMessageWithoutConcreteImpl) }); var serializer = new JsonMessageSerializer(messageMapper); using (var stream = new MemoryStream()) { var msg = messageMapper.CreateInstance<ISuperMessageWithoutConcreteImpl>(); msg.SomeProperty = "test"; serializer.Serialize(msg, stream); stream.Position = 0; var result = (ISuperMessageWithoutConcreteImpl) serializer.Deserialize(stream, new[] { typeof(ISuperMessageWithoutConcreteImpl) })[0]; Assert.AreEqual("test", result.SomeProperty); } }
public void TestInterfaces() { var output = new MemoryStream(); var messageMapper = new MessageMapper(); messageMapper.Initialize(new[] { typeof(IA), typeof(IAImpl) }); var obj = messageMapper.CreateInstance<IA>( x => { x.S = "kalle"; x.I = 42; x.Data = new byte[23]; x.B = new B { BString = "BOO", C = new C { Cstr = "COO" } }; } ); new Random().NextBytes(obj.Data); var serializer = new JsonMessageSerializer(messageMapper); serializer.Serialize(obj, output); output.Position = 0; var filename = $"{GetType().Name}.{MethodBase.GetCurrentMethod().Name}.txt"; File.WriteAllBytes(filename, output.ToArray()); output.Position = 0; var result = serializer.Deserialize(output, new[] { typeof(IAImpl) }); Assert.DoesNotThrow(() => output.Position = 0, "Stream should still be open"); Assert.IsNotEmpty(result); Assert.That(result, Has.Length.EqualTo(1)); Assert.That(result[0], Is.AssignableTo(typeof(IA))); var a = (IA) result[0]; Assert.AreEqual(a.Data, obj.Data); Assert.AreEqual(42, a.I); Assert.AreEqual("kalle", a.S); Assert.IsNotNull(a.B); Assert.AreEqual("BOO", a.B.BString); Assert.AreEqual("COO", ((C) a.B.C).Cstr); }