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  final class FailureState {
11      @NonNull
12      private final Thread testThread;
13      @Nullable
14      private Error errorThrownInAnotherThread;
15  
16      /**
17       * Holds an error associated to an ExpectedInvocation that is to be reported to the user.
18       * <p>
19       * This field is also set if and when an unexpected invocation is detected, so that any future invocations in this
20       * same phase execution can rethrow the original error instead of throwing a new one, which would hide the original.
21       * Such a situation can happen when test code or the code under test contains a "catch" or "finally" block where a
22       * mock invocation is made after a previous such invocation in the "try" block already failed.
23       */
24      @Nullable
25      private Error errorThrown;
26  
27      FailureState() {
28          testThread = Thread.currentThread();
29      }
30  
31      @Nullable
32      Error getErrorThrownInAnotherThreadIfAny() {
33          return errorThrownInAnotherThread;
34      }
35  
36      @Nullable
37      Error getErrorThrown() {
38          return errorThrown;
39      }
40  
41      void setErrorThrown(@Nullable Error error) {
42          errorThrown = error;
43      }
44  
45      void clearErrorThrown() {
46          errorThrown = null;
47      }
48  
49      void reportErrorThrownIfAny() {
50          if (errorThrown != null) {
51              if (testThread == Thread.currentThread()) {
52                  throw errorThrown;
53              }
54  
55              errorThrownInAnotherThread = errorThrown;
56          }
57      }
58  }