|
1 |
| package com.agical.rmock.core.expectation; |
|
2 |
| |
|
3 |
| import java.io.IOException; |
|
4 |
| import java.io.StringWriter; |
|
5 |
| |
|
6 |
| import com.agical.rmock.core.Assert; |
|
7 |
| import com.agical.rmock.core.InvocationHandler; |
|
8 |
| import com.agical.rmock.core.MethodHandle; |
|
9 |
| import com.agical.rmock.core.ProxyFactory; |
|
10 |
| import com.agical.rmock.core.Section; |
|
11 |
| import com.agical.rmock.core.SectionProvider; |
|
12 |
| import com.agical.rmock.core.Verifiable; |
|
13 |
| import com.agical.rmock.core.describe.ExpressionDescriber; |
|
14 |
| import com.agical.rmock.core.describe.impl.ExpressionDescriberImpl; |
|
15 |
| import com.agical.rmock.core.exception.IllegalStateTransitionException; |
|
16 |
| import com.agical.rmock.core.exception.RMockAssertionFailedException; |
|
17 |
| import com.agical.rmock.core.exception.UnsatisfiedExpectationsException; |
|
18 |
| import com.agical.rmock.core.exception.manager.ExceptionVerifier; |
|
19 |
| import com.agical.rmock.core.expectation.modification.LastExpectationProvider; |
|
20 |
| import com.agical.rmock.core.expectation.modification.ModifiableExpectation; |
|
21 |
| import com.agical.rmock.core.match.Expression; |
|
22 |
| import com.agical.rmock.core.match.reference.ReferenceFactory; |
|
23 |
| import com.agical.rmock.core.strategy.StrategyRunner; |
|
24 |
| import com.agical.rmock.core.strategy.TestStep; |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| public class Engine implements InvocationHandler, |
|
33 |
| Verifiable, |
|
34 |
| ExpectationsState, |
|
35 |
| Assert, |
|
36 |
| LastExpectationProvider, |
|
37 |
| StrategyRunner { |
|
38 |
| private InvocationRecorder recordState; |
|
39 |
| private InvocationHandler state; |
|
40 |
| private ExpectationListener expectationListener; |
|
41 |
| private ProxyFactory proxyFactory; |
|
42 |
| private ExceptionVerifier exceptionVerifier; |
|
43 |
| private SectionProvider sectionProvider; |
|
44 |
| |
|
45 |
| |
|
46 |
1693
| public void setSectionProvider(SectionProvider sectionProvider) {
|
|
47 |
1693
| this.sectionProvider = sectionProvider;
|
|
48 |
1693
| this.recordState.setSectionProvider(sectionProvider);
|
|
49 |
| } |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
581
| public Engine() {
|
|
55 |
581
| super();
|
|
56 |
581
| recordState = new InvocationRecorder();
|
|
57 |
581
| state = recordState;
|
|
58 |
| } |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
1674
| public Object invocation(String id, Class objectType, String method, Object[] arguments, MethodHandle methodHandle) throws Throwable {
|
|
65 |
1674
| return state.invocation(id, objectType, method, arguments, methodHandle);
|
|
66 |
| } |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
549
| public void beginVerify() {
|
|
72 |
549
| if( isInVerifyState() ) {
|
|
73 |
1
| throw new IllegalStateTransitionException( "You're not allowed to change to verification mode when already in it." );
|
|
74 |
| } |
|
75 |
| |
|
76 |
548
| Section rootSection = sectionProvider.getRootSection();
|
|
77 |
548
| if( expectationListener != null ) {
|
|
78 |
0
| expectationListener.expectationsRecorded( rootSection );
|
|
79 |
| } |
|
80 |
548
| state = new InvocationVerifier(rootSection);
|
|
81 |
548
| ((InvocationVerifier)state).setExceptionVerifier( exceptionVerifier );
|
|
82 |
| } |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
1078
| public void endVerify() {
|
|
89 |
1078
| Section rootSection = sectionProvider.getRootSection();
|
|
90 |
1078
| if( !rootSection.isSatisfied() ) {
|
|
91 |
1
| throw new UnsatisfiedExpectationsException(rootSection, isInSetupState());
|
|
92 |
| } |
|
93 |
| } |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
508
| public ModifiableExpectation getLastExpectation() {
|
|
99 |
508
| ModifiableExpectation modifiableExpectation = (ModifiableExpectation) recordState.getLast();
|
|
100 |
508
| return modifiableExpectation;
|
|
101 |
| } |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
1649
| public void setProxyFactory(ProxyFactory proxyFactory) {
|
|
107 |
1649
| this.proxyFactory = proxyFactory;
|
|
108 |
1649
| recordState.setProxyFactory(proxyFactory);
|
|
109 |
| } |
|
110 |
| |
|
111 |
6
| public boolean isInSetupState() {
|
|
112 |
6
| return state instanceof InvocationRecorder;
|
|
113 |
| } |
|
114 |
| |
|
115 |
4478
| public boolean isInVerifyState() {
|
|
116 |
4478
| return !(state instanceof InvocationRecorder);
|
|
117 |
| } |
|
118 |
| |
|
119 |
557
| public void assertThat(Object actual, Expression expression) {
|
|
120 |
557
| ReferenceFactory referenceFactory = new ReferenceFactory();
|
|
121 |
557
| if(!expression.passes(actual)) {
|
|
122 |
2
| StringWriter sw = new StringWriter();
|
|
123 |
2
| ExpressionDescriber expressionDescriber = new ExpressionDescriberImpl(sw);
|
|
124 |
2
| try {
|
|
125 |
2
| referenceFactory.create(actual).describeWith(expressionDescriber);
|
|
126 |
2
| sw.write("\ndoes not pass the expression:\n<");
|
|
127 |
2
| expression.describeWith(expressionDescriber);
|
|
128 |
2
| sw.write(">");
|
|
129 |
2
| throw new RMockAssertionFailedException(sectionProvider.getRootSection(), this.isInSetupState(), sw.toString());
|
|
130 |
| } catch (IOException e) { |
|
131 |
0
| throw new RMockAssertionFailedException(sectionProvider.getRootSection(), this.isInSetupState(), "failed to describe constraint: " + e.getMessage());
|
|
132 |
| } |
|
133 |
| } |
|
134 |
| |
|
135 |
| } |
|
136 |
| |
|
137 |
0
| public void setExpectationListener(ExpectationListener expectationListener) {
|
|
138 |
0
| this.expectationListener = expectationListener;
|
|
139 |
| } |
|
140 |
| |
|
141 |
558
| public void runStrategy(TestStep step) throws Throwable {
|
|
142 |
558
| step.run();
|
|
143 |
| } |
|
144 |
| |
|
145 |
1656
| public void setExceptionVerifier(ExceptionVerifier exceptionVerifier) {
|
|
146 |
1656
| this.exceptionVerifier = exceptionVerifier;
|
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
| } |