| |||||||||||||||||
Source File | Conditionals | Statements | Methods | TOTAL | |||||||||||||
Seasar.Framework.Aop\Interceptors\MockInterceptor.cs | 83.3% | 88.2% | 88.9% | 87.5% |
|
1 | #region Copyright | |
2 | /* | |
3 | * Copyright 2005 the Seasar Foundation and the Others. | |
4 | * | |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
14 | * either express or implied. See the License for the specific language | |
15 | * governing permissions and limitations under the License. | |
16 | */ | |
17 | #endregion | |
18 | ||
19 | using System; | |
20 | using System.Collections; | |
21 | ||
22 | namespace Seasar.Framework.Aop.Interceptors | |
23 | { | |
24 | /// <summary> | |
25 | /// Interceptの対象となるメソッドとインスタンスをMockにします | |
26 | /// </summary> | |
27 | public class MockInterceptor : AbstractInterceptor | |
28 | { | |
29 | ||
30 | /// <summary> | |
31 | /// Mockの戻り値 | |
32 | /// </summary> | |
33 | private Hashtable returnValues_ = new Hashtable(); | |
34 | ||
35 | /// <summary> | |
36 | /// Mockの例外 | |
37 | /// </summary> | |
38 | private Hashtable exceptions_ = new Hashtable(); | |
39 | ||
40 | /// <summary> | |
41 | /// メソッドが呼び出し済みかどうか示すフラグ | |
42 | /// </summary> | |
43 | private Hashtable invokedMethods_ = new Hashtable(); | |
44 | ||
45 | /// <summary> | |
46 | /// メソッドを呼び出したときの引数 | |
47 | /// </summary> | |
48 | private Hashtable invokedMethodArgs_ = new Hashtable(); | |
49 | ||
50 | /// <summary> | |
51 | /// コンストラクタ | |
52 | /// </summary> | |
53 | 2 | public MockInterceptor() |
54 | { | |
55 | } | |
56 | ||
57 | #region IMethodInterceptor クラス | |
58 | ||
59 | /// <summary> | |
60 | /// メソッドがInterceptされる場合、このメソッドが呼び出されます | |
61 | /// </summary> | |
62 | /// <param name="invocation">IMethodInvocation</param> | |
63 | /// <returns>Interceptされるメソッドの戻り値</returns> | |
64 | 9 | public override object Invoke(IMethodInvocation invocation) |
65 | { | |
66 | 9 | string methodName = invocation.Method.Name; |
67 | 9 | invokedMethods_[methodName] = true; |
68 | 9 | invokedMethodArgs_[methodName] = invocation.Arguments; |
69 | ||
70 | 9 | if (exceptions_.ContainsKey(methodName)) |
71 | { | |
72 | 1 | throw exceptions_[methodName] as Exception; |
73 | } | |
74 | 8 | else if (exceptions_.ContainsKey("")) |
75 | { | |
76 | 0 | throw exceptions_[""] as Exception; |
77 | } | |
78 | 8 | else if (returnValues_.ContainsKey(methodName)) |
79 | { | |
80 | 3 | return returnValues_[methodName]; |
81 | } | |
82 | else | |
83 | { | |
84 | 5 | return returnValues_[""]; |
85 | } | |
86 | } | |
87 | ||
88 | #endregion | |
89 | ||
90 | /// <summary> | |
91 | /// コンストラクタ | |
92 | /// </summary> | |
93 | /// <param name="value">Mockの戻り値</param> | |
94 | 4 | public MockInterceptor(object value) |
95 | { | |
96 | 4 | SetReturnValue(value); |
97 | } | |
98 | ||
99 | /// <summary> | |
100 | /// 戻り値の設定 | |
101 | /// </summary> | |
102 | /// <remarks> | |
103 | /// Mockのすべてのメソッドの戻り値を設定します。 | |
104 | /// </remarks> | |
105 | /// <param name="returnValue">戻り値</param> | |
106 | 4 | public void SetReturnValue(object returnValue) { |
107 | 4 | SetReturnValue("", returnValue); |
108 | } | |
109 | ||
110 | /// <summary> | |
111 | /// 戻り値の設定 | |
112 | /// </summary> | |
113 | /// <remarks> | |
114 | /// Mockの指定された名前のメソッドの戻り値を設定します。 | |
115 | /// </remarks> | |
116 | /// <param name="methodName">戻り値を設定するメソッド名</param> | |
117 | /// <param name="returnValue">戻り値</param> | |
118 | 8 | public void SetReturnValue(String methodName, object returnValue) |
119 | { | |
120 | 8 | returnValues_[methodName] = returnValue; |
121 | } | |
122 | ||
123 | /// <summary> | |
124 | /// 例外の設定 | |
125 | /// </summary> | |
126 | /// <remarks> | |
127 | /// Mockのすべてのメソッドに例外を設定します。 | |
128 | /// </remarks> | |
129 | /// <param name="exception">例外</param> | |
130 | 0 | public void SetThrowable(Exception exception) |
131 | { | |
132 | SetThrowable("", exception); | |
133 | } | |
134 | ||
135 | /// <summary> | |
136 | /// 例外の設定 | |
137 | /// </summary> | |
138 | /// <remarks> | |
139 | /// Mockのすべてのメソッドに例外を設定します。 | |
140 | /// </remarks> | |
141 | /// <param name="methodName">例外を設定するメソッド名</param> | |
142 | /// <param name="exception">例外</param> | |
143 | 1 | public void SetThrowable(String methodName, Exception exception) |
144 | { | |
145 | 1 | exceptions_[methodName] = exception; |
146 | } | |
147 | ||
148 | /// <summary> | |
149 | /// Mockのメソッドが既に呼び出されているか判定します | |
150 | /// </summary> | |
151 | /// <param name="methodName">呼び出されているかどうか判定するメソッド命</param> | |
152 | /// <returns>Mockのメソッドが既に呼び出されているか</returns> | |
153 | 4 | public bool IsInvoked(String methodName) { |
154 | 4 | return invokedMethods_.ContainsKey(methodName); |
155 | } | |
156 | ||
157 | /// <summary> | |
158 | /// Mockのメソッドが呼ばれたときの引数を取得します | |
159 | /// </summary> | |
160 | /// <param name="methodName">引数を取得するメソッド名</param> | |
161 | /// <returns>引数のリスト</returns> | |
162 | 1 | public object[] GetArgs(String methodName) { |
163 | 1 | return (object[]) invokedMethodArgs_[methodName]; |
164 | } | |
165 | ||
166 | } | |
167 | } | |
168 |
|