diff --git a/src/Spring/Spring.Aop/Aspects/Cache/CacheResultAdvice.cs b/src/Spring/Spring.Aop/Aspects/Cache/CacheResultAdvice.cs index 70347da8..ca5f087b 100644 --- a/src/Spring/Spring.Aop/Aspects/Cache/CacheResultAdvice.cs +++ b/src/Spring/Spring.Aop/Aspects/Cache/CacheResultAdvice.cs @@ -126,8 +126,10 @@ namespace Spring.Aspects.Cache object returnValue = null; bool isLogDebugEnabled = logger.IsDebugEnabled; - IDictionary vars = PrepareVariables(invocation.Method, invocation.Arguments); - + IDictionary vars = PrepareVariables(invocation.Method, invocation.Arguments); + + AssertUtils.ArgumentNotNull(resultInfo.KeyExpression, "KeyExpression", + "The cache attribute is missing the key definition."); object resultKey = resultInfo.KeyExpression.GetValue(null, vars); ICache cache = GetCache(resultInfo.CacheName); AssertUtils.ArgumentNotNull(cache, "CacheName", @@ -196,7 +198,10 @@ namespace Spring.Aspects.Cache ICache cache = GetCache(itemInfo.CacheName); AssertUtils.ArgumentNotNull(cache, "CacheName", "Result item cache with the specified name [" + itemInfo.CacheName + - "] does not exist."); + "] does not exist."); + + AssertUtils.ArgumentNotNull(itemInfo.KeyExpression, "KeyExpression", + "The cache attribute is missing the key definition."); bool isDebugEnabled = logger.IsDebugEnabled; foreach (object item in items) diff --git a/test/Spring/Spring.Aop.Tests/Aspects/Cache/CacheAspectIntegrationTests.cs b/test/Spring/Spring.Aop.Tests/Aspects/Cache/CacheAspectIntegrationTests.cs index 77add864..54785125 100644 --- a/test/Spring/Spring.Aop.Tests/Aspects/Cache/CacheAspectIntegrationTests.cs +++ b/test/Spring/Spring.Aop.Tests/Aspects/Cache/CacheAspectIntegrationTests.cs @@ -82,6 +82,24 @@ namespace Spring.Aspects.Cache Assert.AreEqual(0, cache.Count); } + /// + /// http://jira.springframework.org/browse/SPRNET-1226 + /// + [Test] + [ExpectedException(ExpectedException = typeof(ArgumentNullException))] + public void NoCacheKeySpecified() + { + ICache cache = new NonExpiringCache(); + context.ObjectFactory.RegisterSingleton("inventors", cache); + + ProxyFactory pf = new ProxyFactory(new InventorStore()); + pf.AddAdvisors(cacheAspect); + + IInventorStore store = (IInventorStore)pf.GetProxy(); + IList items = store.GetAllNoCacheKey(); + Assert.IsNotNull(items); + } + #if NET_2_0 [Test(Description = "http://jira.springframework.org/browse/SPRNET-959")] public void UseMethodInfoForKeyGeneration() @@ -111,6 +129,7 @@ namespace Spring.Aspects.Cache public interface IInventorStore { IList GetAll(); + IList GetAllNoCacheKey(); Inventor Load(string name); void Save(Inventor inventor); void Delete(Inventor inventor); @@ -135,6 +154,13 @@ namespace Spring.Aspects.Cache return new ArrayList(inventors.Values); } + + [CacheResult(CacheName = "inventors")] + public IList GetAllNoCacheKey() + { + return new ArrayList(inventors.Values); + } + [CacheResult("inventors", "#name")] public Inventor Load(string name) {