View Javadoc
1   /*
2    * Copyright (c) 2006 JMockit developers
3    * This file is subject to the terms of the MIT license (see LICENSE.txt).
4    */
5   package mockit.internal.expectations;
6   
7   import edu.umd.cs.findbugs.annotations.NonNull;
8   import edu.umd.cs.findbugs.annotations.Nullable;
9   
10  import java.util.ArrayList;
11  import java.util.List;
12  
13  import mockit.internal.expectations.argumentMatching.ArgumentMatcher;
14  import mockit.internal.expectations.argumentMatching.CaptureMatcher;
15  import mockit.internal.expectations.argumentMatching.ClassMatcher;
16  
17  import org.checkerframework.checker.index.qual.NonNegative;
18  
19  public abstract class TestOnlyPhase extends Phase {
20      boolean matchInstance;
21      @Nullable
22      List<ArgumentMatcher<?>> argMatchers;
23      @Nullable
24      Expectation currentExpectation;
25  
26      TestOnlyPhase(@NonNull PhasedExecutionState executionState) {
27          super(executionState);
28      }
29  
30      public final void addArgMatcher(@NonNull ArgumentMatcher<?> matcher) {
31          getArgumentMatchers().add(matcher);
32      }
33  
34      @NonNull
35      private List<ArgumentMatcher<?>> getArgumentMatchers() {
36          if (argMatchers == null) {
37              argMatchers = new ArrayList<>();
38          }
39  
40          return argMatchers;
41      }
42  
43      final void moveArgMatcher(@NonNegative int originalMatcherIndex, @NonNegative int toIndex) {
44          List<ArgumentMatcher<?>> matchers = getArgumentMatchers();
45          int i = getMatcherPositionIgnoringNulls(originalMatcherIndex, matchers);
46  
47          for (i--; i < toIndex; i++) {
48              matchers.add(i, null);
49          }
50      }
51  
52      @NonNegative
53      private static int getMatcherPositionIgnoringNulls(@NonNegative int originalMatcherIndex,
54              @NonNull List<ArgumentMatcher<?>> matchers) {
55          int i = 0;
56  
57          for (int matchersFound = 0; matchersFound <= originalMatcherIndex; i++) {
58              if (matchers.get(i) != null) {
59                  matchersFound++;
60              }
61          }
62  
63          return i;
64      }
65  
66      final void setExpectedSingleArgumentType(@NonNegative int parameterIndex, @NonNull Class<?> argumentType) {
67          ArgumentMatcher<?> newMatcher = ClassMatcher.create(argumentType);
68          getArgumentMatchers().set(parameterIndex, newMatcher);
69      }
70  
71      final void setExpectedMultiArgumentType(@NonNegative int parameterIndex, @NonNull Class<?> argumentType) {
72          CaptureMatcher<?> matcher = (CaptureMatcher<?>) getArgumentMatchers().get(parameterIndex);
73          matcher.setExpectedType(argumentType);
74      }
75  
76      void setMaxInvocationCount(int maxInvocations) {
77          if (currentExpectation != null) {
78              int currentMinimum = currentExpectation.constraints.minInvocations;
79              int minInvocations = maxInvocations < 0 ? currentMinimum : Math.min(currentMinimum, maxInvocations);
80              handleInvocationCountConstraint(minInvocations, maxInvocations);
81          }
82      }
83  
84      abstract void handleInvocationCountConstraint(int minInvocations, int maxInvocations);
85  
86      static boolean isEnumElement(@NonNull Object mock) {
87          Object[] enumElements = mock.getClass().getEnumConstants();
88  
89          if (enumElements != null) {
90              for (Object enumElement : enumElements) {
91                  if (enumElement == mock) {
92                      return true;
93                  }
94              }
95          }
96  
97          return false;
98      }
99  }