HADOOP-16600. StagingTestBase uses methods not available in Mockito 1.8.5 in branch-3.1

Signed-off-by: Steve Loughran <stevel@apache.org>
Signed-off-by: Ayush Saxena <ayushsaxena@apache.org>
Signed-off-by: stack <stack@apache.org>
This commit is contained in:
Duo Zhang 2019-09-28 19:22:47 +08:00 committed by stack
parent 14beaae65e
commit 6097e909ec
1 changed files with 23 additions and 7 deletions

View File

@ -49,6 +49,7 @@ import org.junit.AfterClass;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.mockito.invocation.InvocationOnMock;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -514,6 +515,21 @@ public class StagingTestBase {
} }
} }
/**
* InvocationOnMock.getArgumentAt comes and goes with Mockito versions; this
* helper method is designed to be resilient to change.
* @param invocation invocation to query
* @param index argument index
* @param clazz class of return type
* @param <T> type of return
* @return the argument of the invocation, cast to the given type.
*/
@SuppressWarnings("unchecked")
private static<T> T getArgumentAt(InvocationOnMock invocation, int index,
Class<T> clazz) {
return (T)invocation.getArguments()[index];
}
/** /**
* Instantiate mock client with the results and errors requested. * Instantiate mock client with the results and errors requested.
* @param results results to accrue * @param results results to accrue
@ -539,7 +555,7 @@ public class StagingTestBase {
"Mock Fail on init " + results.requests.size()); "Mock Fail on init " + results.requests.size());
} }
String uploadId = UUID.randomUUID().toString(); String uploadId = UUID.randomUUID().toString();
InitiateMultipartUploadRequest req = invocation.getArgumentAt( InitiateMultipartUploadRequest req = getArgumentAt(invocation,
0, InitiateMultipartUploadRequest.class); 0, InitiateMultipartUploadRequest.class);
results.requests.put(uploadId, req); results.requests.put(uploadId, req);
results.activeUploads.put(uploadId, req.getKey()); results.activeUploads.put(uploadId, req.getKey());
@ -561,7 +577,7 @@ public class StagingTestBase {
throw new AmazonClientException( throw new AmazonClientException(
"Mock Fail on upload " + results.parts.size()); "Mock Fail on upload " + results.parts.size());
} }
UploadPartRequest req = invocation.getArgumentAt( UploadPartRequest req = getArgumentAt(invocation,
0, UploadPartRequest.class); 0, UploadPartRequest.class);
results.parts.add(req); results.parts.add(req);
String etag = UUID.randomUUID().toString(); String etag = UUID.randomUUID().toString();
@ -588,7 +604,7 @@ public class StagingTestBase {
throw new AmazonClientException( throw new AmazonClientException(
"Mock Fail on commit " + results.commits.size()); "Mock Fail on commit " + results.commits.size());
} }
CompleteMultipartUploadRequest req = invocation.getArgumentAt( CompleteMultipartUploadRequest req = getArgumentAt(invocation,
0, CompleteMultipartUploadRequest.class); 0, CompleteMultipartUploadRequest.class);
results.commits.add(req); results.commits.add(req);
results.activeUploads.remove(req.getUploadId()); results.activeUploads.remove(req.getUploadId());
@ -608,7 +624,7 @@ public class StagingTestBase {
throw new AmazonClientException( throw new AmazonClientException(
"Mock Fail on abort " + results.aborts.size()); "Mock Fail on abort " + results.aborts.size());
} }
AbortMultipartUploadRequest req = invocation.getArgumentAt( AbortMultipartUploadRequest req = getArgumentAt(invocation,
0, AbortMultipartUploadRequest.class); 0, AbortMultipartUploadRequest.class);
String id = req.getUploadId(); String id = req.getUploadId();
String p = results.activeUploads.remove(id); String p = results.activeUploads.remove(id);
@ -630,7 +646,7 @@ public class StagingTestBase {
doAnswer(invocation -> { doAnswer(invocation -> {
LOG.debug("deleteObject for {}", mockClient); LOG.debug("deleteObject for {}", mockClient);
synchronized (lock) { synchronized (lock) {
results.deletes.add(invocation.getArgumentAt( results.deletes.add(getArgumentAt(invocation,
0, DeleteObjectRequest.class)); 0, DeleteObjectRequest.class));
return null; return null;
} }
@ -643,8 +659,8 @@ public class StagingTestBase {
LOG.debug("deleteObject for {}", mockClient); LOG.debug("deleteObject for {}", mockClient);
synchronized (lock) { synchronized (lock) {
results.deletes.add(new DeleteObjectRequest( results.deletes.add(new DeleteObjectRequest(
invocation.getArgumentAt(0, String.class), getArgumentAt(invocation, 0, String.class),
invocation.getArgumentAt(1, String.class) getArgumentAt(invocation, 1, String.class)
)); ));
return null; return null;
} }