diff --git a/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Support/Converter/TypeMapper.cs b/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Support/Converter/TypeMapper.cs index a23f911b..f46b4991 100644 --- a/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Support/Converter/TypeMapper.cs +++ b/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Support/Converter/TypeMapper.cs @@ -40,6 +40,7 @@ namespace Spring.Messaging.Nms.Support.Converter private string defaultHashtableTypeId = "Hashtable"; private Type defaultHashtableClass = typeof(Hashtable); + private bool useAssemblyQualifiedName; /// /// Initializes a new instance of the [ERROR: invalid expression DeclaringTypeKind]. @@ -98,15 +99,46 @@ namespace Spring.Messaging.Nms.Support.Converter { return defaultHashtableTypeId; } - return typeOfObjectToConvert.Name; + if (UseAssemblyQualifiedName) + { + return typeOfObjectToConvert.AssemblyQualifiedName; + } + else + { + return typeOfObjectToConvert.Name; + } } } /// - /// Convert from a string to a type + /// Gets or sets a value indicating whether use the assembly qualified name when creating a string from a type reference. + /// + /// + /// By setting this property to true you will be able to easily share types that are available on both the publisher and + /// consumer with minimal configuration. However, this means that the publishers and subscribers would be tightly coupled + /// despite the use of loosely coupled messaging middleware. + /// + /// + /// true if to the assembly qualified name; otherwise, false. + /// + public bool UseAssemblyQualifiedName + { + get { + return useAssemblyQualifiedName; + } + set { + useAssemblyQualifiedName = value; + } + } + + /// + /// Convert from a string to a type. Will look into the IdTypeMapping dictionary first to resolve the + /// type, then check if it is the well known typeId of 'Hashtable' followed by a strategy to resolve a fully qualfied + /// name from a simple type name. This strategy requires that /// /// The type id. - /// + /// The type associated with the string. + /// If the type can not be resolved. public Type ToType(string typeId) { if (idTypeMapping.Contains(typeId)) @@ -118,11 +150,16 @@ namespace Spring.Messaging.Nms.Support.Converter if (typeId.Equals(defaultHashtableTypeId)) { return defaultHashtableClass; + } + if (defaultNamespace != null) + { + string fullyQualifiedTypeName = defaultNamespace + "." + + typeId + ", " + + DefaultAssemblyName; + return TypeResolutionUtils.ResolveType(fullyQualifiedTypeName); } - string fullyQualifiedTypeName = defaultNamespace + "." + - typeId + ", " + - DefaultAssemblyName; - return TypeResolutionUtils.ResolveType(fullyQualifiedTypeName); + + return TypeResolutionUtils.ResolveType(typeId); } } diff --git a/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Support/Converter/XmlMessageConverter.cs b/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Support/Converter/XmlMessageConverter.cs index e1dbd801..a8f75e68 100644 --- a/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Support/Converter/XmlMessageConverter.cs +++ b/src/Spring/Spring.Messaging.Nms/Messaging/Nms/Support/Converter/XmlMessageConverter.cs @@ -18,7 +18,7 @@ namespace Spring.Messaging.Nms.Support.Converter private IMessageConverter defaultMessageConverter = new SimpleMessageConverter(); - private ITypeMapper typeMapper; + private ITypeMapper typeMapper = new TypeMapper(); /// diff --git a/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Support/Converter/TypeMapperTests.cs b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Support/Converter/TypeMapperTests.cs new file mode 100644 index 00000000..15d308d2 --- /dev/null +++ b/test/Spring/Spring.Messaging.Nms.Tests/Messaging/Nms/Support/Converter/TypeMapperTests.cs @@ -0,0 +1,105 @@ +#region License + +/* + * Copyright © 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + +using System; +using System.Collections; +using NUnit.Framework; +using Spring.Objects; + +namespace Spring.Messaging.Nms.Support.Converter +{ + /// + /// Test the TypeMapper + /// + /// Mark Pollack + [TestFixture] + public class TypeMapperTests + { + private TypeMapper tm; + + [SetUp] + public void SetUp() + { + tm = new TypeMapper(); + } + + [Test] + [ExpectedException(typeof(ArgumentException))] + public void ConfigurationNamespaceTests() + { + tm.DefaultAssemblyName = "Spring.Objects"; + tm.AfterPropertiesSet(); + } + + [Test] + public void FromTypeTestsForDictionary() + { + Assert.AreEqual("Hashtable", tm.FromType(typeof(Hashtable))); + } + + [Test] + public void ToTypeForDictionary() + { + Assert.AreEqual(typeof (Hashtable), tm.ToType("Hashtable")); + } + + [Test] + public void FromTypeForNonRegisteredType() + { + Assert.AreEqual("TestObject", tm.FromType(typeof(TestObject))); + } + + [Test] + public void ToTypeForNonRegisteredTypeSettingDefaults() + { + tm.DefaultNamespace = "Spring.Objects"; + tm.DefaultAssemblyName = "Spring.Core.Tests"; + Type resolvedTyped = tm.ToType("TestObject"); + Assert.AreEqual(typeof(TestObject), resolvedTyped); + } + + [Test] + [ExpectedException(typeof(TypeLoadException))] + public void ToTypeForUnresolvableType() + { + tm.ToType("TestObject"); + } + + [Test] + public void MarhsalUsingAssemblyQualifiedName() + { + tm.UseAssemblyQualifiedName = true; + string typeAsString = tm.FromType(typeof (TestObject)); + Type resolvedTyped = tm.ToType(typeAsString); + Assert.AreEqual(typeof(TestObject), resolvedTyped); + } + + [Test] + public void UsingTypeMappings() + { + tm.IdTypeMapping.Add("1", typeof (TestObject)); + tm.AfterPropertiesSet(); + string typeAsString = tm.FromType(typeof (TestObject)); + Assert.AreEqual("1", typeAsString); + Type resolvedType = tm.ToType("1"); + Assert.AreEqual(typeof(TestObject), resolvedType); + } + } +} \ No newline at end of file diff --git a/test/Spring/Spring.Messaging.Nms.Tests/Spring.Messaging.Nms.Tests.2008.csproj b/test/Spring/Spring.Messaging.Nms.Tests/Spring.Messaging.Nms.Tests.2008.csproj index fd64add6..aabc194f 100644 --- a/test/Spring/Spring.Messaging.Nms.Tests/Spring.Messaging.Nms.Tests.2008.csproj +++ b/test/Spring/Spring.Messaging.Nms.Tests/Spring.Messaging.Nms.Tests.2008.csproj @@ -2,7 +2,7 @@ Debug AnyCPU - 9.0.30729 + 9.0.21022 2.0 {FA7A6931-7DBE-4A32-A312-51FAD2E80332} Library @@ -110,6 +110,7 @@ + diff --git a/test/Spring/Spring.Web.Tests/Spring.Web.Tests.2008.csproj b/test/Spring/Spring.Web.Tests/Spring.Web.Tests.2008.csproj index d5434841..d013000a 100644 --- a/test/Spring/Spring.Web.Tests/Spring.Web.Tests.2008.csproj +++ b/test/Spring/Spring.Web.Tests/Spring.Web.Tests.2008.csproj @@ -1,7 +1,7 @@  Local - 9.0.30729 + 9.0.21022 2.0 {C67E47AA-1ACD-41B4-A465-4D336A2319CA} Debug @@ -166,7 +166,9 @@ - + + Designer +