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 System.Collections;
|
21
|
|
using System.Reflection;
|
22
|
|
using MbUnit.Core.Invokers;
|
23
|
|
using MbUnit.Framework;
|
24
|
|
using Seasar.Extension.Unit;
|
25
|
|
using Seasar.Framework.Container;
|
26
|
|
using Seasar.Framework.Container.Factory;
|
27
|
|
using Seasar.Framework.Log;
|
28
|
|
using Seasar.Framework.Message;
|
29
|
|
using Seasar.Framework.Util;
|
30
|
|
|
31
|
|
namespace Seasar.Framework.Unit
|
32
|
|
{
|
33
|
|
public class S2FrameworkTestCaseRunner
|
34
|
|
{
|
35
|
|
private static Logger logger = Logger.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
|
36
|
|
private S2FrameworkTestCaseBase fixture;
|
37
|
|
private MethodInfo method;
|
38
|
|
private IS2Container container;
|
39
|
|
private IList bindedFields;
|
40
|
|
private Hashtable errors;
|
41
|
|
|
42
|
|
protected IS2Container Container
|
43
|
|
{
|
44
|
114
|
get { return container; }
|
45
|
|
}
|
46
|
|
|
47
|
42
|
public virtual object Run(IRunInvoker invoker, object o, IList args)
|
48
|
|
{
|
49
|
42
|
fixture = o as S2FrameworkTestCaseBase;
|
50
|
42
|
method = fixture.GetType().GetMethod(invoker.Name);
|
51
|
42
|
SetUpContainer();
|
52
|
42
|
fixture.Container = container;
|
53
|
42
|
try
|
54
|
|
{
|
55
|
42
|
try
|
56
|
|
{
|
57
|
42
|
SetUpForEachTestMethod();
|
58
|
42
|
container.Init();
|
59
|
42
|
try
|
60
|
|
{
|
61
|
42
|
SetUpAfterContainerInit();
|
62
|
42
|
try
|
63
|
|
{
|
64
|
42
|
BindFields();
|
65
|
42
|
SetUpAfterBindFields();
|
66
|
42
|
try
|
67
|
|
{
|
68
|
42
|
BeginTransactionContext();
|
69
|
42
|
return invoker.Execute(o, args);
|
70
|
|
}
|
71
|
|
catch(Exception e)
|
72
|
|
{
|
73
|
0
|
ExceptionHandler(e);
|
74
|
0
|
throw e;
|
75
|
|
}
|
76
|
|
finally
|
77
|
|
{
|
78
|
42
|
EndTransactionContext();
|
79
|
42
|
TearDownBeforeUnbindFields();
|
80
|
42
|
UnbindFields();
|
81
|
|
}
|
82
|
|
}
|
83
|
|
catch(Exception e)
|
84
|
|
{
|
85
|
0
|
ExceptionHandler(e);
|
86
|
0
|
throw e;
|
87
|
|
}
|
88
|
|
finally
|
89
|
|
{
|
90
|
42
|
TearDownBeforeContainerDestroy();
|
91
|
|
}
|
92
|
|
}
|
93
|
|
catch(Exception e)
|
94
|
|
{
|
95
|
0
|
ExceptionHandler(e);
|
96
|
0
|
throw e;
|
97
|
|
}
|
98
|
|
finally
|
99
|
|
{
|
100
|
42
|
container.Destroy();
|
101
|
|
}
|
102
|
|
}
|
103
|
|
catch(Exception e)
|
104
|
|
{
|
105
|
0
|
ExceptionHandler(e);
|
106
|
0
|
throw e;
|
107
|
|
}
|
108
|
|
finally
|
109
|
|
{
|
110
|
42
|
TearDownForEachTestMethod();
|
111
|
|
}
|
112
|
|
}
|
113
|
|
catch(Exception e)
|
114
|
|
{
|
115
|
0
|
ExceptionHandler(e);
|
116
|
0
|
throw e;
|
117
|
|
}
|
118
|
|
finally
|
119
|
|
{
|
120
|
168
|
for (int i = 0; i < 3; ++i)
|
121
|
|
{
|
122
|
126
|
GC.WaitForPendingFinalizers();
|
123
|
126
|
GC.Collect();
|
124
|
|
}
|
125
|
42
|
TearDownContainer();
|
126
|
|
}
|
127
|
|
}
|
128
|
|
|
129
|
42
|
protected virtual void SetUpContainer()
|
130
|
|
{
|
131
|
42
|
SingletonS2ContainerFactory.Init();
|
132
|
42
|
container = SingletonS2ContainerFactory.Container;
|
133
|
|
}
|
134
|
|
|
135
|
42
|
protected virtual void TearDownContainer()
|
136
|
|
{
|
137
|
42
|
SingletonS2ContainerFactory.Destroy();
|
138
|
42
|
container = null;
|
139
|
|
}
|
140
|
|
|
141
|
42
|
protected virtual void SetUpForEachTestMethod()
|
142
|
|
{
|
143
|
42
|
string targetName = GetTargetName();
|
144
|
42
|
if (targetName.Length > 0)
|
145
|
|
{
|
146
|
42
|
MethodInfo setupMethod = fixture.GetType().GetMethod("SetUp" + targetName);
|
147
|
42
|
if (setupMethod != null)
|
148
|
39
|
MethodUtil.Invoke(setupMethod, fixture, null);
|
149
|
|
}
|
150
|
|
}
|
151
|
|
|
152
|
42
|
protected virtual void TearDownForEachTestMethod()
|
153
|
|
{
|
154
|
42
|
String targetName = GetTargetName();
|
155
|
42
|
if (targetName.Length > 0)
|
156
|
|
{
|
157
|
42
|
MethodInfo tearDownMethod = fixture.GetType().GetMethod("TearDown" + targetName);
|
158
|
42
|
if (tearDownMethod != null)
|
159
|
1
|
MethodUtil.Invoke(tearDownMethod, fixture, null);
|
160
|
|
}
|
161
|
|
}
|
162
|
|
|
163
|
0
|
protected virtual void BeginTransactionContext()
|
164
|
|
{
|
165
|
|
|
166
|
|
}
|
167
|
|
|
168
|
0
|
protected virtual void EndTransactionContext()
|
169
|
|
{
|
170
|
|
|
171
|
|
}
|
172
|
|
|
173
|
42
|
protected virtual void SetUpAfterContainerInit()
|
174
|
|
{
|
175
|
|
}
|
176
|
|
|
177
|
42
|
protected virtual void SetUpAfterBindFields()
|
178
|
|
{
|
179
|
|
}
|
180
|
|
|
181
|
42
|
protected virtual void TearDownBeforeUnbindFields()
|
182
|
|
{
|
183
|
|
}
|
184
|
|
|
185
|
42
|
protected virtual void TearDownBeforeContainerDestroy()
|
186
|
|
{
|
187
|
|
}
|
188
|
|
|
189
|
84
|
protected string GetTargetName()
|
190
|
|
{
|
191
|
84
|
string name = method.Name;
|
192
|
|
|
193
|
84
|
if (name.ToLower().StartsWith("test"))
|
194
|
24
|
name = name.Substring(4);
|
195
|
|
|
196
|
84
|
if (name.ToLower().EndsWith("test"))
|
197
|
0
|
name = name.Substring(0, name.Length - 4);
|
198
|
|
|
199
|
84
|
return name;
|
200
|
|
}
|
201
|
|
|
202
|
42
|
protected void BindFields()
|
203
|
|
{
|
204
|
42
|
bindedFields = new ArrayList();
|
205
|
42
|
for (Type type = fixture.GetType();
|
206
|
84
|
(type != typeof(S2FrameworkTestCaseBase) && type != typeof(S2TestCase) && type != null);
|
207
|
|
type = type.BaseType) {
|
208
|
|
|
209
|
42
|
FieldInfo[] fields = type.GetFields(
|
210
|
|
BindingFlags.DeclaredOnly |
|
211
|
|
BindingFlags.Public|
|
212
|
|
BindingFlags.NonPublic |
|
213
|
|
BindingFlags.Instance |
|
214
|
|
BindingFlags.Static);
|
215
|
|
|
216
|
203
|
for (int i = 0; i < fields.Length; ++i) {
|
217
|
161
|
BindField(fields[i]);
|
218
|
|
}
|
219
|
|
}
|
220
|
|
}
|
221
|
|
|
222
|
161
|
protected void BindField(FieldInfo fieldInfo)
|
223
|
|
{
|
224
|
161
|
if (IsAutoBindable(fieldInfo))
|
225
|
|
{
|
226
|
60
|
if (fieldInfo.FieldType.ToString() == "System.DateTime")
|
227
|
|
{
|
228
|
10
|
DateTime dateValue = (DateTime) fieldInfo.GetValue(fixture);
|
229
|
10
|
if (DateTime.MinValue != dateValue)
|
230
|
5
|
return;
|
231
|
|
}
|
232
|
50
|
else if (fieldInfo.GetValue(fixture) != null)
|
233
|
|
{
|
234
|
10
|
return;
|
235
|
|
}
|
236
|
45
|
string name = NormalizeName(fieldInfo.Name);
|
237
|
45
|
object component = null;
|
238
|
45
|
if (this.container.HasComponentDef(name))
|
239
|
|
{
|
240
|
6
|
Type componentType = this.container.GetComponentDef(name).ComponentType;
|
241
|
6
|
if (componentType == null)
|
242
|
|
{
|
243
|
2
|
component = this.container.GetComponent(name);
|
244
|
2
|
if (component != null)
|
245
|
|
{
|
246
|
2
|
componentType = component.GetType();
|
247
|
|
}
|
248
|
|
}
|
249
|
|
|
250
|
6
|
if (componentType != null
|
251
|
|
&& fieldInfo.FieldType.IsAssignableFrom(componentType))
|
252
|
|
{
|
253
|
4
|
if (component == null)
|
254
|
|
{
|
255
|
2
|
component = this.container.GetComponent(name);
|
256
|
|
}
|
257
|
|
}
|
258
|
|
else
|
259
|
|
{
|
260
|
2
|
component = null;
|
261
|
|
}
|
262
|
|
}
|
263
|
45
|
if (component == null
|
264
|
|
&& this.container.HasComponentDef(fieldInfo.FieldType))
|
265
|
|
{
|
266
|
4
|
component = this.container.GetComponent(fieldInfo.FieldType);
|
267
|
|
}
|
268
|
45
|
if (component != null) {
|
269
|
|
|
270
|
8
|
fieldInfo.SetValue(fixture, component);
|
271
|
8
|
bindedFields.Add(fieldInfo);
|
272
|
|
}
|
273
|
|
}
|
274
|
|
}
|
275
|
|
|
276
|
45
|
protected String NormalizeName(String name)
|
277
|
|
{
|
278
|
45
|
return name.TrimEnd('_').TrimStart('_');
|
279
|
|
}
|
280
|
|
|
281
|
161
|
protected bool IsAutoBindable(FieldInfo fieldInfo)
|
282
|
|
{
|
283
|
161
|
return !fieldInfo.IsStatic && !fieldInfo.IsLiteral
|
284
|
|
&& !fieldInfo.IsInitOnly;
|
285
|
|
}
|
286
|
|
|
287
|
42
|
protected void UnbindFields()
|
288
|
|
{
|
289
|
50
|
for (int i = 0; i < bindedFields.Count; ++i) {
|
290
|
8
|
FieldInfo fieldInfo = (FieldInfo) bindedFields[i];
|
291
|
8
|
try
|
292
|
|
{
|
293
|
8
|
if (!fieldInfo.FieldType.IsValueType)
|
294
|
|
{
|
295
|
7
|
fieldInfo.SetValue(fixture, null);
|
296
|
|
}
|
297
|
|
}
|
298
|
|
catch (ArgumentException e)
|
299
|
|
{
|
300
|
0
|
Console.Error.WriteLine(e);
|
301
|
|
}
|
302
|
|
catch (FieldAccessException e)
|
303
|
|
{
|
304
|
0
|
Console.Error.WriteLine(e);
|
305
|
|
}
|
306
|
|
}
|
307
|
|
}
|
308
|
|
|
309
|
0
|
private void ExceptionHandler(Exception e)
|
310
|
|
{
|
311
|
|
if (errors == null)
|
312
|
|
errors = new Hashtable();
|
313
|
|
|
314
|
|
if (!errors.ContainsKey(e.GetHashCode()))
|
315
|
|
{
|
316
|
|
object[] attrs = method.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false);
|
317
|
|
foreach(ExpectedExceptionAttribute attribute in attrs)
|
318
|
|
{
|
319
|
|
if (IsMatchExpectedException(attribute.ExceptionType, e))
|
320
|
|
return;
|
321
|
|
}
|
322
|
|
|
323
|
|
if (logger.IsDebugEnabled)
|
324
|
|
logger.Debug(MessageFormatter.GetSimpleMessage("ESSR0017", new object[] { e }), e);
|
325
|
|
else
|
326
|
|
Console.Error.WriteLine(e);
|
327
|
|
|
328
|
|
errors.Add(e.GetHashCode(), e);
|
329
|
|
}
|
330
|
|
}
|
331
|
|
|
332
|
0
|
private bool IsMatchExpectedException(Type ExpectedExceptionType, Exception e)
|
333
|
|
{
|
334
|
|
if (ExpectedExceptionType == e.GetType())
|
335
|
|
return true;
|
336
|
|
else if (e.InnerException != null)
|
337
|
|
return IsMatchExpectedException(ExpectedExceptionType, e.InnerException);
|
338
|
|
else
|
339
|
|
return false;
|
340
|
|
}
|
341
|
|
}
|
342
|
|
}
|