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
|
|
|
21
|
|
using Seasar.Framework.Aop;
|
22
|
|
using Seasar.Framework.Aop.Impl;
|
23
|
|
using Seasar.Framework.Aop.Interceptors;
|
24
|
|
using Seasar.Framework.Aop.Proxy;
|
25
|
|
|
26
|
|
using NUnit.Framework;
|
27
|
|
|
28
|
|
|
29
|
|
namespace Seasar.Tests.Framework.Aop.Interceptors
|
30
|
|
{
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
[TestFixture]
|
35
|
|
public class MockInterceptorTest
|
36
|
|
{
|
37
|
|
private const string MSG = "hello";
|
38
|
|
private const string ECHO = "echo";
|
39
|
|
private MockInterceptor target = null;
|
40
|
|
|
41
|
4
|
public static object CreateProxy(IMethodInterceptor interceptor,Type proxyType)
|
42
|
|
{
|
43
|
4
|
IAspect aspect = new AspectImpl(interceptor, null);
|
44
|
4
|
return new AopProxy(proxyType, new IAspect[] { aspect }).GetTransparentProxy();
|
45
|
|
}
|
46
|
|
|
47
|
4
|
[SetUp]
|
48
|
|
public void SetUp()
|
49
|
|
{
|
50
|
4
|
target = new MockInterceptor(MSG);
|
51
|
|
}
|
52
|
|
|
53
|
1
|
[Test]
|
54
|
|
public void TestInvoke()
|
55
|
|
{
|
56
|
1
|
Hello hello = CreateProxy(target, typeof(Hello)) as Hello;
|
57
|
1
|
Assert.AreEqual(MSG, hello.Greeting());
|
58
|
|
}
|
59
|
|
|
60
|
1
|
[Test]
|
61
|
|
public void TestInvoke2()
|
62
|
|
{
|
63
|
1
|
target.SetReturnValue("Greeting", MSG);
|
64
|
1
|
target.SetReturnValue("Echo", ECHO);
|
65
|
1
|
SayHello hello = CreateProxy(target, typeof(SayHello)) as SayHello;
|
66
|
|
|
67
|
1
|
string message = "hoge";
|
68
|
|
|
69
|
1
|
Assert.AreEqual(ECHO, hello.Echo(message));
|
70
|
1
|
Assert.IsTrue(target.IsInvoked("Echo"));
|
71
|
1
|
Assert.IsFalse(target.IsInvoked("Greeting"));
|
72
|
1
|
Assert.AreEqual(message, target.GetArgs("Echo")[0]);
|
73
|
|
}
|
74
|
|
|
75
|
1
|
[Test]
|
76
|
|
public void TestInvoke3()
|
77
|
|
{
|
78
|
1
|
target.SetReturnValue("Greeting", MSG);
|
79
|
1
|
target.SetReturnValue("Echo", ECHO);
|
80
|
1
|
SayHello hello = CreateProxy(target, typeof(SayHello)) as SayHello;
|
81
|
|
|
82
|
1
|
string message = "hoge";
|
83
|
1
|
Assert.AreEqual(MSG, hello.Greeting());
|
84
|
1
|
Assert.AreEqual(ECHO, hello.Echo(message));
|
85
|
1
|
Assert.IsTrue(target.IsInvoked("Greeting"));
|
86
|
1
|
Assert.IsTrue(target.IsInvoked("Echo"));
|
87
|
|
}
|
88
|
|
|
89
|
1
|
[Test]
|
90
|
|
public void TestInvokeThrowException()
|
91
|
|
{
|
92
|
1
|
Exception ex = new ApplicationException();
|
93
|
1
|
target.SetThrowable("Greeting", ex);
|
94
|
1
|
Hello hello = CreateProxy(target, typeof(Hello)) as Hello;
|
95
|
1
|
try
|
96
|
|
{
|
97
|
1
|
hello.Greeting();
|
98
|
0
|
Assert.Fail();
|
99
|
|
} catch(ApplicationException exception)
|
100
|
|
{
|
101
|
1
|
Assert.AreEqual(ex, exception);
|
102
|
|
}
|
103
|
|
}
|
104
|
|
}
|
105
|
|
|
106
|
|
public interface Hello
|
107
|
|
{
|
108
|
|
string Greeting();
|
109
|
|
}
|
110
|
|
|
111
|
|
public interface SayHello : Hello
|
112
|
|
{
|
113
|
|
string Echo(string msg);
|
114
|
|
}
|
115
|
|
}
|
116
|
|
|