1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 
6 package org.mockito.internal.invocation;
7 
8 import org.mockito.internal.exceptions.VerificationAwareInvocation;
9 import org.mockito.internal.invocation.realmethod.RealMethod;
10 import org.mockito.internal.reporting.PrintSettings;
11 import org.mockito.invocation.*;
12 
13 import static org.mockito.internal.exceptions.Reporter.cannotCallAbstractRealMethod;
14 
15 import java.lang.reflect.Method;
16 import java.util.Arrays;
17 
18 /**
19  * Method call on a mock object.
20  * <p>
21  * Contains sequence number which should be globally unique and is used for
22  * verification in order.
23  * <p>
24  * Contains stack trace of invocation
25  */
26 @SuppressWarnings("unchecked")
27 public class InvocationImpl implements Invocation, VerificationAwareInvocation {
28 
29     private static final long serialVersionUID = 8240069639250980199L;
30     private final int sequenceNumber;
31     private final Object mock;
32     private final MockitoMethod method;
33     private final Object[] arguments;
34     private final Object[] rawArguments;
35 
36     private final Location location;
37     private boolean verified;
38     private boolean isIgnoredForVerification;
39 
40     final RealMethod realMethod;
41     private StubInfo stubInfo;
42 
InvocationImpl(Object mock, MockitoMethod mockitoMethod, Object[] args, int sequenceNumber, RealMethod realMethod, Location location)43     public InvocationImpl(Object mock, MockitoMethod mockitoMethod, Object[] args, int sequenceNumber,
44                           RealMethod realMethod, Location location) {
45         this.method = mockitoMethod;
46         this.mock = mock;
47         this.realMethod = realMethod;
48         this.arguments = ArgumentsProcessor.expandVarArgs(mockitoMethod.isVarArgs(), args);
49         this.rawArguments = args;
50         this.sequenceNumber = sequenceNumber;
51         this.location = location;
52     }
53 
getMock()54     public Object getMock() {
55         return mock;
56     }
57 
getMethod()58     public Method getMethod() {
59         return method.getJavaMethod();
60     }
61 
getArguments()62     public Object[] getArguments() {
63         return arguments;
64     }
65 
getArgument(int index)66     public <T> T getArgument(int index) {
67         return (T)arguments[index];
68     }
69 
isVerified()70     public boolean isVerified() {
71         return verified || isIgnoredForVerification;
72     }
73 
getSequenceNumber()74     public int getSequenceNumber() {
75         return sequenceNumber;
76     }
77 
equals(Object o)78     public boolean equals(Object o) {
79         if (o == null || !o.getClass().equals(this.getClass())) {
80             return false;
81         }
82 
83         InvocationImpl other = (InvocationImpl) o;
84 
85         return this.mock.equals(other.mock) && this.method.equals(other.method) && this.equalArguments(other.arguments);
86     }
87 
equalArguments(Object[] arguments)88     private boolean equalArguments(Object[] arguments) {
89         return Arrays.equals(arguments, this.arguments);
90     }
91 
92     @Override
hashCode()93     public int hashCode() {
94         return 1;
95     }
96 
toString()97     public String toString() {
98         return new PrintSettings().print(ArgumentsProcessor.argumentsToMatchers(getArguments()), this);
99     }
100 
getLocation()101     public Location getLocation() {
102         return location;
103     }
104 
getRawArguments()105     public Object[] getRawArguments() {
106         return this.rawArguments;
107     }
108 
getRawReturnType()109     public Class<?> getRawReturnType() {
110         return method.getReturnType();
111     }
112 
callRealMethod()113     public Object callRealMethod() throws Throwable {
114         if (method.isAbstract()) {
115             throw cannotCallAbstractRealMethod();
116         }
117         return realMethod.invoke(mock, rawArguments);
118     }
119 
markVerified()120     public void markVerified() {
121         this.verified = true;
122     }
123 
stubInfo()124     public StubInfo stubInfo() {
125         return stubInfo;
126     }
127 
markStubbed(StubInfo stubInfo)128     public void markStubbed(StubInfo stubInfo) {
129         this.stubInfo = stubInfo;
130     }
131 
isIgnoredForVerification()132     public boolean isIgnoredForVerification() {
133         return isIgnoredForVerification;
134     }
135 
ignoreForVerification()136     public void ignoreForVerification() {
137         isIgnoredForVerification = true;
138     }
139 }
140