1 /* 2 * Copyright (c) 2016 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.invocation.mockref.MockReference; 8 import org.mockito.internal.exceptions.VerificationAwareInvocation; 9 import org.mockito.internal.reporting.PrintSettings; 10 import org.mockito.invocation.Invocation; 11 import org.mockito.invocation.Location; 12 import org.mockito.invocation.StubInfo; 13 14 import java.lang.reflect.Method; 15 import java.util.Arrays; 16 17 import static org.mockito.internal.exceptions.Reporter.cannotCallAbstractRealMethod; 18 19 public class InterceptedInvocation implements Invocation, VerificationAwareInvocation { 20 21 private static final long serialVersionUID = 475027563923510472L; 22 23 private final MockReference<Object> mockRef; 24 private final MockitoMethod mockitoMethod; 25 private final Object[] arguments, rawArguments; 26 private final RealMethod realMethod; 27 28 private final int sequenceNumber; 29 30 private final Location location; 31 32 private boolean verified; 33 private boolean isIgnoredForVerification; 34 private StubInfo stubInfo; 35 InterceptedInvocation(MockReference<Object> mockRef, MockitoMethod mockitoMethod, Object[] arguments, RealMethod realMethod, Location location, int sequenceNumber)36 public InterceptedInvocation(MockReference<Object> mockRef, 37 MockitoMethod mockitoMethod, 38 Object[] arguments, 39 RealMethod realMethod, 40 Location location, 41 int sequenceNumber) { 42 this.mockRef = mockRef; 43 this.mockitoMethod = mockitoMethod; 44 this.arguments = ArgumentsProcessor.expandArgs(mockitoMethod, arguments); 45 this.rawArguments = arguments; 46 this.realMethod = realMethod; 47 this.location = location; 48 this.sequenceNumber = sequenceNumber; 49 } 50 51 @Override isVerified()52 public boolean isVerified() { 53 return verified || isIgnoredForVerification; 54 } 55 56 @Override getSequenceNumber()57 public int getSequenceNumber() { 58 return sequenceNumber; 59 } 60 61 @Override getLocation()62 public Location getLocation() { 63 return location; 64 } 65 66 @Override getRawArguments()67 public Object[] getRawArguments() { 68 return rawArguments; 69 } 70 71 @Override getRawReturnType()72 public Class<?> getRawReturnType() { 73 return mockitoMethod.getReturnType(); 74 } 75 76 @Override markVerified()77 public void markVerified() { 78 verified = true; 79 } 80 81 @Override stubInfo()82 public StubInfo stubInfo() { 83 return stubInfo; 84 } 85 86 @Override markStubbed(StubInfo stubInfo)87 public void markStubbed(StubInfo stubInfo) { 88 this.stubInfo = stubInfo; 89 } 90 91 @Override isIgnoredForVerification()92 public boolean isIgnoredForVerification() { 93 return isIgnoredForVerification; 94 } 95 96 @Override ignoreForVerification()97 public void ignoreForVerification() { 98 isIgnoredForVerification = true; 99 } 100 101 @Override getMock()102 public Object getMock() { 103 return mockRef.get(); 104 } 105 106 @Override getMethod()107 public Method getMethod() { 108 return mockitoMethod.getJavaMethod(); 109 } 110 111 @Override getArguments()112 public Object[] getArguments() { 113 return arguments; 114 } 115 116 @Override 117 @SuppressWarnings("unchecked") getArgument(int index)118 public <T> T getArgument(int index) { 119 return (T) arguments[index]; 120 } 121 122 @Override callRealMethod()123 public Object callRealMethod() throws Throwable { 124 if (!realMethod.isInvokable()) { 125 throw cannotCallAbstractRealMethod(); 126 } 127 return realMethod.invoke(); 128 } 129 130 @Override hashCode()131 public int hashCode() { 132 //TODO SF we need to provide hash code implementation so that there are no unexpected, slight perf issues 133 return 1; 134 } 135 136 @Override equals(Object o)137 public boolean equals(Object o) { 138 if (o == null || !o.getClass().equals(this.getClass())) { 139 return false; 140 } 141 InterceptedInvocation other = (InterceptedInvocation) o; 142 return this.mockRef.get().equals(other.mockRef.get()) 143 && this.mockitoMethod.equals(other.mockitoMethod) 144 && this.equalArguments(other.arguments); 145 } 146 equalArguments(Object[] arguments)147 private boolean equalArguments(Object[] arguments) { 148 return Arrays.equals(arguments, this.arguments); 149 } 150 toString()151 public String toString() { 152 return new PrintSettings().print(ArgumentsProcessor.argumentsToMatchers(getArguments()), this); 153 } 154 155 public final static RealMethod NO_OP = new RealMethod() { 156 public boolean isInvokable() { 157 return false; 158 } 159 public Object invoke() throws Throwable { 160 return null; 161 } 162 }; 163 164 } 165