Skip to content

Commit

Permalink
HADOOP-19243. Upgrade Mockito version to 4.11.0 (#6968)
Browse files Browse the repository at this point in the history
Mockito is now at a JDK-17 compatible version.

Contributed by Muskan Mishra
  • Loading branch information
muskan1012 authored Nov 5, 2024
1 parent 51ebc3c commit f7651e2
Show file tree
Hide file tree
Showing 84 changed files with 266 additions and 143 deletions.
13 changes: 11 additions & 2 deletions hadoop-client-modules/hadoop-client-minicluster/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@
<!-- Add back in Mockito since the hadoop-hdfs test jar needs it. -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<optional>true</optional>
</dependency>
<!-- Add back in the transitive dependencies excluded from hadoop-common in client TODO remove once we have a filter for "is in these artifacts" -->
Expand Down Expand Up @@ -765,7 +765,7 @@

<!-- Mockito tries to include its own unrelocated copy of hamcrest. :( -->
<filter>
<artifact>org.mockito:mockito-core</artifact>
<artifact>org.mockito:mockito-inline</artifact>
<excludes>
<exclude>asm-license.txt</exclude>
<exclude>cglib-license.txt</exclude>
Expand All @@ -777,6 +777,15 @@
<exclude>org/objenesis/*.class</exclude>
</excludes>
</filter>
<!-- Additional filters to exclude unexpected contents -->
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>mockito-extensions/**</exclude>
<exclude>win32-x86/**</exclude>
<exclude>win32-x86-64/**</exclude>
</excludes>
</filter>
<!-- skip grizzly internals we don't need to run. -->
<filter>
<artifact>org.glassfish.grizzly:grizzly-http-servlet</artifact>
Expand Down
18 changes: 15 additions & 3 deletions hadoop-cloud-storage-project/hadoop-huaweicloud/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,27 @@
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.7.4</version>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.7.4</version>
<version>2.0.9</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
1 change: 1 addition & 0 deletions hadoop-common-project/hadoop-auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion hadoop-common-project/hadoop-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.junit.Test;
import org.slf4j.Logger;

import static org.apache.hadoop.test.MockitoUtil.verifyZeroInteractions;

/**
* This is intended to be a set of unit tests for the
* org.apache.hadoop.ipc.Server class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.junit.Test;
import org.mockito.Mockito;

import static org.apache.hadoop.test.MockitoUtil.verifyZeroInteractions;

public class TestCrossOriginFilter {

@Test
Expand All @@ -59,7 +61,7 @@ public void testSameOrigin() throws ServletException, IOException {
filter.init(filterConfig);
filter.doFilter(mockReq, mockRes, mockChain);

Mockito.verifyZeroInteractions(mockRes);
verifyZeroInteractions(mockRes);
Mockito.verify(mockChain).doFilter(mockReq, mockRes);
}

Expand Down Expand Up @@ -224,7 +226,7 @@ public void testDisallowedOrigin() throws ServletException, IOException {
filter.init(filterConfig);
filter.doFilter(mockReq, mockRes, mockChain);

Mockito.verifyZeroInteractions(mockRes);
verifyZeroInteractions(mockRes);
Mockito.verify(mockChain).doFilter(mockReq, mockRes);
}

Expand Down Expand Up @@ -252,7 +254,7 @@ public void testDisallowedMethod() throws ServletException, IOException {
filter.init(filterConfig);
filter.doFilter(mockReq, mockRes, mockChain);

Mockito.verifyZeroInteractions(mockRes);
verifyZeroInteractions(mockRes);
Mockito.verify(mockChain).doFilter(mockReq, mockRes);
}

Expand Down Expand Up @@ -283,7 +285,7 @@ public void testDisallowedHeader() throws ServletException, IOException {
filter.init(filterConfig);
filter.doFilter(mockReq, mockRes, mockChain);

Mockito.verifyZeroInteractions(mockRes);
verifyZeroInteractions(mockRes);
Mockito.verify(mockChain).doFilter(mockReq, mockRes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.junit.Test;
import org.mockito.Mockito;

import static org.apache.hadoop.test.MockitoUtil.verifyZeroInteractions;

/**
* This class tests the behavior of the RestCsrfPreventionFilter.
*
Expand Down Expand Up @@ -75,7 +77,7 @@ public void testNoHeaderDefaultConfigBadRequest()

verify(mockRes, atLeastOnce()).sendError(
HttpServletResponse.SC_BAD_REQUEST, EXPECTED_MESSAGE);
Mockito.verifyZeroInteractions(mockChain);
verifyZeroInteractions(mockChain);
}

@Test
Expand Down Expand Up @@ -110,7 +112,7 @@ public void testNoHeaderCustomAgentConfigBadRequest()

verify(mockRes, atLeastOnce()).sendError(
HttpServletResponse.SC_BAD_REQUEST, EXPECTED_MESSAGE);
Mockito.verifyZeroInteractions(mockChain);
verifyZeroInteractions(mockChain);
}

@Test
Expand Down Expand Up @@ -228,7 +230,7 @@ public void testMissingHeaderWithCustomHeaderConfigBadRequest()
filter.init(filterConfig);
filter.doFilter(mockReq, mockRes, mockChain);

Mockito.verifyZeroInteractions(mockChain);
verifyZeroInteractions(mockChain);
}

@Test
Expand Down Expand Up @@ -260,7 +262,7 @@ public void testMissingHeaderNoMethodsToIgnoreConfigBadRequest()
filter.init(filterConfig);
filter.doFilter(mockReq, mockRes, mockChain);

Mockito.verifyZeroInteractions(mockChain);
verifyZeroInteractions(mockChain);
}

@Test
Expand Down Expand Up @@ -356,6 +358,6 @@ public void testMissingHeaderMultipleIgnoreMethodsConfigBadRequest()
filter.init(filterConfig);
filter.doFilter(mockReq, mockRes, mockChain);

Mockito.verifyZeroInteractions(mockChain);
verifyZeroInteractions(mockChain);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,13 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
}
});
}

/**
* Verifies that there were no interactions with the given mock objects.
*
* @param mocks the mock objects to verify
*/
public static void verifyZeroInteractions(Object... mocks) {
Mockito.verifyNoInteractions(mocks);
}
}
2 changes: 1 addition & 1 deletion hadoop-common-project/hadoop-kms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion hadoop-common-project/hadoop-nfs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion hadoop-hdfs-project/hadoop-hdfs-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd">
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion hadoop-hdfs-project/hadoop-hdfs-httpfs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion hadoop-hdfs-project/hadoop-hdfs-native-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd">
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion hadoop-hdfs-project/hadoop-hdfs-nfs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd">
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion hadoop-hdfs-project/hadoop-hdfs-rbf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd">
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.lang.reflect.Field;
import java.security.PrivilegedExceptionAction;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -68,7 +69,6 @@
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.internal.util.reflection.FieldSetter;

/**
* The administrator interface of the {@link Router} implemented by
Expand Down Expand Up @@ -118,18 +118,25 @@ public static void globalSetUp() throws Exception {
* @throws IOException
* @throws NoSuchFieldException
*/
private static void setUpMocks() throws IOException, NoSuchFieldException {
public static void setField(Object target, String fieldName, Object value)
throws NoSuchFieldException, IllegalAccessException {
Field field = target.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(target, value);
}

private static void setUpMocks()
throws IOException, NoSuchFieldException, IllegalAccessException {
RouterRpcServer spyRpcServer =
Mockito.spy(routerContext.getRouter().createRpcServer());
FieldSetter.setField(routerContext.getRouter(),
Router.class.getDeclaredField("rpcServer"), spyRpcServer);
//Used reflection to set the 'rpcServer field'
setField(routerContext.getRouter(), "rpcServer", spyRpcServer);
Mockito.doReturn(null).when(spyRpcServer).getFileInfo(Mockito.anyString());

// mock rpc client for destination check when editing mount tables.
//spy RPC client and used reflection to set the 'rpcClient' field
mockRpcClient = Mockito.spy(spyRpcServer.getRPCClient());
FieldSetter.setField(spyRpcServer,
RouterRpcServer.class.getDeclaredField("rpcClient"),
mockRpcClient);
setField(spyRpcServer, "rpcClient", mockRpcClient);
RemoteLocation remoteLocation0 =
new RemoteLocation("ns0", "/testdir", null);
RemoteLocation remoteLocation1 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.apache.hadoop.test.Whitebox.getInternalState;
Expand Down
2 changes: 1 addition & 1 deletion hadoop-hdfs-project/hadoop-hdfs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ https://maven.apache.org/xsd/maven-4.0.0.xsd">
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,8 @@ static long toLong(long preferredBlockSize, long layoutRedundancy,

private BlockInfo[] blocks;

INodeFile(long id, byte[] name, PermissionStatus permissions, long mtime,
long atime, BlockInfo[] blklist, short replication,
long preferredBlockSize) {
public INodeFile(long id, byte[] name, PermissionStatus permissions, long mtime, long atime,
BlockInfo[] blklist, short replication, long preferredBlockSize) {
this(id, name, permissions, mtime, atime, blklist, replication, null,
preferredBlockSize, (byte) 0, CONTIGUOUS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ public void testIncrementAndDecrementStripedSafeBlockCount() {
for (long i = 1; i <= BLOCK_TOTAL; i++) {
BlockInfoStriped blockInfo = mock(BlockInfoStriped.class);
when(blockInfo.getRealDataBlockNum()).thenReturn(realDataBlockNum);
when(blockInfo.isStriped()).thenReturn(true);

bmSafeMode.incrementSafeBlockCount(realDataBlockNum, blockInfo);
bmSafeMode.decrementSafeBlockCount(blockInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_CACHING_ENABLED_KEY;
import static org.apache.hadoop.hdfs.protocol.CachePoolInfo.RELATIVE_EXPIRY_NEVER;
import static org.apache.hadoop.test.GenericTestUtils.assertExceptionContains;
import static org.apache.hadoop.test.MockitoUtil.verifyZeroInteractions;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -1575,7 +1576,7 @@ public void testNoLookupsWhenNotUsed() throws Exception {
CacheManager cm = cluster.getNamesystem().getCacheManager();
LocatedBlocks locations = Mockito.mock(LocatedBlocks.class);
cm.setCachedLocations(locations);
Mockito.verifyZeroInteractions(locations);
verifyZeroInteractions(locations);
}

@Test(timeout=120000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ private FSNamesystem makeNameSystemSpy(Block block, INodeFile file)
}
namesystem.dir.getINodeMap().put(file);

FSNamesystem namesystemSpy = spy(namesystem);
BlockInfo blockInfo = new BlockInfoContiguous(block, (short) 1);
blockInfo.convertToBlockUnderConstruction(
HdfsServerConstants.BlockUCState.UNDER_CONSTRUCTION, targets);
Expand All @@ -73,8 +72,10 @@ private FSNamesystem makeNameSystemSpy(Block block, INodeFile file)
doReturn(blockInfo).when(file).removeLastBlock(any(Block.class));
doReturn(true).when(file).isUnderConstruction();
doReturn(new BlockInfoContiguous[1]).when(file).getBlocks();

doReturn(blockInfo).when(namesystemSpy).getStoredBlock(any(Block.class));
FSNamesystem namesystemSpy = spy(namesystem);
doReturn(blockInfo).when(namesystemSpy).getStoredBlock(nullable(Block.class));
doReturn(file).when(namesystemSpy).getBlockCollection(any(BlockInfo.class));
doReturn(false).when(namesystemSpy).isFileDeleted(any(INodeFile.class));
doReturn(blockInfo).when(file).getLastBlock();
doNothing().when(namesystemSpy).closeFileCommitBlocks(
any(), any(INodeFile.class), any(BlockInfo.class));
Expand Down
Loading

0 comments on commit f7651e2

Please sign in to comment.