SPRNET-1126 - XmlMessageConverter/TypeMapper usage should be able to embed fully qualified type name to enable type sharing between producer and subscriber

This commit is contained in:
markpollack
2009-07-22 06:51:33 +00:00
parent 807f100104
commit b3e76a021d
5 changed files with 156 additions and 11 deletions

View File

@@ -40,6 +40,7 @@ namespace Spring.Messaging.Nms.Support.Converter
private string defaultHashtableTypeId = "Hashtable";
private Type defaultHashtableClass = typeof(Hashtable);
private bool useAssemblyQualifiedName;
/// <summary>
/// Initializes a new instance of the <see cref="TypeMapper"/> [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;
}
}
}
/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <value>
/// <c>true</c> if to the assembly qualified name; otherwise, <c>false</c>.
/// </value>
public bool UseAssemblyQualifiedName
{
get {
return useAssemblyQualifiedName;
}
set {
useAssemblyQualifiedName = value;
}
}
/// <summary>
/// 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
/// </summary>
/// <param name="typeId">The type id.</param>
/// <returns></returns>
/// <returns>The type associated with the string.</returns>
/// <exception cref="TypeLoadException">If the type can not be resolved.</exception>
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);
}
}

View File

@@ -18,7 +18,7 @@ namespace Spring.Messaging.Nms.Support.Converter
private IMessageConverter defaultMessageConverter = new SimpleMessageConverter();
private ITypeMapper typeMapper;
private ITypeMapper typeMapper = new TypeMapper();
/// <summary>

View File

@@ -0,0 +1,105 @@
#region License
/*
* Copyright <20> 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
{
/// <summary>
/// Test the TypeMapper
/// </summary>
/// <author>Mark Pollack</author>
[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);
}
}
}

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{FA7A6931-7DBE-4A32-A312-51FAD2E80332}</ProjectGuid>
<OutputType>Library</OutputType>
@@ -110,6 +110,7 @@
<Compile Include="Messaging\Nms\Listener\Adapter\MessageContentsHandler.cs" />
<Compile Include="Messaging\Nms\Listener\Adapter\MessageListenerAdapterTests.cs" />
<Compile Include="Messaging\Nms\StubQueue.cs" />
<Compile Include="Messaging\Nms\Support\Converter\TypeMapperTests.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Messaging\Nms\Integration\SimpleMessageListenerContainerTests.xml" />

View File

@@ -1,7 +1,7 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<ProjectType>Local</ProjectType>
<ProductVersion>9.0.30729</ProductVersion>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{C67E47AA-1ACD-41B4-A465-4D336A2319CA}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -166,7 +166,9 @@
</Content>
<Content Include="Data\Spring\Web\Support\PageHandlerFactoryTests\DisablesSession.aspx" />
<Content Include="Data\Spring\Web\Support\PageHandlerFactoryTests\TransferAfterSetResultSave.aspx" />
<EmbeddedResource Include="Data\Spring\Web\Support\LocalResourceManagerTests\App_LocalResources\WithResources.aspx.resx" />
<EmbeddedResource Include="Data\Spring\Web\Support\LocalResourceManagerTests\App_LocalResources\WithResources.aspx.resx">
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Web\Support\ControlInterceptionTests.objects.xml" />
</ItemGroup>
<ItemGroup>