InvisibleProgrammer commented on code in PR #4707: URL: https://github.com/apache/hive/pull/4707#discussion_r1344257002
########## ql/src/test/org/apache/hadoop/hive/ql/parse/repl/TestCopyUtils.java: ########## @@ -97,151 +96,156 @@ public void shouldThrowExceptionOnDistcpFailure() throws Exception { List<Path> srcPaths = Arrays.asList(source, source); HiveConf conf = mock(HiveConf.class); CopyUtils copyUtils = Mockito.spy(new CopyUtils(null, conf, fs)); + doReturn(false).when(copyUtils).regularCopy(same(fs), anyList()); - mockStatic(FileUtils.class); - mockStatic(Utils.class); - when(destination.getFileSystem(same(conf))).thenReturn(fs); when(source.getFileSystem(same(conf))).thenReturn(fs); - when(FileUtils.distCp(same(fs), anyListOf(Path.class), same(destination), - anyBoolean(), eq(null), same(conf), - same(ShimLoader.getHadoopShims()))) - .thenReturn(false); - when(Utils.getUGI()).thenReturn(mock(UserGroupInformation.class)); - doReturn(false).when(copyUtils).regularCopy(same(fs), anyListOf(ReplChangeManager.FileInfo.class)); - - copyUtils.doCopy(destination, srcPaths); + try (MockedStatic<FileUtils> fileUtilsMockedStatic = mockStatic(FileUtils.class); + MockedStatic<Utils> utilsMockedStatic = mockStatic(Utils.class)) { + fileUtilsMockedStatic.when( + () -> FileUtils.distCp(same(fs), anyList(), same(destination), anyBoolean(), eq(null), same(conf), + same(ShimLoader.getHadoopShims()))).thenReturn(false); + utilsMockedStatic.when(Utils::getUGI).thenReturn(mock(UserGroupInformation.class)); + + copyUtils.doCopy(destination, srcPaths); + } } @Test public void testFSCallsFailOnParentExceptions() throws Exception { - mockStatic(UserGroupInformation.class); - mockStatic(ReplChangeManager.class); - when(UserGroupInformation.getCurrentUser()).thenReturn(mock(UserGroupInformation.class)); - HiveConf conf = mock(HiveConf.class); - conf.set(HiveConf.ConfVars.REPL_RETRY_INTIAL_DELAY.varname, "1s"); - FileSystem fs = mock(FileSystem.class); - Path source = mock(Path.class); - Path destination = mock(Path.class); - ContentSummary cs = mock(ContentSummary.class); - - Exception exception = new org.apache.hadoop.fs.PathPermissionException("Failed"); - when(ReplChangeManager.checksumFor(source, fs)).thenThrow(exception).thenReturn("dummy"); - when(fs.exists(same(source))).thenThrow(exception).thenReturn(true); - when(fs.delete(same(source), anyBoolean())).thenThrow(exception).thenReturn(true); - when(fs.mkdirs(same(source))).thenThrow(exception).thenReturn(true); - when(fs.rename(same(source), same(destination))).thenThrow(exception).thenReturn(true); - when(fs.getContentSummary(same(source))).thenThrow(exception).thenReturn(cs); - - CopyUtils copyUtils = new CopyUtils(UserGroupInformation.getCurrentUser().getUserName(), conf, fs); - CopyUtils copyUtilsSpy = Mockito.spy(copyUtils); - try { - copyUtilsSpy.exists(fs, source); - } catch (Exception e) { - assertEquals(exception.getClass(), e.getCause().getClass()); - } - Mockito.verify(fs, Mockito.times(1)).exists(source); - try { - copyUtils.delete(fs, source, true); - } catch (Exception e) { - assertEquals(exception.getClass(), e.getCause().getClass()); - } - Mockito.verify(fs, Mockito.times(1)).delete(source, true); - try { - copyUtils.mkdirs(fs, source); - } catch (Exception e) { - assertEquals(exception.getClass(), e.getCause().getClass()); - } - Mockito.verify(fs, Mockito.times(1)).mkdirs(source); - try { - copyUtils.rename(fs, source, destination); - } catch (Exception e) { - assertEquals(exception.getClass(), e.getCause().getClass()); + try ( + MockedStatic<UserGroupInformation> userGroupInformationMockedStatic = mockStatic(UserGroupInformation.class); + MockedStatic<ReplChangeManager> replChangeManagerMockedStatic = mockStatic(ReplChangeManager.class) + ) { + userGroupInformationMockedStatic.when(UserGroupInformation::getCurrentUser).thenReturn(mock(UserGroupInformation.class)); + HiveConf conf = mock(HiveConf.class); + conf.set(HiveConf.ConfVars.REPL_RETRY_INTIAL_DELAY.varname, "1s"); + FileSystem fs = mock(FileSystem.class); + Path source = mock(Path.class); + Path destination = mock(Path.class); + ContentSummary cs = mock(ContentSummary.class); + + Exception exception = new org.apache.hadoop.fs.PathPermissionException("Failed"); + replChangeManagerMockedStatic.when(() -> ReplChangeManager.checksumFor(source, fs)).thenThrow(exception).thenReturn("dummy"); + when(fs.exists(same(source))).thenThrow(exception).thenReturn(true); + when(fs.delete(same(source), anyBoolean())).thenThrow(exception).thenReturn(true); + when(fs.mkdirs(same(source))).thenThrow(exception).thenReturn(true); + when(fs.rename(same(source), same(destination))).thenThrow(exception).thenReturn(true); + when(fs.getContentSummary(same(source))).thenThrow(exception).thenReturn(cs); + + CopyUtils copyUtils = new CopyUtils(UserGroupInformation.getCurrentUser().getUserName(), conf, fs); + CopyUtils copyUtilsSpy = Mockito.spy(copyUtils); + try { + copyUtilsSpy.exists(fs, source); + } catch (Exception e) { + assertEquals(exception.getClass(), e.getCause().getClass()); + } + Mockito.verify(fs, Mockito.times(1)).exists(source); + try { + copyUtils.delete(fs, source, true); + } catch (Exception e) { + assertEquals(exception.getClass(), e.getCause().getClass()); + } + Mockito.verify(fs, Mockito.times(1)).delete(source, true); + try { + copyUtils.mkdirs(fs, source); + } catch (Exception e) { + assertEquals(exception.getClass(), e.getCause().getClass()); + } + Mockito.verify(fs, Mockito.times(1)).mkdirs(source); + try { + copyUtils.rename(fs, source, destination); + } catch (Exception e) { + assertEquals(exception.getClass(), e.getCause().getClass()); + } + Mockito.verify(fs, Mockito.times(1)).rename(source, destination); + try { + copyUtilsSpy.getContentSummary(fs, source); + } catch (Exception e) { + assertEquals(exception.getClass(), e.getCause().getClass());; + } + Mockito.verify(fs, Mockito.times(1)).getContentSummary(source); + try { + copyUtilsSpy.checkSumFor(source, fs); + } catch (Exception e) { + assertEquals(exception.getClass(), e.getCause().getClass()); + } + Mockito.verify(copyUtilsSpy, Mockito.times(1)).checkSumFor(source, fs); } - Mockito.verify(fs, Mockito.times(1)).rename(source, destination); - try { - copyUtilsSpy.getContentSummary(fs, source); - } catch (Exception e) { - assertEquals(exception.getClass(), e.getCause().getClass());; - } - Mockito.verify(fs, Mockito.times(1)).getContentSummary(source); - try { - copyUtilsSpy.checkSumFor(source, fs); - } catch (Exception e) { - assertEquals(exception.getClass(), e.getCause().getClass()); - } - Mockito.verify(copyUtilsSpy, Mockito.times(1)).checkSumFor(source, fs); } @Test public void testRetryableFSCalls() throws Exception { - mockStatic(UserGroupInformation.class); - mockStatic(ReplChangeManager.class); - when(UserGroupInformation.getCurrentUser()).thenReturn(mock(UserGroupInformation.class)); - HiveConf conf = mock(HiveConf.class); - conf.set(HiveConf.ConfVars.REPL_RETRY_INTIAL_DELAY.varname, "1s"); - FileSystem fs = mock(FileSystem.class); - Path source = mock(Path.class); - Path destination = mock(Path.class); - ContentSummary cs = mock(ContentSummary.class); - - when(ReplChangeManager.checksumFor(source, fs)).thenThrow(new IOException("Failed")).thenReturn("dummy"); - when(fs.exists(same(source))).thenThrow(new IOException("Failed")).thenReturn(true); - when(fs.delete(same(source), anyBoolean())).thenThrow(new IOException("Failed")).thenReturn(true); - when(fs.mkdirs(same(source))).thenThrow(new IOException("Failed")).thenReturn(true); - when(fs.rename(same(source), same(destination))).thenThrow(new IOException("Failed")).thenReturn(true); - when(fs.getContentSummary(same(source))).thenThrow(new IOException("Failed")).thenReturn(cs); - - CopyUtils copyUtils = new CopyUtils(UserGroupInformation.getCurrentUser().getUserName(), conf, fs); - CopyUtils copyUtilsSpy = Mockito.spy(copyUtils); - assertEquals (true, copyUtilsSpy.exists(fs, source)); - Mockito.verify(fs, Mockito.times(2)).exists(source); - assertEquals (true, copyUtils.delete(fs, source, true)); - Mockito.verify(fs, Mockito.times(2)).delete(source, true); - assertEquals (true, copyUtils.mkdirs(fs, source)); - Mockito.verify(fs, Mockito.times(2)).mkdirs(source); - assertEquals (true, copyUtils.rename(fs, source, destination)); - Mockito.verify(fs, Mockito.times(2)).rename(source, destination); - assertEquals (cs, copyUtilsSpy.getContentSummary(fs, source)); - Mockito.verify(fs, Mockito.times(2)).getContentSummary(source); - assertEquals ("dummy", copyUtilsSpy.checkSumFor(source, fs)); + try ( + MockedStatic<UserGroupInformation> userGroupInformationMockedStatic = mockStatic(UserGroupInformation.class); + MockedStatic<ReplChangeManager> replChangeManagerMockedStatic = mockStatic(ReplChangeManager.class) + ) { Review Comment: Modified. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org