From f9ec39437002f64211c13c4268205a8d19b51eab Mon Sep 17 00:00:00 2001 From: sbohlen Date: Mon, 23 Aug 2010 17:15:43 +0000 Subject: [PATCH] SPRNET-1360 Improve exception messages in the case where the DynamicProxyManager attempts to register a Proxy using an already-registered name but pointing to a DIFFERENT type (this is usually a configuration error associated with attempting to export two or more services from two or more config sources that accidentally share a single common name) --- .../Spring.Core/Proxy/DynamicProxyManager.cs | 34 ++++++++++++++----- .../Proxy/DynamicProxyManagerTests.cs | 13 +++++-- .../ServicedComponentExporterTests.cs | 12 +++++-- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/Spring/Spring.Core/Proxy/DynamicProxyManager.cs b/src/Spring/Spring.Core/Proxy/DynamicProxyManager.cs index 478f78b2..ee092327 100644 --- a/src/Spring/Spring.Core/Proxy/DynamicProxyManager.cs +++ b/src/Spring/Spring.Core/Proxy/DynamicProxyManager.cs @@ -30,9 +30,9 @@ using Spring.Util; namespace Spring.Proxy { - /// + /// /// Allows easy access to existing and creation of new dynamic proxies. - /// + /// /// Aleksandar Seovic /// Bruno Baia public sealed class DynamicProxyManager @@ -43,7 +43,7 @@ namespace Spring.Proxy /// The name of the assembly that defines proxy types created. /// public const string ASSEMBLY_NAME = "Spring.Proxy"; - + /// /// The attributes of the proxy type to generate. /// @@ -62,14 +62,30 @@ namespace Spring.Proxy public static TypeBuilder CreateTypeBuilder(string typeName, Type baseType) { ModuleBuilder module = DynamicCodeManager.GetModuleBuilder(ASSEMBLY_NAME); - - if (baseType == null) + + try { - return module.DefineType(typeName, TYPE_ATTRIBUTES); + if (baseType == null) + { + return module.DefineType(typeName, TYPE_ATTRIBUTES); + } + else + { + return module.DefineType(typeName, TYPE_ATTRIBUTES, baseType); + } } - else + catch (ArgumentException ex) { - return module.DefineType(typeName, TYPE_ATTRIBUTES, baseType); + Type alreadyRegisteredType = module.GetType(typeName, true); + + string msg; + + if (alreadyRegisteredType != null) + msg = "Proxy already registered for \"{0}\" as Type \"{1}\"."; + else + msg = "Proxy already registered for \"{0}\"."; + + throw new ArgumentException(string.Format(msg, typeName, alreadyRegisteredType.FullName), ex); } } @@ -80,7 +96,7 @@ namespace Spring.Proxy [Conditional("DEBUG_DYNAMIC")] public static void SaveAssembly() { - DynamicCodeManager.SaveAssembly( ASSEMBLY_NAME ); + DynamicCodeManager.SaveAssembly(ASSEMBLY_NAME); } #endregion diff --git a/test/Spring/Spring.Core.Tests/Proxy/DynamicProxyManagerTests.cs b/test/Spring/Spring.Core.Tests/Proxy/DynamicProxyManagerTests.cs index f8b1cea7..4bf85141 100644 --- a/test/Spring/Spring.Core.Tests/Proxy/DynamicProxyManagerTests.cs +++ b/test/Spring/Spring.Core.Tests/Proxy/DynamicProxyManagerTests.cs @@ -24,6 +24,7 @@ using System; using System.Reflection.Emit; using System.Threading; using NUnit.Framework; +using System.Reflection; #endregion @@ -82,11 +83,19 @@ namespace Spring.Proxy #endregion WorkerThread Class [Test] - [ExpectedException(typeof(ArgumentException))] public void CreateTypeBuilderMustNotBeCalledTwiceWithSameArguments() { TypeBuilder tb1 = DynamicProxyManager.CreateTypeBuilder("testtypename", null); - TypeBuilder tb2 = DynamicProxyManager.CreateTypeBuilder("testtypename", typeof(AbstractProxyTypeBuilder) ); + + try + { + TypeBuilder tb2 = DynamicProxyManager.CreateTypeBuilder("testtypename", typeof(AbstractProxyTypeBuilder)); + Assert.Fail("Did not throw expected ArgumentException."); + } + catch (ArgumentException) + { + } + } [Test] diff --git a/test/Spring/Spring.Services.Tests/EnterpriseServices/ServicedComponentExporterTests.cs b/test/Spring/Spring.Services.Tests/EnterpriseServices/ServicedComponentExporterTests.cs index ff8173ef..8326fb4f 100644 --- a/test/Spring/Spring.Services.Tests/EnterpriseServices/ServicedComponentExporterTests.cs +++ b/test/Spring/Spring.Services.Tests/EnterpriseServices/ServicedComponentExporterTests.cs @@ -54,11 +54,19 @@ namespace Spring.EnterpriseServices } [Test] - [ExpectedException(typeof(ArgumentException))] public void BailsWhenNotConfigured() { ServicedComponentExporter exp = new ServicedComponentExporter(); - exp.AfterPropertiesSet(); + try + { + exp.AfterPropertiesSet(); + Assert.Fail("Did not throw expected ArgumentException!"); + } + catch (ArgumentException) + { + + } + } [Test]