Use 'invocation.Method' as variable for cache attributes key expression resolution [SPRNET-959]
This commit is contained in:
@@ -72,6 +72,23 @@ namespace Spring.Aspects.Cache
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestSimpleWithMethodInfoParameterCaching()
|
||||
{
|
||||
MethodInfo method = typeof(SimpleWithMethodInfoCacheParameterTarget).GetMethod("Save");
|
||||
object[] args = new object[] { new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian") };
|
||||
|
||||
ExpectCacheInstanceRetrieval("cache", cache);
|
||||
mocks.ReplayAll();
|
||||
|
||||
// parameter value should be added to cache
|
||||
advice.AfterReturning(null, method, args, null);
|
||||
Assert.AreEqual(1, cache.Count);
|
||||
Assert.AreEqual(args[0], cache.Get("Save-Nikola Tesla"));
|
||||
|
||||
mocks.VerifyAll();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMultipleParameterCaching()
|
||||
{
|
||||
@@ -126,6 +143,13 @@ namespace Spring.Aspects.Cache
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SimpleWithMethodInfoCacheParameterTarget : ICacheParameterTarget
|
||||
{
|
||||
public void Save([CacheParameter("cache", "#Save.Name + '-' + Name")] Inventor inventor)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class MultipleCacheParameterTarget : ICacheParameterTarget
|
||||
{
|
||||
public void Save([CacheParameter("cache", "Name")][CacheParameter("cache", "Nationality")] Inventor inventor)
|
||||
|
||||
@@ -195,6 +195,7 @@ namespace Spring.Aspects.Cache
|
||||
object expectedReturnValue = new object[] { "one", "two", "three" };
|
||||
|
||||
ExpectAttributeRetrieval(method);
|
||||
ExpectCacheKeyGeneration(method, 5, expectedReturnValue);
|
||||
ExpectCallToProceed(new object[] { "one", "two", "three" });
|
||||
ExpectCacheInstanceRetrieval("items", itemCache);
|
||||
|
||||
@@ -228,6 +229,7 @@ namespace Spring.Aspects.Cache
|
||||
object expectedReturnValue = new object[] { "one", "two", "three" };
|
||||
|
||||
ExpectAttributeRetrieval(method);
|
||||
ExpectCacheKeyGeneration(method, 5, expectedReturnValue);
|
||||
ExpectCallToProceed(new object[] { "one", "two", "three" });
|
||||
ExpectCacheInstanceRetrieval( "items", itemCache );
|
||||
ExpectCacheInstanceRetrieval( "items", itemCache );
|
||||
@@ -264,6 +266,7 @@ namespace Spring.Aspects.Cache
|
||||
object expectedReturnValue = new object[] { "one", "two", "three" };
|
||||
|
||||
ExpectAttributeRetrieval(method);
|
||||
ExpectCacheKeyGeneration(method, 5, expectedReturnValue);
|
||||
ExpectCallToProceed(new object[] { "one", "two", "three" });
|
||||
ExpectCacheInstanceRetrieval( "items", itemCache );
|
||||
|
||||
@@ -370,6 +373,49 @@ namespace Spring.Aspects.Cache
|
||||
mockContext.Verify();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CacheResultWithMethodInfo()
|
||||
{
|
||||
MethodInfo method = new EnumerableResultMethod(cacheResultTarget.CacheResultWithMethodInfo).Method;
|
||||
object expectedReturnValue = new object[] { "one", "two", "three" };
|
||||
|
||||
ExpectAttributeRetrieval(method);
|
||||
ExpectCacheKeyGeneration(method, 5, expectedReturnValue);
|
||||
ExpectCacheInstanceRetrieval("results", resultCache);
|
||||
ExpectCallToProceed(new object[] { "one", "two", "three" });
|
||||
|
||||
// return value should be added to cache
|
||||
object returnValue = advice.Invoke((IMethodInvocation)mockInvocation.Object);
|
||||
Assert.AreEqual(expectedReturnValue, returnValue);
|
||||
Assert.AreEqual(1, resultCache.Count);
|
||||
Assert.AreEqual(returnValue, resultCache.Get("CacheResultWithMethodInfo-5"));
|
||||
|
||||
mockInvocation.Verify();
|
||||
mockContext.Verify();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CacheResultItemsWithMethodInfo()
|
||||
{
|
||||
MethodInfo method = new EnumerableResultMethod(cacheResultTarget.CacheResultItemsWithMethodInfo).Method;
|
||||
object expectedReturnValue = new object[] { "one", "two", "three" };
|
||||
|
||||
ExpectAttributeRetrieval(method);
|
||||
ExpectCacheKeyGeneration(method, 5, expectedReturnValue);
|
||||
ExpectCallToProceed(new object[] { "one", "two", "three" });
|
||||
ExpectCacheInstanceRetrieval("items", itemCache);
|
||||
|
||||
// return value should be added to result cache and each item to item cache
|
||||
object returnValue = advice.Invoke((IMethodInvocation)mockInvocation.Object);
|
||||
Assert.AreEqual(expectedReturnValue, returnValue);
|
||||
Assert.AreEqual(0, resultCache.Count);
|
||||
Assert.AreEqual(3, itemCache.Count);
|
||||
Assert.AreEqual("two", itemCache.Get("CacheResultItemsWithMethodInfo-two"));
|
||||
|
||||
mockInvocation.Verify();
|
||||
mockContext.Verify();
|
||||
}
|
||||
|
||||
|
||||
#region Helper methods
|
||||
|
||||
@@ -499,6 +545,18 @@ namespace Spring.Aspects.Cache
|
||||
return elements;
|
||||
}
|
||||
|
||||
[CacheResult("results", "#CacheResultWithMethodInfo.Name + '-' + #key")]
|
||||
public IEnumerable CacheResultWithMethodInfo(int key, params object[] elements)
|
||||
{
|
||||
return elements;
|
||||
}
|
||||
|
||||
[CacheResultItems("items", "#CacheResultItemsWithMethodInfo.Name + '-' + #this")]
|
||||
public IEnumerable CacheResultItemsWithMethodInfo(int key, params object[] elements)
|
||||
{
|
||||
return elements;
|
||||
}
|
||||
|
||||
[CacheResultItems( "items", "#this", Condition = "#this.StartsWith('t')" )]
|
||||
public IEnumerable CacheResultItemsWithCondition( int key, params object[] elements )
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user