From 0ee59eaddf8551e225a45a10709590bf4c2557c1 Mon Sep 17 00:00:00 2001 From: bbaia Date: Fri, 1 Aug 2008 22:27:48 +0000 Subject: [PATCH] Use 'invocation.Method' as variable for CacheResultAdvice Key expression resolution [SPRNET-959]. --- .../Aspects/Cache/BaseCacheAdvice.cs | 8 ++-- .../Cache/CacheAspectIntegrationTests.cs | 42 +++++++++++++++++-- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/Spring/Spring.Aop/Aspects/Cache/BaseCacheAdvice.cs b/src/Spring/Spring.Aop/Aspects/Cache/BaseCacheAdvice.cs index 0d2c84ce..e671a406 100644 --- a/src/Spring/Spring.Aop/Aspects/Cache/BaseCacheAdvice.cs +++ b/src/Spring/Spring.Aop/Aspects/Cache/BaseCacheAdvice.cs @@ -78,10 +78,12 @@ namespace Spring.Aspects.Cache /// A dictionary containing all method arguments, keyed by method name. /// protected static IDictionary PrepareVariables(MethodInfo method, object[] arguments) - { - ParameterInfo[] parameters = method.GetParameters(); + { + IDictionary vars = new Hashtable(); + + vars[method.Name] = method; - IDictionary vars = new Hashtable(); + ParameterInfo[] parameters = method.GetParameters(); for (int i = 0; i < parameters.Length; i++) { ParameterInfo p = parameters[i]; diff --git a/test/Spring/Spring.Aop.Tests/Aspects/Cache/CacheAspectIntegrationTests.cs b/test/Spring/Spring.Aop.Tests/Aspects/Cache/CacheAspectIntegrationTests.cs index 2d4dea81..4d88cebc 100644 --- a/test/Spring/Spring.Aop.Tests/Aspects/Cache/CacheAspectIntegrationTests.cs +++ b/test/Spring/Spring.Aop.Tests/Aspects/Cache/CacheAspectIntegrationTests.cs @@ -42,15 +42,11 @@ namespace Spring.Aspects.Cache { private IApplicationContext context; private CacheAspect cacheAspect; - private ICache cache; [SetUp] public void SetUp() { - cache = new NonExpiringCache(); - context = new XmlApplicationContext(); - ((IConfigurableApplicationContext) context).ObjectFactory.RegisterSingleton("inventors", cache); cacheAspect = new CacheAspect(); cacheAspect.ApplicationContext = context; @@ -59,6 +55,9 @@ namespace Spring.Aspects.Cache [Test] public void TestCaching() { + ICache cache = new NonExpiringCache(); + ((IConfigurableApplicationContext)context).ObjectFactory.RegisterSingleton("inventors", cache); + ProxyFactory pf = new ProxyFactory(new InventorStore()); pf.AddAdvisors(cacheAspect); @@ -82,6 +81,27 @@ namespace Spring.Aspects.Cache store.DeleteAll(); Assert.AreEqual(0, cache.Count); } + + [Test(Description = "http://jira.springframework.org/browse/SPRNET-959")] + public void UseMethodInfoForKeyGeneration() + { + ICache cache = new NonExpiringCache(); + ((IConfigurableApplicationContext)context).ObjectFactory.RegisterSingleton("defaultCache", cache); + + ProxyFactory pf = new ProxyFactory(new GenericDao()); + pf.AddAdvisors(cacheAspect); + + IGenericDao dao = (IGenericDao)pf.GetProxy(); + + Assert.AreEqual(0, cache.Count); + + dao.Load(1); + Assert.AreEqual(1, cache.Count); + + // actually, it should be null, because default(string) = null + // but it returns the NullValue marker created by the CacheResultAttribute + Assert.IsNotNull(cache.Get("String_1")); + } } #region Inner Class : CacheParameterTarget @@ -135,5 +155,19 @@ namespace Spring.Aspects.Cache } } + public interface IGenericDao + { + T Load(IdT id); + } + + public sealed class GenericDao : IGenericDao + { + [CacheResult("defaultCache", "#Load.ReturnType.Name + '_' + #id")] + public T Load(IdT id) + { + return default(T); + } + } + #endregion } \ No newline at end of file