From 6097e909ec99f5d85fec7b0eb9642451e910982e Mon Sep 17 00:00:00 2001 From: Duo Zhang Date: Sat, 28 Sep 2019 19:22:47 +0800 Subject: [PATCH] HADOOP-16600. StagingTestBase uses methods not available in Mockito 1.8.5 in branch-3.1 Signed-off-by: Steve Loughran Signed-off-by: Ayush Saxena Signed-off-by: stack --- .../s3a/commit/staging/StagingTestBase.java | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/commit/staging/StagingTestBase.java b/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/commit/staging/StagingTestBase.java index 38d5156ea06..d81c747fcea 100644 --- a/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/commit/staging/StagingTestBase.java +++ b/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/commit/staging/StagingTestBase.java @@ -49,6 +49,7 @@ import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; +import org.mockito.invocation.InvocationOnMock; import org.slf4j.Logger; 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 type of return + * @return the argument of the invocation, cast to the given type. + */ + @SuppressWarnings("unchecked") + private static T getArgumentAt(InvocationOnMock invocation, int index, + Class clazz) { + return (T)invocation.getArguments()[index]; + } + /** * Instantiate mock client with the results and errors requested. * @param results results to accrue @@ -539,7 +555,7 @@ public class StagingTestBase { "Mock Fail on init " + results.requests.size()); } String uploadId = UUID.randomUUID().toString(); - InitiateMultipartUploadRequest req = invocation.getArgumentAt( + InitiateMultipartUploadRequest req = getArgumentAt(invocation, 0, InitiateMultipartUploadRequest.class); results.requests.put(uploadId, req); results.activeUploads.put(uploadId, req.getKey()); @@ -561,7 +577,7 @@ public class StagingTestBase { throw new AmazonClientException( "Mock Fail on upload " + results.parts.size()); } - UploadPartRequest req = invocation.getArgumentAt( + UploadPartRequest req = getArgumentAt(invocation, 0, UploadPartRequest.class); results.parts.add(req); String etag = UUID.randomUUID().toString(); @@ -588,7 +604,7 @@ public class StagingTestBase { throw new AmazonClientException( "Mock Fail on commit " + results.commits.size()); } - CompleteMultipartUploadRequest req = invocation.getArgumentAt( + CompleteMultipartUploadRequest req = getArgumentAt(invocation, 0, CompleteMultipartUploadRequest.class); results.commits.add(req); results.activeUploads.remove(req.getUploadId()); @@ -608,7 +624,7 @@ public class StagingTestBase { throw new AmazonClientException( "Mock Fail on abort " + results.aborts.size()); } - AbortMultipartUploadRequest req = invocation.getArgumentAt( + AbortMultipartUploadRequest req = getArgumentAt(invocation, 0, AbortMultipartUploadRequest.class); String id = req.getUploadId(); String p = results.activeUploads.remove(id); @@ -630,7 +646,7 @@ public class StagingTestBase { doAnswer(invocation -> { LOG.debug("deleteObject for {}", mockClient); synchronized (lock) { - results.deletes.add(invocation.getArgumentAt( + results.deletes.add(getArgumentAt(invocation, 0, DeleteObjectRequest.class)); return null; } @@ -643,8 +659,8 @@ public class StagingTestBase { LOG.debug("deleteObject for {}", mockClient); synchronized (lock) { results.deletes.add(new DeleteObjectRequest( - invocation.getArgumentAt(0, String.class), - invocation.getArgumentAt(1, String.class) + getArgumentAt(invocation, 0, String.class), + getArgumentAt(invocation, 1, String.class) )); return null; }