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