forked from mockito/mockito
/
ReturnsArgumentAt.java
177 lines (147 loc) · 6.92 KB
/
ReturnsArgumentAt.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing.answers;
import static org.mockito.internal.exceptions.Reporter.invalidArgumentPositionRangeAtInvocationTime;
import static org.mockito.internal.exceptions.Reporter.invalidArgumentRangeAtIdentityAnswerCreationTime;
import static org.mockito.internal.exceptions.Reporter.wrongTypeOfArgumentToReturn;
import java.io.Serializable;
import java.lang.reflect.Method;
import org.mockito.invocation.Invocation;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.ValidableAnswer;
/**
* Returns the passed parameter identity at specified index.
* <p>
* <p>
* The <code>argumentIndex</code> represents the index in the argument array of the invocation.
* </p>
* <p>
* If this number equals -1 then the last argument is returned.
* </p>
*
* @see org.mockito.AdditionalAnswers
* @since 1.9.5
*/
public class ReturnsArgumentAt implements Answer<Object>, ValidableAnswer, Serializable {
private static final long serialVersionUID = -589315085166295101L;
public static final int LAST_ARGUMENT = -1;
private final int wantedArgumentPosition;
/**
* Build the identity answer to return the argument at the given position in the argument array.
*
* @param wantedArgumentPosition
* The position of the argument identity to return in the invocation. Using <code>-1</code> indicates the last argument ({@link #LAST_ARGUMENT}).
*/
public ReturnsArgumentAt(int wantedArgumentPosition) {
if (wantedArgumentPosition != LAST_ARGUMENT && wantedArgumentPosition < 0) {
throw invalidArgumentRangeAtIdentityAnswerCreationTime();
}
this.wantedArgumentPosition = wantedArgumentPosition;
}
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
Invocation invocation = (Invocation) invocationOnMock;
if (wantedArgIndexIsVarargAndSameTypeAsReturnType(invocation)) {
// answer raw vararg array argument
return invocation.getRawArguments()[invocation.getRawArguments().length - 1];
}
int argumentPosition = inferWantedArgumentPosition(invocation);
validateIndexWithinInvocationRange(invocation, argumentPosition);
// answer expanded argument at wanted position
return invocation.getArgument(argumentPosition);
}
@Override
public void validateFor(InvocationOnMock invocationOnMock) {
Invocation invocation = (Invocation) invocationOnMock;
int argumentPosition = inferWantedArgumentPosition(invocation);
validateIndexWithinTheoreticalInvocationRange(invocation, argumentPosition);
validateArgumentTypeCompatibility(invocation, argumentPosition);
}
private int inferWantedArgumentPosition(InvocationOnMock invocation) {
if (wantedArgumentPosition == LAST_ARGUMENT) {
return invocation.getArguments().length - 1;
}
return wantedArgumentPosition;
}
private int inferWantedRawArgumentPosition(Invocation invocation) {
if (wantedArgumentPosition == LAST_ARGUMENT) {
return invocation.getRawArguments().length - 1;
}
return wantedArgumentPosition;
}
private void validateIndexWithinInvocationRange(
InvocationOnMock invocation, int argumentPosition) {
if (invocation.getArguments().length <= argumentPosition) {
throw invalidArgumentPositionRangeAtInvocationTime(
invocation, wantedArgumentPosition == LAST_ARGUMENT, wantedArgumentPosition);
}
}
private void validateIndexWithinTheoreticalInvocationRange(
InvocationOnMock invocation, int argumentPosition) {
if (!wantedArgumentPositionIsValidForTheoreticalInvocation(invocation, argumentPosition)) {
throw invalidArgumentPositionRangeAtInvocationTime(
invocation, wantedArgumentPosition == LAST_ARGUMENT, wantedArgumentPosition);
}
}
private void validateArgumentTypeCompatibility(Invocation invocation, int argumentPosition) {
InvocationInfo invocationInfo = new InvocationInfo(invocation);
Class<?> inferredArgumentType = inferArgumentType(invocation, argumentPosition);
if (!invocationInfo.isValidReturnType(inferredArgumentType)) {
throw wrongTypeOfArgumentToReturn(
invocation,
invocationInfo.printMethodReturnType(),
inferredArgumentType,
wantedArgumentPosition);
}
}
private boolean wantedArgIndexIsVarargAndSameTypeAsReturnType(Invocation invocation) {
int rawArgumentPosition = inferWantedRawArgumentPosition(invocation);
Method method = invocation.getMethod();
Class<?>[] parameterTypes = method.getParameterTypes();
return method.isVarArgs()
&& rawArgumentPosition == /* vararg index */ parameterTypes.length - 1
&& method.getReturnType().isAssignableFrom(parameterTypes[rawArgumentPosition]);
}
private boolean wantedArgumentPositionIsValidForTheoreticalInvocation(
InvocationOnMock invocation, int argumentPosition) {
if (argumentPosition < 0) {
return false;
}
if (!invocation.getMethod().isVarArgs()) {
return invocation.getArguments().length > argumentPosition;
}
// for all varargs accepts positive ranges
return true;
}
private Class<?> inferArgumentType(Invocation invocation, int argumentIndex) {
Class<?>[] parameterTypes = invocation.getMethod().getParameterTypes();
// Easy when the method is not a vararg
if (!invocation.getMethod().isVarArgs()) {
Class<?> argumentType = parameterTypes[argumentIndex];
Object argumentValue = invocation.getArgument(argumentIndex);
// we don't want to return primitive wrapper types
if (argumentType.isPrimitive() || argumentValue == null) {
return argumentType;
}
return argumentValue.getClass();
}
// Now for varargs
int varargIndex = parameterTypes.length - 1; // vararg always last
if (argumentIndex < varargIndex) {
// Same for non vararg arguments
return parameterTypes[argumentIndex];
}
// if wanted argument is vararg
if (wantedArgIndexIsVarargAndSameTypeAsReturnType(invocation)) {
// return the vararg array if return type is compatible
// because the user probably want to return the array itself if the return type is
// compatible
return parameterTypes[argumentIndex]; // move to MethodInfo ?
}
// return the type in this vararg array
return parameterTypes[varargIndex].getComponentType();
}
}