Home | History | Annotate | Download | only in invocation
      1 /*
      2  * Copyright (c) 2017 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 package org.mockito.internal.invocation;
      6 
      7 import org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter;
      8 import org.mockito.invocation.InvocationFactory;
      9 import org.mockito.invocation.InvocationOnMock;
     10 
     11 import java.io.Serializable;
     12 import java.util.concurrent.Callable;
     13 
     14 /**
     15  * Interface that wraps a 'real' method of the mock object.
     16  * Needed for test spies or {@link InvocationOnMock#callRealMethod()}.
     17  */
     18 public interface RealMethod extends Serializable {
     19 
     20     enum IsIllegal implements RealMethod {
     21 
     22         INSTANCE;
     23 
     24         @Override
     25         public boolean isInvokable() {
     26             return false;
     27         }
     28 
     29         @Override
     30         public Object invoke() {
     31             throw new IllegalStateException();
     32         }
     33     }
     34 
     35     class FromCallable extends FromBehavior implements RealMethod {
     36         public FromCallable(final Callable<?> callable) {
     37             super(new InvocationFactory.RealMethodBehavior() {
     38                 @Override
     39                 public Object call() throws Throwable {
     40                     return callable.call();
     41                 }
     42             });
     43         }
     44     }
     45 
     46     class FromBehavior implements RealMethod {
     47 
     48         private final InvocationFactory.RealMethodBehavior<?> behavior;
     49 
     50         FromBehavior(InvocationFactory.RealMethodBehavior<?> behavior) {
     51             this.behavior = behavior;
     52         }
     53 
     54         @Override
     55         public boolean isInvokable() {
     56             return true;
     57         }
     58 
     59         @Override
     60         public Object invoke() throws Throwable {
     61             try {
     62                 return behavior.call();
     63             } catch (Throwable t) {
     64                 new ConditionalStackTraceFilter().filter(t);
     65                 throw t;
     66             }
     67         }
     68     }
     69 
     70     boolean isInvokable();
     71 
     72     Object invoke() throws Throwable;
     73 }
     74