From cca51e916b7387ea358688e8f8188ead948fbdcc Mon Sep 17 00:00:00 2001 From: Akira Ajisaka Date: Tue, 25 Jul 2017 15:24:56 +0900 Subject: [PATCH] HADOOP-14681. Remove MockitoMaker class. Contributed by Andras Bokor. --- .../org/apache/hadoop/test/MockitoMaker.java | 132 ------------------ .../apache/hadoop/util/TestDiskChecker.java | 12 +- .../v2/app/metrics/TestMRAppMetrics.java | 9 +- .../lib/input/TestMRCJCFileInputFormat.java | 11 +- .../hadoop/mapred/TestShuffleHandler.java | 32 ++--- .../scheduler/TestQueueMetrics.java | 14 +- 6 files changed, 38 insertions(+), 172 deletions(-) delete mode 100644 hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MockitoMaker.java diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MockitoMaker.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MockitoMaker.java deleted file mode 100644 index 28c2011c264..00000000000 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MockitoMaker.java +++ /dev/null @@ -1,132 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.test; - -import static org.mockito.Mockito.*; - -/** - * Helper class to create one-liner stubs, so that instead of:
- * SomeType someDescriptiveMock = mock(SomeType.class);
- * when(someDescriptiveMock.someMethod()).thenReturn(someValue);
- *

You can now do:

- * SomeType someDescriptiveMock = make(stub(SomeType.class)
- *     .returning(someValue).from.someMethod());
- */ -public class MockitoMaker { - - /** - * Create a mock object from a mocked method call. - * - * @param type of mocked object - * @param methodCall for mocked object - * @return mocked object - */ - @SuppressWarnings("unchecked") - public static T make(Object methodCall) { - StubBuilder sb = StubBuilder.current(); - when(methodCall).thenReturn(sb.firstReturn, sb.laterReturns); - return (T) StubBuilder.current().from; - } - - /** - * Create a stub builder of a mocked object. - * - * @param type of the target object to be mocked - * @param target class of the target object to be mocked - * @return the stub builder of the mocked object - */ - public static StubBuilder stub(Class target) { - return new StubBuilder(mock(target)); - } - - /** - * Builder class for stubs - * @param type of the object to be mocked - */ - public static class StubBuilder { - - /** - * The target mock object - */ - public final T from; - - // We want to be able to use this even when the tests are run in parallel. - @SuppressWarnings("rawtypes") - private static final ThreadLocal tls = - new ThreadLocal() { - @Override protected StubBuilder initialValue() { - return new StubBuilder(); - } - }; - - private Object firstReturn = null; - private Object[] laterReturns = {}; - - /** - * Default constructor for the initial stub builder - */ - public StubBuilder() { - this.from = null; - } - - /** - * Construct a stub builder with a mock instance - * - * @param mockInstance the mock object - */ - public StubBuilder(T mockInstance) { - tls.set(this); - this.from = mockInstance; - } - - /** - * Get the current stub builder from thread local - * - * @param - * @return the stub builder of the mocked object - */ - @SuppressWarnings("unchecked") - public static StubBuilder current() { - return tls.get(); - } - - /** - * Set the return value for the current stub builder - * - * @param value the return value - * @return the stub builder - */ - public StubBuilder returning(Object value) { - this.firstReturn = value; - return this; - } - - /** - * Set the return values for the current stub builder - * - * @param value the first return value - * @param values the return values for later invocations - * @return the stub builder - */ - public StubBuilder returning(Object value, Object... values) { - this.firstReturn = value; - this.laterReturns = values; - return this; - } - } -} diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDiskChecker.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDiskChecker.java index 40b8d0dd970..bd8e1dd48e5 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDiskChecker.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDiskChecker.java @@ -29,7 +29,6 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.*; -import static org.apache.hadoop.test.MockitoMaker.*; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; @@ -83,13 +82,14 @@ public class TestDiskChecker { private void _mkdirs(boolean exists, FsPermission before, FsPermission after) throws Throwable { - File localDir = make(stub(File.class).returning(exists).from.exists()); + File localDir = mock(File.class); + when(localDir.exists()).thenReturn(exists); when(localDir.mkdir()).thenReturn(true); Path dir = mock(Path.class); // use default stubs - LocalFileSystem fs = make(stub(LocalFileSystem.class) - .returning(localDir).from.pathToFile(dir)); - FileStatus stat = make(stub(FileStatus.class) - .returning(after).from.getPermission()); + LocalFileSystem fs = mock(LocalFileSystem.class); + when(fs.pathToFile(dir)).thenReturn(localDir); + FileStatus stat = mock(FileStatus.class); + when(stat.getPermission()).thenReturn(after); when(fs.getFileStatus(dir)).thenReturn(stat); try { diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/metrics/TestMRAppMetrics.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/metrics/TestMRAppMetrics.java index 8bfc2a83a1d..02552bce897 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/metrics/TestMRAppMetrics.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/metrics/TestMRAppMetrics.java @@ -23,7 +23,6 @@ import org.apache.hadoop.mapreduce.v2.app.job.Task; import org.apache.hadoop.metrics2.MetricsRecordBuilder; import static org.apache.hadoop.test.MetricsAsserts.*; -import static org.apache.hadoop.test.MockitoMaker.*; import org.junit.Test; @@ -33,10 +32,10 @@ public class TestMRAppMetrics { @Test public void testNames() { Job job = mock(Job.class); - Task mapTask = make(stub(Task.class).returning(TaskType.MAP). - from.getType()); - Task reduceTask = make(stub(Task.class).returning(TaskType.REDUCE). - from.getType()); + Task mapTask = mock(Task.class); + when(mapTask.getType()).thenReturn(TaskType.MAP); + Task reduceTask = mock(Task.class); + when(reduceTask.getType()).thenReturn(TaskType.REDUCE); MRAppMetrics metrics = MRAppMetrics.create(); metrics.submittedJob(job); diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-jobclient/src/test/java/org/apache/hadoop/mapreduce/lib/input/TestMRCJCFileInputFormat.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-jobclient/src/test/java/org/apache/hadoop/mapreduce/lib/input/TestMRCJCFileInputFormat.java index b806630c940..ef25876dec1 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-jobclient/src/test/java/org/apache/hadoop/mapreduce/lib/input/TestMRCJCFileInputFormat.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-jobclient/src/test/java/org/apache/hadoop/mapreduce/lib/input/TestMRCJCFileInputFormat.java @@ -27,7 +27,6 @@ import org.junit.Test; import static org.junit.Assert.*; import static org.mockito.Mockito.*; -import static org.apache.hadoop.test.MockitoMaker.*; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.BlockLocation; @@ -80,12 +79,14 @@ public class TestMRCJCFileInputFormat { @Test public void testNumInputFiles() throws Exception { Configuration conf = spy(new Configuration()); - Job job = make(stub(Job.class).returning(conf).from.getConfiguration()); - FileStatus stat = make(stub(FileStatus.class).returning(0L).from.getLen()); + Job mockedJob = mock(Job.class); + when(mockedJob.getConfiguration()).thenReturn(conf); + FileStatus stat = mock(FileStatus.class); + when(stat.getLen()).thenReturn(0L); TextInputFormat ispy = spy(new TextInputFormat()); - doReturn(Arrays.asList(stat)).when(ispy).listStatus(job); + doReturn(Arrays.asList(stat)).when(ispy).listStatus(mockedJob); - ispy.getSplits(job); + ispy.getSplits(mockedJob); verify(conf).setLong(FileInputFormat.NUM_INPUT_FILES, 1); } diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-shuffle/src/test/java/org/apache/hadoop/mapred/TestShuffleHandler.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-shuffle/src/test/java/org/apache/hadoop/mapred/TestShuffleHandler.java index 7fb2051cb35..849ce1a1563 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-shuffle/src/test/java/org/apache/hadoop/mapred/TestShuffleHandler.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-shuffle/src/test/java/org/apache/hadoop/mapred/TestShuffleHandler.java @@ -20,14 +20,14 @@ package org.apache.hadoop.mapred; import static org.apache.hadoop.test.MetricsAsserts.assertCounter; import static org.apache.hadoop.test.MetricsAsserts.assertGauge; import static org.apache.hadoop.test.MetricsAsserts.getMetrics; -import static org.apache.hadoop.test.MockitoMaker.make; -import static org.apache.hadoop.test.MockitoMaker.stub; import static org.junit.Assert.assertTrue; import static org.jboss.netty.buffer.ChannelBuffers.wrappedBuffer; import static org.jboss.netty.handler.codec.http.HttpResponseStatus.OK; import static org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1; import static org.junit.Assert.assertEquals; import static org.junit.Assume.assumeTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.io.DataInputStream; import java.io.EOFException; @@ -188,8 +188,8 @@ public class TestShuffleHandler { public void testShuffleMetrics() throws Exception { MetricsSystem ms = new MetricsSystemImpl(); ShuffleHandler sh = new ShuffleHandler(ms); - ChannelFuture cf = make(stub(ChannelFuture.class). - returning(true, false).from.isSuccess()); + ChannelFuture cf = mock(ChannelFuture.class); + when(cf.isSuccess()).thenReturn(true).thenReturn(false); sh.metrics.shuffleConnections.incr(); sh.metrics.shuffleOutputBytes.incr(1*MiB); @@ -1080,10 +1080,10 @@ public class TestShuffleHandler { new ArrayList(); final ChannelHandlerContext mockCtx = - Mockito.mock(ChannelHandlerContext.class); - final MessageEvent mockEvt = Mockito.mock(MessageEvent.class); - final Channel mockCh = Mockito.mock(AbstractChannel.class); - final ChannelPipeline mockPipeline = Mockito.mock(ChannelPipeline.class); + mock(ChannelHandlerContext.class); + final MessageEvent mockEvt = mock(MessageEvent.class); + final Channel mockCh = mock(AbstractChannel.class); + final ChannelPipeline mockPipeline = mock(ChannelPipeline.class); // Mock HttpRequest and ChannelFuture final HttpRequest mockHttpRequest = createMockHttpRequest(); @@ -1094,16 +1094,16 @@ public class TestShuffleHandler { // Mock Netty Channel Context and Channel behavior Mockito.doReturn(mockCh).when(mockCtx).getChannel(); - Mockito.when(mockCh.getPipeline()).thenReturn(mockPipeline); - Mockito.when(mockPipeline.get( + when(mockCh.getPipeline()).thenReturn(mockPipeline); + when(mockPipeline.get( Mockito.any(String.class))).thenReturn(timerHandler); - Mockito.when(mockCtx.getChannel()).thenReturn(mockCh); + when(mockCtx.getChannel()).thenReturn(mockCh); Mockito.doReturn(mockFuture).when(mockCh).write(Mockito.any(Object.class)); - Mockito.when(mockCh.write(Object.class)).thenReturn(mockFuture); + when(mockCh.write(Object.class)).thenReturn(mockFuture); //Mock MessageEvent behavior Mockito.doReturn(mockCh).when(mockEvt).getChannel(); - Mockito.when(mockEvt.getChannel()).thenReturn(mockCh); + when(mockEvt.getChannel()).thenReturn(mockCh); Mockito.doReturn(mockHttpRequest).when(mockEvt).getMessage(); final ShuffleHandler sh = new MockShuffleHandler(); @@ -1127,8 +1127,8 @@ public class TestShuffleHandler { public ChannelFuture createMockChannelFuture(Channel mockCh, final List listenerList) { - final ChannelFuture mockFuture = Mockito.mock(ChannelFuture.class); - Mockito.when(mockFuture.getChannel()).thenReturn(mockCh); + final ChannelFuture mockFuture = mock(ChannelFuture.class); + when(mockFuture.getChannel()).thenReturn(mockCh); Mockito.doReturn(true).when(mockFuture).isSuccess(); Mockito.doAnswer(new Answer() { @Override @@ -1146,7 +1146,7 @@ public class TestShuffleHandler { } public HttpRequest createMockHttpRequest() { - HttpRequest mockHttpRequest = Mockito.mock(HttpRequest.class); + HttpRequest mockHttpRequest = mock(HttpRequest.class); Mockito.doReturn(HttpMethod.GET).when(mockHttpRequest).getMethod(); Mockito.doAnswer(new Answer() { @Override diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/TestQueueMetrics.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/TestQueueMetrics.java index 13144e9e04c..196d4c291cd 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/TestQueueMetrics.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/TestQueueMetrics.java @@ -21,8 +21,6 @@ package org.apache.hadoop.yarn.server.resourcemanager.scheduler; import static org.apache.hadoop.test.MetricsAsserts.assertCounter; import static org.apache.hadoop.test.MetricsAsserts.assertGauge; import static org.apache.hadoop.test.MetricsAsserts.getMetrics; -import static org.apache.hadoop.test.MockitoMaker.make; -import static org.apache.hadoop.test.MockitoMaker.stub; import static org.junit.Assert.assertNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -226,8 +224,8 @@ public class TestQueueMetrics { QueueMetrics parentMetrics = QueueMetrics.forQueue(ms, parentQueueName, null, true, conf); - Queue parentQueue = make(stub(Queue.class).returning(parentMetrics). - from.getMetrics()); + Queue parentQueue = mock(Queue.class); + when(parentQueue.getMetrics()).thenReturn(parentMetrics); QueueMetrics metrics = QueueMetrics.forQueue(ms, leafQueueName, parentQueue, true, conf); MetricsSource parentQueueSource = queueSource(ms, parentQueueName); @@ -272,8 +270,8 @@ public class TestQueueMetrics { QueueMetrics parentMetrics = QueueMetrics.forQueue(ms, parentQueueName, null, true, conf); - Queue parentQueue = make(stub(Queue.class).returning(parentMetrics). - from.getMetrics()); + Queue parentQueue = mock(Queue.class); + when(parentQueue.getMetrics()).thenReturn(parentMetrics); QueueMetrics metrics = QueueMetrics.forQueue(ms, leafQueueName, parentQueue, true, conf); MetricsSource parentQueueSource = queueSource(ms, parentQueueName); @@ -359,8 +357,8 @@ public class TestQueueMetrics { QueueMetrics p1Metrics = QueueMetrics.forQueue(ms, p1, null, true, conf); - Queue parentQueue1 = make(stub(Queue.class).returning(p1Metrics). - from.getMetrics()); + Queue parentQueue1 = mock(Queue.class); + when(parentQueue1.getMetrics()).thenReturn(p1Metrics); QueueMetrics metrics = QueueMetrics.forQueue(ms, leafQueueName, parentQueue1, true, conf);