1
2
3
4
5 package mockit.internal.expectations;
6
7 import static mockit.internal.expectations.state.ExecutingTest.isInstanceMethodWithStandardBehavior;
8
9 import edu.umd.cs.findbugs.annotations.NonNull;
10 import edu.umd.cs.findbugs.annotations.Nullable;
11
12 import java.util.Map;
13
14 import mockit.internal.expectations.invocation.ExpectedInvocation;
15
16 public final class RecordPhase extends TestOnlyPhase {
17 RecordPhase(@NonNull PhasedExecutionState executionState) {
18 super(executionState);
19 }
20
21 void addResult(@Nullable Object result) {
22 if (currentExpectation != null) {
23 currentExpectation.addResult(result);
24 }
25 }
26
27 public void addSequenceOfReturnValues(@NonNull Object[] values) {
28 if (currentExpectation != null) {
29 currentExpectation.addSequenceOfReturnValues(values);
30 }
31 }
32
33 @Nullable
34 @Override
35 Object handleInvocation(@Nullable Object mock, int mockAccess, @NonNull String mockClassDesc,
36 @NonNull String mockNameAndDesc, @Nullable String genericSignature, boolean withRealImpl,
37 @NonNull Object[] args) {
38 mock = configureMatchingOnMockInstanceIfSpecified(mock);
39
40 ExpectedInvocation invocation = new ExpectedInvocation(mock, mockAccess, mockClassDesc, mockNameAndDesc,
41 matchInstance, genericSignature, args);
42
43 boolean nonStrictInvocation = false;
44
45 if (!matchInstance && invocation.isConstructor()) {
46 Map<Object, Object> replacementMap = getReplacementMap();
47 replacementMap.put(mock, mock);
48 } else {
49 nonStrictInvocation = isInstanceMethodWithStandardBehavior(mock, mockNameAndDesc);
50 }
51
52 currentExpectation = new Expectation(this, invocation, nonStrictInvocation);
53
54 if (argMatchers != null) {
55 invocation.arguments.setMatchers(argMatchers);
56 argMatchers = null;
57 }
58
59 executionState.addExpectation(currentExpectation);
60
61 return invocation.getDefaultValueForReturnType();
62 }
63
64 @Nullable
65 private Object configureMatchingOnMockInstanceIfSpecified(@Nullable Object mock) {
66 matchInstance = false;
67
68 if (mock == null) {
69 return null;
70 }
71
72 Map<Object, Object> replacementMap = getReplacementMap();
73 Object replacementInstance = replacementMap.get(mock);
74 matchInstance = mock == replacementInstance || isEnumElement(mock);
75 return mock;
76 }
77
78 @Override
79 void handleInvocationCountConstraint(int minInvocations, int maxInvocations) {
80 if (currentExpectation != null) {
81 currentExpectation.constraints.setLimits(minInvocations, maxInvocations);
82 }
83 }
84 }