1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
#endregion
|
18
|
|
|
19
|
|
using System;
|
20
|
|
using Seasar.Framework.Aop;
|
21
|
|
using Seasar.Framework.Aop.Impl;
|
22
|
|
using Seasar.Framework.Aop.Proxy;
|
23
|
|
using NUnit.Framework;
|
24
|
|
|
25
|
|
namespace Seasar.Tests.Framework.Aop.Impl
|
26
|
|
{
|
27
|
|
|
28
|
|
|
29
|
|
|
30
|
|
[TestFixture]
|
31
|
|
public class MethodInvocationImplTest
|
32
|
|
{
|
33
|
1
|
public MethodInvocationImplTest()
|
34
|
|
{
|
35
|
|
}
|
36
|
|
|
37
|
1
|
[Test]
|
38
|
|
public void TestProceed()
|
39
|
|
{
|
40
|
1
|
TestInterceptor interceptor = new TestInterceptor();
|
41
|
1
|
TestInterceptor interceptor2 = new TestInterceptor();
|
42
|
1
|
IPointcut pointcut = new PointcutImpl(new string[] { "Foo" });
|
43
|
1
|
IAspect aspect = new AspectImpl(interceptor,pointcut);
|
44
|
1
|
IAspect aspect2 = new AspectImpl(interceptor2,pointcut);
|
45
|
|
|
46
|
1
|
Hoge proxy = new HogeImpl();
|
47
|
1
|
AopProxy aopProxy = new AopProxy(typeof(Hoge),new IAspect[] {aspect,aspect2},null,proxy);
|
48
|
1
|
proxy = (Hoge) aopProxy.GetTransparentProxy();
|
49
|
1
|
Console.WriteLine(proxy.Foo());
|
50
|
1
|
Assert.AreEqual(true,interceptor.invoked_);
|
51
|
1
|
Assert.AreEqual(true,interceptor2.invoked_);
|
52
|
|
}
|
53
|
|
|
54
|
1
|
[Test]
|
55
|
|
public void TestProceedForAbstractMethod()
|
56
|
|
{
|
57
|
1
|
HogeInterceptor interceptor = new HogeInterceptor();
|
58
|
1
|
IAspect aspect = new AspectImpl(interceptor);
|
59
|
1
|
Hoge proxy = new HogeImpl();
|
60
|
1
|
AopProxy aopProxy = new AopProxy(typeof(Hoge),new IAspect[] { aspect },null,proxy);
|
61
|
1
|
proxy = (Hoge) aopProxy.GetTransparentProxy();
|
62
|
1
|
Assert.AreEqual("Hello",proxy.Foo());
|
63
|
|
}
|
64
|
|
|
65
|
|
public class TestInterceptor : IMethodInterceptor
|
66
|
|
{
|
67
|
|
internal bool invoked_ = false;
|
68
|
|
|
69
|
2
|
public object Invoke(Seasar.Framework.Aop.IMethodInvocation invocation)
|
70
|
|
{
|
71
|
2
|
invoked_ = true;
|
72
|
2
|
Console.WriteLine("before");
|
73
|
2
|
object ret = invocation.Proceed();
|
74
|
2
|
Console.WriteLine("after");
|
75
|
2
|
return ret;
|
76
|
|
}
|
77
|
|
}
|
78
|
|
|
79
|
|
public interface Hoge
|
80
|
|
{
|
81
|
|
string Foo();
|
82
|
|
}
|
83
|
|
|
84
|
|
public class HogeImpl : Hoge
|
85
|
|
{
|
86
|
|
#region Hoge メンバ
|
87
|
|
|
88
|
1
|
public string Foo()
|
89
|
|
{
|
90
|
|
|
91
|
1
|
Console.WriteLine("Foo");
|
92
|
1
|
return "hogehoge";
|
93
|
|
}
|
94
|
|
|
95
|
|
#endregion
|
96
|
|
}
|
97
|
|
|
98
|
|
public class HogeInterceptor : IMethodInterceptor
|
99
|
|
{
|
100
|
1
|
public object Invoke(Seasar.Framework.Aop.IMethodInvocation invocation)
|
101
|
|
{
|
102
|
1
|
return "Hello";
|
103
|
|
}
|
104
|
|
}
|
105
|
|
}
|
106
|
|
}
|
107
|
|
|