-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy pathServiceExecution.cs
More file actions
100 lines (86 loc) · 3.71 KB
/
ServiceExecution.cs
File metadata and controls
100 lines (86 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Linq;
using System.Reflection;
using TestStack.White.ScreenObjects.Configuration;
namespace TestStack.White.ScreenObjects.Services
{
public class ServiceExecution
{
private readonly ExecutionHistory executionHistory;
private readonly IWorkEnvironment workEnvironment;
private readonly ServiceCalls currentServiceCalls = new ServiceCalls();
private ServiceCall currentServiceCall;
private object o;
protected ServiceExecution() {}
public ServiceExecution(ExecutionHistory executionHistory, IWorkEnvironment workEnvironment)
{
this.executionHistory = executionHistory;
this.workEnvironment = workEnvironment;
}
public virtual LastServiceCallStatus Invoking(Service service, MethodInfo methodInfo)
{
currentServiceCall = new ServiceCall(service, methodInfo);
ServiceCalls matchingHistoryCalls = executionHistory.FindCalls(currentServiceCall);
ServiceCalls matchingCurrentServiceCalls = currentServiceCalls.Matching(currentServiceCall);
if (matchingHistoryCalls.Count > matchingCurrentServiceCalls.Count)
{
matchingCurrentServiceCalls.Sort((x, y) => x.CallNumber.CompareTo(y.CallNumber));
int currentCallNumberForService = matchingHistoryCalls.Last().CallNumber;
ServiceCall matchingCall =
matchingHistoryCalls.Find(obj => obj.CallNumber.Equals(currentCallNumberForService));
currentServiceCalls.Add(matchingCall);
return new LastServiceCallStatus(matchingCall.ReturnValue);
}
currentServiceCall.CallNumber = matchingCurrentServiceCalls.Count;
return new NullLastServiceCallStatus();
}
public virtual void Invoked(object returnValue)
{
currentServiceCall.ReturnValue = returnValue;
executionHistory.Add(currentServiceCall);
currentServiceCalls.Add(currentServiceCall);
executionHistory.Save();
}
public virtual void TakeSnapshot()
{
if (executionHistory.LastSnapshot != null)
{
workEnvironment.DropSnapshot(executionHistory.LastSnapshot);
executionHistory.LastSnapshot = null;
}
object snapshotId = workEnvironment.TakeSnapshot();
executionHistory.LastSnapshot = snapshotId;
executionHistory.Save();
}
public virtual void Error()
{
executionHistory.HasError = true;
}
public virtual void Dispose()
{
if (executionHistory.DropSnapshot())
workEnvironment.DropSnapshot(executionHistory.LastSnapshot);
executionHistory.SaveIfNoError();
}
public virtual T CreateData<T>(params object[] objs)
{
if (executionHistory.Data == null)
{
o = Activator.CreateInstance(typeof(T), objs);
executionHistory.Data = o;
}
return (T) executionHistory.Data;
}
public virtual void RevertToSnapshot()
{
if (executionHistory.HasError) workEnvironment.RevertToSnapshot(executionHistory.LastSnapshot);
}
public static ServiceExecution Create(IWorkEnvironment workEnvironment)
{
if (!RepositoryConfigurationLocator.Get().UseHistory)
return new NullServiceExecution();
ExecutionHistory executionHistory = ExecutionHistory.Create();
return new ServiceExecution(executionHistory, workEnvironment ?? new NullWorkEnvironment());
}
}
}