InvocationImpl.java revision d174d376bccaf52c3273473bcc34bdb11b50bd67
1/* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 6package org.mockito.internal.invocation; 7 8import org.mockito.exceptions.Reporter; 9import org.mockito.internal.debugging.LocationImpl; 10import org.mockito.internal.exceptions.VerificationAwareInvocation; 11import org.mockito.internal.invocation.realmethod.RealMethod; 12import org.mockito.internal.reporting.PrintSettings; 13import org.mockito.invocation.*; 14 15import java.lang.reflect.Method; 16import 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") 27public 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 43 public InvocationImpl(Object mock, MockitoMethod mockitoMethod, Object[] args, int sequenceNumber, RealMethod realMethod) { 44 this.method = mockitoMethod; 45 this.mock = mock; 46 this.realMethod = realMethod; 47 this.arguments = ArgumentsProcessor.expandVarArgs(mockitoMethod.isVarArgs(), args); 48 this.rawArguments = args; 49 this.sequenceNumber = sequenceNumber; 50 this.location = new LocationImpl(); 51 } 52 53 public Object getMock() { 54 return mock; 55 } 56 57 public Method getMethod() { 58 return method.getJavaMethod(); 59 } 60 61 public Object[] getArguments() { 62 return arguments; 63 } 64 65 public <T> T getArgumentAt(int index, Class<T> clazz) { 66 return (T) arguments[index]; 67 } 68 69 public boolean isVerified() { 70 return verified || isIgnoredForVerification; 71 } 72 73 public int getSequenceNumber() { 74 return sequenceNumber; 75 } 76 77 public boolean equals(Object o) { 78 if (o == null || !o.getClass().equals(this.getClass())) { 79 return false; 80 } 81 82 InvocationImpl other = (InvocationImpl) o; 83 84 return this.mock.equals(other.mock) && this.method.equals(other.method) && this.equalArguments(other.arguments); 85 } 86 87 private boolean equalArguments(Object[] arguments) { 88 return Arrays.equals(arguments, this.arguments); 89 } 90 91 @Override 92 public int hashCode() { 93 return 1; 94 } 95 96 public String toString() { 97 return new PrintSettings().print(ArgumentsProcessor.argumentsToMatchers(getArguments()), this); 98 } 99 100 public Location getLocation() { 101 return location; 102 } 103 104 public Object[] getRawArguments() { 105 return this.rawArguments; 106 } 107 108 public Object callRealMethod() throws Throwable { 109 if (method.isAbstract()) { 110 new Reporter().cannotCallAbstractRealMethod(); 111 } 112 return realMethod.invoke(mock, rawArguments); 113 } 114 115 public void markVerified() { 116 this.verified = true; 117 } 118 119 public StubInfo stubInfo() { 120 return stubInfo; 121 } 122 123 public void markStubbed(StubInfo stubInfo) { 124 this.stubInfo = stubInfo; 125 } 126 127 public boolean isIgnoredForVerification() { 128 return isIgnoredForVerification; 129 } 130 131 public void ignoreForVerification() { 132 isIgnoredForVerification = true; 133 } 134}