SPRNET-1226: Provider more meaningful error message if cache attribute does not have key specified

This commit is contained in:
lahma
2009-08-16 14:56:42 +00:00
parent 0e1facc834
commit 9fe54d243c
2 changed files with 34 additions and 3 deletions

View File

@@ -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)

View File

@@ -82,6 +82,24 @@ namespace Spring.Aspects.Cache
Assert.AreEqual(0, cache.Count);
}
/// <summary>
/// http://jira.springframework.org/browse/SPRNET-1226
/// </summary>
[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)
{