1
2
3
4
5 package mockit.internal.expectations;
6
7 import static mockit.internal.expectations.argumentMatching.AlwaysTrueMatcher.ANY_BOOLEAN;
8 import static mockit.internal.expectations.argumentMatching.AlwaysTrueMatcher.ANY_BYTE;
9 import static mockit.internal.expectations.argumentMatching.AlwaysTrueMatcher.ANY_CHAR;
10 import static mockit.internal.expectations.argumentMatching.AlwaysTrueMatcher.ANY_DOUBLE;
11 import static mockit.internal.expectations.argumentMatching.AlwaysTrueMatcher.ANY_FLOAT;
12 import static mockit.internal.expectations.argumentMatching.AlwaysTrueMatcher.ANY_INT;
13 import static mockit.internal.expectations.argumentMatching.AlwaysTrueMatcher.ANY_LONG;
14 import static mockit.internal.expectations.argumentMatching.AlwaysTrueMatcher.ANY_SHORT;
15 import static mockit.internal.expectations.argumentMatching.AlwaysTrueMatcher.ANY_STRING;
16 import static mockit.internal.expectations.argumentMatching.AlwaysTrueMatcher.ANY_VALUE;
17
18 import edu.umd.cs.findbugs.annotations.NonNull;
19 import edu.umd.cs.findbugs.annotations.Nullable;
20
21 import mockit.internal.expectations.argumentMatching.ArgumentMatcher;
22 import mockit.internal.expectations.transformation.ArgumentCapturing;
23 import mockit.internal.state.TestRun;
24 import mockit.internal.util.ClassLoad;
25
26 import org.checkerframework.checker.index.qual.NonNegative;
27
28 @SuppressWarnings("unused")
29 public final class ActiveInvocations {
30 private ActiveInvocations() {
31 }
32
33 public static void anyString() {
34 addArgMatcher(ANY_STRING);
35 }
36
37 public static void anyBoolean() {
38 addArgMatcher(ANY_BOOLEAN);
39 }
40
41 public static void anyChar() {
42 addArgMatcher(ANY_CHAR);
43 }
44
45 public static void anyByte() {
46 addArgMatcher(ANY_BYTE);
47 }
48
49 public static void anyShort() {
50 addArgMatcher(ANY_SHORT);
51 }
52
53 public static void anyInt() {
54 addArgMatcher(ANY_INT);
55 }
56
57 public static void anyFloat() {
58 addArgMatcher(ANY_FLOAT);
59 }
60
61 public static void anyLong() {
62 addArgMatcher(ANY_LONG);
63 }
64
65 public static void anyDouble() {
66 addArgMatcher(ANY_DOUBLE);
67 }
68
69 public static void any() {
70 addArgMatcher(ANY_VALUE);
71 }
72
73 private static void addArgMatcher(@NonNull ArgumentMatcher<?> argumentMatcher) {
74 RecordAndReplayExecution instance = TestRun.getRecordAndReplayForRunningTest();
75
76 if (instance != null) {
77 TestOnlyPhase currentPhase = instance.getCurrentTestOnlyPhase();
78
79 if (currentPhase != null) {
80 currentPhase.addArgMatcher(argumentMatcher);
81 }
82 }
83 }
84
85 public static void moveArgMatcher(@NonNegative int originalMatcherIndex, @NonNegative int toIndex) {
86 RecordAndReplayExecution instance = TestRun.getRecordAndReplayForRunningTest();
87
88 if (instance != null) {
89 TestOnlyPhase currentPhase = instance.getCurrentTestOnlyPhase();
90
91 if (currentPhase != null) {
92 currentPhase.moveArgMatcher(originalMatcherIndex, toIndex);
93 }
94 }
95 }
96
97 public static void setExpectedArgumentType(@NonNegative int parameterIndex, @NonNull String typeDesc) {
98 RecordAndReplayExecution instance = TestRun.getRecordAndReplayForRunningTest();
99
100 if (instance != null) {
101 TestOnlyPhase currentPhase = instance.getCurrentTestOnlyPhase();
102
103 if (currentPhase != null) {
104 Class<?> argumentType = ClassLoad.loadByInternalName(typeDesc);
105 currentPhase.setExpectedSingleArgumentType(parameterIndex, argumentType);
106 }
107 }
108 }
109
110 public static void setExpectedArgumentType(@NonNegative int parameterIndex, int varIndex) {
111 RecordAndReplayExecution instance = TestRun.getRecordAndReplayForRunningTest();
112
113 if (instance != null) {
114 String typeDesc = ArgumentCapturing.extractArgumentType(varIndex);
115
116 if (typeDesc != null) {
117 TestOnlyPhase currentPhase = instance.getCurrentTestOnlyPhase();
118
119 if (currentPhase != null) {
120 Class<?> argumentType = ClassLoad.loadByInternalName(typeDesc);
121 currentPhase.setExpectedMultiArgumentType(parameterIndex, argumentType);
122 }
123 }
124 }
125 }
126
127 @Nullable
128 public static Object matchedArgument(@NonNegative int parameterIndex, @Nullable String argTypeDesc) {
129 RecordAndReplayExecution instance = TestRun.getRecordAndReplayForRunningTest();
130
131 if (instance != null) {
132 BaseVerificationPhase verificationPhase = (BaseVerificationPhase) instance.getCurrentTestOnlyPhase();
133
134 if (verificationPhase != null) {
135 return verificationPhase.getArgumentValueForCurrentVerification(parameterIndex);
136 }
137 }
138
139 return null;
140 }
141
142 public static void addResult(@Nullable Object result) {
143 RecordAndReplayExecution instance = TestRun.getRecordAndReplayForRunningTest();
144
145 if (instance != null) {
146 RecordPhase recordPhase = instance.getRecordPhase();
147
148 if (recordPhase != null) {
149 recordPhase.addResult(result);
150 }
151 }
152 }
153
154 public static void times(int n) {
155 RecordAndReplayExecution instance = TestRun.getRecordAndReplayForRunningTest();
156
157 if (instance != null) {
158 TestOnlyPhase currentPhase = instance.getCurrentTestOnlyPhase();
159
160 if (currentPhase != null) {
161 currentPhase.handleInvocationCountConstraint(n, n);
162 }
163 }
164 }
165
166 public static void minTimes(int n) {
167 RecordAndReplayExecution instance = TestRun.getRecordAndReplayForRunningTest();
168
169 if (instance != null) {
170 TestOnlyPhase currentPhase = instance.getCurrentTestOnlyPhase();
171
172 if (currentPhase != null) {
173 currentPhase.handleInvocationCountConstraint(n, -1);
174 }
175 }
176 }
177
178 public static void maxTimes(int n) {
179 RecordAndReplayExecution instance = TestRun.getRecordAndReplayForRunningTest();
180
181 if (instance != null) {
182 TestOnlyPhase currentPhase = instance.getCurrentTestOnlyPhase();
183
184 if (currentPhase != null) {
185 currentPhase.setMaxInvocationCount(n);
186 }
187 }
188 }
189
190 public static void endInvocations() {
191 TestRun.enterNoMockingZone();
192
193 try {
194 RecordAndReplayExecution instance = TestRun.getRecordAndReplayForRunningTest();
195 assert instance != null;
196 instance.endInvocations();
197 } finally {
198 TestRun.exitNoMockingZone();
199 }
200 }
201 }