diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfigRedactor.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfigRedactor.java index 315b507cc6a49..a59db14e23572 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfigRedactor.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/conf/TestConfigRedactor.java @@ -18,7 +18,7 @@ package org.apache.hadoop.conf; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Arrays; import java.util.List; diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java index 177223dc08254..e79985063016a 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java @@ -19,13 +19,16 @@ import static org.apache.hadoop.test.LambdaTestUtils.intercept; import static org.apache.hadoop.test.PlatformAssumptions.assumeNotWindows; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -66,22 +69,21 @@ import org.apache.tools.tar.TarEntry; import org.apache.tools.tar.TarOutputStream; -import org.assertj.core.api.Assertions; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.api.io.TempDir; +import org.mockito.exceptions.misusing.MissingMethodInvocationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TestFileUtil { private static final Logger LOG = LoggerFactory.getLogger(TestFileUtil.class); - @Rule - public TemporaryFolder testFolder = new TemporaryFolder(); + @TempDir + private java.nio.file.Path testFolder; private static final String FILE = "x"; private static final String LINK = "y"; @@ -139,11 +141,11 @@ public class TestFileUtil { * file: part-r-00000, contents: "foo" * file: part-r-00001, contents: "bar" */ - @Before + @BeforeEach public void setup() throws IOException { - del = testFolder.newFolder("del"); - tmp = testFolder.newFolder("tmp"); - partitioned = testFolder.newFolder("partitioned"); + del = testFolder.resolve("del").toFile(); + tmp = testFolder.resolve("tmp").toFile(); + partitioned = testFolder.resolve("partitioned").toFile(); zlink = new File(del, "zlink"); @@ -161,6 +163,8 @@ public void setup() throws IOException { FileUtils.forceMkdir(dir1); FileUtils.forceMkdir(dir2); + FileUtils.forceMkdir(tmp); + FileUtils.forceMkdir(partitioned); Verify.createNewFile(new File(del, FILE)); File tmpFile = Verify.createNewFile(new File(tmp, FILE)); @@ -176,7 +180,7 @@ public void setup() throws IOException { // create a symlink to dir File linkDir = new File(del, "tmpDir"); FileUtil.symLink(tmp.toString(), linkDir.toString()); - Assert.assertEquals(5, Objects.requireNonNull(del.listFiles()).length); + assertEquals(5, Objects.requireNonNull(del.listFiles()).length); // create files in partitioned directories createFile(partitioned, "part-r-00000", "foo"); @@ -186,9 +190,19 @@ public void setup() throws IOException { FileUtil.symLink(del.toString(), dir1.toString() + "/cycle"); } - @After + @AfterEach public void tearDown() throws IOException { - testFolder.delete(); + if (Files.exists(testFolder)) { + Files.walk(testFolder) + .map(java.nio.file.Path::toFile) + .forEach(file -> { + if (file.isDirectory()) { + file.delete(); + } else { + file.delete(); + } + }); + } } /** @@ -209,62 +223,65 @@ private File createFile(File directory, String name, String contents) return newFile; } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testListFiles() throws IOException { //Test existing files case File[] files = FileUtil.listFiles(partitioned); - Assert.assertEquals(2, files.length); + assertEquals(2, files.length); //Test existing directory with no files case File newDir = new File(tmp.getPath(),"test"); Verify.mkdir(newDir); - Assert.assertTrue("Failed to create test dir", newDir.exists()); + assertTrue(newDir.exists(), "Failed to create test dir"); files = FileUtil.listFiles(newDir); - Assert.assertEquals(0, files.length); + assertEquals(0, files.length); assertTrue(newDir.delete()); - Assert.assertFalse("Failed to delete test dir", newDir.exists()); + assertFalse(newDir.exists(), "Failed to delete test dir"); //Test non-existing directory case, this throws //IOException try { files = FileUtil.listFiles(newDir); - Assert.fail("IOException expected on listFiles() for non-existent dir " + fail("IOException expected on listFiles() for non-existent dir " + newDir.toString()); } catch(IOException ioe) { //Expected an IOException } } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testListAPI() throws IOException { //Test existing files case String[] files = FileUtil.list(partitioned); - Assert.assertEquals("Unexpected number of pre-existing files", 2, files.length); + assertEquals(2, files.length, "Unexpected number of pre-existing files"); //Test existing directory with no files case File newDir = new File(tmp.getPath(),"test"); Verify.mkdir(newDir); - Assert.assertTrue("Failed to create test dir", newDir.exists()); + assertTrue(newDir.exists(), "Failed to create test dir"); files = FileUtil.list(newDir); - Assert.assertEquals("New directory unexpectedly contains files", 0, files.length); + assertEquals(0, files.length, "New directory unexpectedly contains files"); assertTrue(newDir.delete()); - Assert.assertFalse("Failed to delete test dir", newDir.exists()); + assertFalse(newDir.exists(), "Failed to delete test dir"); //Test non-existing directory case, this throws //IOException try { files = FileUtil.list(newDir); - Assert.fail("IOException expected on list() for non-existent dir " + fail("IOException expected on list() for non-existent dir " + newDir.toString()); } catch(IOException ioe) { //Expected an IOException } } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testFullyDelete() throws IOException { boolean ret = FileUtil.fullyDelete(del); - Assert.assertTrue(ret); + assertTrue(ret); Verify.notExists(del); validateTmpDir(); } @@ -275,14 +292,15 @@ public void testFullyDelete() throws IOException { * (b) symlink to dir only and not the dir pointed to by symlink. * @throws IOException */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testFullyDeleteSymlinks() throws IOException { File link = new File(del, LINK); assertDelListLength(5); // Since tmpDir is symlink to tmp, fullyDelete(tmpDir) should not // delete contents of tmp. See setupDirs for details. boolean ret = FileUtil.fullyDelete(link); - Assert.assertTrue(ret); + assertTrue(ret); Verify.notExists(link); assertDelListLength(4); validateTmpDir(); @@ -291,7 +309,7 @@ public void testFullyDeleteSymlinks() throws IOException { // Since tmpDir is symlink to tmp, fullyDelete(tmpDir) should not // delete contents of tmp. See setupDirs for details. ret = FileUtil.fullyDelete(linkDir); - Assert.assertTrue(ret); + assertTrue(ret); Verify.notExists(linkDir); assertDelListLength(3); validateTmpDir(); @@ -303,12 +321,13 @@ public void testFullyDeleteSymlinks() throws IOException { * (b) dangling symlink to directory properly * @throws IOException */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testFullyDeleteDanglingSymlinks() throws IOException { // delete the directory tmp to make tmpDir a dangling link to dir tmp and // to make y as a dangling link to file tmp/x boolean ret = FileUtil.fullyDelete(tmp); - Assert.assertTrue(ret); + assertTrue(ret); Verify.notExists(tmp); // dangling symlink to file @@ -317,7 +336,7 @@ public void testFullyDeleteDanglingSymlinks() throws IOException { // Even though 'y' is dangling symlink to file tmp/x, fullyDelete(y) // should delete 'y' properly. ret = FileUtil.fullyDelete(link); - Assert.assertTrue(ret); + assertTrue(ret); assertDelListLength(4); // dangling symlink to directory @@ -325,22 +344,23 @@ public void testFullyDeleteDanglingSymlinks() throws IOException { // Even though tmpDir is dangling symlink to tmp, fullyDelete(tmpDir) should // delete tmpDir properly. ret = FileUtil.fullyDelete(linkDir); - Assert.assertTrue(ret); + assertTrue(ret); assertDelListLength(3); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testFullyDeleteContents() throws IOException { boolean ret = FileUtil.fullyDeleteContents(del); - Assert.assertTrue(ret); + assertTrue(ret); Verify.exists(del); - Assert.assertEquals(0, Objects.requireNonNull(del.listFiles()).length); + assertEquals(0, Objects.requireNonNull(del.listFiles()).length); validateTmpDir(); } private void validateTmpDir() { Verify.exists(tmp); - Assert.assertEquals(1, Objects.requireNonNull(tmp.listFiles()).length); + assertEquals(1, Objects.requireNonNull(tmp.listFiles()).length); Verify.exists(new File(tmp, FILE)); } @@ -408,28 +428,23 @@ private void validateAndSetWritablePermissions( grantPermissions(xSubDir); grantPermissions(xSubSubDir); - Assert.assertFalse("The return value should have been false.", ret); - Assert.assertTrue("The file file1 should not have been deleted.", - new File(del, FILE_1_NAME).exists()); - - Assert.assertEquals( - "The directory xSubDir *should* not have been deleted.", - expectedRevokedPermissionDirsExist, xSubDir.exists()); - Assert.assertEquals("The file file2 *should* not have been deleted.", - expectedRevokedPermissionDirsExist, file2.exists()); - Assert.assertEquals( - "The directory xSubSubDir *should* not have been deleted.", - expectedRevokedPermissionDirsExist, xSubSubDir.exists()); - Assert.assertEquals("The file file22 *should* not have been deleted.", - expectedRevokedPermissionDirsExist, file22.exists()); - - Assert.assertFalse("The directory ySubDir should have been deleted.", - ySubDir.exists()); - Assert.assertFalse("The link zlink should have been deleted.", - zlink.exists()); + assertFalse(ret, "The return value should have been false."); + assertTrue(new File(del, FILE_1_NAME).exists(), + "The file file1 should not have been deleted."); + assertEquals(expectedRevokedPermissionDirsExist, xSubDir.exists(), + "The directory xSubDir *should* not have been deleted."); + assertEquals(expectedRevokedPermissionDirsExist, file2.exists(), + "The file file2 *should* not have been deleted."); + assertEquals(expectedRevokedPermissionDirsExist, xSubSubDir.exists(), + "The directory xSubSubDir *should* not have been deleted."); + assertEquals(expectedRevokedPermissionDirsExist, file22.exists(), + "The file file22 *should* not have been deleted."); + assertFalse(ySubDir.exists(), "The directory ySubDir should have been deleted."); + assertFalse(zlink.exists(), "The link zlink should have been deleted."); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testFailFullyDelete() throws IOException { // Windows Dir.setWritable(false) does not work for directories assumeNotWindows(); @@ -439,7 +454,8 @@ public void testFailFullyDelete() throws IOException { validateAndSetWritablePermissions(true, ret); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testFailFullyDeleteGrantPermissions() throws IOException { setupDirsAndNonWritablePermissions(); boolean ret = FileUtil.fullyDelete(new MyFile(del), true); @@ -452,7 +468,8 @@ public void testFailFullyDeleteGrantPermissions() throws IOException { * Tests if fullyDelete deletes symlink's content when deleting unremovable dir symlink. * @throws IOException */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testFailFullyDeleteDirSymlinks() throws IOException { File linkDir = new File(del, "tmpDir"); FileUtil.setWritable(del, false); @@ -460,7 +477,7 @@ public void testFailFullyDeleteDirSymlinks() throws IOException { // delete contents of tmp. See setupDirs for details. boolean ret = FileUtil.fullyDelete(linkDir); // fail symlink deletion - Assert.assertFalse(ret); + assertFalse(ret); Verify.exists(linkDir); assertDelListLength(5); // tmp dir should exist @@ -469,7 +486,7 @@ public void testFailFullyDeleteDirSymlinks() throws IOException { FileUtil.setWritable(del, true); ret = FileUtil.fullyDelete(linkDir); // success symlink deletion - Assert.assertTrue(ret); + assertTrue(ret); Verify.notExists(linkDir); assertDelListLength(4); // tmp dir should exist @@ -482,7 +499,7 @@ public void testFailFullyDeleteDirSymlinks() throws IOException { * @param expectedLength The expected length of the {@link TestFileUtil#del}. */ private void assertDelListLength(int expectedLength) { - Assertions.assertThat(del.list()).describedAs("del list").isNotNull().hasSize(expectedLength); + assertThat(del.list()).describedAs("del list").isNotNull().hasSize(expectedLength); } /** @@ -497,7 +514,7 @@ public static class Verify { * @throws IOException As per {@link File#createNewFile()}. */ public static File createNewFile(File file) throws IOException { - assertTrue("Unable to create new file " + file, file.createNewFile()); + assertTrue(file.createNewFile(), "Unable to create new file " + file); return file; } @@ -508,7 +525,7 @@ public static File createNewFile(File file) throws IOException { * @return The result of {@link File#mkdir()}. */ public static File mkdir(File file) { - assertTrue("Unable to mkdir for " + file, file.mkdir()); + assertTrue(file.mkdir(), "Unable to mkdir for " + file); return file; } @@ -519,7 +536,7 @@ public static File mkdir(File file) { * @return The result of {@link File#mkdirs()}. */ public static File mkdirs(File file) { - assertTrue("Unable to mkdirs for " + file, file.mkdirs()); + assertTrue(file.mkdirs(), "Unable to mkdirs for " + file); return file; } @@ -530,7 +547,7 @@ public static File mkdirs(File file) { * @return The result of {@link File#delete()}. */ public static File delete(File file) { - assertTrue("Unable to delete " + file, file.delete()); + assertTrue(file.delete(), "Unable to delete " + file); return file; } @@ -541,7 +558,7 @@ public static File delete(File file) { * @return The result of {@link File#exists()}. */ public static File exists(File file) { - assertTrue("Expected file " + file + " doesn't exist", file.exists()); + assertTrue(file.exists(), "Expected file " + file + " doesn't exist"); return file; } @@ -553,7 +570,7 @@ public static File exists(File file) { * @return The negation of the result of {@link File#exists()}. */ public static File notExists(File file) { - assertFalse("Expected file " + file + " must not exist", file.exists()); + assertFalse(file.exists(), "Expected file " + file + " must not exist"); return file; } } @@ -619,7 +636,8 @@ public File[] listFiles() { } } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testFailFullyDeleteContents() throws IOException { // Windows Dir.setWritable(false) does not work for directories assumeNotWindows(); @@ -629,7 +647,8 @@ public void testFailFullyDeleteContents() throws IOException { validateAndSetWritablePermissions(true, ret); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testFailFullyDeleteContentsGrantPermissions() throws IOException { setupDirsAndNonWritablePermissions(); boolean ret = FileUtil.fullyDeleteContents(new MyFile(del), true); @@ -642,13 +661,14 @@ public void testFailFullyDeleteContentsGrantPermissions() throws IOException { * and that directory sizes are not added to the final calculated size * @throws IOException */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testGetDU() throws Exception { - long du = FileUtil.getDU(testFolder.getRoot()); + long du = FileUtil.getDU(testFolder.toFile()); // Only two files (in partitioned). Each has 3 characters + system-specific // line separator. final long expected = 2 * (3 + System.getProperty("line.separator").length()); - Assert.assertEquals(expected, du); + assertEquals(expected, du); // target file does not exist: final File doesNotExist = new File(tmp, "QuickBrownFoxJumpsOverTheLazyDog"); @@ -691,7 +711,8 @@ public void testGetDU() throws Exception { } } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testUnTar() throws Exception { // make a simple tar: final File simpleTar = new File(del, FILE); @@ -718,7 +739,8 @@ public void testUnTar() throws Exception { LambdaTestUtils.intercept(IOException.class, () -> FileUtil.unTar(simpleTar, regularFile)); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testReplaceFile() throws IOException { // src exists, and target does not exist: final File srcFile = Verify.createNewFile(new File(tmp, "src")); @@ -754,7 +776,8 @@ public void testReplaceFile() throws IOException { Verify.exists(obstacle); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testCreateLocalTempFile() throws IOException { final File baseFile = new File(tmp, "base"); File tmp1 = FileUtil.createLocalTempFile(baseFile, "foo", false); @@ -769,7 +792,8 @@ public void testCreateLocalTempFile() throws IOException { assertTrue(!tmp1.exists() && !tmp2.exists()); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testUnZip() throws Exception { // make sa simple zip final File simpleZip = new File(del, FILE); @@ -811,40 +835,41 @@ public void testUnZip() throws Exception { assertTrue(foo6.exists()); assertEquals(12, foo0.length()); // tests whether file foo_0 has executable permissions - assertTrue("file lacks execute permissions", foo0.canExecute()); - assertFalse("file has write permissions", foo0.canWrite()); - assertFalse("file has read permissions", foo0.canRead()); + assertTrue(foo0.canExecute(), "file lacks execute permissions"); + assertFalse(foo0.canWrite(), "file has write permissions"); + assertFalse(foo0.canRead(), "file has read permissions"); // tests whether file foo_1 has writable permissions - assertFalse("file has execute permissions", foo1.canExecute()); - assertTrue("file lacks write permissions", foo1.canWrite()); - assertFalse("file has read permissions", foo1.canRead()); + assertFalse(foo1.canExecute(), "file has execute permissions"); + assertTrue(foo1.canWrite(), "file lacks write permissions"); + assertFalse(foo1.canRead(), "file has read permissions"); // tests whether file foo_2 has executable and writable permissions - assertTrue("file lacks execute permissions", foo2.canExecute()); - assertTrue("file lacks write permissions", foo2.canWrite()); - assertFalse("file has read permissions", foo2.canRead()); + assertTrue(foo2.canExecute(), "file lacks execute permissions"); + assertTrue(foo2.canWrite(), "file lacks write permissions"); + assertFalse(foo2.canRead(), "file has read permissions"); // tests whether file foo_3 has readable permissions - assertFalse("file has execute permissions", foo3.canExecute()); - assertFalse("file has write permissions", foo3.canWrite()); - assertTrue("file lacks read permissions", foo3.canRead()); + assertFalse(foo3.canExecute(), "file has execute permissions"); + assertFalse(foo3.canWrite(), "file has write permissions"); + assertTrue(foo3.canRead(), "file lacks read permissions"); // tests whether file foo_4 has readable and executable permissions - assertTrue("file lacks execute permissions", foo4.canExecute()); - assertFalse("file has write permissions", foo4.canWrite()); - assertTrue("file lacks read permissions", foo4.canRead()); + assertTrue(foo4.canExecute(), "file lacks execute permissions"); + assertFalse(foo4.canWrite(), "file has write permissions"); + assertTrue(foo4.canRead(), "file lacks read permissions"); // tests whether file foo_5 has readable and writable permissions - assertFalse("file has execute permissions", foo5.canExecute()); - assertTrue("file lacks write permissions", foo5.canWrite()); - assertTrue("file lacks read permissions", foo5.canRead()); + assertFalse(foo5.canExecute(), "file has execute permissions"); + assertTrue(foo5.canWrite(), "file lacks write permissions"); + assertTrue(foo5.canRead(), "file lacks read permissions"); // tests whether file foo_6 has readable, writable and executable permissions - assertTrue("file lacks execute permissions", foo6.canExecute()); - assertTrue("file lacks write permissions", foo6.canWrite()); - assertTrue("file lacks read permissions", foo6.canRead()); + assertTrue(foo6.canExecute(), "file lacks execute permissions"); + assertTrue(foo6.canWrite(), "file lacks write permissions"); + assertTrue(foo6.canRead(), "file lacks read permissions"); final File regularFile = Verify.createNewFile(new File(tmp, "QuickBrownFoxJumpsOverTheLazyDog")); LambdaTestUtils.intercept(IOException.class, () -> FileUtil.unZip(simpleZip, regularFile)); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testUnZip2() throws IOException { // make a simple zip final File simpleZip = new File(del, FILE); @@ -871,7 +896,8 @@ public void testUnZip2() throws IOException { } } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) /* * Test method copy(FileSystem srcFS, Path src, File dst, boolean deleteSource, Configuration conf) */ @@ -919,7 +945,8 @@ public void testCopy5() throws IOException { Verify.notExists(partitioned); // should be deleted } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testStat2Paths1() { assertNull(FileUtil.stat2Paths(null)); @@ -939,7 +966,8 @@ public void testStat2Paths1() { assertEquals(paths[1], path2); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testStat2Paths2() { Path defaultPath = new Path("file://default"); Path[] paths = FileUtil.stat2Paths(null, defaultPath); @@ -963,7 +991,8 @@ public void testStat2Paths2() { assertEquals(paths[1], path2); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testSymlink() throws Exception { byte[] data = "testSymLink".getBytes(); @@ -979,8 +1008,8 @@ public void testSymlink() throws Exception { FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath()); //ensure that symlink length is correctly reported by Java - Assert.assertEquals(data.length, file.length()); - Assert.assertEquals(data.length, link.length()); + assertEquals(data.length, file.length()); + assertEquals(data.length, link.length()); //ensure that we can read from link. FileInputStream in = new FileInputStream(link); @@ -989,13 +1018,14 @@ public void testSymlink() throws Exception { len++; } in.close(); - Assert.assertEquals(data.length, len); + assertEquals(data.length, len); } /** * Test that rename on a symlink works as expected. */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testSymlinkRenameTo() throws Exception { File file = new File(del, FILE); file.createNewFile(); @@ -1010,7 +1040,7 @@ public void testSymlinkRenameTo() throws Exception { File link2 = new File(del, "_link2"); // Rename the symlink - Assert.assertTrue(link.renameTo(link2)); + assertTrue(link.renameTo(link2)); // Make sure the file still exists // (NOTE: this would fail on Java6 on Windows if we didn't @@ -1024,7 +1054,8 @@ public void testSymlinkRenameTo() throws Exception { /** * Test that deletion of a symlink works as expected. */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testSymlinkDelete() throws Exception { File file = new File(del, FILE); file.createNewFile(); @@ -1045,7 +1076,8 @@ public void testSymlinkDelete() throws Exception { /** * Test that length on a symlink works as expected. */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testSymlinkLength() throws Exception { byte[] data = "testSymLinkData".getBytes(); @@ -1057,19 +1089,19 @@ public void testSymlinkLength() throws Exception { os.write(data); os.close(); - Assert.assertEquals(0, link.length()); + assertEquals(0, link.length()); // create the symlink FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath()); // ensure that File#length returns the target file and link size - Assert.assertEquals(data.length, file.length()); - Assert.assertEquals(data.length, link.length()); + assertEquals(data.length, file.length()); + assertEquals(data.length, link.length()); Verify.delete(file); Verify.notExists(file); - Assert.assertEquals(0, link.length()); + assertEquals(0, link.length()); Verify.delete(link); Verify.notExists(link); @@ -1089,17 +1121,17 @@ public void testSymlinkWithNullInput() throws IOException { // Create the same symbolic link // The operation should fail and returns 1 int result = FileUtil.symLink(null, null); - Assert.assertEquals(1, result); + assertEquals(1, result); // Create the same symbolic link // The operation should fail and returns 1 result = FileUtil.symLink(file.getAbsolutePath(), null); - Assert.assertEquals(1, result); + assertEquals(1, result); // Create the same symbolic link // The operation should fail and returns 1 result = FileUtil.symLink(null, link.getAbsolutePath()); - Assert.assertEquals(1, result); + assertEquals(1, result); } /** @@ -1118,13 +1150,13 @@ public void testSymlinkFileAlreadyExists() throws IOException { int result1 = FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath()); - Assert.assertEquals(0, result1); + assertEquals(0, result1); // Create the same symbolic link // The operation should fail and returns 1 result1 = FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath()); - Assert.assertEquals(1, result1); + assertEquals(1, result1); } /** @@ -1145,7 +1177,7 @@ public void testSymlinkSameFile() throws IOException { int result = FileUtil.symLink(file.getAbsolutePath(), file.getAbsolutePath()); - Assert.assertEquals(0, result); + assertEquals(0, result); } /** @@ -1166,13 +1198,13 @@ public void testSymlink2DifferentFile() throws IOException { int result = FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath()); - Assert.assertEquals(0, result); + assertEquals(0, result); // The operation should fail and returns 1 result = FileUtil.symLink(fileSecond.getAbsolutePath(), link.getAbsolutePath()); - Assert.assertEquals(1, result); + assertEquals(1, result); } /** @@ -1193,13 +1225,13 @@ public void testSymlink2DifferentLinks() throws IOException { int result = FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath()); - Assert.assertEquals(0, result); + assertEquals(0, result); // The operation should succeed result = FileUtil.symLink(file.getAbsolutePath(), linkSecond.getAbsolutePath()); - Assert.assertEquals(0, result); + assertEquals(0, result); } private void doUntarAndVerify(File tarFile, File untarDir) @@ -1212,24 +1244,25 @@ private void doUntarAndVerify(File tarFile, File untarDir) String parentDir = untarDir.getCanonicalPath() + Path.SEPARATOR + "name"; File testFile = new File(parentDir + Path.SEPARATOR + "version"); Verify.exists(testFile); - Assert.assertTrue(testFile.length() == 0); + assertTrue(testFile.length() == 0); String imageDir = parentDir + Path.SEPARATOR + "image"; testFile = new File(imageDir + Path.SEPARATOR + "fsimage"); Verify.exists(testFile); - Assert.assertTrue(testFile.length() == 157); + assertTrue(testFile.length() == 157); String currentDir = parentDir + Path.SEPARATOR + "current"; testFile = new File(currentDir + Path.SEPARATOR + "fsimage"); Verify.exists(testFile); - Assert.assertTrue(testFile.length() == 4331); + assertTrue(testFile.length() == 4331); testFile = new File(currentDir + Path.SEPARATOR + "edits"); Verify.exists(testFile); - Assert.assertTrue(testFile.length() == 1033); + assertTrue(testFile.length() == 1033); testFile = new File(currentDir + Path.SEPARATOR + "fstime"); Verify.exists(testFile); - Assert.assertTrue(testFile.length() == 8); + assertTrue(testFile.length() == 8); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testUntar() throws IOException { String tarGzFileName = System.getProperty("test.cache.data", "target/test/cache") + "/test-untar.tgz"; @@ -1247,7 +1280,8 @@ public void testUntar() throws IOException { * This will test different codepaths on Windows from unix, * but both MUST throw an IOE of some kind. */ - @Test(timeout = 30000) + @Test + @Timeout(value = 30) public void testUntarMissingFile() throws Throwable { File dataDir = GenericTestUtils.getTestDir(); File tarFile = new File(dataDir, "missing; true"); @@ -1262,7 +1296,8 @@ public void testUntarMissingFile() throws Throwable { * This is how {@code FileUtil.unTar(File, File} * will behave on Windows, */ - @Test(timeout = 30000) + @Test + @Timeout(value = 30) public void testUntarMissingFileThroughJava() throws Throwable { File dataDir = GenericTestUtils.getTestDir(); File tarFile = new File(dataDir, "missing; true"); @@ -1274,15 +1309,16 @@ public void testUntarMissingFileThroughJava() throws Throwable { FileUtil.unTarUsingJava(tarFile, untarDir, false)); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testCreateJarWithClassPath() throws Exception { // create files expected to match a wildcard List wildcardMatches = Arrays.asList(new File(tmp, "wildcard1.jar"), new File(tmp, "wildcard2.jar"), new File(tmp, "wildcard3.JAR"), new File(tmp, "wildcard4.JAR")); for (File wildcardMatch: wildcardMatches) { - Assert.assertTrue("failure creating file: " + wildcardMatch, - wildcardMatch.createNewFile()); + assertTrue(wildcardMatch.createNewFile(), + "failure creating file: " + wildcardMatch); } // create non-jar files, which we expect to not be included in the classpath @@ -1297,22 +1333,24 @@ public void testCreateJarWithClassPath() throws Exception { List classPaths = Arrays.asList("", "cp1.jar", "cp2.jar", wildcardPath, "cp3.jar", nonExistentSubdir); String inputClassPath = StringUtils.join(File.pathSeparator, classPaths); - String[] jarCp = FileUtil.createJarWithClassPath(inputClassPath + File.pathSeparator + "unexpandedwildcard/*", - new Path(tmp.getCanonicalPath()), System.getenv()); + String[] jarCp = FileUtil.createJarWithClassPath(inputClassPath + + File.pathSeparator + "unexpandedwildcard/*", + new Path(tmp.getCanonicalPath()), System.getenv()); String classPathJar = jarCp[0]; - assertNotEquals("Unexpanded wildcard was not placed in extra classpath", jarCp[1].indexOf("unexpanded"), -1); + assertNotEquals(jarCp[1].indexOf("unexpanded"), -1, + "Unexpanded wildcard was not placed in extra classpath"); // verify classpath by reading manifest from jar file JarFile jarFile = null; try { jarFile = new JarFile(classPathJar); Manifest jarManifest = jarFile.getManifest(); - Assert.assertNotNull(jarManifest); + assertNotNull(jarManifest); Attributes mainAttributes = jarManifest.getMainAttributes(); - Assert.assertNotNull(mainAttributes); - Assert.assertTrue(mainAttributes.containsKey(Attributes.Name.CLASS_PATH)); + assertNotNull(mainAttributes); + assertTrue(mainAttributes.containsKey(Attributes.Name.CLASS_PATH)); String classPathAttr = mainAttributes.getValue(Attributes.Name.CLASS_PATH); - Assert.assertNotNull(classPathAttr); + assertNotNull(classPathAttr); List expectedClassPaths = new ArrayList(); for (String classPath: classPaths) { if (classPath.length() == 0) { @@ -1346,7 +1384,7 @@ public void testCreateJarWithClassPath() throws Exception { List actualClassPaths = Arrays.asList(classPathAttr.split(" ")); Collections.sort(expectedClassPaths); Collections.sort(actualClassPaths); - Assert.assertEquals(expectedClassPaths, actualClassPaths); + assertEquals(expectedClassPaths, actualClassPaths); } finally { if (jarFile != null) { try { @@ -1361,8 +1399,8 @@ public void testCreateJarWithClassPath() throws Exception { @Test public void testGetJarsInDirectory() throws Exception { List jars = FileUtil.getJarsInDirectory("/foo/bar/bogus/"); - assertTrue("no jars should be returned for a bogus path", - jars.isEmpty()); + assertTrue(jars.isEmpty(), + "no jars should be returned for a bogus path"); // create jar files to be returned @@ -1370,7 +1408,7 @@ public void testGetJarsInDirectory() throws Exception { File jar2 = new File(tmp, "wildcard2.JAR"); List matches = Arrays.asList(jar1, jar2); for (File match: matches) { - assertTrue("failure creating file: " + match, match.createNewFile()); + assertTrue(match.createNewFile(), "failure creating file: " + match); } // create non-jar files, which we expect to not be included in the result @@ -1381,16 +1419,16 @@ public void testGetJarsInDirectory() throws Exception { // pass in the directory String directory = tmp.getCanonicalPath(); jars = FileUtil.getJarsInDirectory(directory); - assertEquals("there should be 2 jars", 2, jars.size()); + assertEquals(2, jars.size(), "there should be 2 jars"); for (Path jar: jars) { URL url = jar.toUri().toURL(); - assertTrue("the jar should match either of the jars", - url.equals(jar1.getCanonicalFile().toURI().toURL()) || - url.equals(jar2.getCanonicalFile().toURI().toURL())); + assertTrue(url.equals(jar1.getCanonicalFile().toURI().toURL()) || + url.equals(jar2.getCanonicalFile().toURI().toURL()), + "the jar should match either of the jars"); } } - @Ignore + @Disabled public void setupCompareFs() { // Set up Strings String host1 = "1.2.3.4"; @@ -1434,7 +1472,7 @@ public void setupCompareFs() { when(InetAddress.getByName(uris3)).thenReturn(inet3); when(InetAddress.getByName(uris4)).thenReturn(inet4); when(InetAddress.getByName(uris5)).thenReturn(inet5); - } catch (UnknownHostException ignored) { + } catch (UnknownHostException | MissingMethodInvocationException ignored) { } fs1 = mock(FileSystem.class); @@ -1468,7 +1506,8 @@ public void testCompareFsDirectories() throws Exception { assertFalse(FileUtil.compareFs(fs1, fs6)); } - @Test(timeout = 8000) + @Test + @Timeout(value = 8) public void testCreateSymbolicLinkUsingJava() throws IOException { final File simpleTar = new File(del, FILE); OutputStream os = new FileOutputStream(simpleTar); @@ -1502,39 +1541,40 @@ public void testCreateSymbolicLinkUsingJava() throws IOException { } } - @Test(expected = IOException.class) + @Test public void testCreateArbitrarySymlinkUsingJava() throws IOException { - final File simpleTar = new File(del, FILE); - OutputStream os = new FileOutputStream(simpleTar); - - File rootDir = new File("tmp"); - try (TarArchiveOutputStream tos = new TarArchiveOutputStream(os)) { - tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); - - // Create arbitrary dir - File arbitraryDir = new File(rootDir, "arbitrary-dir/"); - Verify.mkdirs(arbitraryDir); - - // We will tar from the tar-root lineage - File tarRoot = new File(rootDir, "tar-root/"); - File symlinkRoot = new File(tarRoot, "dir1/"); - Verify.mkdirs(symlinkRoot); - - // Create Symbolic Link to an arbitrary dir - java.nio.file.Path symLink = Paths.get(symlinkRoot.getPath(), "sl"); - Files.createSymbolicLink(symLink, arbitraryDir.toPath().toAbsolutePath()); - - // Put entries in tar file - putEntriesInTar(tos, tarRoot); - putEntriesInTar(tos, new File(symLink.toFile(), "dir-outside-tar-root/")); - tos.close(); - - // Untar using Java - File untarFile = new File(rootDir, "extracted"); - FileUtil.unTarUsingJava(simpleTar, untarFile, false); - } finally { - FileUtils.deleteDirectory(rootDir); - } + assertThrows(IOException.class, () -> { + final File simpleTar = new File(del, FILE); + OutputStream os = new FileOutputStream(simpleTar); + File rootDir = new File("tmp"); + try (TarArchiveOutputStream tos = new TarArchiveOutputStream(os)) { + tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); + + // Create arbitrary dir + File arbitraryDir = new File(rootDir, "arbitrary-dir/"); + Verify.mkdirs(arbitraryDir); + + // We will tar from the tar-root lineage + File tarRoot = new File(rootDir, "tar-root/"); + File symlinkRoot = new File(tarRoot, "dir1/"); + Verify.mkdirs(symlinkRoot); + + // Create Symbolic Link to an arbitrary dir + java.nio.file.Path symLink = Paths.get(symlinkRoot.getPath(), "sl"); + Files.createSymbolicLink(symLink, arbitraryDir.toPath().toAbsolutePath()); + + // Put entries in tar file + putEntriesInTar(tos, tarRoot); + putEntriesInTar(tos, new File(symLink.toFile(), "dir-outside-tar-root/")); + tos.close(); + + // Untar using Java + File untarFile = new File(rootDir, "extracted"); + FileUtil.unTarUsingJava(simpleTar, untarFile, false); + } finally { + FileUtils.deleteDirectory(rootDir); + } + }); } private void putEntriesInTar(TarArchiveOutputStream tos, File f) @@ -1578,7 +1618,7 @@ private void putEntriesInTar(TarArchiveOutputStream tos, File f) @Test public void testReadSymlinkWithNullInput() { String result = FileUtil.readLink(null); - Assert.assertEquals("", result); + assertEquals("", result); } /** @@ -1595,7 +1635,7 @@ public void testReadSymlink() throws IOException { FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath()); String result = FileUtil.readLink(link); - Assert.assertEquals(file.getAbsolutePath(), result); + assertEquals(file.getAbsolutePath(), result); } @Test @@ -1626,7 +1666,7 @@ public void testReadSymlinkWithAFileAsInput() throws IOException { File file = new File(del, FILE); String result = FileUtil.readLink(file); - Assert.assertEquals("", result); + assertEquals("", result); Verify.delete(file); } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java index 4204faaada332..c0406c43eeb3b 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java @@ -18,8 +18,8 @@ package org.apache.hadoop.fs; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -40,10 +40,11 @@ import static org.apache.hadoop.test.PlatformAssumptions.assumeNotWindows; import static org.apache.hadoop.test.PlatformAssumptions.assumeWindows; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; /** * Test Hadoop Filesystem Paths. @@ -76,7 +77,8 @@ public static String mergeStatuses(FileStatus statuses[]) { return mergeStatuses(paths); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testToString() { toStringTest("/"); toStringTest("/foo"); @@ -109,7 +111,8 @@ private void toStringTest(String pathString) { assertEquals(pathString, new Path(pathString).toString()); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testNormalize() throws URISyntaxException { assertEquals("", new Path(".").toString()); assertEquals("..", new Path("..").toString()); @@ -133,7 +136,8 @@ public void testNormalize() throws URISyntaxException { } } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testIsAbsolute() { assertTrue(new Path("/").isAbsolute()); assertTrue(new Path("/foo").isAbsolute()); @@ -146,7 +150,8 @@ public void testIsAbsolute() { } } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testParent() { assertEquals(new Path("/foo"), new Path("/foo/bar").getParent()); assertEquals(new Path("foo"), new Path("foo/bar").getParent()); @@ -157,7 +162,8 @@ public void testParent() { } } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testChild() { assertEquals(new Path("."), new Path(".", ".")); assertEquals(new Path("/"), new Path("/", ".")); @@ -177,7 +183,8 @@ public void testChild() { } } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testPathThreeArgContructor() { assertEquals(new Path("foo"), new Path(null, null, "foo")); assertEquals(new Path("scheme:///foo"), new Path("scheme", null, "/foo")); @@ -213,12 +220,14 @@ public void testPathThreeArgContructor() { } } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testEquals() { assertFalse(new Path("/").equals(new Path("/foo"))); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testDots() { // Test Path(String) assertEquals(new Path("/foo/bar/baz").toString(), "/foo/bar/baz"); @@ -257,7 +266,8 @@ public void testDots() { } /** Test that Windows paths are correctly handled */ - @Test (timeout = 5000) + @Test + @Timeout(value = 5) public void testWindowsPaths() throws URISyntaxException, IOException { assumeWindows(); @@ -268,7 +278,8 @@ public void testWindowsPaths() throws URISyntaxException, IOException { } /** Test invalid paths on Windows are correctly rejected */ - @Test (timeout = 5000) + @Test + @Timeout(value = 5) public void testInvalidWindowsPaths() throws URISyntaxException, IOException { assumeWindows(); @@ -286,20 +297,23 @@ public void testInvalidWindowsPaths() throws URISyntaxException, IOException { } /** Test Path objects created from other Path objects */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testChildParentResolution() throws URISyntaxException, IOException { Path parent = new Path("foo1://bar1/baz1"); Path child = new Path("foo2://bar2/baz2"); assertEquals(child, new Path(parent, child)); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testScheme() throws java.io.IOException { assertEquals("foo:/bar", new Path("foo:/","/bar").toString()); assertEquals("foo://bar/baz", new Path("foo://bar/","/baz").toString()); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testURI() throws URISyntaxException, IOException { URI uri = new URI("file:///bar#baz"); Path path = new Path(uri); @@ -322,18 +336,16 @@ public void testURI() throws URISyntaxException, IOException { } /** Test URIs created from Path objects */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testPathToUriConversion() throws URISyntaxException, IOException { // Path differs from URI in that it ignores the query part.. - assertEquals("? mark char in to URI", - new URI(null, null, "/foo?bar", null, null), - new Path("/foo?bar").toUri()); - assertEquals("escape slashes chars in to URI", - new URI(null, null, "/foo\"bar", null, null), - new Path("/foo\"bar").toUri()); - assertEquals("spaces in chars to URI", - new URI(null, null, "/foo bar", null, null), - new Path("/foo bar").toUri()); + assertEquals(new URI(null, null, "/foo?bar", null, null), + new Path("/foo?bar").toUri(), "? mark char in to URI"); + assertEquals(new URI(null, null, "/foo\"bar", null, null), + new Path("/foo\"bar").toUri(), "escape slashes chars in to URI"); + assertEquals(new URI(null, null, "/foo bar", null, null), + new Path("/foo bar").toUri(), "spaces in chars to URI"); // therefore "foo?bar" is a valid Path, so a URI created from a Path // has path "foo?bar" where in a straight URI the path part is just "foo" assertEquals("/foo?bar", @@ -350,7 +362,8 @@ public void testPathToUriConversion() throws URISyntaxException, IOException { } /** Test reserved characters in URIs (and therefore Paths) */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testReservedCharacters() throws URISyntaxException, IOException { // URI encodes the path assertEquals("/foo%20bar", @@ -380,7 +393,8 @@ public void testReservedCharacters() throws URISyntaxException, IOException { toURL().getPath()); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testMakeQualified() throws URISyntaxException { URI defaultUri = new URI("hdfs://host1/dir1"); URI wd = new URI("hdfs://host2/dir2"); @@ -394,7 +408,8 @@ public void testMakeQualified() throws URISyntaxException { new Path("file").makeQualified(defaultUri, new Path(wd))); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testGetName() { assertEquals("", new Path("/").getName()); assertEquals("foo", new Path("foo").getName()); @@ -404,7 +419,8 @@ public void testGetName() { assertEquals("bar", new Path("hdfs://host/foo/bar").getName()); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testAvroReflect() throws Exception { // Avro expects explicitely stated, trusted packages used for (de-)serialization System.setProperty(ConfigConstants.CONFIG_AVRO_SERIALIZABLE_PACKAGES, "org.apache.hadoop.fs"); @@ -413,7 +429,8 @@ public void testAvroReflect() throws Exception { "{\"type\":\"string\",\"java-class\":\"org.apache.hadoop.fs.Path\"}"); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testGlobEscapeStatus() throws Exception { // This test is not meaningful on Windows where * is disallowed in file name. assumeNotWindows(); @@ -472,7 +489,8 @@ public void testGlobEscapeStatus() throws Exception { assertEquals(new Path(testRoot, "*/f"), stats[0].getPath()); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testMergePaths() { assertEquals(new Path("/foo/bar"), Path.mergePaths(new Path("/foo"), @@ -506,7 +524,8 @@ public void testMergePaths() { new Path("file://fileauthority/bar"))); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testIsWindowsAbsolutePath() { assumeWindows(); assertTrue(Path.isWindowsAbsolutePath("C:\\test", false)); @@ -518,7 +537,8 @@ public void testIsWindowsAbsolutePath() { assertFalse(Path.isWindowsAbsolutePath("/C:test", true)); } - @Test(timeout = 30000) + @Test + @Timeout(value = 30) public void testSerDeser() throws Throwable { Path source = new Path("hdfs://localhost:4040/scratch"); ByteArrayOutputStream baos = new ByteArrayOutputStream(256); @@ -528,15 +548,16 @@ public void testSerDeser() throws Throwable { ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); try (ObjectInputStream ois = new ObjectInputStream(bais)) { Path deser = (Path) ois.readObject(); - Assert.assertEquals(source, deser); + assertEquals(source, deser); } } - @Test(timeout = 30000) + @Test + @Timeout(value = 30) public void testSuffixFromRoot() { Path root = new Path("/"); - Assert.assertNull(root.getParent()); - Assert.assertEquals(new Path("/bar"), root.suffix("bar")); + assertNull(root.getParent()); + assertEquals(new Path("/bar"), root.suffix("bar")); } } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestStat.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestStat.java index 0e518e28851dd..6f64cba460695 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestStat.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestStat.java @@ -17,19 +17,20 @@ */ package org.apache.hadoop.fs; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assumptions.assumeTrue; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.StringReader; import org.apache.hadoop.conf.Configuration; -import org.junit.Assume; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; public class TestStat extends FileSystemTestHelper { static { @@ -37,7 +38,7 @@ public class TestStat extends FileSystemTestHelper { } private static Stat stat; - @BeforeClass + @BeforeAll public static void setup() throws Exception { stat = new Stat(new Path("/dummypath"), 4096l, false, FileSystem.get(new Configuration())); @@ -95,7 +96,8 @@ void test() throws Exception { } } - @Test(timeout=10000) + @Test + @Timeout(value = 10) public void testStatLinux() throws Exception { String[] symlinks = new String[] { "6,symbolic link,1373584236,1373584236,777,andrew,andrew,`link' -> `target'", @@ -110,7 +112,8 @@ public void testStatLinux() throws Exception { linux.test(); } - @Test(timeout=10000) + @Test + @Timeout(value = 10) public void testStatFreeBSD() throws Exception { String[] symlinks = new String[] { "6,Symbolic Link,1373508941,1373508941,120755,awang,awang,`link' -> `target'" @@ -125,9 +128,10 @@ public void testStatFreeBSD() throws Exception { freebsd.test(); } - @Test(timeout=10000) + @Test + @Timeout(value = 10) public void testStatFileNotFound() throws Exception { - Assume.assumeTrue(Stat.isAvailable()); + assumeTrue(Stat.isAvailable()); try { stat.getFileStatus(); fail("Expected FileNotFoundException"); @@ -136,14 +140,16 @@ public void testStatFileNotFound() throws Exception { } } - @Test(timeout=10000) + @Test + @Timeout(value = 10) public void testStatEnvironment() throws Exception { assertEquals("C", stat.getEnvironment("LANG")); } - @Test(timeout=10000) + @Test + @Timeout(value = 10) public void testStat() throws Exception { - Assume.assumeTrue(Stat.isAvailable()); + assumeTrue(Stat.isAvailable()); FileSystem fs = FileSystem.getLocal(new Configuration()); Path testDir = new Path(getTestRootPath(fs), "teststat"); fs.mkdirs(testDir); diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/AbstractFSContract.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/AbstractFSContract.java index 76d3116c3abdc..57894ff2e4862 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/AbstractFSContract.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/AbstractFSContract.java @@ -18,11 +18,12 @@ package org.apache.hadoop.fs.contract; +import static org.junit.jupiter.api.Assertions.assertTrue; + import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; -import org.junit.Assert; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -84,7 +85,7 @@ public void teardown() throws IOException { */ protected void addConfResource(String resource) { boolean found = maybeAddConfResource(resource); - Assert.assertTrue("Resource not found " + resource, found); + assertTrue(found, "Resource not found " + resource); } /** diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/ContractTestUtils.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/ContractTestUtils.java index 518f18bdf8c0d..e894f87078c5d 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/ContractTestUtils.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/ContractTestUtils.java @@ -36,8 +36,7 @@ import org.apache.hadoop.util.functional.RemoteIterators; import org.apache.hadoop.util.functional.FutureIO; -import org.assertj.core.api.Assertions; -import org.junit.Assert; +import org.junit.jupiter.api.Assertions; import org.junit.AssumptionViolatedException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -66,11 +65,12 @@ import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.IO_FILE_BUFFER_SIZE_DEFAULT; import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.IO_FILE_BUFFER_SIZE_KEY; import static org.apache.hadoop.util.functional.RemoteIterators.foreach; +import static org.assertj.core.api.Assertions.assertThat; /** * Utilities used across test cases. */ -public class ContractTestUtils extends Assert { +public class ContractTestUtils extends Assertions { private static final Logger LOG = LoggerFactory.getLogger(ContractTestUtils.class); @@ -99,11 +99,9 @@ public static void assertPropertyEquals(Properties props, String expected) { String val = props.getProperty(key); if (expected == null) { - assertNull("Non null property " + key + " = " + val, val); + assertNull(val, "Non null property " + key + " = " + val); } else { - assertEquals("property " + key + " = " + val, - expected, - val); + assertEquals(expected, val, "property " + key + " = " + val); } } @@ -147,7 +145,7 @@ public static void writeAndRead(FileSystem fs, if (delete) { rejectRootOperation(path); boolean deleted = fs.delete(path, false); - assertTrue("Deleted", deleted); + assertTrue(deleted, "Deleted"); assertPathDoesNotExist(fs, "Cleanup failed", path); } } @@ -187,9 +185,8 @@ public static void writeDataset(FileSystem fs, public static void writeDataset(FileSystem fs, Path path, byte[] src, int len, int buffersize, boolean overwrite, boolean useBuilder) throws IOException { - assertTrue( - "Not enough data in source array to write " + len + " bytes", - src.length >= len); + assertTrue(src.length >= len, + "Not enough data in source array to write " + len + " bytes"); FSDataOutputStream out; if (useBuilder) { out = fs.createFile(path) @@ -308,7 +305,7 @@ public static void verifyFileContents(FileSystem fs, FileStatus stat = fs.getFileStatus(path); assertIsFile(path, stat); String statText = stat.toString(); - assertEquals("wrong length " + statText, original.length, stat.getLen()); + assertEquals(original.length, stat.getLen(), "wrong length " + statText); byte[] bytes = readDataset(fs, path, original.length); compareByteArrays(original, bytes, original.length); } @@ -343,8 +340,8 @@ public static void verifyRead(FSDataInputStream stm, byte[] fileContents, public static void compareByteArrays(byte[] original, byte[] received, int len) { - assertEquals("Number of bytes read != number written", - len, received.length); + assertEquals(len, received.length, + "Number of bytes read != number written"); int errors = 0; int firstErrorByte = -1; for (int i = 0; i < len; i++) { @@ -488,8 +485,8 @@ public static boolean rm(FileSystem fileSystem, public static void rename(FileSystem fileSystem, Path src, Path dst) throws IOException { rejectRootOperation(src, false); - assertTrue("rename(" + src + ", " + dst + ") failed", - fileSystem.rename(src, dst)); + assertTrue(fileSystem.rename(src, dst), + "rename(" + src + ", " + dst + ") failed"); assertPathDoesNotExist(fileSystem, "renamed source dir", src); } @@ -602,7 +599,7 @@ public static void skip(String message) { * @param thrown a (possibly null) throwable to init the cause with * @throws AssertionError with the text and throwable -always */ - public static void fail(String text, Throwable thrown) { + public static Object fail(String text, Throwable thrown) { throw new AssertionError(text, thrown); } @@ -616,10 +613,8 @@ public static void fail(String text, Throwable thrown) { public static void assertFileHasLength(FileSystem fs, Path path, int expected) throws IOException { FileStatus status = fs.getFileStatus(path); - assertEquals( - "Wrong file length of file " + path + " status: " + status, - expected, - status.getLen()); + assertEquals(expected, status.getLen(), + "Wrong file length of file " + path + " status: " + status); } /** @@ -640,8 +635,8 @@ public static void assertIsDirectory(FileSystem fs, * @param fileStatus stats to check */ public static void assertIsDirectory(FileStatus fileStatus) { - assertTrue("Should be a directory -but isn't: " + fileStatus, - fileStatus.isDirectory()); + assertTrue(fileStatus.isDirectory(), + "Should be a directory -but isn't: " + fileStatus); } /** @@ -654,7 +649,7 @@ public static void assertIsDirectory(FileStatus fileStatus) { public static void assertErasureCoded(final FileSystem fs, final Path path) throws IOException { FileStatus fileStatus = fs.getFileStatus(path); - assertTrue(path + " must be erasure coded!", fileStatus.isErasureCoded()); + assertTrue(fileStatus.isErasureCoded(), path + " must be erasure coded!"); } /** @@ -667,8 +662,8 @@ public static void assertErasureCoded(final FileSystem fs, final Path path) public static void assertNotErasureCoded(final FileSystem fs, final Path path) throws IOException { FileStatus fileStatus = fs.getFileStatus(path); - assertFalse(path + " should not be erasure coded!", - fileStatus.isErasureCoded()); + assertFalse(fileStatus.isErasureCoded(), + path + " should not be erasure coded!"); } /** @@ -817,7 +812,7 @@ public static void assertDeleted(FileSystem fs, boolean deleted = fs.delete(file, recursive); if (!deleted) { String dir = ls(fs, file.getParent()); - assertTrue("Delete failed on " + file + ": " + dir, deleted); + assertTrue(deleted, "Delete failed on " + file + ": " + dir); } assertPathDoesNotExist(fs, "Deleted file", file); } @@ -967,10 +962,10 @@ public static void assertIsFile(FileContext fileContext, Path filename) */ public static void assertIsFile(Path filename, FileStatus status) { String fileInfo = filename + " " + status; - assertFalse("File claims to be a directory " + fileInfo, - status.isDirectory()); - assertFalse("File claims to be a symlink " + fileInfo, - status.isSymlink()); + assertFalse(status.isDirectory(), + "File claims to be a directory " + fileInfo); + assertFalse(status.isSymlink(), + "File claims to be a symlink " + fileInfo); } /** @@ -1135,9 +1130,8 @@ public static void assertListStatusFinds(FileSystem fs, found = true; } } - assertTrue("Path " + subdir - + " not found in directory " + dir + ":" + builder, - found); + assertTrue(found, + "Path " + subdir + " not found in directory " + dir + ":" + builder); } /** @@ -1149,7 +1143,7 @@ public static void assertListStatusFinds(FileSystem fs, * @throws IOException IO Problem */ public static void assertMkdirs(FileSystem fs, Path dir) throws IOException { - assertTrue("mkdirs(" + dir + ") returned false", fs.mkdirs(dir)); + assertTrue(fs.mkdirs(dir), "mkdirs(" + dir + ") returned false"); } /** @@ -1180,8 +1174,8 @@ public static void validateFileContent(byte[] concat, byte[][] bytes) { break; } } - assertFalse("File content of file is not as expected at offset " + idx, - mismatch); + assertFalse(mismatch, + "File content of file is not as expected at offset " + idx); } /** @@ -1256,7 +1250,7 @@ public static void assertDatasetEquals( int o = readOffset + i; final byte orig = originalData[o]; final byte current = data.get(); - Assertions.assertThat(current) + assertThat(current) .describedAs("%s with read offset %d: data[0x%02X] != DATASET[0x%02X]", operation, o, i, current) .isEqualTo(orig); @@ -1716,23 +1710,23 @@ public static List toListThroughNextCallsAlone( public static void assertCapabilities( Object stream, String[] shouldHaveCapabilities, String[] shouldNotHaveCapabilities) { - assertTrue("Stream should be instanceof StreamCapabilities", - stream instanceof StreamCapabilities); + assertTrue(stream instanceof StreamCapabilities, + "Stream should be instanceof StreamCapabilities"); StreamCapabilities source = (StreamCapabilities) stream; if (shouldHaveCapabilities != null) { for (String shouldHaveCapability : shouldHaveCapabilities) { - assertTrue("Should have capability: " + shouldHaveCapability - + " in " + source, - source.hasCapability(shouldHaveCapability)); + assertTrue(source.hasCapability(shouldHaveCapability), + "Should have capability: " + shouldHaveCapability + + " in " + source); } } if (shouldNotHaveCapabilities != null) { for (String shouldNotHaveCapability : shouldNotHaveCapabilities) { - assertFalse("Should not have capability: " + shouldNotHaveCapability - + " in " + source, - source.hasCapability(shouldNotHaveCapability)); + assertFalse(source.hasCapability(shouldNotHaveCapability), + "Should not have capability: " + shouldNotHaveCapability + + " in " + source); } } } @@ -1778,10 +1772,10 @@ public static void assertHasPathCapabilities( final String...capabilities) throws IOException { for (String shouldHaveCapability: capabilities) { - assertTrue("Should have capability: " + shouldHaveCapability - + " under " + path - + " in " + source, - source.hasPathCapability(path, shouldHaveCapability)); + assertTrue(source.hasPathCapability(path, shouldHaveCapability), + "Should have capability: " + shouldHaveCapability + + " under " + path + + " in " + source); } } @@ -1799,9 +1793,9 @@ public static void assertLacksPathCapabilities( final String...capabilities) throws IOException { for (String shouldHaveCapability: capabilities) { - assertFalse("Path must not support capability: " + shouldHaveCapability - + " under " + path, - source.hasPathCapability(path, shouldHaveCapability)); + assertFalse(source.hasPathCapability(path, shouldHaveCapability), + "Path must not support capability: " + shouldHaveCapability + + " under " + path); } } @@ -1913,7 +1907,7 @@ public TreeScanResults(RemoteIterator results) * @param stats statistics array. Must not be null. */ public TreeScanResults(FileStatus[] stats) { - assertNotNull("Null file status array", stats); + assertNotNull(stats, "Null file status array"); for (FileStatus stat : stats) { add(stat); } @@ -2025,12 +2019,12 @@ public int hashCode() { */ public void assertSizeEquals(String text, long f, long d, long o) { String self = dump(); - Assert.assertEquals(text + ": file count in " + self, - f, getFileCount()); - Assert.assertEquals(text + ": directory count in " + self, - d, getDirCount()); - Assert.assertEquals(text + ": 'other' count in " + self, - o, getOtherCount()); + assertEquals(f, getFileCount(), + text + ": file count in " + self); + assertEquals(d, getDirCount(), + text + ": directory count in " + self); + assertEquals(o, getOtherCount(), + text + ": 'other' count in " + self); } /** @@ -2055,13 +2049,13 @@ public void assertEquivalent(TreeScanResults that) { public void assertFieldsEquivalent(String fieldname, TreeScanResults that, List ours, List theirs) { - Assertions.assertThat(ours). + assertThat(ours). describedAs("list of %s", fieldname) .doesNotHaveDuplicates(); - Assertions.assertThat(theirs). + assertThat(theirs). describedAs("list of %s in %s", fieldname, that) .doesNotHaveDuplicates(); - Assertions.assertThat(ours) + assertThat(ours) .describedAs("Elements of %s", fieldname) .containsExactlyInAnyOrderElementsOf(theirs); diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/ftp/FTPContract.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/ftp/FTPContract.java index 62648ec58bcc7..5a9929e4e6d05 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/ftp/FTPContract.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/ftp/FTPContract.java @@ -25,7 +25,7 @@ import java.net.URI; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; /** * The contract of FTP; requires the option "test.testdir" to be set @@ -55,7 +55,7 @@ public String getScheme() { @Override public Path getTestPath() { String pathString = getOption(TEST_FS_TESTDIR, null); - assertNotNull("Undefined test option " + TEST_FS_TESTDIR, pathString); + assertNotNull(pathString, "Undefined test option " + TEST_FS_TESTDIR); Path path = new Path(pathString); return path; } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/rawlocal/TestRawLocalContractUnderlyingFileBehavior.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/rawlocal/TestRawLocalContractUnderlyingFileBehavior.java index 6eb24985f4ff3..24e638efa3a07 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/rawlocal/TestRawLocalContractUnderlyingFileBehavior.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/contract/rawlocal/TestRawLocalContractUnderlyingFileBehavior.java @@ -20,17 +20,17 @@ import org.apache.hadoop.conf.Configuration; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import java.io.File; -public class TestRawLocalContractUnderlyingFileBehavior extends Assert { +public class TestRawLocalContractUnderlyingFileBehavior extends Assertions { private static File testDirectory; - @BeforeClass + @BeforeAll public static void before() { RawlocalFSContract contract = new RawlocalFSContract(new Configuration()); @@ -44,6 +44,6 @@ public static void before() { public void testDeleteEmptyPath() throws Throwable { File nonexistent = new File(testDirectory, "testDeleteEmptyPath"); assertFalse(nonexistent.exists()); - assertFalse("nonexistent.delete() returned true", nonexistent.delete()); + assertFalse(nonexistent.delete(), "nonexistent.delete() returned true"); } } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/shell/TestTextCommand.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/shell/TestTextCommand.java index d7923823c84ae..ab4d0b2cc97a0 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/shell/TestTextCommand.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/shell/TestTextCommand.java @@ -19,6 +19,7 @@ package org.apache.hadoop.fs.shell; import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.IO_FILE_BUFFER_SIZE_KEY; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.File; @@ -35,15 +36,14 @@ import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.SequenceFile; import org.apache.hadoop.test.GenericTestUtils; -import org.assertj.core.api.Assertions; -import org.junit.Rule; import org.junit.jupiter.api.Test; -import org.junit.rules.Timeout; +import org.junit.jupiter.api.Timeout; /** * This class tests the logic for displaying the binary formats supported * by the Text command. */ +@Timeout(30) public class TestTextCommand { private static final File TEST_ROOT_DIR = GenericTestUtils.getTestDir("testText"); @@ -66,9 +66,6 @@ public class TestTextCommand { private static final String SEQUENCE_FILE_EXPECTED_OUTPUT = "Key1\tValue1\nKey2\tValue2\n"; - @Rule - public final Timeout testTimeout = new Timeout(30000); - /** * Tests whether binary Avro data files are displayed correctly. */ @@ -76,7 +73,7 @@ public class TestTextCommand { public void testDisplayForAvroFiles() throws Exception { String output = readUsingTextCommand(AVRO_FILENAME, generateWeatherAvroBinaryData()); - Assertions.assertThat(output).describedAs("output").isEqualTo(AVRO_EXPECTED_OUTPUT); + assertThat(output).describedAs("output").isEqualTo(AVRO_EXPECTED_OUTPUT); } @Test @@ -86,14 +83,14 @@ public void testDisplayForAvroFilesSmallMultiByteReads() throws Exception { createFile(AVRO_FILENAME, generateWeatherAvroBinaryData()); URI uri = new URI(AVRO_FILENAME); String output = readUsingTextCommand(uri, conf); - Assertions.assertThat(output).describedAs("output").isEqualTo(AVRO_EXPECTED_OUTPUT); + assertThat(output).describedAs("output").isEqualTo(AVRO_EXPECTED_OUTPUT); } @Test public void testEmptyAvroFile() throws Exception { String output = readUsingTextCommand(AVRO_FILENAME, generateEmptyAvroBinaryData()); - Assertions.assertThat(output).describedAs("output").isEmpty(); + assertThat(output).describedAs("output").isEmpty(); } @Test @@ -138,7 +135,7 @@ public void testAvroFileInputStreamZeroLengthRead() throws Exception { URI uri = new URI(AVRO_FILENAME); Configuration conf = new Configuration(); try (InputStream is = getInputStream(uri, conf)) { - Assertions.assertThat(is.read(new byte[10], 0, 0)).describedAs("bytes read").isEqualTo(0); + assertThat(is.read(new byte[10], 0, 0)).describedAs("bytes read").isEqualTo(0); } } @@ -149,8 +146,8 @@ public void testAvroFileInputStreamConsistentEOF() throws Exception { Configuration conf = new Configuration(); try (InputStream is = getInputStream(uri, conf)) { inputStreamToString(is); - Assertions.assertThat(is.read()).describedAs("single byte EOF").isEqualTo(-1); - Assertions.assertThat(is.read(new byte[10], 0, 10)).describedAs("multi byte EOF") + assertThat(is.read()).describedAs("single byte EOF").isEqualTo(-1); + assertThat(is.read(new byte[10], 0, 10)).describedAs("multi byte EOF") .isEqualTo(-1); } } @@ -164,7 +161,7 @@ public void testAvroFileInputStreamSingleAndMultiByteReads() throws Exception { InputStream is2 = getInputStream(uri, conf)) { String multiByteReads = inputStreamToString(is1); String singleByteReads = inputStreamSingleByteReadsToString(is2); - Assertions.assertThat(multiByteReads) + assertThat(multiByteReads) .describedAs("same bytes read from multi and single byte reads") .isEqualTo(singleByteReads); } @@ -177,7 +174,7 @@ public void testAvroFileInputStreamSingleAndMultiByteReads() throws Exception { public void testEmptyTextFile() throws Exception { byte[] emptyContents = {}; String output = readUsingTextCommand(TEXT_FILENAME, emptyContents); - Assertions.assertThat(output).describedAs("output").isEmpty(); + assertThat(output).describedAs("output").isEmpty(); } /** @@ -188,7 +185,7 @@ public void testOneByteTextFile() throws Exception { byte[] oneByteContents = {'x'}; String output = readUsingTextCommand(TEXT_FILENAME, oneByteContents); String expected = new String(oneByteContents, StandardCharsets.UTF_8); - Assertions.assertThat(output).describedAs("output").isEqualTo(expected); + assertThat(output).describedAs("output").isEqualTo(expected); } /** @@ -199,7 +196,7 @@ public void testTwoByteTextFile() throws Exception { byte[] twoByteContents = {'x', 'y'}; String output = readUsingTextCommand(TEXT_FILENAME, twoByteContents); String expected = new String(twoByteContents, StandardCharsets.UTF_8); - Assertions.assertThat(output).describedAs("output").isEqualTo(expected); + assertThat(output).describedAs("output").isEqualTo(expected); } @Test @@ -208,7 +205,7 @@ public void testDisplayForNonWritableSequenceFile() throws Exception { createNonWritableSequenceFile(SEQUENCE_FILENAME, conf); URI uri = new URI(SEQUENCE_FILENAME); String output = readUsingTextCommand(uri, conf); - Assertions.assertThat(output).describedAs("output").isEqualTo(SEQUENCE_FILE_EXPECTED_OUTPUT); + assertThat(output).describedAs("output").isEqualTo(SEQUENCE_FILE_EXPECTED_OUTPUT); } @Test @@ -218,7 +215,7 @@ public void testDisplayForSequenceFileSmallMultiByteReads() throws Exception { createNonWritableSequenceFile(SEQUENCE_FILENAME, conf); URI uri = new URI(SEQUENCE_FILENAME); String output = readUsingTextCommand(uri, conf); - Assertions.assertThat(output).describedAs("output").isEqualTo(SEQUENCE_FILE_EXPECTED_OUTPUT); + assertThat(output).describedAs("output").isEqualTo(SEQUENCE_FILE_EXPECTED_OUTPUT); } @Test @@ -227,7 +224,7 @@ public void testEmptySequenceFile() throws Exception { createEmptySequenceFile(SEQUENCE_FILENAME, conf); URI uri = new URI(SEQUENCE_FILENAME); String output = readUsingTextCommand(uri, conf); - Assertions.assertThat(output).describedAs("output").isEmpty(); + assertThat(output).describedAs("output").isEmpty(); } @Test @@ -272,7 +269,7 @@ public void testSequenceFileInputStreamZeroLengthRead() throws Exception { createNonWritableSequenceFile(SEQUENCE_FILENAME, conf); URI uri = new URI(SEQUENCE_FILENAME); try (InputStream is = getInputStream(uri, conf)) { - Assertions.assertThat(is.read(new byte[10], 0, 0)).describedAs("bytes read").isEqualTo(0); + assertThat(is.read(new byte[10], 0, 0)).describedAs("bytes read").isEqualTo(0); } } @@ -283,8 +280,8 @@ public void testSequenceFileInputStreamConsistentEOF() throws Exception { URI uri = new URI(SEQUENCE_FILENAME); try (InputStream is = getInputStream(uri, conf)) { inputStreamToString(is); - Assertions.assertThat(is.read()).describedAs("single byte EOF").isEqualTo(-1); - Assertions.assertThat(is.read(new byte[10], 0, 10)).describedAs("multi byte EOF") + assertThat(is.read()).describedAs("single byte EOF").isEqualTo(-1); + assertThat(is.read(new byte[10], 0, 10)).describedAs("multi byte EOF") .isEqualTo(-1); } } @@ -298,7 +295,7 @@ public void testSequenceFileInputStreamSingleAndMultiByteReads() throws Exceptio InputStream is2 = getInputStream(uri, conf)) { String multiByteReads = inputStreamToString(is1); String singleByteReads = inputStreamSingleByteReadsToString(is2); - Assertions.assertThat(multiByteReads) + assertThat(multiByteReads) .describedAs("same bytes read from multi and single byte reads") .isEqualTo(singleByteReads); } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestChRootedFileSystem.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestChRootedFileSystem.java index 8267b214d53bc..797f8f0221bc2 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestChRootedFileSystem.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestChRootedFileSystem.java @@ -17,6 +17,17 @@ */ package org.apache.hadoop.fs.viewfs; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.reset; + import java.io.FileNotFoundException; import java.io.IOException; import java.net.URI; @@ -33,11 +44,10 @@ import org.apache.hadoop.fs.FsConstants; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.permission.AclEntry; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import static org.mockito.Mockito.*; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; public class TestChRootedFileSystem { FileSystem fSys; // The ChRoootedFs @@ -45,7 +55,7 @@ public class TestChRootedFileSystem { Path chrootedTo; FileSystemTestHelper fileSystemTestHelper; - @Before + @BeforeEach public void setUp() throws Exception { // create the test root on local_fs Configuration conf = new Configuration(); @@ -62,7 +72,7 @@ public void setUp() throws Exception { fSys = new ChRootedFileSystem(chrootedTo.toUri(), conf); } - @After + @AfterEach public void tearDown() throws Exception { fSysTarget.delete(chrootedTo, true); } @@ -70,17 +80,17 @@ public void tearDown() throws Exception { @Test public void testURI() { URI uri = fSys.getUri(); - Assert.assertEquals(chrootedTo.toUri(), uri); + assertEquals(chrootedTo.toUri(), uri); } @Test public void testBasicPaths() { URI uri = fSys.getUri(); - Assert.assertEquals(chrootedTo.toUri(), uri); - Assert.assertEquals(fSys.makeQualified( + assertEquals(chrootedTo.toUri(), uri); + assertEquals(fSys.makeQualified( new Path(System.getProperty("user.home"))), fSys.getWorkingDirectory()); - Assert.assertEquals(fSys.makeQualified( + assertEquals(fSys.makeQualified( new Path(System.getProperty("user.home"))), fSys.getHomeDirectory()); /* @@ -90,13 +100,13 @@ public void testBasicPaths() { * But if we were to fix Path#makeQualified() then the next test should * have been: - Assert.assertEquals( + assertEquals( new Path(chrootedTo + "/foo/bar").makeQualified( FsConstants.LOCAL_FS_URI, null), fSys.makeQualified(new Path( "/foo/bar"))); */ - Assert.assertEquals( + assertEquals( new Path("/foo/bar").makeQualified(FsConstants.LOCAL_FS_URI, null), fSys.makeQualified(new Path("/foo/bar"))); } @@ -113,50 +123,50 @@ public void testCreateDelete() throws IOException { // Create file fileSystemTestHelper.createFile(fSys, "/foo"); - Assert.assertTrue(fSys.isFile(new Path("/foo"))); - Assert.assertTrue(fSysTarget.isFile(new Path(chrootedTo, "foo"))); + assertTrue(fSys.isFile(new Path("/foo"))); + assertTrue(fSysTarget.isFile(new Path(chrootedTo, "foo"))); // Create file with recursive dir fileSystemTestHelper.createFile(fSys, "/newDir/foo"); - Assert.assertTrue(fSys.isFile(new Path("/newDir/foo"))); - Assert.assertTrue(fSysTarget.isFile(new Path(chrootedTo,"newDir/foo"))); + assertTrue(fSys.isFile(new Path("/newDir/foo"))); + assertTrue(fSysTarget.isFile(new Path(chrootedTo, "newDir/foo"))); // Delete the created file - Assert.assertTrue(fSys.delete(new Path("/newDir/foo"), false)); - Assert.assertFalse(fSys.exists(new Path("/newDir/foo"))); - Assert.assertFalse(fSysTarget.exists(new Path(chrootedTo, "newDir/foo"))); + assertTrue(fSys.delete(new Path("/newDir/foo"), false)); + assertFalse(fSys.exists(new Path("/newDir/foo"))); + assertFalse(fSysTarget.exists(new Path(chrootedTo, "newDir/foo"))); // Create file with a 2 component dirs recursively fileSystemTestHelper.createFile(fSys, "/newDir/newDir2/foo"); - Assert.assertTrue(fSys.isFile(new Path("/newDir/newDir2/foo"))); - Assert.assertTrue(fSysTarget.isFile(new Path(chrootedTo,"newDir/newDir2/foo"))); + assertTrue(fSys.isFile(new Path("/newDir/newDir2/foo"))); + assertTrue(fSysTarget.isFile(new Path(chrootedTo, "newDir/newDir2/foo"))); // Delete the created file - Assert.assertTrue(fSys.delete(new Path("/newDir/newDir2/foo"), false)); - Assert.assertFalse(fSys.exists(new Path("/newDir/newDir2/foo"))); - Assert.assertFalse(fSysTarget.exists(new Path(chrootedTo,"newDir/newDir2/foo"))); + assertTrue(fSys.delete(new Path("/newDir/newDir2/foo"), false)); + assertFalse(fSys.exists(new Path("/newDir/newDir2/foo"))); + assertFalse(fSysTarget.exists(new Path(chrootedTo, "newDir/newDir2/foo"))); } @Test public void testMkdirDelete() throws IOException { fSys.mkdirs(fileSystemTestHelper.getTestRootPath(fSys, "/dirX")); - Assert.assertTrue(fSys.isDirectory(new Path("/dirX"))); - Assert.assertTrue(fSysTarget.isDirectory(new Path(chrootedTo,"dirX"))); + assertTrue(fSys.isDirectory(new Path("/dirX"))); + assertTrue(fSysTarget.isDirectory(new Path(chrootedTo, "dirX"))); fSys.mkdirs(fileSystemTestHelper.getTestRootPath(fSys, "/dirX/dirY")); - Assert.assertTrue(fSys.isDirectory(new Path("/dirX/dirY"))); - Assert.assertTrue(fSysTarget.isDirectory(new Path(chrootedTo,"dirX/dirY"))); + assertTrue(fSys.isDirectory(new Path("/dirX/dirY"))); + assertTrue(fSysTarget.isDirectory(new Path(chrootedTo, "dirX/dirY"))); // Delete the created dir - Assert.assertTrue(fSys.delete(new Path("/dirX/dirY"), false)); - Assert.assertFalse(fSys.exists(new Path("/dirX/dirY"))); - Assert.assertFalse(fSysTarget.exists(new Path(chrootedTo,"dirX/dirY"))); + assertTrue(fSys.delete(new Path("/dirX/dirY"), false)); + assertFalse(fSys.exists(new Path("/dirX/dirY"))); + assertFalse(fSysTarget.exists(new Path(chrootedTo, "dirX/dirY"))); - Assert.assertTrue(fSys.delete(new Path("/dirX"), false)); - Assert.assertFalse(fSys.exists(new Path("/dirX"))); - Assert.assertFalse(fSysTarget.exists(new Path(chrootedTo,"dirX"))); + assertTrue(fSys.delete(new Path("/dirX"), false)); + assertFalse(fSys.exists(new Path("/dirX"))); + assertFalse(fSysTarget.exists(new Path(chrootedTo, "dirX"))); } @Test @@ -164,19 +174,19 @@ public void testRename() throws IOException { // Rename a file fileSystemTestHelper.createFile(fSys, "/newDir/foo"); fSys.rename(new Path("/newDir/foo"), new Path("/newDir/fooBar")); - Assert.assertFalse(fSys.exists(new Path("/newDir/foo"))); - Assert.assertFalse(fSysTarget.exists(new Path(chrootedTo,"newDir/foo"))); - Assert.assertTrue(fSys.isFile(fileSystemTestHelper.getTestRootPath(fSys,"/newDir/fooBar"))); - Assert.assertTrue(fSysTarget.isFile(new Path(chrootedTo,"newDir/fooBar"))); + assertFalse(fSys.exists(new Path("/newDir/foo"))); + assertFalse(fSysTarget.exists(new Path(chrootedTo, "newDir/foo"))); + assertTrue(fSys.isFile(fileSystemTestHelper.getTestRootPath(fSys, "/newDir/fooBar"))); + assertTrue(fSysTarget.isFile(new Path(chrootedTo, "newDir/fooBar"))); // Rename a dir fSys.mkdirs(new Path("/newDir/dirFoo")); fSys.rename(new Path("/newDir/dirFoo"), new Path("/newDir/dirFooBar")); - Assert.assertFalse(fSys.exists(new Path("/newDir/dirFoo"))); - Assert.assertFalse(fSysTarget.exists(new Path(chrootedTo,"newDir/dirFoo"))); - Assert.assertTrue(fSys.isDirectory(fileSystemTestHelper.getTestRootPath(fSys,"/newDir/dirFooBar"))); - Assert.assertTrue(fSysTarget.isDirectory(new Path(chrootedTo,"newDir/dirFooBar"))); + assertFalse(fSys.exists(new Path("/newDir/dirFoo"))); + assertFalse(fSysTarget.exists(new Path(chrootedTo, "newDir/dirFoo"))); + assertTrue(fSys.isDirectory(fileSystemTestHelper.getTestRootPath(fSys, "/newDir/dirFooBar"))); + assertTrue(fSysTarget.isDirectory(new Path(chrootedTo, "newDir/dirFooBar"))); } @Test @@ -184,8 +194,8 @@ public void testGetContentSummary() throws IOException { // GetContentSummary of a dir fSys.mkdirs(new Path("/newDir/dirFoo")); ContentSummary cs = fSys.getContentSummary(new Path("/newDir/dirFoo")); - Assert.assertEquals(-1L, cs.getQuota()); - Assert.assertEquals(-1L, cs.getSpaceQuota()); + assertEquals(-1L, cs.getQuota()); + assertEquals(-1L, cs.getSpaceQuota()); } /** @@ -207,15 +217,15 @@ public void testRenameAcrossFs() throws IOException { public void testList() throws IOException { FileStatus fs = fSys.getFileStatus(new Path("/")); - Assert.assertTrue(fs.isDirectory()); + assertTrue(fs.isDirectory()); // should return the full path not the chrooted path - Assert.assertEquals(fs.getPath(), chrootedTo); + assertEquals(fs.getPath(), chrootedTo); // list on Slash FileStatus[] dirPaths = fSys.listStatus(new Path("/")); - Assert.assertEquals(0, dirPaths.length); + assertEquals(0, dirPaths.length); @@ -226,21 +236,21 @@ public void testList() throws IOException { fSys.mkdirs(new Path("/dirX/dirXX")); dirPaths = fSys.listStatus(new Path("/")); - Assert.assertEquals(4, dirPaths.length); // note 2 crc files + assertEquals(4, dirPaths.length); // note 2 crc files // Note the the file status paths are the full paths on target fs = FileSystemTestHelper.containsPath(new Path(chrootedTo, "foo"), dirPaths); - Assert.assertNotNull(fs); - Assert.assertTrue(fs.isFile()); + assertNotNull(fs); + assertTrue(fs.isFile()); fs = FileSystemTestHelper.containsPath(new Path(chrootedTo, "bar"), dirPaths); - Assert.assertNotNull(fs); - Assert.assertTrue(fs.isFile()); + assertNotNull(fs); + assertTrue(fs.isFile()); fs = FileSystemTestHelper.containsPath(new Path(chrootedTo, "dirX"), dirPaths); - Assert.assertNotNull(fs); - Assert.assertTrue(fs.isDirectory()); + assertNotNull(fs); + assertTrue(fs.isDirectory()); fs = FileSystemTestHelper.containsPath(new Path(chrootedTo, "dirY"), dirPaths); - Assert.assertNotNull(fs); - Assert.assertTrue(fs.isDirectory()); + assertNotNull(fs); + assertTrue(fs.isDirectory()); } @Test @@ -250,31 +260,31 @@ public void testWorkingDirectory() throws Exception { fSys.mkdirs(new Path("/testWd")); Path workDir = new Path("/testWd"); fSys.setWorkingDirectory(workDir); - Assert.assertEquals(workDir, fSys.getWorkingDirectory()); + assertEquals(workDir, fSys.getWorkingDirectory()); fSys.setWorkingDirectory(new Path(".")); - Assert.assertEquals(workDir, fSys.getWorkingDirectory()); + assertEquals(workDir, fSys.getWorkingDirectory()); fSys.setWorkingDirectory(new Path("..")); - Assert.assertEquals(workDir.getParent(), fSys.getWorkingDirectory()); + assertEquals(workDir.getParent(), fSys.getWorkingDirectory()); // cd using a relative path // Go back to our test root workDir = new Path("/testWd"); fSys.setWorkingDirectory(workDir); - Assert.assertEquals(workDir, fSys.getWorkingDirectory()); + assertEquals(workDir, fSys.getWorkingDirectory()); Path relativeDir = new Path("existingDir1"); Path absoluteDir = new Path(workDir,"existingDir1"); fSys.mkdirs(absoluteDir); fSys.setWorkingDirectory(relativeDir); - Assert.assertEquals(absoluteDir, fSys.getWorkingDirectory()); + assertEquals(absoluteDir, fSys.getWorkingDirectory()); // cd using a absolute path absoluteDir = new Path("/test/existingDir2"); fSys.mkdirs(absoluteDir); fSys.setWorkingDirectory(absoluteDir); - Assert.assertEquals(absoluteDir, fSys.getWorkingDirectory()); + assertEquals(absoluteDir, fSys.getWorkingDirectory()); // Now open a file relative to the wd we just set above. Path absoluteFooPath = new Path(absoluteDir, "foo"); @@ -283,14 +293,14 @@ public void testWorkingDirectory() throws Exception { // Now mkdir relative to the dir we cd'ed to fSys.mkdirs(new Path("newDir")); - Assert.assertTrue(fSys.isDirectory(new Path(absoluteDir, "newDir"))); + assertTrue(fSys.isDirectory(new Path(absoluteDir, "newDir"))); /* Filesystem impls (RawLocal and DistributedFileSystem do not check * for existing of working dir absoluteDir = getTestRootPath(fSys, "nonexistingPath"); try { fSys.setWorkingDirectory(absoluteDir); - Assert.fail("cd to non existing dir should have failed"); + fail("cd to non existing dir should have failed"); } catch (Exception e) { // Exception as expected } @@ -301,7 +311,7 @@ public void testWorkingDirectory() throws Exception { absoluteDir = new Path(LOCAL_FS_ROOT_URI + "/existingDir"); fSys.mkdirs(absoluteDir); fSys.setWorkingDirectory(absoluteDir); - Assert.assertEquals(absoluteDir, fSys.getWorkingDirectory()); + assertEquals(absoluteDir, fSys.getWorkingDirectory()); } @@ -311,15 +321,17 @@ public void testWorkingDirectory() throws Exception { @Test public void testResolvePath() throws IOException { - Assert.assertEquals(chrootedTo, fSys.resolvePath(new Path("/"))); + assertEquals(chrootedTo, fSys.resolvePath(new Path("/"))); fileSystemTestHelper.createFile(fSys, "/foo"); - Assert.assertEquals(new Path(chrootedTo, "foo"), + assertEquals(new Path(chrootedTo, "foo"), fSys.resolvePath(new Path("/foo"))); } - @Test(expected=FileNotFoundException.class) + @Test public void testResolvePathNonExisting() throws IOException { + assertThrows(FileNotFoundException.class, () -> { fSys.resolvePath(new Path("/nonExisting")); + }); } @Test @@ -435,7 +447,8 @@ public void initialize(URI name, Configuration conf) throws IOException { } } - @Test(timeout = 30000) + @Test + @Timeout(value = 30) public void testCreateSnapshot() throws Exception { Path snapRootPath = new Path("/snapPath"); Path chRootedSnapRootPath = new Path("/a/b/snapPath"); @@ -452,7 +465,8 @@ public void testCreateSnapshot() throws Exception { verify(mockFs).createSnapshot(chRootedSnapRootPath, "snap1"); } - @Test(timeout = 30000) + @Test + @Timeout(value = 30) public void testDeleteSnapshot() throws Exception { Path snapRootPath = new Path("/snapPath"); Path chRootedSnapRootPath = new Path("/a/b/snapPath"); @@ -469,7 +483,8 @@ public void testDeleteSnapshot() throws Exception { verify(mockFs).deleteSnapshot(chRootedSnapRootPath, "snap1"); } - @Test(timeout = 30000) + @Test + @Timeout(value = 30) public void testRenameSnapshot() throws Exception { Path snapRootPath = new Path("/snapPath"); Path chRootedSnapRootPath = new Path("/a/b/snapPath"); @@ -487,7 +502,8 @@ public void testRenameSnapshot() throws Exception { "snapNewName"); } - @Test(timeout = 30000) + @Test + @Timeout(value = 30) public void testSetStoragePolicy() throws Exception { Path storagePolicyPath = new Path("/storagePolicy"); Path chRootedStoragePolicyPath = new Path("/a/b/storagePolicy"); @@ -504,7 +520,8 @@ public void testSetStoragePolicy() throws Exception { verify(mockFs).setStoragePolicy(chRootedStoragePolicyPath, "HOT"); } - @Test(timeout = 30000) + @Test + @Timeout(value = 30) public void testUnsetStoragePolicy() throws Exception { Path storagePolicyPath = new Path("/storagePolicy"); Path chRootedStoragePolicyPath = new Path("/a/b/storagePolicy"); @@ -521,7 +538,8 @@ public void testUnsetStoragePolicy() throws Exception { verify(mockFs).unsetStoragePolicy(chRootedStoragePolicyPath); } - @Test(timeout = 30000) + @Test + @Timeout(value = 30) public void testGetStoragePolicy() throws Exception { Path storagePolicyPath = new Path("/storagePolicy"); Path chRootedStoragePolicyPath = new Path("/a/b/storagePolicy"); @@ -538,7 +556,8 @@ public void testGetStoragePolicy() throws Exception { verify(mockFs).getStoragePolicy(chRootedStoragePolicyPath); } - @Test(timeout = 30000) + @Test + @Timeout(value = 30) public void testGetAllStoragePolicy() throws Exception { Configuration conf = new Configuration(); conf.setClass("fs.mockfs.impl", MockFileSystem.class, FileSystem.class); diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestHCFSMountTableConfigLoader.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestHCFSMountTableConfigLoader.java index bf7a6e32c8e93..cba797f556b91 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestHCFSMountTableConfigLoader.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestHCFSMountTableConfigLoader.java @@ -17,6 +17,9 @@ */ package org.apache.hadoop.fs.viewfs; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -28,11 +31,10 @@ import org.apache.hadoop.fs.FsConstants; import org.apache.hadoop.fs.LocalFileSystem; import org.apache.hadoop.fs.Path; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; /** * Tests the mount table loading. @@ -69,7 +71,7 @@ public class TestHCFSMountTableConfigLoader { .append(TABLE_NAME).append(DOT).append(Constants.CONFIG_VIEWFS_LINK) .append(DOT).append(SRC_TWO).toString(); - @BeforeClass + @BeforeAll public static void init() throws Exception { fsTarget = new LocalFileSystem(); fsTarget.initialize(new URI("file:///"), new Configuration()); @@ -78,7 +80,7 @@ public static void init() throws Exception { fsTarget.mkdirs(targetTestRoot); } - @Before + @BeforeEach public void setUp() throws Exception { conf = new Configuration(); conf.set(String.format( @@ -100,8 +102,8 @@ public void testMountTableFileLoadingWhenMultipleFilesExist() TARGET_TWO }, new Path(newVersionMountTableFile.toURI()), conf); loader.load(targetTestRoot.toString(), conf); - Assert.assertEquals(conf.get(MOUNT_LINK_KEY_SRC_TWO), TARGET_TWO); - Assert.assertEquals(conf.get(MOUNT_LINK_KEY_SRC_ONE), TARGET_ONE); + assertEquals(conf.get(MOUNT_LINK_KEY_SRC_TWO), TARGET_TWO); + assertEquals(conf.get(MOUNT_LINK_KEY_SRC_ONE), TARGET_ONE); } @Test @@ -119,8 +121,8 @@ public void testMountTableFileWithInvalidFormat() throws Exception { new Path(invalidMountFileName.toURI()), conf); // Pass mount table directory loader.load(path.toString(), conf); - Assert.assertEquals(null, conf.get(MOUNT_LINK_KEY_SRC_TWO)); - Assert.assertEquals(null, conf.get(MOUNT_LINK_KEY_SRC_ONE)); + assertEquals(null, conf.get(MOUNT_LINK_KEY_SRC_TWO)); + assertEquals(null, conf.get(MOUNT_LINK_KEY_SRC_ONE)); invalidMountFileName.delete(); } @@ -135,15 +137,16 @@ public void testMountTableFileWithInvalidFormatWithNoDotsInName() invalidMountFileName.createNewFile(); // Pass mount table directory loader.load(path.toString(), conf); - Assert.assertEquals(null, conf.get(MOUNT_LINK_KEY_SRC_TWO)); - Assert.assertEquals(null, conf.get(MOUNT_LINK_KEY_SRC_ONE)); + assertEquals(null, conf.get(MOUNT_LINK_KEY_SRC_TWO)); + assertEquals(null, conf.get(MOUNT_LINK_KEY_SRC_ONE)); invalidMountFileName.delete(); } - @Test(expected = FileNotFoundException.class) + @Test public void testLoadWithMountFile() throws Exception { - loader.load(new URI(targetTestRoot.toString() + "/Non-Existent-File.xml") - .toString(), conf); + assertThrows(FileNotFoundException.class, () -> { + loader.load(new URI(targetTestRoot.toString() + "/Non-Existent-File.xml").toString(), conf); + }); } @Test @@ -153,11 +156,11 @@ public void testLoadWithNonExistentMountFile() throws Exception { new String[] {TARGET_ONE, TARGET_TWO }, new Path(oldVersionMountTableFile.toURI()), conf); loader.load(oldVersionMountTableFile.toURI().toString(), conf); - Assert.assertEquals(conf.get(MOUNT_LINK_KEY_SRC_TWO), TARGET_TWO); - Assert.assertEquals(conf.get(MOUNT_LINK_KEY_SRC_ONE), TARGET_ONE); + assertEquals(conf.get(MOUNT_LINK_KEY_SRC_TWO), TARGET_TWO); + assertEquals(conf.get(MOUNT_LINK_KEY_SRC_ONE), TARGET_ONE); } - @AfterClass + @AfterAll public static void tearDown() throws IOException { fsTarget.delete(targetTestRoot, true); } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestNestedMountPoint.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestNestedMountPoint.java index 4a7aafd0a2b25..eaf8711fd7d3c 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestNestedMountPoint.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestNestedMountPoint.java @@ -17,6 +17,10 @@ */ package org.apache.hadoop.fs.viewfs; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.net.URI; import java.util.List; import java.util.function.Function; @@ -24,10 +28,9 @@ import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.FsConstants; import org.apache.hadoop.fs.Path; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** @@ -65,7 +68,7 @@ static class TestNestMountPointInternalFileSystem extends TestNestMountPointFile private static final URI NN5_TARGET = URI.create("hdfs://nn05/b/c/d/e"); private static final URI NN6_TARGET = URI.create("hdfs://nn06/b/c/d/e/f"); - @Before + @BeforeEach public void setUp() throws Exception { conf = new Configuration(); mtName = TestNestedMountPoint.class.getName(); @@ -107,7 +110,7 @@ protected TestNestedMountPoint.TestNestMountPointInternalFileSystem getTargetFil }; } - @After + @AfterEach public void tearDown() throws Exception { inodeTree = null; } @@ -116,250 +119,272 @@ public void tearDown() throws Exception { public void testPathResolveToLink() throws Exception { // /a/b/c/d/e/f resolves to /a/b/c/d/e and /f InodeTree.ResolveResult resolveResult = inodeTree.resolve("/a/b/c/d/e/f", true); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); - Assert.assertEquals("/a/b/c/d/e", resolveResult.resolvedPath); - Assert.assertEquals(new Path("/f"), resolveResult.remainingPath); - Assert.assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN4_TARGET, ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); + assertEquals("/a/b/c/d/e", resolveResult.resolvedPath); + assertEquals(new Path("/f"), resolveResult.remainingPath); + assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN4_TARGET, + ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); + assertTrue(resolveResult.isLastInternalDirLink()); // /a/b/c/d/e resolves to /a/b/c/d/e and / InodeTree.ResolveResult resolveResult2 = inodeTree.resolve("/a/b/c/d/e", true); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult2.kind); - Assert.assertEquals("/a/b/c/d/e", resolveResult2.resolvedPath); - Assert.assertEquals(new Path("/"), resolveResult2.remainingPath); - Assert.assertTrue(resolveResult2.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN4_TARGET, ((TestNestMountPointFileSystem) resolveResult2.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult2.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult2.kind); + assertEquals("/a/b/c/d/e", resolveResult2.resolvedPath); + assertEquals(new Path("/"), resolveResult2.remainingPath); + assertTrue(resolveResult2.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN4_TARGET, + ((TestNestMountPointFileSystem) resolveResult2.targetFileSystem).getUri()); + assertTrue(resolveResult2.isLastInternalDirLink()); // /a/b/c/d/e/f/g/h/i resolves to /a/b/c/d/e and /f/g/h/i InodeTree.ResolveResult resolveResult3 = inodeTree.resolve("/a/b/c/d/e/f/g/h/i", true); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult3.kind); - Assert.assertEquals("/a/b/c/d/e", resolveResult3.resolvedPath); - Assert.assertEquals(new Path("/f/g/h/i"), resolveResult3.remainingPath); - Assert.assertTrue(resolveResult3.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN4_TARGET, ((TestNestMountPointFileSystem) resolveResult3.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult3.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult3.kind); + assertEquals("/a/b/c/d/e", resolveResult3.resolvedPath); + assertEquals(new Path("/f/g/h/i"), resolveResult3.remainingPath); + assertTrue(resolveResult3.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN4_TARGET, + ((TestNestMountPointFileSystem) resolveResult3.targetFileSystem).getUri()); + assertTrue(resolveResult3.isLastInternalDirLink()); } @Test public void testPathResolveToLinkNotResolveLastComponent() throws Exception { // /a/b/c/d/e/f resolves to /a/b/c/d/e and /f InodeTree.ResolveResult resolveResult = inodeTree.resolve("/a/b/c/d/e/f", false); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); - Assert.assertEquals("/a/b/c/d/e", resolveResult.resolvedPath); - Assert.assertEquals(new Path("/f"), resolveResult.remainingPath); - Assert.assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN4_TARGET, ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); + assertEquals("/a/b/c/d/e", resolveResult.resolvedPath); + assertEquals(new Path("/f"), resolveResult.remainingPath); + assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN4_TARGET, + ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); + assertTrue(resolveResult.isLastInternalDirLink()); // /a/b/c/d/e resolves to /a/b/c/d and /e InodeTree.ResolveResult resolveResult2 = inodeTree.resolve("/a/b/c/d/e", false); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult2.kind); - Assert.assertEquals("/a/b/c/d", resolveResult2.resolvedPath); - Assert.assertEquals(new Path("/e"), resolveResult2.remainingPath); - Assert.assertTrue(resolveResult2.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN3_TARGET, ((TestNestMountPointFileSystem) resolveResult2.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult2.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult2.kind); + assertEquals("/a/b/c/d", resolveResult2.resolvedPath); + assertEquals(new Path("/e"), resolveResult2.remainingPath); + assertTrue(resolveResult2.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN3_TARGET, + ((TestNestMountPointFileSystem) resolveResult2.targetFileSystem).getUri()); + assertTrue(resolveResult2.isLastInternalDirLink()); // /a/b/c/d/e/f/g/h/i resolves to /a/b/c/d/e and /f/g/h/i InodeTree.ResolveResult resolveResult3 = inodeTree.resolve("/a/b/c/d/e/f/g/h/i", false); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult3.kind); - Assert.assertEquals("/a/b/c/d/e", resolveResult3.resolvedPath); - Assert.assertEquals(new Path("/f/g/h/i"), resolveResult3.remainingPath); - Assert.assertTrue(resolveResult3.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN4_TARGET, ((TestNestMountPointFileSystem) resolveResult3.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult3.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult3.kind); + assertEquals("/a/b/c/d/e", resolveResult3.resolvedPath); + assertEquals(new Path("/f/g/h/i"), resolveResult3.remainingPath); + assertTrue(resolveResult3.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN4_TARGET, + ((TestNestMountPointFileSystem) resolveResult3.targetFileSystem).getUri()); + assertTrue(resolveResult3.isLastInternalDirLink()); } @Test public void testPathResolveToDirLink() throws Exception { // /a/b/c/d/f resolves to /a/b/c/d, /f InodeTree.ResolveResult resolveResult = inodeTree.resolve("/a/b/c/d/f", true); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); - Assert.assertEquals("/a/b/c/d", resolveResult.resolvedPath); - Assert.assertEquals(new Path("/f"), resolveResult.remainingPath); - Assert.assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN3_TARGET, ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); + assertEquals("/a/b/c/d", resolveResult.resolvedPath); + assertEquals(new Path("/f"), resolveResult.remainingPath); + assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN3_TARGET, + ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); + assertTrue(resolveResult.isLastInternalDirLink()); // /a/b/c/d resolves to /a/b/c/d and / InodeTree.ResolveResult resolveResult2 = inodeTree.resolve("/a/b/c/d", true); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult2.kind); - Assert.assertEquals("/a/b/c/d", resolveResult2.resolvedPath); - Assert.assertEquals(new Path("/"), resolveResult2.remainingPath); - Assert.assertTrue(resolveResult2.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN3_TARGET, ((TestNestMountPointFileSystem) resolveResult2.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult2.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult2.kind); + assertEquals("/a/b/c/d", resolveResult2.resolvedPath); + assertEquals(new Path("/"), resolveResult2.remainingPath); + assertTrue(resolveResult2.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN3_TARGET, + ((TestNestMountPointFileSystem) resolveResult2.targetFileSystem).getUri()); + assertTrue(resolveResult2.isLastInternalDirLink()); // /a/b/c/d/f/g/h/i resolves to /a/b/c/d and /f/g/h/i InodeTree.ResolveResult resolveResult3 = inodeTree.resolve("/a/b/c/d/f/g/h/i", true); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult3.kind); - Assert.assertEquals("/a/b/c/d", resolveResult3.resolvedPath); - Assert.assertEquals(new Path("/f/g/h/i"), resolveResult3.remainingPath); - Assert.assertTrue(resolveResult3.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN3_TARGET, ((TestNestMountPointFileSystem) resolveResult3.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult3.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult3.kind); + assertEquals("/a/b/c/d", resolveResult3.resolvedPath); + assertEquals(new Path("/f/g/h/i"), resolveResult3.remainingPath); + assertTrue(resolveResult3.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN3_TARGET, + ((TestNestMountPointFileSystem) resolveResult3.targetFileSystem).getUri()); + assertTrue(resolveResult3.isLastInternalDirLink()); } @Test public void testPathResolveToDirLinkNotResolveLastComponent() throws Exception { // /a/b/c/d/f resolves to /a/b/c/d, /f InodeTree.ResolveResult resolveResult = inodeTree.resolve("/a/b/c/d/f", false); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); - Assert.assertEquals("/a/b/c/d", resolveResult.resolvedPath); - Assert.assertEquals(new Path("/f"), resolveResult.remainingPath); - Assert.assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN3_TARGET, ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); + assertEquals("/a/b/c/d", resolveResult.resolvedPath); + assertEquals(new Path("/f"), resolveResult.remainingPath); + assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN3_TARGET, + ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); + assertTrue(resolveResult.isLastInternalDirLink()); // /a/b/c/d resolves to /a/b and /c/d InodeTree.ResolveResult resolveResult2 = inodeTree.resolve("/a/b/c/d", false); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult2.kind); - Assert.assertEquals("/a/b", resolveResult2.resolvedPath); - Assert.assertEquals(new Path("/c/d"), resolveResult2.remainingPath); - Assert.assertTrue(resolveResult2.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN1_TARGET, ((TestNestMountPointFileSystem) resolveResult2.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult2.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult2.kind); + assertEquals("/a/b", resolveResult2.resolvedPath); + assertEquals(new Path("/c/d"), resolveResult2.remainingPath); + assertTrue(resolveResult2.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN1_TARGET, + ((TestNestMountPointFileSystem) resolveResult2.targetFileSystem).getUri()); + assertTrue(resolveResult2.isLastInternalDirLink()); // /a/b/c/d/f/g/h/i resolves to /a/b/c/d and /f/g/h/i InodeTree.ResolveResult resolveResult3 = inodeTree.resolve("/a/b/c/d/f/g/h/i", false); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult3.kind); - Assert.assertEquals("/a/b/c/d", resolveResult3.resolvedPath); - Assert.assertEquals(new Path("/f/g/h/i"), resolveResult3.remainingPath); - Assert.assertTrue(resolveResult3.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN3_TARGET, ((TestNestMountPointFileSystem) resolveResult3.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult3.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult3.kind); + assertEquals("/a/b/c/d", resolveResult3.resolvedPath); + assertEquals(new Path("/f/g/h/i"), resolveResult3.remainingPath); + assertTrue(resolveResult3.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN3_TARGET, + ((TestNestMountPointFileSystem) resolveResult3.targetFileSystem).getUri()); + assertTrue(resolveResult3.isLastInternalDirLink()); } @Test public void testMultiNestedMountPointsPathResolveToDirLink() throws Exception { // /a/b/f resolves to /a/b and /f InodeTree.ResolveResult resolveResult = inodeTree.resolve("/a/b/f", true); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); - Assert.assertEquals("/a/b", resolveResult.resolvedPath); - Assert.assertEquals(new Path("/f"), resolveResult.remainingPath); - Assert.assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN1_TARGET, ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); + assertEquals("/a/b", resolveResult.resolvedPath); + assertEquals(new Path("/f"), resolveResult.remainingPath); + assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN1_TARGET, + ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); + assertTrue(resolveResult.isLastInternalDirLink()); // /a/b resolves to /a/b and / InodeTree.ResolveResult resolveResult2 = inodeTree.resolve("/a/b", true); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult2.kind); - Assert.assertEquals("/a/b", resolveResult2.resolvedPath); - Assert.assertEquals(new Path("/"), resolveResult2.remainingPath); - Assert.assertTrue(resolveResult2.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN1_TARGET, ((TestNestMountPointFileSystem) resolveResult2.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult2.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult2.kind); + assertEquals("/a/b", resolveResult2.resolvedPath); + assertEquals(new Path("/"), resolveResult2.remainingPath); + assertTrue(resolveResult2.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN1_TARGET, + ((TestNestMountPointFileSystem) resolveResult2.targetFileSystem).getUri()); + assertTrue(resolveResult2.isLastInternalDirLink()); } @Test public void testMultiNestedMountPointsPathResolveToDirLinkNotResolveLastComponent() throws Exception { // /a/b/f resolves to /a/b and /f InodeTree.ResolveResult resolveResult = inodeTree.resolve("/a/b/f", false); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); - Assert.assertEquals("/a/b", resolveResult.resolvedPath); - Assert.assertEquals(new Path("/f"), resolveResult.remainingPath); - Assert.assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN1_TARGET, ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); + assertEquals("/a/b", resolveResult.resolvedPath); + assertEquals(new Path("/f"), resolveResult.remainingPath); + assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN1_TARGET, + ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); + assertTrue(resolveResult.isLastInternalDirLink()); // /a/b resolves to /a and /b InodeTree.ResolveResult resolveResult2 = inodeTree.resolve("/a/b", false); - Assert.assertEquals(InodeTree.ResultKind.INTERNAL_DIR, resolveResult2.kind); - Assert.assertEquals("/a", resolveResult2.resolvedPath); - Assert.assertEquals(new Path("/b"), resolveResult2.remainingPath); - Assert.assertTrue(resolveResult2.targetFileSystem instanceof TestNestMountPointInternalFileSystem); - Assert.assertEquals(fsUri, ((TestNestMountPointInternalFileSystem) resolveResult2.targetFileSystem).getUri()); - Assert.assertFalse(resolveResult2.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.INTERNAL_DIR, resolveResult2.kind); + assertEquals("/a", resolveResult2.resolvedPath); + assertEquals(new Path("/b"), resolveResult2.remainingPath); + assertTrue(resolveResult2.targetFileSystem instanceof TestNestMountPointInternalFileSystem); + assertEquals(fsUri, + ((TestNestMountPointInternalFileSystem) resolveResult2.targetFileSystem).getUri()); + assertFalse(resolveResult2.isLastInternalDirLink()); } @Test public void testPathResolveToDirLinkLastComponentInternalDir() throws Exception { // /a/b/c resolves to /a/b and /c InodeTree.ResolveResult resolveResult = inodeTree.resolve("/a/b/c", true); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); - Assert.assertEquals("/a/b", resolveResult.resolvedPath); - Assert.assertEquals(new Path("/c"), resolveResult.remainingPath); - Assert.assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN1_TARGET, ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); + assertEquals("/a/b", resolveResult.resolvedPath); + assertEquals(new Path("/c"), resolveResult.remainingPath); + assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN1_TARGET, + ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); + assertTrue(resolveResult.isLastInternalDirLink()); } @Test public void testPathResolveToDirLinkLastComponentInternalDirNotResolveLastComponent() throws Exception { // /a/b/c resolves to /a/b and /c InodeTree.ResolveResult resolveResult = inodeTree.resolve("/a/b/c", false); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); - Assert.assertEquals("/a/b", resolveResult.resolvedPath); - Assert.assertEquals(new Path("/c"), resolveResult.remainingPath); - Assert.assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(NN1_TARGET, ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); - Assert.assertTrue(resolveResult.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); + assertEquals("/a/b", resolveResult.resolvedPath); + assertEquals(new Path("/c"), resolveResult.remainingPath); + assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(NN1_TARGET, + ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); + assertTrue(resolveResult.isLastInternalDirLink()); } @Test public void testPathResolveToLinkFallBack() throws Exception { // /a/e resolves to linkfallback InodeTree.ResolveResult resolveResult = inodeTree.resolve("/a/e", true); - Assert.assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); - Assert.assertEquals("/", resolveResult.resolvedPath); - Assert.assertEquals(new Path("/a/e"), resolveResult.remainingPath); - Assert.assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); - Assert.assertEquals(LINKFALLBACK_TARGET, ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); - Assert.assertFalse(resolveResult.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.EXTERNAL_DIR, resolveResult.kind); + assertEquals("/", resolveResult.resolvedPath); + assertEquals(new Path("/a/e"), resolveResult.remainingPath); + assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointFileSystem); + assertEquals(LINKFALLBACK_TARGET, + ((TestNestMountPointFileSystem) resolveResult.targetFileSystem).getUri()); + assertFalse(resolveResult.isLastInternalDirLink()); } @Test public void testPathNotResolveToLinkFallBackNotResolveLastComponent() throws Exception { // /a/e resolves to internalDir instead of linkfallback InodeTree.ResolveResult resolveResult = inodeTree.resolve("/a/e", false); - Assert.assertEquals(InodeTree.ResultKind.INTERNAL_DIR, resolveResult.kind); - Assert.assertEquals("/a", resolveResult.resolvedPath); - Assert.assertEquals(new Path("/e"), resolveResult.remainingPath); - Assert.assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointInternalFileSystem); - Assert.assertEquals(fsUri, ((TestNestMountPointInternalFileSystem) resolveResult.targetFileSystem).getUri()); - Assert.assertFalse(resolveResult.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.INTERNAL_DIR, resolveResult.kind); + assertEquals("/a", resolveResult.resolvedPath); + assertEquals(new Path("/e"), resolveResult.remainingPath); + assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointInternalFileSystem); + assertEquals(fsUri, + ((TestNestMountPointInternalFileSystem) resolveResult.targetFileSystem).getUri()); + assertFalse(resolveResult.isLastInternalDirLink()); } @Test public void testPathResolveToInternalDir() throws Exception { // /b/c resolves to internal dir InodeTree.ResolveResult resolveResult = inodeTree.resolve("/b/c", true); - Assert.assertEquals(InodeTree.ResultKind.INTERNAL_DIR, resolveResult.kind); - Assert.assertEquals("/b/c", resolveResult.resolvedPath); - Assert.assertEquals(new Path("/"), resolveResult.remainingPath); - Assert.assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointInternalFileSystem); - Assert.assertEquals(fsUri, ((TestNestMountPointInternalFileSystem) resolveResult.targetFileSystem).getUri()); - Assert.assertFalse(resolveResult.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.INTERNAL_DIR, resolveResult.kind); + assertEquals("/b/c", resolveResult.resolvedPath); + assertEquals(new Path("/"), resolveResult.remainingPath); + assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointInternalFileSystem); + assertEquals(fsUri, + ((TestNestMountPointInternalFileSystem) resolveResult.targetFileSystem).getUri()); + assertFalse(resolveResult.isLastInternalDirLink()); } @Test public void testPathResolveToInternalDirNotResolveLastComponent() throws Exception { // /b/c resolves to internal dir InodeTree.ResolveResult resolveResult = inodeTree.resolve("/b/c", false); - Assert.assertEquals(InodeTree.ResultKind.INTERNAL_DIR, resolveResult.kind); - Assert.assertEquals("/b", resolveResult.resolvedPath); - Assert.assertEquals(new Path("/c"), resolveResult.remainingPath); - Assert.assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointInternalFileSystem); - Assert.assertEquals(fsUri, ((TestNestMountPointInternalFileSystem) resolveResult.targetFileSystem).getUri()); - Assert.assertFalse(resolveResult.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.INTERNAL_DIR, resolveResult.kind); + assertEquals("/b", resolveResult.resolvedPath); + assertEquals(new Path("/c"), resolveResult.remainingPath); + assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointInternalFileSystem); + assertEquals(fsUri, + ((TestNestMountPointInternalFileSystem) resolveResult.targetFileSystem).getUri()); + assertFalse(resolveResult.isLastInternalDirLink()); } @Test public void testSlashResolveToInternalDir() throws Exception { // / resolves to internal dir InodeTree.ResolveResult resolveResult = inodeTree.resolve("/", true); - Assert.assertEquals(InodeTree.ResultKind.INTERNAL_DIR, resolveResult.kind); - Assert.assertEquals("/", resolveResult.resolvedPath); - Assert.assertEquals(new Path("/"), resolveResult.remainingPath); - Assert.assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointInternalFileSystem); - Assert.assertFalse(resolveResult.isLastInternalDirLink()); + assertEquals(InodeTree.ResultKind.INTERNAL_DIR, resolveResult.kind); + assertEquals("/", resolveResult.resolvedPath); + assertEquals(new Path("/"), resolveResult.remainingPath); + assertTrue(resolveResult.targetFileSystem instanceof TestNestMountPointInternalFileSystem); + assertFalse(resolveResult.isLastInternalDirLink()); } @Test public void testInodeTreeMountPoints() throws Exception { List> mountPoints = inodeTree.getMountPoints(); - Assert.assertEquals(6, mountPoints.size()); + assertEquals(6, mountPoints.size()); } } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestRegexMountPoint.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestRegexMountPoint.java index a5df2bab41322..4267955aa268f 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestRegexMountPoint.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestRegexMountPoint.java @@ -17,6 +17,9 @@ */ package org.apache.hadoop.fs.viewfs; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + import java.util.function.Function; import java.io.IOException; import java.net.URI; @@ -24,10 +27,9 @@ import java.util.Set; import org.apache.hadoop.conf.Configuration; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -55,7 +57,7 @@ public URI getUri() { } } - @Before + @BeforeEach public void setUp() throws Exception { conf = new Configuration(); ConfigUtil.addLink(conf, TestRegexMountPoint.class.getName(), "/mnt", @@ -88,7 +90,7 @@ protected TestRegexMountPointFileSystem getTargetFileSystem( }; } - @After + @AfterEach public void tearDown() throws Exception { inodeTree = null; } @@ -101,14 +103,14 @@ public void testGetVarListInString() throws IOException { new RegexMountPoint(inodeTree, srcRegex, target, null); regexMountPoint.initialize(); Map> varMap = regexMountPoint.getVarInDestPathMap(); - Assert.assertEquals(varMap.size(), 3); - Assert.assertEquals(varMap.get("0").size(), 1); - Assert.assertTrue(varMap.get("0").contains("$0")); - Assert.assertEquals(varMap.get("1").size(), 2); - Assert.assertTrue(varMap.get("1").contains("${1}")); - Assert.assertTrue(varMap.get("1").contains("$1")); - Assert.assertEquals(varMap.get("2").size(), 1); - Assert.assertTrue(varMap.get("2").contains("${2}")); + assertEquals(varMap.size(), 3); + assertEquals(varMap.get("0").size(), 1); + assertTrue(varMap.get("0").contains("$0")); + assertEquals(varMap.get("1").size(), 2); + assertTrue(varMap.get("1").contains("${1}")); + assertTrue(varMap.get("1").contains("$1")); + assertEquals(varMap.get("2").size(), 1); + assertTrue(varMap.get("2").contains("${2}")); } @Test @@ -121,18 +123,18 @@ public void testResolve() throws IOException { regexMountPoint.initialize(); InodeTree.ResolveResult resolveResult = regexMountPoint.resolve("/user/hadoop/file1", true); - Assert.assertEquals(resolveResult.kind, InodeTree.ResultKind.EXTERNAL_DIR); - Assert.assertTrue( + assertEquals(resolveResult.kind, InodeTree.ResultKind.EXTERNAL_DIR); + assertTrue( resolveResult.targetFileSystem instanceof TestRegexMountPointFileSystem); - Assert.assertEquals("/user/hadoop", resolveResult.resolvedPath); - Assert.assertTrue( + assertEquals("/user/hadoop", resolveResult.resolvedPath); + assertTrue( resolveResult.targetFileSystem instanceof TestRegexMountPointFileSystem); - Assert.assertEquals("/namenode1/testResolve/hadoop", + assertEquals("/namenode1/testResolve/hadoop", ((TestRegexMountPointFileSystem) resolveResult.targetFileSystem) .getUri().toString()); - Assert.assertEquals("/file1", resolveResult.remainingPath.toString()); + assertEquals("/file1", resolveResult.remainingPath.toString()); } @Test @@ -149,18 +151,18 @@ public void testResolveWithInterceptor() throws IOException { regexMountPoint.initialize(); InodeTree.ResolveResult resolveResult = regexMountPoint.resolve("/user/hadoop_user1/file_index", true); - Assert.assertEquals(resolveResult.kind, InodeTree.ResultKind.EXTERNAL_DIR); - Assert.assertTrue( + assertEquals(resolveResult.kind, InodeTree.ResultKind.EXTERNAL_DIR); + assertTrue( resolveResult.targetFileSystem instanceof TestRegexMountPointFileSystem); - Assert.assertEquals("/user/hadoop_user1", resolveResult.resolvedPath); - Assert.assertTrue( + assertEquals("/user/hadoop_user1", resolveResult.resolvedPath); + assertTrue( resolveResult.targetFileSystem instanceof TestRegexMountPointFileSystem); - Assert.assertEquals("/namenode1/testResolve/hadoop-user1", + assertEquals("/namenode1/testResolve/hadoop-user1", ((TestRegexMountPointFileSystem) resolveResult.targetFileSystem) .getUri().toString()); - Assert.assertEquals("/file_index", + assertEquals("/file_index", resolveResult.remainingPath.toString()); } } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFileSystemLocalFileSystem.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFileSystemLocalFileSystem.java index 9ba4d4b3daecf..6dc7c36110888 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFileSystemLocalFileSystem.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFileSystemLocalFileSystem.java @@ -17,7 +17,6 @@ */ package org.apache.hadoop.fs.viewfs; - import java.io.IOException; import java.net.URI; @@ -38,7 +37,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFileSystemWithAuthorityLocalFileSystem.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFileSystemWithAuthorityLocalFileSystem.java index 3049bac0a8080..a475bfb2cadb9 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFileSystemWithAuthorityLocalFileSystem.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFileSystemWithAuthorityLocalFileSystem.java @@ -17,6 +17,7 @@ */ package org.apache.hadoop.fs.viewfs; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.net.URI; @@ -30,7 +31,6 @@ import java.io.IOException; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -76,15 +76,16 @@ Path getTrashRootInFallBackFS() throws IOException { @Override @Test public void testBasicPaths() { - Assertions.assertEquals(fsView.getUri(), schemeWithAuthority); - Assertions.assertEquals(fsView.getWorkingDirectory(), - fsView.makeQualified( - new Path("/user/" + System.getProperty("user.name")))); - Assertions.assertEquals(fsView.getHomeDirectory(), - fsView.makeQualified( - new Path("/user/" + System.getProperty("user.name")))); - Assertions.assertEquals( - fsView.makeQualified(new Path("/foo/bar")), - new Path("/foo/bar").makeQualified(schemeWithAuthority, null)); + assertEquals(schemeWithAuthority, + fsView.getUri()); + assertEquals(fsView.makeQualified( + new Path("/user/" + System.getProperty("user.name"))), + fsView.getWorkingDirectory()); + assertEquals(fsView.makeQualified( + new Path("/user/" + System.getProperty("user.name"))), + fsView.getHomeDirectory()); + assertEquals( + new Path("/foo/bar").makeQualified(schemeWithAuthority, null), + fsView.makeQualified(new Path("/foo/bar"))); } } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsOverloadSchemeListStatus.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsOverloadSchemeListStatus.java index 7afc78981f6e3..d1c6c18f50222 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsOverloadSchemeListStatus.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsOverloadSchemeListStatus.java @@ -32,12 +32,15 @@ import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.permission.FsPermission; import org.apache.hadoop.test.GenericTestUtils; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * ViewFsOverloadScheme ListStatus. @@ -49,7 +52,7 @@ public class TestViewFsOverloadSchemeListStatus { private Configuration conf; private static final String FILE_NAME = "file"; - @Before + @BeforeEach public void setUp() { conf = new Configuration(); conf.set(String.format("fs.%s.impl", FILE_NAME), @@ -61,7 +64,7 @@ public void setUp() { assertTrue(TEST_DIR.mkdirs()); } - @After + @AfterEach public void tearDown() throws IOException { FileUtil.fullyDelete(TEST_DIR); } @@ -130,7 +133,8 @@ public void testListStatusACL() throws IOException, URISyntaxException { * if there are no mount links configured. It should add fallback with the * chrootedFS at it's uri's root. */ - @Test(timeout = 30000) + @Test + @Timeout(value = 30) public void testViewFSOverloadSchemeWithoutAnyMountLinks() throws Exception { Path initUri = new Path(TEST_DIR.toURI().toString(), "init"); try (FileSystem fs = FileSystem.get(initUri.toUri(), conf)) { @@ -154,7 +158,7 @@ public void testViewFSOverloadSchemeWithoutAnyMountLinks() throws Exception { } } - @AfterClass + @AfterAll public static void cleanup() throws IOException { FileUtil.fullyDelete(TEST_DIR); } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java index 4b3f496609695..7d37bddf2a0b8 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java @@ -61,8 +61,6 @@ import org.apache.hadoop.security.token.Token; import org.apache.hadoop.test.GenericTestUtils; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -75,7 +73,15 @@ import static org.apache.hadoop.fs.FileSystem.TRASH_PREFIX; import static org.apache.hadoop.test.GenericTestUtils.assertExceptionContains; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assumptions.assumeTrue; /** *

@@ -174,7 +180,7 @@ public void testGetMountPoints() { LOG.info("MountPoint: " + mountPoint.getMountedOnPath() + " => " + mountPoint.getTargetFileSystemURIs()[0]); } - Assertions.assertEquals(mountPoints.length, getExpectedMountPoints()); + assertEquals(mountPoints.length, getExpectedMountPoints()); } int getExpectedMountPoints() { @@ -190,7 +196,7 @@ int getExpectedMountPoints() { public void testGetDelegationTokens() throws IOException { Token[] delTokens = fsView.addDelegationTokens("sanjay", new Credentials()); - Assertions.assertEquals(delTokens.length, getExpectedDelegationTokenCount()); + assertEquals(delTokens.length, getExpectedDelegationTokenCount()); } int getExpectedDelegationTokenCount() { @@ -205,7 +211,7 @@ public void testGetDelegationTokensWithCredentials() throws IOException { int expectedTokenCount = getExpectedDelegationTokenCountWithCredentials(); - Assertions.assertEquals(delTokens.size(), expectedTokenCount); + assertEquals(delTokens.size(), expectedTokenCount); Credentials newCredentials = new Credentials(); for (int i = 0; i < expectedTokenCount / 2; i++) { Token token = delTokens.get(i); @@ -214,7 +220,7 @@ public void testGetDelegationTokensWithCredentials() throws IOException { List> delTokens2 = Arrays.asList(fsView.addDelegationTokens("sanjay", newCredentials)); - Assertions.assertEquals(delTokens2.size(), (expectedTokenCount + 1) / 2); + assertEquals(delTokens2.size(), (expectedTokenCount + 1) / 2); } int getExpectedDelegationTokenCountWithCredentials() { @@ -223,15 +229,15 @@ int getExpectedDelegationTokenCountWithCredentials() { @Test public void testBasicPaths() { - Assertions.assertEquals(fsView.getUri(), FsConstants.VIEWFS_URI); - Assertions.assertEquals(fsView.getWorkingDirectory(), + assertEquals(fsView.getUri(), FsConstants.VIEWFS_URI); + assertEquals(fsView.getWorkingDirectory(), fsView.makeQualified( new Path("/user/" + System.getProperty("user.name")))); - Assertions.assertEquals( + assertEquals( fsView.getHomeDirectory(), fsView.makeQualified( new Path("/user/" + System.getProperty("user.name")))); - Assertions.assertEquals( + assertEquals( fsView.makeQualified(new Path("/foo/bar")), new Path("/foo/bar").makeQualified(FsConstants.VIEWFS_URI, null)); } @@ -259,116 +265,116 @@ private void testOperationsThroughMountLinksInternal(boolean located) throws IOException { // Create file fileSystemTestHelper.createFile(fsView, "/user/foo"); - Assertions.assertTrue(fsView.isFile(new Path("/user/foo")), + assertTrue(fsView.isFile(new Path("/user/foo")), "Created file should be type file"); - Assertions.assertTrue(fsTarget.isFile(new Path(targetTestRoot,"user/foo")), + assertTrue(fsTarget.isFile(new Path(targetTestRoot, "user/foo")), "Target of created file should be type file"); // Delete the created file - Assertions.assertTrue(fsView.delete(new Path("/user/foo"), false), + assertTrue(fsView.delete(new Path("/user/foo"), false), "Delete should succeed"); - Assertions.assertFalse(fsView.exists(new Path("/user/foo")), + assertFalse(fsView.exists(new Path("/user/foo")), "File should not exist after delete"); - Assertions.assertFalse(fsTarget.exists(new Path(targetTestRoot,"user/foo")), + assertFalse(fsTarget.exists(new Path(targetTestRoot, "user/foo")), "Target File should not exist after delete"); // Create file with a 2 component dirs fileSystemTestHelper.createFile(fsView, "/internalDir/linkToDir2/foo"); - Assertions.assertTrue(fsView.isFile(new Path("/internalDir/linkToDir2/foo")), + assertTrue(fsView.isFile(new Path("/internalDir/linkToDir2/foo")), "Created file should be type file"); - Assertions.assertTrue(fsTarget.isFile(new Path(targetTestRoot,"dir2/foo")), + assertTrue(fsTarget.isFile(new Path(targetTestRoot, "dir2/foo")), "Target of created file should be type file"); // Delete the created file - Assertions.assertTrue(fsView.delete(new Path("/internalDir/linkToDir2/foo"), false), + assertTrue(fsView.delete(new Path("/internalDir/linkToDir2/foo"), false), "Delete should succeed"); - Assertions.assertFalse(fsView.exists(new Path("/internalDir/linkToDir2/foo")), + assertFalse(fsView.exists(new Path("/internalDir/linkToDir2/foo")), "File should not exist after delete"); - Assertions.assertFalse(fsTarget.exists(new Path(targetTestRoot,"dir2/foo")), + assertFalse(fsTarget.exists(new Path(targetTestRoot, "dir2/foo")), "Target File should not exist after delete"); // Create file with a 3 component dirs fileSystemTestHelper.createFile(fsView, "/internalDir/internalDir2/linkToDir3/foo"); - Assertions.assertTrue(fsView.isFile(new Path("/internalDir/internalDir2/linkToDir3/foo")), + assertTrue(fsView.isFile(new Path("/internalDir/internalDir2/linkToDir3/foo")), "Created file should be type file"); - Assertions.assertTrue(fsTarget.isFile(new Path(targetTestRoot,"dir3/foo")), + assertTrue(fsTarget.isFile(new Path(targetTestRoot, "dir3/foo")), "Target of created file should be type file"); // Recursive Create file with missing dirs fileSystemTestHelper.createFile(fsView, "/internalDir/linkToDir2/missingDir/miss2/foo"); - Assertions.assertTrue(fsView.isFile(new Path("/internalDir/linkToDir2/missingDir/miss2/foo")), + assertTrue(fsView.isFile(new Path("/internalDir/linkToDir2/missingDir/miss2/foo")), "Created file should be type file"); - Assertions.assertTrue(fsTarget.isFile(new Path(targetTestRoot,"dir2/missingDir/miss2/foo")), + assertTrue(fsTarget.isFile(new Path(targetTestRoot, "dir2/missingDir/miss2/foo")), "Target of created file should be type file"); // Delete the created file - Assertions.assertTrue(fsView.delete( + assertTrue(fsView.delete( new Path("/internalDir/internalDir2/linkToDir3/foo"), false), "Delete should succeed"); - Assertions.assertFalse(fsView.exists(new Path("/internalDir/internalDir2/linkToDir3/foo")), + assertFalse(fsView.exists(new Path("/internalDir/internalDir2/linkToDir3/foo")), "File should not exist after delete"); - Assertions.assertFalse(fsTarget.exists(new Path(targetTestRoot,"dir3/foo")), + assertFalse(fsTarget.exists(new Path(targetTestRoot, "dir3/foo")), "Target File should not exist after delete"); // mkdir fsView.mkdirs(fileSystemTestHelper.getTestRootPath(fsView, "/user/dirX")); - Assertions.assertTrue(fsView.isDirectory(new Path("/user/dirX")), + assertTrue(fsView.isDirectory(new Path("/user/dirX")), "New dir should be type dir"); - Assertions.assertTrue(fsTarget.isDirectory(new Path(targetTestRoot,"user/dirX")), + assertTrue(fsTarget.isDirectory(new Path(targetTestRoot, "user/dirX")), "Target of new dir should be of type dir"); fsView.mkdirs( fileSystemTestHelper.getTestRootPath(fsView, "/user/dirX/dirY")); - Assertions.assertTrue(fsView.isDirectory(new Path("/user/dirX/dirY")), + assertTrue(fsView.isDirectory(new Path("/user/dirX/dirY")), "New dir should be type dir"); - Assertions.assertTrue(fsTarget.isDirectory(new Path(targetTestRoot,"user/dirX/dirY")), + assertTrue(fsTarget.isDirectory(new Path(targetTestRoot, "user/dirX/dirY")), "Target of new dir should be of type dir"); // Delete the created dir - Assertions.assertTrue(fsView.delete(new Path("/user/dirX/dirY"), false), + assertTrue(fsView.delete(new Path("/user/dirX/dirY"), false), "Delete should succeed"); - Assertions.assertFalse(fsView.exists(new Path("/user/dirX/dirY")), + assertFalse(fsView.exists(new Path("/user/dirX/dirY")), "File should not exist after delete"); - Assertions.assertFalse(fsTarget.exists(new Path(targetTestRoot,"user/dirX/dirY")), + assertFalse(fsTarget.exists(new Path(targetTestRoot, "user/dirX/dirY")), "Target File should not exist after delete"); - Assertions.assertTrue(fsView.delete(new Path("/user/dirX"), false), + assertTrue(fsView.delete(new Path("/user/dirX"), false), "Delete should succeed"); - Assertions.assertFalse(fsView.exists(new Path("/user/dirX")), + assertFalse(fsView.exists(new Path("/user/dirX")), "File should not exist after delete"); - Assertions.assertFalse(fsTarget.exists(new Path(targetTestRoot,"user/dirX"))); + assertFalse(fsTarget.exists(new Path(targetTestRoot, "user/dirX"))); // Rename a file fileSystemTestHelper.createFile(fsView, "/user/foo"); fsView.rename(new Path("/user/foo"), new Path("/user/fooBar")); - Assertions.assertFalse(fsView.exists(new Path("/user/foo")), + assertFalse(fsView.exists(new Path("/user/foo")), "Renamed src should not exist"); - Assertions.assertFalse(fsTarget.exists(new Path(targetTestRoot,"user/foo")), + assertFalse(fsTarget.exists(new Path(targetTestRoot, "user/foo")), "Renamed src should not exist in target"); - Assertions.assertTrue(fsView.isFile(fileSystemTestHelper.getTestRootPath(fsView,"/user/fooBar")), + assertTrue(fsView.isFile(fileSystemTestHelper.getTestRootPath(fsView, "/user/fooBar")), "Renamed dest should exist as file"); - Assertions.assertTrue(fsTarget.isFile(new Path(targetTestRoot,"user/fooBar")), + assertTrue(fsTarget.isFile(new Path(targetTestRoot, "user/fooBar")), "Renamed dest should exist as file in target"); fsView.mkdirs(new Path("/user/dirFoo")); fsView.rename(new Path("/user/dirFoo"), new Path("/user/dirFooBar")); - Assertions.assertFalse(fsView.exists(new Path("/user/dirFoo")), + assertFalse(fsView.exists(new Path("/user/dirFoo")), "Renamed src should not exist"); - Assertions.assertFalse(fsTarget.exists(new Path(targetTestRoot,"user/dirFoo")), + assertFalse(fsTarget.exists(new Path(targetTestRoot, "user/dirFoo")), "Renamed src should not exist in target"); - Assertions.assertTrue(fsView.isDirectory(fileSystemTestHelper.getTestRootPath(fsView,"/user/dirFooBar")), + assertTrue(fsView.isDirectory(fileSystemTestHelper.getTestRootPath(fsView, "/user/dirFooBar")), "Renamed dest should exist as dir"); - Assertions.assertTrue(fsTarget.isDirectory(new Path(targetTestRoot,"user/dirFooBar")), + assertTrue(fsTarget.isDirectory(new Path(targetTestRoot, "user/dirFooBar")), "Renamed dest should exist as dir in target"); // Make a directory under a directory that's mounted from the root of another FS fsView.mkdirs(new Path("/targetRoot/dirFoo")); - Assertions.assertTrue(fsView.exists(new Path("/targetRoot/dirFoo"))); + assertTrue(fsView.exists(new Path("/targetRoot/dirFoo"))); boolean dirFooPresent = false; for (FileStatus fileStatus : listStatusInternal(located, new Path("/targetRoot/"))) { @@ -376,7 +382,7 @@ private void testOperationsThroughMountLinksInternal(boolean located) dirFooPresent = true; } } - Assertions.assertTrue(dirFooPresent); + assertTrue(dirFooPresent); } // rename across mount points that point to same target also fail @@ -554,32 +560,32 @@ public void testOperationsThroughNestedMountPointsInternal() setUpNestedMountPoint(); // Create file with nested mount point fileSystemTestHelper.createFile(fsView, "/user/userB/foo"); - Assertions.assertTrue(fsView.getFileStatus(new Path("/user/userB/foo")).isFile(), + assertTrue(fsView.getFileStatus(new Path("/user/userB/foo")).isFile(), "Created file should be type file"); - Assertions.assertTrue(fsTarget.getFileStatus(new Path(targetTestRoot,"userB/foo")).isFile(), + assertTrue(fsTarget.getFileStatus(new Path(targetTestRoot, "userB/foo")).isFile(), "Target of created file should be type file"); // Delete the created file with nested mount point - Assertions.assertTrue(fsView.delete(new Path("/user/userB/foo"), false), + assertTrue(fsView.delete(new Path("/user/userB/foo"), false), "Delete should succeed"); - Assertions.assertFalse(fsView.exists(new Path("/user/userB/foo")), + assertFalse(fsView.exists(new Path("/user/userB/foo")), "File should not exist after delete"); - Assertions.assertFalse(fsTarget.exists(new Path(targetTestRoot,"userB/foo")), + assertFalse(fsTarget.exists(new Path(targetTestRoot, "userB/foo")), "Target File should not exist after delete"); // Create file with a 2 component dirs with nested mount point fileSystemTestHelper.createFile(fsView, "/internalDir/linkToDir2/linkToDir2/foo"); - Assertions.assertTrue(fsView.getFileStatus(new Path("/internalDir/linkToDir2/linkToDir2/foo")).isFile(), + assertTrue(fsView.getFileStatus(new Path("/internalDir/linkToDir2/linkToDir2/foo")).isFile(), "Created file should be type file"); - Assertions.assertTrue(fsTarget.getFileStatus(new Path(targetTestRoot,"linkToDir2/foo")).isFile(), + assertTrue(fsTarget.getFileStatus(new Path(targetTestRoot, "linkToDir2/foo")).isFile(), "Target of created file should be type file"); // Delete the created file with nested mount point - Assertions.assertTrue(fsView.delete(new Path("/internalDir/linkToDir2/linkToDir2/foo"), false), + assertTrue(fsView.delete(new Path("/internalDir/linkToDir2/linkToDir2/foo"), false), "Delete should succeed"); - Assertions.assertFalse(fsView.exists(new Path("/internalDir/linkToDir2/linkToDir2/foo")), + assertFalse(fsView.exists(new Path("/internalDir/linkToDir2/linkToDir2/foo")), "File should not exist after delete"); - Assertions.assertFalse(fsTarget.exists(new Path(targetTestRoot,"linkToDir2/foo")), + assertFalse(fsTarget.exists(new Path(targetTestRoot, "linkToDir2/foo")), "Target File should not exist after delete"); } @@ -607,10 +613,10 @@ public void testGetBlockLocations() throws IOException { FileSystemTestHelper.createFile(fsTarget, targetFilePath, 10, 1024); Path viewFilePath = new Path("/data/largeFile"); - Assertions.assertTrue(fsView.isFile(viewFilePath), + assertTrue(fsView.isFile(viewFilePath), "Created File should be type File"); BlockLocation[] viewBL = fsView.getFileBlockLocations(fsView.getFileStatus(viewFilePath), 0, 10240+100); - Assertions.assertEquals(SupportsBlocks ? 10 : 1, viewBL.length); + assertEquals(SupportsBlocks ? 10 : 1, viewBL.length); BlockLocation[] targetBL = fsTarget.getFileBlockLocations(fsTarget.getFileStatus(targetFilePath), 0, 10240+100); compareBLs(viewBL, targetBL); @@ -624,12 +630,12 @@ public void testGetBlockLocations() throws IOException { } void compareBLs(BlockLocation[] viewBL, BlockLocation[] targetBL) { - Assertions.assertEquals(viewBL.length, targetBL.length); + assertEquals(viewBL.length, targetBL.length); int i = 0; for (BlockLocation vbl : viewBL) { - Assertions.assertEquals(vbl.toString(), targetBL[i].toString()); - Assertions.assertEquals(vbl.getOffset(), targetBL[i].getOffset()); - Assertions.assertEquals(vbl.getLength(), targetBL[i].getLength()); + assertEquals(vbl.toString(), targetBL[i].toString()); + assertEquals(vbl.getOffset(), targetBL[i].getOffset()); + assertEquals(vbl.getLength(), targetBL[i].getLength()); i++; } } @@ -663,35 +669,35 @@ private void testListOnInternalDirsOfMountTableInternal(boolean located) // list on internal dir dirPaths = listStatusInternal(located, new Path("/internalDir")); - Assertions.assertEquals(dirPaths.length, 2); + assertEquals(dirPaths.length, 2); fs = fileSystemTestHelper.containsPath(fsView, "/internalDir/internalDir2", dirPaths); - Assertions.assertNotNull(fs); - Assertions.assertTrue(fs.isDirectory(), "A mount should appear as symlink"); + assertNotNull(fs); + assertTrue(fs.isDirectory(), "A mount should appear as symlink"); fs = fileSystemTestHelper.containsPath(fsView, "/internalDir/linkToDir2", dirPaths); - Assertions.assertNotNull(fs); - Assertions.assertTrue(fs.isSymlink(), "A mount should appear as symlink"); + assertNotNull(fs); + assertTrue(fs.isSymlink(), "A mount should appear as symlink"); } private void verifyRootChildren(FileStatus[] dirPaths) throws IOException { FileStatus fs; - Assertions.assertEquals(dirPaths.length, getExpectedDirPaths()); + assertEquals(dirPaths.length, getExpectedDirPaths()); fs = fileSystemTestHelper.containsPath(fsView, "/user", dirPaths); - Assertions.assertNotNull(fs); - Assertions.assertTrue(fs.isSymlink(), "A mount should appear as symlink"); + assertNotNull(fs); + assertTrue(fs.isSymlink(), "A mount should appear as symlink"); fs = fileSystemTestHelper.containsPath(fsView, "/data", dirPaths); - Assertions.assertNotNull(fs); - Assertions.assertTrue(fs.isSymlink(), "A mount should appear as symlink"); + assertNotNull(fs); + assertTrue(fs.isSymlink(), "A mount should appear as symlink"); fs = fileSystemTestHelper.containsPath(fsView, "/internalDir", dirPaths); - Assertions.assertNotNull(fs); - Assertions.assertTrue(fs.isDirectory(), "A mount should appear as symlink"); + assertNotNull(fs); + assertTrue(fs.isDirectory(), "A mount should appear as symlink"); fs = fileSystemTestHelper.containsPath(fsView, "/danglingLink", dirPaths); - Assertions.assertNotNull(fs); - Assertions.assertTrue(fs.isSymlink(), "A mount should appear as symlink"); + assertNotNull(fs); + assertTrue(fs.isSymlink(), "A mount should appear as symlink"); fs = fileSystemTestHelper.containsPath(fsView, "/linkToAFile", dirPaths); - Assertions.assertNotNull(fs); - Assertions.assertTrue(fs.isSymlink(), "A mount should appear as symlink"); + assertNotNull(fs); + assertTrue(fs.isSymlink(), "A mount should appear as symlink"); } int getExpectedDirPaths() { @@ -715,27 +721,27 @@ private void testListOnMountTargetDirsInternal(boolean located) FileStatus[] dirPaths = listStatusInternal(located, dataPath); FileStatus fs; - Assertions.assertEquals(dirPaths.length, 0); + assertEquals(dirPaths.length, 0); // add a file long len = fileSystemTestHelper.createFile(fsView, "/data/foo"); dirPaths = listStatusInternal(located, dataPath); - Assertions.assertEquals(dirPaths.length, 1); + assertEquals(dirPaths.length, 1); fs = fileSystemTestHelper.containsPath(fsView, "/data/foo", dirPaths); - Assertions.assertNotNull(fs); - Assertions.assertTrue(fs.isFile(), "Created file shoudl appear as a file"); - Assertions.assertEquals(fs.getLen(), len); + assertNotNull(fs); + assertTrue(fs.isFile(), "Created file shoudl appear as a file"); + assertEquals(fs.getLen(), len); // add a dir fsView.mkdirs(fileSystemTestHelper.getTestRootPath(fsView, "/data/dirX")); dirPaths = listStatusInternal(located, dataPath); - Assertions.assertEquals(dirPaths.length, 2); + assertEquals(dirPaths.length, 2); fs = fileSystemTestHelper.containsPath(fsView, "/data/foo", dirPaths); - Assertions.assertNotNull(fs); - Assertions.assertTrue(fs.isFile(), "Created file shoudl appear as a file"); + assertNotNull(fs); + assertTrue(fs.isFile(), "Created file shoudl appear as a file"); fs = fileSystemTestHelper.containsPath(fsView, "/data/dirX", dirPaths); - Assertions.assertNotNull(fs); - Assertions.assertTrue(fs.isDirectory(), "Created dir should appear as a dir"); + assertNotNull(fs); + assertTrue(fs.isDirectory(), "Created dir should appear as a dir"); } private FileStatus[] listStatusInternal(boolean located, Path dataPath) throws IOException { @@ -756,7 +762,7 @@ private FileStatus[] listStatusInternal(boolean located, Path dataPath) throws I @Test public void testFileStatusOnMountLink() throws IOException { - Assertions.assertTrue(fsView.getFileStatus(new Path("/")).isDirectory()); + assertTrue(fsView.getFileStatus(new Path("/")).isDirectory()); checkFileStatus(fsView, "/", fileType.isDir); checkFileStatus(fsView, "/user", fileType.isDir); // link followed => dir checkFileStatus(fsView, "/data", fileType.isDir); @@ -769,13 +775,13 @@ public void testFileStatusOnMountLink() throws IOException { @Test public void testgetFSonDanglingLink() throws IOException { - Assertions.assertThrows(FileNotFoundException.class, + assertThrows(FileNotFoundException.class, () -> fsView.getFileStatus(new Path("/danglingLink"))); } @Test public void testgetFSonNonExistingInternalDir() throws IOException { - Assertions.assertThrows(FileNotFoundException.class, + assertThrows(FileNotFoundException.class, () -> fsView.getFileStatus(new Path("/internalDir/nonExisting"))); } @@ -785,19 +791,19 @@ public void testgetFSonNonExistingInternalDir() throws IOException { @Test public void testResolvePathInternalPaths() throws IOException { - Assertions.assertEquals(fsView.resolvePath(new Path("/")), new Path("/")); - Assertions.assertEquals(fsView.resolvePath(new Path("/internalDir")), + assertEquals(fsView.resolvePath(new Path("/")), new Path("/")); + assertEquals(fsView.resolvePath(new Path("/internalDir")), new Path("/internalDir")); } @Test public void testResolvePathMountPoints() throws IOException { - Assertions.assertEquals(fsView.resolvePath(new Path("/user")), + assertEquals(fsView.resolvePath(new Path("/user")), new Path(targetTestRoot,"user")); - Assertions.assertEquals(fsView.resolvePath(new Path("/data")), + assertEquals(fsView.resolvePath(new Path("/data")), new Path(targetTestRoot,"data")); - Assertions.assertEquals(fsView.resolvePath(new Path("/internalDir/linkToDir2")), + assertEquals(fsView.resolvePath(new Path("/internalDir/linkToDir2")), new Path(targetTestRoot,"dir2")); - Assertions.assertEquals(fsView.resolvePath(new Path("/internalDir/internalDir2/linkToDir3")), + assertEquals(fsView.resolvePath(new Path("/internalDir/internalDir2/linkToDir3")), new Path(targetTestRoot,"dir3")); } @@ -805,30 +811,30 @@ public void testResolvePathMountPoints() throws IOException { @Test public void testResolvePathThroughMountPoints() throws IOException { fileSystemTestHelper.createFile(fsView, "/user/foo"); - Assertions.assertEquals(fsView.resolvePath(new Path("/user/foo")), + assertEquals(fsView.resolvePath(new Path("/user/foo")), new Path(targetTestRoot,"user/foo")); fsView.mkdirs( fileSystemTestHelper.getTestRootPath(fsView, "/user/dirX")); - Assertions.assertEquals(fsView.resolvePath(new Path("/user/dirX")), + assertEquals(fsView.resolvePath(new Path("/user/dirX")), new Path(targetTestRoot,"user/dirX")); - + fsView.mkdirs( fileSystemTestHelper.getTestRootPath(fsView, "/user/dirX/dirY")); - Assertions.assertEquals(fsView.resolvePath(new Path("/user/dirX/dirY")), + assertEquals(fsView.resolvePath(new Path("/user/dirX/dirY")), new Path(targetTestRoot,"user/dirX/dirY")); } @Test public void testResolvePathDanglingLink() throws IOException { - Assertions.assertThrows(FileNotFoundException.class, + assertThrows(FileNotFoundException.class, () -> fsView.resolvePath(new Path("/danglingLink"))); } @Test public void testResolvePathMissingThroughMountPoints() throws IOException { - Assertions.assertThrows(FileNotFoundException.class, + assertThrows(FileNotFoundException.class, () -> fsView.resolvePath(new Path("/user/nonExisting"))); } @@ -837,7 +843,7 @@ public void testResolvePathMissingThroughMountPoints() throws IOException { public void testResolvePathMissingThroughMountPoints2() throws IOException { fsView.mkdirs( fileSystemTestHelper.getTestRootPath(fsView, "/user/dirX")); - Assertions.assertThrows(FileNotFoundException.class, + assertThrows(FileNotFoundException.class, () -> fsView.resolvePath(new Path("/user/dirX/nonExisting"))); } @@ -853,29 +859,29 @@ public void testResolvePathMissingThroughMountPoints2() throws IOException { // Mkdir on existing internal mount table succeed except for / @Test public void testInternalMkdirSlash() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.mkdirs(fileSystemTestHelper.getTestRootPath(fsView, "/"))); } public void testInternalMkdirExisting1() throws IOException { - Assertions.assertTrue(fsView.mkdirs(fileSystemTestHelper.getTestRootPath(fsView, + assertTrue(fsView.mkdirs(fileSystemTestHelper.getTestRootPath(fsView, "/internalDir")), "mkdir of existing dir should succeed"); } public void testInternalMkdirExisting2() throws IOException { - Assertions.assertTrue(fsView.mkdirs(fileSystemTestHelper.getTestRootPath(fsView, + assertTrue(fsView.mkdirs(fileSystemTestHelper.getTestRootPath(fsView, "/internalDir/linkToDir2")), "mkdir of existing dir should succeed"); } // Mkdir for new internal mount table should fail @Test public void testInternalMkdirNew() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.mkdirs(fileSystemTestHelper.getTestRootPath(fsView, "/dirNew"))); } @Test public void testInternalMkdirNew2() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.mkdirs(fileSystemTestHelper.getTestRootPath(fsView, "/internalDir/dirNew"))); } @@ -883,32 +889,32 @@ public void testInternalMkdirNew2() throws IOException { @Test public void testInternalCreate1() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fileSystemTestHelper.createFile(fsView, "/foo")); // 1 component } @Test public void testInternalCreate2() throws IOException { // 2 component - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fileSystemTestHelper.createFile(fsView, "/internalDir/foo")); } @Test public void testInternalCreateMissingDir() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fileSystemTestHelper.createFile(fsView, "/missingDir/foo")); } @Test public void testInternalCreateMissingDir2() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fileSystemTestHelper.createFile(fsView, "/missingDir/miss2/foo")); } @Test public void testInternalCreateMissingDir3() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fileSystemTestHelper.createFile(fsView, "/internalDir/miss2/foo")); } @@ -916,24 +922,24 @@ public void testInternalCreateMissingDir3() throws IOException { @Test public void testInternalDeleteNonExisting() throws IOException { - Assertions.assertThrows(FileNotFoundException.class, + assertThrows(FileNotFoundException.class, () -> fsView.delete(new Path("/NonExisting"), false)); } @Test public void testInternalDeleteNonExisting2() throws IOException { - Assertions.assertThrows(FileNotFoundException.class, + assertThrows(FileNotFoundException.class, () -> fsView.delete(new Path("/internalDir/NonExisting"), false)); } @Test public void testInternalDeleteExisting() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.delete(new Path("/internalDir"), false)); } @Test public void testInternalDeleteExisting2() throws IOException { fsView.getFileStatus( new Path("/internalDir/linkToDir2")).isDirectory(); - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.delete(new Path("/internalDir/linkToDir2"), false)); } @@ -941,7 +947,7 @@ public void testInternalDeleteExisting2() throws IOException { public void testMkdirOfMountLink() throws IOException { // data exists - mkdirs returns true even though no permission in internal // mount table - Assertions.assertTrue(fsView.mkdirs(new Path("/data")), + assertTrue(fsView.mkdirs(new Path("/data")), "mkdir of existing mount link should succeed"); } @@ -950,35 +956,35 @@ public void testMkdirOfMountLink() throws IOException { @Test public void testInternalRename1() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.rename(new Path("/internalDir"), new Path("/newDir"))); } @Test public void testInternalRename2() throws IOException { fsView.getFileStatus(new Path("/internalDir/linkToDir2")).isDirectory(); - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.rename(new Path("/internalDir/linkToDir2"), new Path("/internalDir/dir1"))); } @Test public void testInternalRename3() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.rename(new Path("/user"), new Path("/internalDir/linkToDir2"))); } @Test public void testInternalRenameToSlash() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.rename(new Path("/internalDir/linkToDir2/foo"), new Path("/"))); } @Test public void testInternalRenameFromSlash() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.rename(new Path("/"), new Path("/bar"))); } @Test public void testInternalSetOwner() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.setOwner(new Path("/internalDir"), "foo", "bar")); } @@ -987,9 +993,9 @@ public void testCreateNonRecursive() throws IOException { Path path = fileSystemTestHelper.getTestRootPath(fsView, "/user/foo"); fsView.createNonRecursive(path, false, 1024, (short)1, 1024L, null); FileStatus status = fsView.getFileStatus(new Path("/user/foo")); - Assertions.assertTrue(fsView.isFile(new Path("/user/foo")), + assertTrue(fsView.isFile(new Path("/user/foo")), "Created file should be type file"); - Assertions.assertTrue(fsTarget.isFile(new Path(targetTestRoot, "user/foo")), + assertTrue(fsTarget.isFile(new Path(targetTestRoot, "user/foo")), "Target of created file should be type file"); } @@ -1007,10 +1013,10 @@ private void testRootReadableExecutableInternal(boolean located) throws IOException { // verify executable permission on root: cd / // - Assertions.assertFalse(fsView.getWorkingDirectory().isRoot(), + assertFalse(fsView.getWorkingDirectory().isRoot(), "In root before cd"); fsView.setWorkingDirectory(new Path("/")); - Assertions.assertTrue(fsView.getWorkingDirectory().isRoot(), + assertTrue(fsView.getWorkingDirectory().isRoot(), "Not in root dir after cd"); // verify readable @@ -1024,17 +1030,17 @@ private void testRootReadableExecutableInternal(boolean located) fsView.getFileStatus(fsView.getWorkingDirectory()); final FsPermission perms = rootStatus.getPermission(); - Assertions.assertTrue(perms.getUserAction().implies(FsAction.EXECUTE), + assertTrue(perms.getUserAction().implies(FsAction.EXECUTE), "User-executable permission not set!"); - Assertions.assertTrue(perms.getUserAction().implies(FsAction.READ), + assertTrue(perms.getUserAction().implies(FsAction.READ), "User-readable permission not set!"); - Assertions.assertTrue(perms.getGroupAction().implies(FsAction.EXECUTE), + assertTrue(perms.getGroupAction().implies(FsAction.EXECUTE), "Group-executable permission not set!"); - Assertions.assertTrue(perms.getGroupAction().implies(FsAction.READ), + assertTrue(perms.getGroupAction().implies(FsAction.READ), "Group-readable permission not set!"); - Assertions.assertTrue(perms.getOtherAction().implies(FsAction.EXECUTE), + assertTrue(perms.getOtherAction().implies(FsAction.EXECUTE), "Other-executable permission not set!"); - Assertions.assertTrue(perms.getOtherAction().implies(FsAction.READ), + assertTrue(perms.getOtherAction().implies(FsAction.READ), "Other-readable permission not set!"); } @@ -1045,33 +1051,33 @@ private void testRootReadableExecutableInternal(boolean located) @Test public void testInternalModifyAclEntries() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.modifyAclEntries(new Path("/internalDir"), new ArrayList())); } @Test public void testInternalRemoveAclEntries() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.removeAclEntries(new Path("/internalDir"), new ArrayList())); } @Test public void testInternalRemoveDefaultAcl() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.removeDefaultAcl(new Path("/internalDir"))); } @Test public void testInternalRemoveAcl() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.removeAcl(new Path("/internalDir"))); } @Test public void testInternalSetAcl() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.setAcl(new Path("/internalDir"), new ArrayList())); } @@ -1080,94 +1086,94 @@ public void testInternalGetAclStatus() throws IOException { final UserGroupInformation currentUser = UserGroupInformation.getCurrentUser(); AclStatus aclStatus = fsView.getAclStatus(new Path("/internalDir")); - Assertions.assertEquals(currentUser.getUserName(), aclStatus.getOwner()); - Assertions.assertEquals(currentUser.getGroupNames()[0], aclStatus.getGroup()); - Assertions.assertEquals(AclUtil.getMinimalAcl(PERMISSION_555), aclStatus.getEntries()); - Assertions.assertFalse(aclStatus.isStickyBit()); + assertEquals(currentUser.getUserName(), aclStatus.getOwner()); + assertEquals(currentUser.getGroupNames()[0], aclStatus.getGroup()); + assertEquals(AclUtil.getMinimalAcl(PERMISSION_555), aclStatus.getEntries()); + assertFalse(aclStatus.isStickyBit()); } @Test public void testInternalSetXAttr() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.setXAttr(new Path("/internalDir"), "xattrName", null)); } @Test public void testInternalGetXAttr() throws IOException { - Assertions.assertThrows(NotInMountpointException.class, + assertThrows(NotInMountpointException.class, () -> fsView.getXAttr(new Path("/internalDir"), "xattrName")); } @Test public void testInternalGetXAttrs() throws IOException { - Assertions.assertThrows(NotInMountpointException.class, + assertThrows(NotInMountpointException.class, () -> fsView.getXAttrs(new Path("/internalDir"))); } @Test public void testInternalGetXAttrsWithNames() throws IOException { - Assertions.assertThrows(NotInMountpointException.class, + assertThrows(NotInMountpointException.class, () -> fsView.getXAttrs(new Path("/internalDir"), new ArrayList())); } @Test public void testInternalListXAttr() throws IOException { - Assertions.assertThrows(NotInMountpointException.class, + assertThrows(NotInMountpointException.class, () -> fsView.listXAttrs(new Path("/internalDir"))); } @Test public void testInternalRemoveXAttr() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.removeXAttr(new Path("/internalDir"), "xattrName")); } @Test public void testInternalCreateSnapshot1() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.createSnapshot(new Path("/internalDir"))); } @Test public void testInternalCreateSnapshot2() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.createSnapshot(new Path("/internalDir"), "snap1")); } @Test public void testInternalRenameSnapshot() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.renameSnapshot(new Path("/internalDir"), "snapOldName", "snapNewName")); } @Test public void testInternalDeleteSnapshot() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.deleteSnapshot(new Path("/internalDir"), "snap1")); } @Test public void testInternalSetStoragePolicy() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.setStoragePolicy(new Path("/internalDir"), "HOT")); } @Test public void testInternalUnsetStoragePolicy() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.unsetStoragePolicy(new Path("/internalDir"))); } @Test public void testInternalSatisfyStoragePolicy() throws IOException { - Assertions.assertThrows(AccessControlException.class, + assertThrows(AccessControlException.class, () -> fsView.satisfyStoragePolicy(new Path("/internalDir"))); } @Test public void testInternalgetStoragePolicy() throws IOException { - Assertions.assertThrows(NotInMountpointException.class, + assertThrows(NotInMountpointException.class, () -> fsView.getStoragePolicy(new Path("/internalDir"))); } @@ -1178,7 +1184,7 @@ public void testInternalGetAllStoragePolicies() throws IOException { for (FileSystem fs : fsView.getChildFileSystems()) { try { for (BlockStoragePolicySpi s : fs.getAllStoragePolicies()) { - Assertions.assertTrue(policies.contains(s), "Missing policy: " + s); + assertTrue(policies.contains(s), "Missing policy: " + s); } } catch (UnsupportedOperationException e) { // ignore @@ -1231,17 +1237,17 @@ public void testTrashRoot() throws IOException { // Verify if Trash roots from ViewFileSystem matches that of the ones // from the target mounted FileSystem. - Assertions.assertEquals(fsTargetRootTrashRoot.toUri().getPath(), + assertEquals(fsTargetRootTrashRoot.toUri().getPath(), mountDataRootTrashPath.toUri().getPath()); - Assertions.assertEquals(fsTargetFileTrashPath.toUri().getPath(), + assertEquals(fsTargetFileTrashPath.toUri().getPath(), mountDataFileTrashPath.toUri().getPath()); - Assertions.assertEquals(mountDataFileTrashPath.toUri().getPath(), + assertEquals(mountDataFileTrashPath.toUri().getPath(), mountDataRootTrashPath.toUri().getPath()); // Verify trash root for an non-existing file but on a valid mountpoint. Path trashRoot = fsView.getTrashRoot(mountDataNonExistingFilePath); - Assertions.assertEquals(trashRoot.toUri().getPath(), + assertEquals(trashRoot.toUri().getPath(), mountDataRootTrashPath.toUri().getPath()); // Verify trash root for invalid mounts. @@ -1272,12 +1278,12 @@ public void testTrashRoot() throws IOException { newConf.setLong("fs.trash.interval", 1000); Trash lTrash = new Trash(fsTarget, newConf); boolean trashed = lTrash.moveToTrash(fsTargetFilePath); - Assertions.assertTrue(trashed, "File " + fileStatus + " move to " + + assertTrue(trashed, "File " + fileStatus + " move to " + "trash failed."); // Verify ViewFileSystem trash roots shows the ones from // target mounted FileSystem. - Assertions.assertTrue(fsView.getTrashRoots(true).size() > 0, ""); + assertTrue(fsView.getTrashRoots(true).size() > 0, ""); } // Default implementation of getTrashRoot for a fallback FS mounted at root: @@ -1303,14 +1309,14 @@ public void testTrashRootForceInsideMountPoint() throws IOException { Path dataTestPath = new Path("/data/dir/file"); Path dataTrashRoot = fsView2.makeQualified( new Path("/data/" + TRASH_PREFIX + "/" + ugi.getShortUserName())); - Assertions.assertEquals(fsView2.getTrashRoot(dataTestPath), dataTrashRoot); + assertEquals(fsView2.getTrashRoot(dataTestPath), dataTrashRoot); // Case 2: path p not found in mount table. // Return a trash root in fallback FS. Path nonExistentPath = new Path("/nonExistentDir/nonExistentFile"); Path expectedTrash = fsView2.makeQualified(getTrashRootInFallBackFS()); - Assertions.assertEquals(fsView2.getTrashRoot(nonExistentPath), expectedTrash); + assertEquals(fsView2.getTrashRoot(nonExistentPath), expectedTrash); // Case 3: turn off the CONFIG_VIEWFS_TRASH_FORCE_INSIDE_MOUNT_POINT flag. // Return a trash root in user home dir. @@ -1318,7 +1324,7 @@ public void testTrashRootForceInsideMountPoint() throws IOException { fsView2 = FileSystem.get(FsConstants.VIEWFS_URI, conf2); Path targetFSUserHomeTrashRoot = fsTarget.makeQualified( new Path(fsTarget.getHomeDirectory(), TRASH_PREFIX)); - Assertions.assertEquals(fsView2.getTrashRoot(dataTestPath), + assertEquals(fsView2.getTrashRoot(dataTestPath), targetFSUserHomeTrashRoot); // Case 4: viewFS without fallback. Expect exception for a nonExistent path @@ -1359,7 +1365,7 @@ public void testTrashRootDeepTrashDir() throws IOException { FileSystem fsView2 = FileSystem.get(FsConstants.VIEWFS_URI, conf2); Path expectedTrash = fsView2.makeQualified( new Path("/mnt/datavol1/very/deep/deep/trash/dir/.Trash")); - Assertions.assertEquals(fsView2.getTrashRoot(testPath), expectedTrash); + assertEquals(fsView2.getTrashRoot(testPath), expectedTrash); } /** @@ -1374,7 +1380,7 @@ public void testTrashRootsAllUsers() throws IOException { // Case 1: verify correct trash roots from fsView and fsView2 int beforeTrashRootNum = fsView.getTrashRoots(true).size(); int beforeTrashRootNum2 = fsView2.getTrashRoots(true).size(); - Assertions.assertEquals(beforeTrashRootNum2, beforeTrashRootNum); + assertEquals(beforeTrashRootNum2, beforeTrashRootNum); fsView.mkdirs(new Path("/data/" + TRASH_PREFIX + "/user1")); fsView.mkdirs(new Path("/data/" + TRASH_PREFIX + "/user2")); @@ -1383,8 +1389,8 @@ public void testTrashRootsAllUsers() throws IOException { fsView.mkdirs(new Path("/user2/" + TRASH_PREFIX + "/user5")); int afterTrashRootsNum = fsView.getTrashRoots(true).size(); int afterTrashRootsNum2 = fsView2.getTrashRoots(true).size(); - Assertions.assertEquals(afterTrashRootsNum, beforeTrashRootNum); - Assertions.assertEquals(afterTrashRootsNum2, beforeTrashRootNum2 + 5); + assertEquals(afterTrashRootsNum, beforeTrashRootNum); + assertEquals(afterTrashRootsNum2, beforeTrashRootNum2 + 5); // Case 2: per-user mount point fsTarget.mkdirs(new Path(targetTestRoot, "Users/userA/.Trash/userA")); @@ -1393,7 +1399,7 @@ public void testTrashRootsAllUsers() throws IOException { new Path(targetTestRoot, "Users/userA").toUri()); FileSystem fsView3 = FileSystem.get(FsConstants.VIEWFS_URI, conf3); int trashRootsNum3 = fsView3.getTrashRoots(true).size(); - Assertions.assertEquals(trashRootsNum3, afterTrashRootsNum2 + 1); + assertEquals(trashRootsNum3, afterTrashRootsNum2 + 1); // Case 3: single /Users mount point for all users fsTarget.mkdirs(new Path(targetTestRoot, "Users/.Trash/user1")); @@ -1403,7 +1409,7 @@ public void testTrashRootsAllUsers() throws IOException { new Path(targetTestRoot, "Users").toUri()); FileSystem fsView4 = FileSystem.get(FsConstants.VIEWFS_URI, conf4); int trashRootsNum4 = fsView4.getTrashRoots(true).size(); - Assertions.assertEquals(trashRootsNum4, afterTrashRootsNum2 + 2); + assertEquals(trashRootsNum4, afterTrashRootsNum2 + 2); // Case 4: test trash roots in fallback FS fsTarget.mkdirs(new Path(targetTestRoot, ".Trash/user10")); @@ -1413,7 +1419,7 @@ public void testTrashRootsAllUsers() throws IOException { ConfigUtil.addLinkFallback(conf5, targetTestRoot.toUri()); FileSystem fsView5 = FileSystem.get(FsConstants.VIEWFS_URI, conf5); int trashRootsNum5 = fsView5.getTrashRoots(true).size(); - Assertions.assertEquals(trashRootsNum5, afterTrashRootsNum2 + 3); + assertEquals(trashRootsNum5, afterTrashRootsNum2 + 3); } /** @@ -1429,7 +1435,7 @@ public void testTrashRootsCurrentUser() throws IOException { int beforeTrashRootNum = fsView.getTrashRoots(false).size(); int beforeTrashRootNum2 = fsView2.getTrashRoots(false).size(); - Assertions.assertEquals(beforeTrashRootNum2, beforeTrashRootNum); + assertEquals(beforeTrashRootNum2, beforeTrashRootNum); fsView.mkdirs(new Path("/data/" + TRASH_PREFIX + "/" + currentUser)); fsView.mkdirs(new Path("/data/" + TRASH_PREFIX + "/user2")); @@ -1438,8 +1444,8 @@ public void testTrashRootsCurrentUser() throws IOException { fsView.mkdirs(new Path("/user2/" + TRASH_PREFIX + "/user5")); int afterTrashRootsNum = fsView.getTrashRoots(false).size(); int afterTrashRootsNum2 = fsView2.getTrashRoots(false).size(); - Assertions.assertEquals(afterTrashRootsNum, beforeTrashRootNum); - Assertions.assertEquals(afterTrashRootsNum2, beforeTrashRootNum2 + 2); + assertEquals(afterTrashRootsNum, beforeTrashRootNum); + assertEquals(afterTrashRootsNum2, beforeTrashRootNum2 + 2); // Test trash roots in fallback FS Configuration conf3 = new Configuration(conf2); @@ -1447,7 +1453,7 @@ public void testTrashRootsCurrentUser() throws IOException { ConfigUtil.addLinkFallback(conf3, targetTestRoot.toUri()); FileSystem fsView3 = FileSystem.get(FsConstants.VIEWFS_URI, conf3); int trashRootsNum3 = fsView3.getTrashRoots(false).size(); - Assertions.assertEquals(trashRootsNum3, afterTrashRootsNum2 + 1); + assertEquals(trashRootsNum3, afterTrashRootsNum2 + 1); } @Test @@ -1456,41 +1462,41 @@ public void testViewFileSystemUtil() throws Exception { FileSystem fileSystem = FileSystem.get(FsConstants.LOCAL_FS_URI, newConf); - Assertions.assertFalse(ViewFileSystemUtil.isViewFileSystem(fileSystem), + assertFalse(ViewFileSystemUtil.isViewFileSystem(fileSystem), "Unexpected FileSystem: " + fileSystem); fileSystem = FileSystem.get(FsConstants.VIEWFS_URI, newConf); - Assertions.assertTrue(ViewFileSystemUtil.isViewFileSystem(fileSystem), + assertTrue(ViewFileSystemUtil.isViewFileSystem(fileSystem), "Unexpected FileSystem: " + fileSystem); // Case 1: Verify FsStatus of root path returns all MountPoints status. Map mountPointFsStatusMap = ViewFileSystemUtil.getStatus(fileSystem, InodeTree.SlashPath); - Assertions.assertEquals(mountPointFsStatusMap.size(), getExpectedMountPoints()); + assertEquals(mountPointFsStatusMap.size(), getExpectedMountPoints()); // Case 2: Verify FsStatus of an internal dir returns all // MountPoints status. mountPointFsStatusMap = ViewFileSystemUtil.getStatus(fileSystem, new Path("/internalDir")); - Assertions.assertEquals(mountPointFsStatusMap.size(), getExpectedMountPoints()); + assertEquals(mountPointFsStatusMap.size(), getExpectedMountPoints()); // Case 3: Verify FsStatus of a matching MountPoint returns exactly // the corresponding MountPoint status. mountPointFsStatusMap = ViewFileSystemUtil.getStatus(fileSystem, new Path("/user")); - Assertions.assertEquals(mountPointFsStatusMap.size(), 1); + assertEquals(mountPointFsStatusMap.size(), 1); for (Entry entry : mountPointFsStatusMap.entrySet()) { - Assertions.assertEquals("/user", entry.getKey().getMountedOnPath().toString()); + assertEquals("/user", entry.getKey().getMountedOnPath().toString()); } // Case 4: Verify FsStatus of a path over a MountPoint returns the // corresponding MountPoint status. mountPointFsStatusMap = ViewFileSystemUtil.getStatus(fileSystem, new Path("/user/cloud")); - Assertions.assertEquals(mountPointFsStatusMap.size(), 1); + assertEquals(mountPointFsStatusMap.size(), 1); for (Entry entry : mountPointFsStatusMap.entrySet()) { - Assertions.assertEquals("/user", entry.getKey().getMountedOnPath().toString()); + assertEquals("/user", entry.getKey().getMountedOnPath().toString()); } // Case 5: Verify FsStatus of any level of an internal dir @@ -1498,20 +1504,20 @@ public void testViewFileSystemUtil() throws Exception { mountPointFsStatusMap = ViewFileSystemUtil.getStatus(fileSystem, new Path("/internalDir/internalDir2")); - Assertions.assertEquals(mountPointFsStatusMap.size(), getExpectedMountPoints()); + assertEquals(mountPointFsStatusMap.size(), getExpectedMountPoints()); // Case 6: Verify FsStatus of a MountPoint URI returns // the MountPoint status. mountPointFsStatusMap = ViewFileSystemUtil.getStatus(fileSystem, new Path("viewfs:/user/")); - Assertions.assertEquals(mountPointFsStatusMap.size(), 1); + assertEquals(mountPointFsStatusMap.size(), 1); for (Entry entry : mountPointFsStatusMap.entrySet()) { - Assertions.assertEquals("/user", entry.getKey().getMountedOnPath().toString()); + assertEquals("/user", entry.getKey().getMountedOnPath().toString()); } // Case 7: Verify FsStatus of a non MountPoint path throws exception final FileSystem fsCopy = fileSystem; - Assertions.assertThrows(NotInMountpointException.class, + assertThrows(NotInMountpointException.class, () -> ViewFileSystemUtil.getStatus(fsCopy, new Path("/non-existing"))); } @@ -1525,10 +1531,10 @@ public void testCheckOwnerWithFileStatus() public Object run() throws IOException { UserGroupInformation ugi = UserGroupInformation.getCurrentUser(); String doAsUserName = ugi.getUserName(); - Assertions.assertEquals("user@HADOOP.COM", doAsUserName); + assertEquals("user@HADOOP.COM", doAsUserName); FileSystem vfs = FileSystem.get(FsConstants.VIEWFS_URI, conf); FileStatus stat = vfs.getFileStatus(new Path("/internalDir")); - Assertions.assertEquals(stat.getOwner(), userUgi.getShortUserName()); + assertEquals(stat.getOwner(), userUgi.getShortUserName()); return null; } }); @@ -1546,7 +1552,7 @@ public void testUsed() throws IOException { long usedSpaceByPathViaViewFs = fsView.getUsed(new Path("/user")); long usedSpaceByPathViaTargetFs = fsTarget.getUsed(new Path(targetTestRoot, "user")); - Assertions.assertEquals(usedSpaceByPathViaTargetFs, usedSpaceByPathViaViewFs, + assertEquals(usedSpaceByPathViaTargetFs, usedSpaceByPathViaViewFs, "Space used not matching between ViewFileSystem and " + "the mounted FileSystem!"); @@ -1558,7 +1564,7 @@ public void testUsed() throws IOException { usedSpaceByPathViaViewFs = fsView.getUsed(mountDataFilePath); usedSpaceByPathViaTargetFs = fsTarget.getUsed(fsTargetFilePath); - Assertions.assertEquals(usedSpaceByPathViaTargetFs, usedSpaceByPathViaViewFs, + assertEquals(usedSpaceByPathViaTargetFs, usedSpaceByPathViaViewFs, "Space used not matching between ViewFileSystem and " + "the mounted FileSystem!"); } @@ -1566,7 +1572,7 @@ public void testUsed() throws IOException { @Test public void testLinkTarget() throws Exception { - Assumptions.assumeTrue(fsTarget.supportsSymlinks() && + assumeTrue(fsTarget.supportsSymlinks() && fsTarget.areSymlinksEnabled()); // Symbolic link @@ -1586,7 +1592,7 @@ public void testLinkTarget() throws Exception { final Path actualMountLinkTarget = fsView.getLinkTarget( mountTargetSymLinkPath); - Assertions.assertEquals(expectedMountLinkTarget, actualMountLinkTarget, + assertEquals(expectedMountLinkTarget, actualMountLinkTarget, "Resolved link target path not matching!"); // Relative symbolic link @@ -1603,7 +1609,7 @@ public void testLinkTarget() throws Exception { final Path actualMountRelLinkTarget = fsView.getLinkTarget( mountTargetRelativeSymLinkPath); - Assertions.assertEquals(expectedMountRelLinkTarget, actualMountRelLinkTarget, + assertEquals(expectedMountRelLinkTarget, actualMountRelLinkTarget, "Resolved relative link target path not matching!"); try { @@ -1639,10 +1645,10 @@ public void testViewFileSystemInnerCache() throws Exception { FileSystem fs = cache.get(fsTarget.getUri(), conf); // InnerCache caches filesystem. - Assertions.assertSame(cache.get(fsTarget.getUri(), conf), fs); + assertSame(cache.get(fsTarget.getUri(), conf), fs); // InnerCache and FileSystem.CACHE are independent. - Assertions.assertNotSame(FileSystem.get(fsTarget.getUri(), conf), fs); + assertNotSame(FileSystem.get(fsTarget.getUri(), conf), fs); // close InnerCache. cache.closeAll(); @@ -1667,14 +1673,14 @@ public void testCloseChildrenFileSystem() throws Exception { URI uri = new URI("viewfs://" + clusterName + "/"); ViewFileSystem viewFs = (ViewFileSystem) FileSystem.get(uri, config); - Assertions.assertTrue(viewFs.getChildFileSystems().length > 0, + assertTrue(viewFs.getChildFileSystems().length > 0, "viewfs should have at least one child fs."); // viewFs is cached in FileSystem.CACHE - Assertions.assertSame(FileSystem.get(uri, config), viewFs); + assertSame(FileSystem.get(uri, config), viewFs); // child fs is not cached in FileSystem.CACHE FileSystem child = viewFs.getChildFileSystems()[0]; - Assertions.assertNotSame(FileSystem.get(child.getUri(), config), child); + assertNotSame(FileSystem.get(child.getUri(), config), child); viewFs.close(); for (FileSystem childfs : viewFs.getChildFileSystems()) { @@ -1702,9 +1708,9 @@ public void testChildrenFileSystemLeak() throws Exception { .get(new URI("viewfs://" + clusterName + "/"), config); viewFs.resolvePath( new Path(String.format("viewfs://%s/%s", clusterName, "/user"))); - Assertions.assertEquals(TestFileUtil.getCacheSize(), cacheSize + 1); + assertEquals(TestFileUtil.getCacheSize(), cacheSize + 1); viewFs.close(); - Assertions.assertEquals(TestFileUtil.getCacheSize(), cacheSize); + assertEquals(TestFileUtil.getCacheSize(), cacheSize); } @Test @@ -1719,12 +1725,12 @@ public void testDeleteOnExit() throws Exception { ViewFileSystem viewFs = (ViewFileSystem) FileSystem .get(new URI("viewfs://" + clusterName + "/"), config); viewFs.mkdirs(testDir); - Assertions.assertTrue(viewFs.exists(testDir)); - Assertions.assertTrue(fsTarget.exists(realTestPath)); + assertTrue(viewFs.exists(testDir)); + assertTrue(fsTarget.exists(realTestPath)); viewFs.deleteOnExit(testDir); viewFs.close(); - Assertions.assertFalse(fsTarget.exists(realTestPath)); + assertFalse(fsTarget.exists(realTestPath)); } @Test @@ -1744,13 +1750,13 @@ public void testGetContentSummary() throws IOException { ContentSummary summaryAfter = fsView.getContentSummary(new Path("/internalDir")); - Assertions.assertEquals(summaryBefore.getFileCount() + 1, + assertEquals(summaryBefore.getFileCount() + 1, summaryAfter.getFileCount(), "The file count didn't match"); - Assertions.assertEquals(summaryBefore.getLength() + expected.length(), + assertEquals(summaryBefore.getLength() + expected.length(), summaryAfter.getLength(), "The size didn't match"); - Assertions.assertEquals(summaryBefore.getDirectoryCount() + 1, + assertEquals(summaryBefore.getDirectoryCount() + 1, summaryAfter.getDirectoryCount(), "The directory count didn't match"); } @@ -1770,10 +1776,10 @@ public void testGetContentSummaryWithFileInLocalFS() throws Exception { try (FileSystem fs = FileSystem.get(FsConstants.VIEWFS_URI, conf)) { ContentSummary summaryAfter = fs.getContentSummary(new Path("/internalDir")); - Assertions.assertEquals(summaryAfter.getFileCount(), + assertEquals(summaryAfter.getFileCount(), summaryBefore.getFileCount() + 1, "The file count didn't match"); - Assertions.assertEquals(summaryAfter.getLength(), + assertEquals(summaryAfter.getLength(), summaryBefore.getLength() + expected.length(), "The directory count didn't match"); } @@ -1799,7 +1805,7 @@ public void testTargetFileSystemLazyInitialization() throws Exception { // cache size will remain the same // cache is disabled for viewfs scheme, so the viewfs:// instance won't // go in the cache even after the initialization - Assertions.assertEquals(TestFileUtil.getCacheSize(), cacheSize); + assertEquals(TestFileUtil.getCacheSize(), cacheSize); // This resolve path will initialize the file system corresponding // to the mount table entry of the path "/user" @@ -1807,17 +1813,17 @@ public void testTargetFileSystemLazyInitialization() throws Exception { new Path(String.format("viewfs://%s/%s", clusterName, "/user"))); // Cache size will increase by 1. - Assertions.assertEquals(TestFileUtil.getCacheSize(), cacheSize + 1); + assertEquals(TestFileUtil.getCacheSize(), cacheSize + 1); // This resolve path will initialize the file system corresponding // to the mount table entry of the path "/mock" viewFs.resolvePath(new Path(String.format("viewfs://%s/%s", clusterName, "/mock"))); // One more file system instance will get initialized. - Assertions.assertEquals(TestFileUtil.getCacheSize(), cacheSize + 2); + assertEquals(TestFileUtil.getCacheSize(), cacheSize + 2); viewFs.close(); // Initialized FileSystem instances will not be removed from cache as // viewfs inner cache is disabled - Assertions.assertEquals(TestFileUtil.getCacheSize(), cacheSize + 2); + assertEquals(TestFileUtil.getCacheSize(), cacheSize + 2); } @Test @@ -1841,13 +1847,13 @@ public void testTargetFileSystemLazyInitializationForChecksumMethods() // cache size will remain the same // cache is disabled for viewfs scheme, so the viewfs:// instance won't // go in the cache even after the initialization - Assertions.assertEquals(TestFileUtil.getCacheSize(), cacheSize); + assertEquals(TestFileUtil.getCacheSize(), cacheSize); // This is not going to initialize any filesystem instance viewFs.setVerifyChecksum(true); // Cache size will remain the same - Assertions.assertEquals(TestFileUtil.getCacheSize(), cacheSize); + assertEquals(TestFileUtil.getCacheSize(), cacheSize); // This resolve path will initialize the file system corresponding // to the mount table entry of the path "/user" @@ -1855,12 +1861,12 @@ public void testTargetFileSystemLazyInitializationForChecksumMethods() new Path(String.format("viewfs://%s/%s", clusterName, "/user"))); // Cache size will increase by 1. - Assertions.assertEquals(TestFileUtil.getCacheSize(), cacheSize + 1); + assertEquals(TestFileUtil.getCacheSize(), cacheSize + 1); viewFs.close(); // Initialized FileSystem instances will not be removed from cache as // viewfs inner cache is disabled - Assertions.assertEquals(TestFileUtil.getCacheSize(), cacheSize + 1); + assertEquals(TestFileUtil.getCacheSize(), cacheSize + 1); } @Test @@ -1876,7 +1882,7 @@ public void testInvalidMountPoints() throws Exception { new URI("viewfs://" + clusterName + "/"), config); fail("FileSystem should not initialize. Should fail with IOException"); } catch (IOException ex) { - Assertions.assertTrue(ex.getMessage().startsWith("URISyntax exception"), + assertTrue(ex.getMessage().startsWith("URISyntax exception"), "Should get URISyntax Exception"); } } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFsTestSetup.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFsTestSetup.java index b2d7416aa7675..8f9ac895946e0 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFsTestSetup.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFsTestSetup.java @@ -17,6 +17,8 @@ */ package org.apache.hadoop.fs.viewfs; +import static org.junit.jupiter.api.Assertions.assertEquals; + import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @@ -31,7 +33,6 @@ import org.apache.hadoop.fs.viewfs.ViewFileSystemOverloadScheme.ChildFsGetter; import org.apache.hadoop.util.Shell; import org.eclipse.jetty.util.log.Log; -import org.junit.Assert; /** @@ -164,7 +165,7 @@ static void addMountLinksToFile(String mountTable, String[] sources, out.writeBytes(""); if (isNfly) { String[] srcParts = src.split("[.]"); - Assert.assertEquals("Invalid NFlyLink format", 3, srcParts.length); + assertEquals(3, srcParts.length, "Invalid NFlyLink format"); String actualSrc = srcParts[srcParts.length - 1]; String params = srcParts[srcParts.length - 2]; out.writeBytes(prefix + Constants.CONFIG_VIEWFS_LINK_NFLY + "." @@ -202,7 +203,7 @@ public static void addMountLinksToConf(String mountTable, String[] sources, boolean isNfly = src.startsWith(Constants.CONFIG_VIEWFS_LINK_NFLY); if (isNfly) { String[] srcParts = src.split("[.]"); - Assert.assertEquals("Invalid NFlyLink format", 3, srcParts.length); + assertEquals(3, srcParts.length, "Invalid NFlyLink format"); String actualSrc = srcParts[srcParts.length - 1]; String params = srcParts[srcParts.length - 2]; ConfigUtil.addLinkNfly(config, mountTableName, actualSrc, params, diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/ClientBaseWithFixes.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/ClientBaseWithFixes.java index fdb5c90ad56f4..8976a7cf9fa25 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/ClientBaseWithFixes.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/ClientBaseWithFixes.java @@ -18,6 +18,10 @@ package org.apache.hadoop.ha; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -44,9 +48,8 @@ import org.apache.zookeeper.server.ZKDatabase; import org.apache.zookeeper.server.ZooKeeperServer; import org.apache.zookeeper.server.persistence.FileTxnLog; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -73,409 +76,397 @@ public abstract class ClientBaseWithFixes extends ZKTestCase { System.setProperty("zookeeper.4lw.commands.whitelist", "*"); } - protected final String hostPort = initHostPort(); - protected int maxCnxns = 0; - protected ServerCnxnFactory serverFactory = null; - protected File tmpDir = null; + protected final String hostPort = initHostPort(); + protected int maxCnxns = 0; + protected ServerCnxnFactory serverFactory = null; + protected File tmpDir = null; - long initialFdCount; + long initialFdCount; - /** - * In general don't use this. Only use in the special case that you - * want to ignore results (for whatever reason) in your test. Don't - * use empty watchers in real code! - * - */ - protected class NullWatcher implements Watcher { - @Override - public void process(WatchedEvent event) { /* nada */ } - } + /** + * In general don't use this. Only use in the special case that you + * want to ignore results (for whatever reason) in your test. Don't + * use empty watchers in real code! + * + */ + protected class NullWatcher implements Watcher { + @Override + public void process(WatchedEvent event) { /* nada */ } + } - protected static class CountdownWatcher implements Watcher { - // XXX this doesn't need to be volatile! (Should probably be final) - volatile CountDownLatch clientConnected; - volatile boolean connected; - protected ZooKeeper client; + protected static class CountdownWatcher implements Watcher { + // XXX this doesn't need to be volatile! (Should probably be final) + volatile CountDownLatch clientConnected; + volatile boolean connected; + protected ZooKeeper client; + + public void initializeWatchedClient(ZooKeeper zk) { + if (client != null) { + throw new RuntimeException("Watched Client was already set"); + } + client = zk; + } - public void initializeWatchedClient(ZooKeeper zk) { - if (client != null) { - throw new RuntimeException("Watched Client was already set"); - } - client = zk; - } + public CountdownWatcher() { + reset(); + } - public CountdownWatcher() { - reset(); - } - synchronized public void reset() { - clientConnected = new CountDownLatch(1); - connected = false; - } - @Override - synchronized public void process(WatchedEvent event) { - if (event.getState() == KeeperState.SyncConnected || - event.getState() == KeeperState.ConnectedReadOnly) { - connected = true; - notifyAll(); - clientConnected.countDown(); - } else { - connected = false; - notifyAll(); - } - } - synchronized boolean isConnected() { - return connected; - } - @VisibleForTesting - public synchronized void waitForConnected(long timeout) - throws InterruptedException, TimeoutException { - long expire = Time.now() + timeout; - long left = timeout; - while(!connected && left > 0) { - wait(left); - left = expire - Time.now(); - } - if (!connected) { - throw new TimeoutException("Did not connect"); + synchronized public void reset() { + clientConnected = new CountDownLatch(1); + connected = false; + } - } - } - @VisibleForTesting - public synchronized void waitForDisconnected(long timeout) - throws InterruptedException, TimeoutException { - long expire = Time.now() + timeout; - long left = timeout; - while(connected && left > 0) { - wait(left); - left = expire - Time.now(); - } - if (connected) { - throw new TimeoutException("Did not disconnect"); + @Override + synchronized public void process(WatchedEvent event) { + if (event.getState() == KeeperState.SyncConnected || + event.getState() == KeeperState.ConnectedReadOnly) { + connected = true; + notifyAll(); + clientConnected.countDown(); + } else { + connected = false; + notifyAll(); + } + } - } - } + synchronized boolean isConnected() { + return connected; } - protected TestableZooKeeper createClient() - throws IOException, InterruptedException - { - return createClient(hostPort); + @VisibleForTesting + public synchronized void waitForConnected(long timeout) + throws InterruptedException, TimeoutException { + long expire = Time.now() + timeout; + long left = timeout; + while(!connected && left > 0) { + wait(left); + left = expire - Time.now(); + } + if (!connected) { + throw new TimeoutException("Did not connect"); + } } - protected TestableZooKeeper createClient(String hp) - throws IOException, InterruptedException - { - CountdownWatcher watcher = new CountdownWatcher(); - return createClient(watcher, hp); + @VisibleForTesting + public synchronized void waitForDisconnected(long timeout) + throws InterruptedException, TimeoutException { + long expire = Time.now() + timeout; + long left = timeout; + while(connected && left > 0) { + wait(left); + left = expire - Time.now(); + } + if (connected) { + throw new TimeoutException("Did not disconnect"); + } } + } + + protected TestableZooKeeper createClient() + throws IOException, InterruptedException { + return createClient(hostPort); + } - private LinkedList allClients; - private boolean allClientsSetup = false; + protected TestableZooKeeper createClient(String hp) + throws IOException, InterruptedException { + CountdownWatcher watcher = new CountdownWatcher(); + return createClient(watcher, hp); + } - protected TestableZooKeeper createClient(CountdownWatcher watcher, String hp) - throws IOException, InterruptedException - { - return createClient(watcher, hp, CONNECTION_TIMEOUT); - } + private LinkedList allClients; + private boolean allClientsSetup = false; - protected TestableZooKeeper createClient(CountdownWatcher watcher, - String hp, int timeout) - throws IOException, InterruptedException - { - watcher.reset(); - TestableZooKeeper zk = new TestableZooKeeper(hp, timeout, watcher); - if (!watcher.clientConnected.await(timeout, TimeUnit.MILLISECONDS)) - { - Assert.fail("Unable to connect to server"); - } - synchronized(this) { - if (!allClientsSetup) { - LOG.error("allClients never setup"); - Assert.fail("allClients never setup"); - } - if (allClients != null) { - allClients.add(zk); - } else { - // test done - close the zk, not needed - zk.close(); - } - } - watcher.initializeWatchedClient(zk); - return zk; - } + protected TestableZooKeeper createClient(CountdownWatcher watcher, String hp) + throws IOException, InterruptedException { + return createClient(watcher, hp, CONNECTION_TIMEOUT); + } - public static class HostPort { - String host; - int port; - public HostPort(String host, int port) { - this.host = host; - this.port = port; - } + protected TestableZooKeeper createClient(CountdownWatcher watcher, + String hp, int timeout) throws IOException, InterruptedException { + watcher.reset(); + TestableZooKeeper zk = new TestableZooKeeper(hp, timeout, watcher); + if (!watcher.clientConnected.await(timeout, TimeUnit.MILLISECONDS)) { + fail("Unable to connect to server"); } - public static List parseHostPortList(String hplist) { - ArrayList alist = new ArrayList(); - for (String hp: hplist.split(",")) { - int idx = hp.lastIndexOf(':'); - String host = hp.substring(0, idx); - int port; - try { - port = Integer.parseInt(hp.substring(idx + 1)); - } catch(RuntimeException e) { - throw new RuntimeException("Problem parsing " + hp + e.toString()); - } - alist.add(new HostPort(host,port)); - } - return alist; + synchronized (this) { + if (!allClientsSetup) { + LOG.error("allClients never setup"); + fail("allClients never setup"); + } + if (allClients != null) { + allClients.add(zk); + } else { + // test done - close the zk, not needed + zk.close(); + } } + watcher.initializeWatchedClient(zk); + return zk; + } - /** - * Send the 4letterword - * @param host the destination host - * @param port the destination port - * @param cmd the 4letterword - * @return - * @throws IOException - */ - public static String send4LetterWord(String host, int port, String cmd) - throws IOException - { - LOG.info("connecting to " + host + " " + port); - Socket sock = new Socket(host, port); - BufferedReader reader = null; - try { - OutputStream outstream = sock.getOutputStream(); - outstream.write(cmd.getBytes()); - outstream.flush(); - // this replicates NC - close the output stream before reading - sock.shutdownOutput(); - - reader = - new BufferedReader( - new InputStreamReader(sock.getInputStream())); - StringBuilder sb = new StringBuilder(); - String line; - while((line = reader.readLine()) != null) { - sb.append(line + "\n"); - } - return sb.toString(); - } finally { - sock.close(); - if (reader != null) { - reader.close(); - } - } + public static class HostPort { + String host; + int port; + public HostPort(String host, int port) { + this.host = host; + this.port = port; } + } - public static boolean waitForServerUp(String hp, long timeout) { - long start = Time.now(); - while (true) { - try { - // if there are multiple hostports, just take the first one - HostPort hpobj = parseHostPortList(hp).get(0); - String result = send4LetterWord(hpobj.host, hpobj.port, "stat"); - if (result.startsWith("Zookeeper version:") && - !result.contains("READ-ONLY")) { - return true; - } - } catch (IOException e) { - // ignore as this is expected - LOG.info("server " + hp + " not up " + e); - } + public static List parseHostPortList(String hplist) { + ArrayList alist = new ArrayList(); + for (String hp: hplist.split(",")) { + int idx = hp.lastIndexOf(':'); + String host = hp.substring(0, idx); + int port; + try { + port = Integer.parseInt(hp.substring(idx + 1)); + } catch(RuntimeException e) { + throw new RuntimeException("Problem parsing " + hp + e.toString()); + } + alist.add(new HostPort(host, port)); + } + return alist; + } - if (Time.now() > start + timeout) { - break; - } - try { - Thread.sleep(250); - } catch (InterruptedException e) { - // ignore - } - } - return false; + /** + * Send the 4letterword. + * @param host the destination host + * @param port the destination port + * @param cmd the 4letterword + * @return + * @throws IOException + */ + public static String send4LetterWord(String host, int port, String cmd) + throws IOException { + LOG.info("connecting to " + host + " " + port); + Socket sock = new Socket(host, port); + BufferedReader reader = null; + try { + OutputStream outstream = sock.getOutputStream(); + outstream.write(cmd.getBytes()); + outstream.flush(); + // this replicates NC - close the output stream before reading + sock.shutdownOutput(); + reader = new BufferedReader( + new InputStreamReader(sock.getInputStream())); + StringBuilder sb = new StringBuilder(); + String line; + while((line = reader.readLine()) != null) { + sb.append(line + "\n"); + } + return sb.toString(); + } finally { + sock.close(); + if (reader != null) { + reader.close(); + } } - public static boolean waitForServerDown(String hp, long timeout) { - long start = Time.now(); - while (true) { - try { - HostPort hpobj = parseHostPortList(hp).get(0); - send4LetterWord(hpobj.host, hpobj.port, "stat"); - } catch (IOException e) { - return true; - } + } - if (Time.now() > start + timeout) { - break; - } - try { - Thread.sleep(250); - } catch (InterruptedException e) { - // ignore - } + public static boolean waitForServerUp(String hp, long timeout) { + long start = Time.now(); + while (true) { + try { + // if there are multiple hostports, just take the first one + HostPort hpobj = parseHostPortList(hp).get(0); + String result = send4LetterWord(hpobj.host, hpobj.port, "stat"); + if (result.startsWith("Zookeeper version:") && + !result.contains("READ-ONLY")) { + return true; } - return false; + } catch (IOException e) { + // ignore as this is expected + LOG.info("server " + hp + " not up " + e); + } + + if (Time.now() > start + timeout) { + break; + } + try { + Thread.sleep(250); + } catch (InterruptedException e) { + // ignore + } } + return false; + } - public static File createTmpDir() throws IOException { - return createTmpDir(BASETEST); - } - static File createTmpDir(File parentDir) throws IOException { - File tmpFile = File.createTempFile("test", ".junit", parentDir); - // don't delete tmpFile - this ensures we don't attempt to create - // a tmpDir with a duplicate name - File tmpDir = new File(tmpFile + ".dir"); - Assert.assertFalse(tmpDir.exists()); // never true if tmpfile does it's job - Assert.assertTrue(tmpDir.mkdirs()); - - return tmpDir; + public static boolean waitForServerDown(String hp, long timeout) { + long start = Time.now(); + while (true) { + try { + HostPort hpobj = parseHostPortList(hp).get(0); + send4LetterWord(hpobj.host, hpobj.port, "stat"); + } catch (IOException e) { + return true; + } + + if (Time.now() > start + timeout) { + break; + } + + try { + Thread.sleep(250); + } catch (InterruptedException e) { + // ignore + } } + return false; + } - private static int getPort(String hostPort) { - String[] split = hostPort.split(":"); - String portstr = split[split.length-1]; - String[] pc = portstr.split("/"); - if (pc.length > 1) { - portstr = pc[0]; - } - return Integer.parseInt(portstr); - } + public static File createTmpDir() throws IOException { + return createTmpDir(BASETEST); + } - static ServerCnxnFactory createNewServerInstance(File dataDir, - ServerCnxnFactory factory, String hostPort, int maxCnxns) - throws IOException, InterruptedException - { - ZooKeeperServer zks = new ZooKeeperServer(dataDir, dataDir, 3000); - final int PORT = getPort(hostPort); - if (factory == null) { - factory = ServerCnxnFactory.createFactory(PORT, maxCnxns); - } - factory.startup(zks); - Assert.assertTrue("waiting for server up", - ClientBaseWithFixes.waitForServerUp("127.0.0.1:" + PORT, - CONNECTION_TIMEOUT)); + static File createTmpDir(File parentDir) throws IOException { + File tmpFile = File.createTempFile("test", ".junit", parentDir); + // don't delete tmpFile - this ensures we don't attempt to create + // a tmpDir with a duplicate name + File tmpDir = new File(tmpFile + ".dir"); + assertFalse(tmpDir.exists()); // never true if tmpfile does it's job + assertTrue(tmpDir.mkdirs()); + return tmpDir; + } - return factory; + private static int getPort(String hostPort) { + String[] split = hostPort.split(":"); + String portstr = split[split.length-1]; + String[] pc = portstr.split("/"); + if (pc.length > 1) { + portstr = pc[0]; } + return Integer.parseInt(portstr); + } - static void shutdownServerInstance(ServerCnxnFactory factory, - String hostPort) - { - if (factory != null) { - ZKDatabase zkDb; - { - ZooKeeperServer zs = getServer(factory); - - zkDb = zs.getZKDatabase(); - } - factory.shutdown(); - try { - zkDb.close(); - } catch (IOException ie) { - LOG.warn("Error closing logs ", ie); - } - final int PORT = getPort(hostPort); - - Assert.assertTrue("waiting for server down", - ClientBaseWithFixes.waitForServerDown("127.0.0.1:" + PORT, - CONNECTION_TIMEOUT)); - } + static ServerCnxnFactory createNewServerInstance(File dataDir, + ServerCnxnFactory factory, String hostPort, int maxCnxns) + throws IOException, InterruptedException { + ZooKeeperServer zks = new ZooKeeperServer(dataDir, dataDir, 3000); + final int port = getPort(hostPort); + if (factory == null) { + factory = ServerCnxnFactory.createFactory(port, maxCnxns); } + factory.startup(zks); + assertTrue(ClientBaseWithFixes.waitForServerUp("127.0.0.1:" + port, + CONNECTION_TIMEOUT), "waiting for server up"); + return factory; + } - /** - * Test specific setup - */ - public static void setupTestEnv() { - // during the tests we run with 100K prealloc in the logs. - // on windows systems prealloc of 64M was seen to take ~15seconds - // resulting in test Assert.failure (client timeout on first session). - // set env and directly in order to handle static init/gc issues - System.setProperty("zookeeper.preAllocSize", "100"); - FileTxnLog.setPreallocSize(100 * 1024); + static void shutdownServerInstance(ServerCnxnFactory factory, + String hostPort) { + if (factory != null) { + ZKDatabase zkDb; + ZooKeeperServer zs = getServer(factory); + zkDb = zs.getZKDatabase(); + factory.shutdown(); + try { + zkDb.close(); + } catch (IOException ie) { + LOG.warn("Error closing logs ", ie); + } + final int port = getPort(hostPort); + assertTrue(ClientBaseWithFixes.waitForServerDown("127.0.0.1:" + port, + CONNECTION_TIMEOUT), "waiting for server down"); } + } - protected void setUpAll() throws Exception { - allClients = new LinkedList(); - allClientsSetup = true; - } + /** + * Test specific setup. + */ + public static void setupTestEnv() { + // during the tests we run with 100K prealloc in the logs. + // on windows systems prealloc of 64M was seen to take ~15seconds + // resulting in test failure (client timeout on first session). + // set env and directly in order to handle static init/gc issues + System.setProperty("zookeeper.preAllocSize", "100"); + FileTxnLog.setPreallocSize(100 * 1024); + } - @Before - public void setUp() throws Exception { - BASETEST.mkdirs(); + protected void setUpAll() throws Exception { + allClients = new LinkedList(); + allClientsSetup = true; + } - setupTestEnv(); + @BeforeEach + public void setUp() throws Exception { + BASETEST.mkdirs(); - setUpAll(); + setupTestEnv(); - tmpDir = createTmpDir(BASETEST); + setUpAll(); - startServer(); + tmpDir = createTmpDir(BASETEST); - LOG.info("Client test setup finished"); - } + startServer(); - private String initHostPort() { - BASETEST.mkdirs(); - int port = 0; - try { - port = ServerSocketUtil.getPort(port, 100); - } catch (IOException e) { - throw new RuntimeException(e); - } - return "127.0.0.1:" + port; - } + LOG.info("Client test setup finished"); + } - protected void startServer() throws Exception { - LOG.info("STARTING server"); - serverFactory = createNewServerInstance(tmpDir, serverFactory, hostPort, maxCnxns); + private String initHostPort() { + BASETEST.mkdirs(); + int port = 0; + try { + port = ServerSocketUtil.getPort(port, 100); + } catch (IOException e) { + throw new RuntimeException(e); } + return "127.0.0.1:" + port; + } - protected void stopServer() throws Exception { - LOG.info("STOPPING server"); - shutdownServerInstance(serverFactory, hostPort); - serverFactory = null; - } + protected void startServer() throws Exception { + LOG.info("STARTING server"); + serverFactory = createNewServerInstance(tmpDir, serverFactory, hostPort, maxCnxns); + } + protected void stopServer() throws Exception { + LOG.info("STOPPING server"); + shutdownServerInstance(serverFactory, hostPort); + serverFactory = null; + } - protected static ZooKeeperServer getServer(ServerCnxnFactory fac) { - return fac.getZooKeeperServer(); - } + protected static ZooKeeperServer getServer(ServerCnxnFactory fac) { + return fac.getZooKeeperServer(); + } - protected void tearDownAll() throws Exception { - synchronized (this) { - if (allClients != null) for (ZooKeeper zk : allClients) { - try { - if (zk != null) - zk.close(); - } catch (InterruptedException e) { - LOG.warn("ignoring interrupt", e); - } + protected void tearDownAll() throws Exception { + synchronized (this) { + if (allClients != null) for (ZooKeeper zk : allClients) { + try { + if (zk != null) { + zk.close(); } - allClients = null; + } catch (InterruptedException e) { + LOG.warn("ignoring interrupt", e); } + } + allClients = null; } + } - @After - public void tearDown() throws Exception { - LOG.info("tearDown starting"); - - tearDownAll(); + @AfterEach + public void tearDown() throws Exception { + LOG.info("tearDown starting"); - stopServer(); + tearDownAll(); - if (tmpDir != null) { - Assert.assertTrue("delete " + tmpDir.toString(), recursiveDelete(tmpDir)); - } + stopServer(); - // This has to be set to null when the same instance of this class is reused between test cases - serverFactory = null; + if (tmpDir != null) { + assertTrue(recursiveDelete(tmpDir), "delete " + tmpDir.toString()); } - public static boolean recursiveDelete(File d) { - if (d.isDirectory()) { - File children[] = d.listFiles(); - for (File f : children) { - Assert.assertTrue("delete " + f.toString(), recursiveDelete(f)); - } - } - return d.delete(); + // This has to be set to null when the same instance of this class is reused between test cases + serverFactory = null; + } + + public static boolean recursiveDelete(File d) { + if (d.isDirectory()) { + File[] children = d.listFiles(); + for (File f : children) { + assertTrue(recursiveDelete(f), "delete " + f.toString()); + } } + return d.delete(); + } } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestActiveStandbyElectorRealZK.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestActiveStandbyElectorRealZK.java index 7003e99f15382..88ce4db7e8967 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestActiveStandbyElectorRealZK.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestActiveStandbyElectorRealZK.java @@ -18,8 +18,14 @@ package org.apache.hadoop.ha; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.reset; import java.util.Collections; import java.util.UUID; @@ -33,9 +39,10 @@ import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.server.ZooKeeperServer; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import org.mockito.AdditionalMatchers; -import org.mockito.Mockito; import org.apache.hadoop.thirdparty.com.google.common.primitives.Ints; import org.slf4j.event.Level; @@ -58,7 +65,7 @@ public class TestActiveStandbyElectorRealZK extends ClientBaseWithFixes { new ActiveStandbyElectorCallback[NUM_ELECTORS]; private ZooKeeperServer zkServer; - + @BeforeEach @Override public void setUp() throws Exception { super.setUp(); @@ -66,7 +73,7 @@ public void setUp() throws Exception { zkServer = getServer(serverFactory); for (int i = 0; i < NUM_ELECTORS; i++) { - cbs[i] = Mockito.mock(ActiveStandbyElectorCallback.class); + cbs[i] = mock(ActiveStandbyElectorCallback.class); appDatas[i] = Ints.toByteArray(i); electors[i] = new ActiveStandbyElector(hostPort, 5000, PARENT_DIR, Ids.OPEN_ACL_UNSAFE, Collections. emptyList(), cbs[i], @@ -76,9 +83,9 @@ Ids.OPEN_ACL_UNSAFE, Collections. emptyList(), cbs[i], private void checkFatalsAndReset() throws Exception { for (int i = 0; i < NUM_ELECTORS; i++) { - Mockito.verify(cbs[i], Mockito.never()).notifyFatalError( - Mockito.anyString()); - Mockito.reset(cbs[i]); + verify(cbs[i], never()).notifyFatalError( + anyString()); + reset(cbs[i]); } } @@ -88,7 +95,8 @@ private void checkFatalsAndReset() throws Exception { * Upon becoming active the leader quits election and the test verifies that * the standby now becomes active. */ - @Test(timeout=20000) + @Test + @Timeout(value = 20) public void testActiveStandbyTransition() throws Exception { LOG.info("starting test with parentDir:" + PARENT_DIR); @@ -100,24 +108,24 @@ public void testActiveStandbyTransition() throws Exception { electors[0].joinElection(appDatas[0]); ActiveStandbyElectorTestUtil.waitForActiveLockData(null, zkServer, PARENT_DIR, appDatas[0]); - Mockito.verify(cbs[0], Mockito.timeout(1000)).becomeActive(); + verify(cbs[0], timeout(1000)).becomeActive(); checkFatalsAndReset(); // Second elector joins election, becomes standby. electors[1].joinElection(appDatas[1]); - Mockito.verify(cbs[1], Mockito.timeout(1000)).becomeStandby(); + verify(cbs[1], timeout(1000)).becomeStandby(); checkFatalsAndReset(); // First elector quits, second one should become active electors[0].quitElection(true); ActiveStandbyElectorTestUtil.waitForActiveLockData(null, zkServer, PARENT_DIR, appDatas[1]); - Mockito.verify(cbs[1], Mockito.timeout(1000)).becomeActive(); + verify(cbs[1], timeout(1000)).becomeActive(); checkFatalsAndReset(); // First one rejoins, becomes standby, second one stays active electors[0].joinElection(appDatas[0]); - Mockito.verify(cbs[0], Mockito.timeout(1000)).becomeStandby(); + verify(cbs[0], timeout(1000)).becomeStandby(); checkFatalsAndReset(); // Second one expires, first one becomes active @@ -127,16 +135,16 @@ public void testActiveStandbyTransition() throws Exception { ActiveStandbyElectorTestUtil.waitForActiveLockData(null, zkServer, PARENT_DIR, appDatas[0]); - Mockito.verify(cbs[1], Mockito.timeout(1000)).enterNeutralMode(); - Mockito.verify(cbs[0], Mockito.timeout(1000)).fenceOldActive( + verify(cbs[1], timeout(1000)).enterNeutralMode(); + verify(cbs[0], timeout(1000)).fenceOldActive( AdditionalMatchers.aryEq(appDatas[1])); - Mockito.verify(cbs[0], Mockito.timeout(1000)).becomeActive(); + verify(cbs[0], timeout(1000)).becomeActive(); } finally { electors[1].allowSessionReestablishmentForTests(); } // Second one eventually reconnects and becomes standby - Mockito.verify(cbs[1], Mockito.timeout(5000)).becomeStandby(); + verify(cbs[1], timeout(5000)).becomeStandby(); checkFatalsAndReset(); // First one expires, second one should become active @@ -146,10 +154,10 @@ public void testActiveStandbyTransition() throws Exception { ActiveStandbyElectorTestUtil.waitForActiveLockData(null, zkServer, PARENT_DIR, appDatas[1]); - Mockito.verify(cbs[0], Mockito.timeout(1000)).enterNeutralMode(); - Mockito.verify(cbs[1], Mockito.timeout(1000)).fenceOldActive( + verify(cbs[0], timeout(1000)).enterNeutralMode(); + verify(cbs[1], timeout(1000)).fenceOldActive( AdditionalMatchers.aryEq(appDatas[0])); - Mockito.verify(cbs[1], Mockito.timeout(1000)).becomeActive(); + verify(cbs[1], timeout(1000)).becomeActive(); } finally { electors[0].allowSessionReestablishmentForTests(); } @@ -157,7 +165,8 @@ public void testActiveStandbyTransition() throws Exception { checkFatalsAndReset(); } - @Test(timeout=15000) + @Test + @Timeout(value = 15) public void testHandleSessionExpiration() throws Exception { ActiveStandbyElectorCallback cb = cbs[0]; byte[] appData = appDatas[0]; @@ -169,19 +178,19 @@ public void testHandleSessionExpiration() throws Exception { ZooKeeperServer zks = getServer(serverFactory); ActiveStandbyElectorTestUtil.waitForActiveLockData(null, zks, PARENT_DIR, appData); - Mockito.verify(cb, Mockito.timeout(1000)).becomeActive(); + verify(cb, timeout(1000)).becomeActive(); checkFatalsAndReset(); LOG.info("========================== Expiring session"); zks.closeSession(elector.getZKSessionIdForTests()); // Should enter neutral mode when disconnected - Mockito.verify(cb, Mockito.timeout(1000)).enterNeutralMode(); + verify(cb, timeout(1000)).enterNeutralMode(); // Should re-join the election and regain active ActiveStandbyElectorTestUtil.waitForActiveLockData(null, zks, PARENT_DIR, appData); - Mockito.verify(cb, Mockito.timeout(1000)).becomeActive(); + verify(cb, timeout(1000)).becomeActive(); checkFatalsAndReset(); LOG.info("========================== Quitting election"); @@ -192,14 +201,15 @@ public void testHandleSessionExpiration() throws Exception { // Double check that we don't accidentally re-join the election // due to receiving the "expired" event. Thread.sleep(1000); - Mockito.verify(cb, Mockito.never()).becomeActive(); + verify(cb, never()).becomeActive(); ActiveStandbyElectorTestUtil.waitForActiveLockData(null, zks, PARENT_DIR, null); checkFatalsAndReset(); } - @Test(timeout=15000) + @Test + @Timeout(value = 15) public void testHandleSessionExpirationOfStandby() throws Exception { // Let elector 0 be active electors[0].ensureParentZNode(); @@ -207,7 +217,7 @@ public void testHandleSessionExpirationOfStandby() throws Exception { ZooKeeperServer zks = getServer(serverFactory); ActiveStandbyElectorTestUtil.waitForActiveLockData(null, zks, PARENT_DIR, appDatas[0]); - Mockito.verify(cbs[0], Mockito.timeout(1000)).becomeActive(); + verify(cbs[0], timeout(1000)).becomeActive(); checkFatalsAndReset(); // Let elector 1 be standby @@ -219,7 +229,7 @@ public void testHandleSessionExpirationOfStandby() throws Exception { zks.closeSession(electors[1].getZKSessionIdForTests()); // Should enter neutral mode when disconnected - Mockito.verify(cbs[1], Mockito.timeout(1000)).enterNeutralMode(); + verify(cbs[1], timeout(1000)).enterNeutralMode(); // Should re-join the election and go back to STANDBY ActiveStandbyElectorTestUtil.waitForElectorState(null, electors[1], @@ -235,14 +245,15 @@ public void testHandleSessionExpirationOfStandby() throws Exception { // due to receiving the "expired" event. Thread.sleep(1000); - Mockito.verify(cbs[1], Mockito.never()).becomeActive(); + verify(cbs[1], never()).becomeActive(); ActiveStandbyElectorTestUtil.waitForActiveLockData(null, zks, PARENT_DIR, null); checkFatalsAndReset(); } - @Test(timeout=15000) + @Test + @Timeout(value = 15) public void testDontJoinElectionOnDisconnectAndReconnect() throws Exception { electors[0].ensureParentZNode(); @@ -253,8 +264,8 @@ public void testDontJoinElectionOnDisconnectAndReconnect() throws Exception { waitForServerUp(hostPort, CONNECTION_TIMEOUT); // Have to sleep to allow time for the clients to reconnect. Thread.sleep(2000); - Mockito.verify(cbs[0], Mockito.never()).becomeActive(); - Mockito.verify(cbs[1], Mockito.never()).becomeActive(); + verify(cbs[0], never()).becomeActive(); + verify(cbs[1], never()).becomeActive(); checkFatalsAndReset(); } @@ -262,11 +273,12 @@ public void testDontJoinElectionOnDisconnectAndReconnect() throws Exception { * Test to verify that proper ZooKeeper ACLs can be updated on * ActiveStandbyElector's parent znode. */ - @Test(timeout = 15000) + @Test + @Timeout(value = 15) public void testSetZooKeeperACLsOnParentZnodeName() throws Exception { ActiveStandbyElectorCallback cb = - Mockito.mock(ActiveStandbyElectorCallback.class); + mock(ActiveStandbyElectorCallback.class); ActiveStandbyElector elector = new ActiveStandbyElector(hostPort, 5000, PARENT_DIR, Ids.READ_ACL_UNSAFE, Collections.emptyList(), cb, diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestZKFailoverController.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestZKFailoverController.java index d90702380178e..c85112ba2b2bc 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestZKFailoverController.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestZKFailoverController.java @@ -17,12 +17,19 @@ */ package org.apache.hadoop.ha; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.same; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; import java.net.InetSocketAddress; import java.security.NoSuchAlgorithmException; -import java.util.concurrent.TimeUnit; import java.util.function.Supplier; import org.apache.hadoop.HadoopIllegalArgumentException; import org.apache.hadoop.conf.Configuration; @@ -41,24 +48,18 @@ import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat; import org.apache.zookeeper.server.auth.DigestAuthenticationProvider; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.Timeout; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import org.mockito.Mockito; import org.slf4j.event.Level; +@Timeout(180) public class TestZKFailoverController extends ClientBaseWithFixes { private Configuration conf; private MiniZKFCCluster cluster; - /** - * Set the timeout for every test - */ - @Rule - public Timeout testTimeout = new Timeout(3, TimeUnit.MINUTES); - // Set up ZK digest-based credentials for the purposes of the tests, // to make sure all of our functionality works with auth and ACLs // present. @@ -81,7 +82,7 @@ public class TestZKFailoverController extends ClientBaseWithFixes { GenericTestUtils.setLogLevel(ActiveStandbyElector.LOG, Level.TRACE); } - @Before + @BeforeEach public void setupConfAndServices() { conf = new Configuration(); conf.set(ZKFailoverController.ZK_ACL_KEY, TEST_ACL); @@ -91,7 +92,7 @@ public void setupConfAndServices() { this.cluster = new MiniZKFCCluster(conf, getServer(serverFactory)); } - @After + @AfterEach public void teardown() { if (cluster != null) { try { @@ -216,8 +217,8 @@ protected String getScopeInsideParentNode() { @Test public void testWontRunWhenAutoFailoverDisabled() throws Exception { DummyHAService svc = cluster.getService(1); - svc = Mockito.spy(svc); - Mockito.doReturn(false).when(svc).isAutoFailoverEnabled(); + svc = spy(svc); + doReturn(false).when(svc).isAutoFailoverEnabled(); assertEquals(ZKFailoverController.ERR_CODE_AUTO_FAILOVER_NOT_ENABLED, runFC(svc, "-formatZK")); @@ -253,8 +254,8 @@ public void testFormatSetsAcls() throws Exception { */ @Test public void testFencingMustBeConfigured() throws Exception { - DummyHAService svc = Mockito.spy(cluster.getService(0)); - Mockito.doThrow(new BadFencingConfigurationException("no fencing")) + DummyHAService svc = spy(cluster.getService(0)); + doThrow(new BadFencingConfigurationException("no fencing")) .when(svc).checkFencingConfigured(); // Format the base dir, should succeed assertEquals(0, runFC(svc, "-formatZK")); @@ -289,7 +290,7 @@ public void testAutoFailoverOnBadHealth() throws Exception { // Should fail back to svc0 at this point cluster.waitForHAState(0, HAServiceState.ACTIVE); // and fence svc1 - Mockito.verify(svc1.fencer).fence(Mockito.same(svc1)); + verify(svc1.fencer).fence(same(svc1)); } /** @@ -394,7 +395,7 @@ public void testBecomingActiveFails() throws Exception { cluster.waitForActiveLockHolder(null); - Mockito.verify(svc1.proxy, Mockito.timeout(2000).atLeastOnce()) + verify(svc1.proxy, timeout(2000).atLeastOnce()) .transitionToActive(Mockito.any()); cluster.waitForHAState(0, HAServiceState.INITIALIZING); @@ -476,8 +477,8 @@ public void testCedeActive() throws Exception { long st = Time.now(); proxy.cedeActive(3000); long et = Time.now(); - assertTrue("RPC to cedeActive took " + (et - st) + " ms", - et - st < 1000); + assertTrue(et - st < 1000, + "RPC to cedeActive took " + (et - st) + " ms"); // Should be in "INIT" state since it's not in the election // at this point. @@ -488,9 +489,9 @@ public void testCedeActive() throws Exception { // since the other node in the cluster would have taken ACTIVE. cluster.waitForElectorState(0, ActiveStandbyElector.State.STANDBY); long et2 = Time.now(); - assertTrue("Should take ~3 seconds to rejoin. Only took " + (et2 - et) + - "ms before rejoining.", - et2 - et > 2800); + assertTrue(et2 - et > 2800, + "Should take ~3 seconds to rejoin. Only took " + (et2 - et) + + "ms before rejoining."); } @Test diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestZKFailoverControllerStress.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestZKFailoverControllerStress.java index 1fd339bfc359b..0e2e3e06cec79 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestZKFailoverControllerStress.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestZKFailoverControllerStress.java @@ -24,9 +24,10 @@ import org.apache.hadoop.fs.CommonConfigurationKeys; import org.apache.hadoop.util.Time; import org.apache.zookeeper.server.ServerCnxn; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -45,14 +46,14 @@ public class TestZKFailoverControllerStress extends ClientBaseWithFixes { private Configuration conf; private MiniZKFCCluster cluster; - @Before + @BeforeEach public void setupConfAndServices() throws Exception { conf = new Configuration(); conf.set(ZKFailoverController.ZK_QUORUM_KEY, hostPort); this.cluster = new MiniZKFCCluster(conf, getServer(serverFactory)); } - @After + @AfterEach public void stopCluster() throws Exception { if (cluster != null) { cluster.stop(); @@ -63,7 +64,8 @@ public void stopCluster() throws Exception { * Simply fail back and forth between two services for the * configured amount of time, via expiring their ZK sessions. */ - @Test(timeout=(STRESS_RUNTIME_SECS + EXTRA_TIMEOUT_SECS) * 1000) + @Test + @Timeout(STRESS_RUNTIME_SECS + EXTRA_TIMEOUT_SECS) public void testExpireBackAndForth() throws Exception { cluster.start(); long st = Time.now(); @@ -89,7 +91,8 @@ public void testExpireBackAndForth() throws Exception { * we just do random expirations and expect neither one to ever * generate fatal exceptions. */ - @Test(timeout=(STRESS_RUNTIME_SECS + EXTRA_TIMEOUT_SECS) * 1000) + @Test + @Timeout(STRESS_RUNTIME_SECS + EXTRA_TIMEOUT_SECS) public void testRandomExpirations() throws Exception { cluster.start(); long st = Time.now(); @@ -116,7 +119,8 @@ public void testRandomExpirations() throws Exception { * cluster. Meanwhile, causes ZK to disconnect clients every * 50ms, to trigger the retry code and failures to become active. */ - @Test(timeout=(STRESS_RUNTIME_SECS + EXTRA_TIMEOUT_SECS) * 1000) + @Test + @Timeout(STRESS_RUNTIME_SECS + EXTRA_TIMEOUT_SECS) public void testRandomHealthAndDisconnects() throws Exception { long runFor = STRESS_RUNTIME_SECS * 1000; Mockito.doAnswer(new RandomlyThrow(0)) diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/impl/MetricsRecords.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/impl/MetricsRecords.java index 786571441fd1b..3f87cb147c045 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/impl/MetricsRecords.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/impl/MetricsRecords.java @@ -24,8 +24,8 @@ import org.apache.hadoop.metrics2.MetricsRecord; import org.apache.hadoop.metrics2.MetricsTag; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; /** * Utility class mainly for tests @@ -61,8 +61,8 @@ public static void assertMetricNotNull(MetricsRecord record, String metricName) { AbstractMetric resourceLimitMetric = getFirstMetricByName( record, metricName); - assertNotNull("Metric " + metricName + " doesn't exist", - resourceLimitMetric); + assertNotNull(resourceLimitMetric, + "Metric " + metricName + " doesn't exist"); } private static MetricsTag getFirstTagByName(MetricsRecord record, diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/sink/RollingFileSystemSinkTestBase.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/sink/RollingFileSystemSinkTestBase.java index 0f90d82f53c8f..fdf087a44c96f 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/sink/RollingFileSystemSinkTestBase.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/sink/RollingFileSystemSinkTestBase.java @@ -54,12 +54,12 @@ import org.apache.hadoop.metrics2.lib.MutableGaugeInt; import org.apache.hadoop.metrics2.lib.MutableGaugeLong; import org.apache.hadoop.test.GenericTestUtils; -import org.junit.AfterClass; -import org.junit.Rule; -import org.junit.rules.TestName; -import org.junit.Before; -import org.junit.BeforeClass; -import static org.junit.Assert.assertTrue; +import org.apache.hadoop.test.TestName; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.RegisterExtension; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * This class is a base class for testing the {@link RollingFileSystemSink} @@ -78,8 +78,8 @@ public class RollingFileSystemSinkTestBase { /** * The name of the current test method. */ - @Rule - public TestName methodName = new TestName(); + @RegisterExtension + private TestName methodName = new TestName(); /** * A sample metric class @@ -119,7 +119,7 @@ public MyMetrics2 registerWith(MetricsSystem ms) { /** * Set the date format's timezone to GMT. */ - @BeforeClass + @BeforeAll public static void setup() { DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT")); FileUtil.fullyDelete(ROOT_TEST_DIR); @@ -129,7 +129,7 @@ public static void setup() { * Delete the test directory for this test. * @throws IOException thrown if the delete fails */ - @AfterClass + @AfterAll public static void deleteBaseDir() throws IOException { FileUtil.fullyDelete(ROOT_TEST_DIR); } @@ -138,12 +138,11 @@ public static void deleteBaseDir() throws IOException { * Create the test directory for this test. * @throws IOException thrown if the create fails */ - @Before + @BeforeEach public void createMethodDir() throws IOException { methodDir = new File(ROOT_TEST_DIR, methodName.getMethodName()); - assertTrue("Test directory already exists: " + methodDir, - methodDir.mkdirs()); + assertTrue(methodDir.mkdirs(), "Test directory already exists: " + methodDir); } /** @@ -265,7 +264,7 @@ protected String readLogFile(String path, String then, int count) } } - assertTrue("No valid log directories found", found); + assertTrue(found, "No valid log directories found"); return metrics.toString(); } @@ -342,8 +341,9 @@ protected void assertMetricsContents(String contents) { + "\\s+testTag22=testTagValue22,\\s+Hostname=.*$[\\n\\r]*", Pattern.MULTILINE); - assertTrue("Sink did not produce the expected output. Actual output was: " - + contents, expectedContentPattern.matcher(contents).matches()); + assertTrue(expectedContentPattern.matcher(contents).matches(), + "Sink did not produce the expected output. Actual output was: " + + contents); } /** @@ -366,8 +366,9 @@ protected void assertExtraContents(String contents) { + "\\s+testTag22=testTagValue22,\\s+Hostname=.*$[\\n\\r]*", Pattern.MULTILINE); - assertTrue("Sink did not produce the expected output. Actual output was: " - + contents, expectedContentPattern.matcher(contents).matches()); + assertTrue(expectedContentPattern.matcher(contents).matches(), + "Sink did not produce the expected output. Actual output was: " + + contents); } /** @@ -500,10 +501,12 @@ public void assertFileCount(FileSystem fs, Path dir, int expected) count++; } - assertTrue("The sink created additional unexpected log files. " + count - + " files were created", expected >= count); - assertTrue("The sink created too few log files. " + count + " files were " - + "created", expected <= count); + assertTrue(expected >= count, + "The sink created additional unexpected log files. " + count + + " files were created"); + assertTrue(expected <= count, + "The sink created too few log files. " + count + " files were " + + "created"); } /** diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/sink/TestRollingFileSystemSinkWithLocal.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/sink/TestRollingFileSystemSinkWithLocal.java index 1a69c8dd882b3..c9a6db89f9c58 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/sink/TestRollingFileSystemSinkWithLocal.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/sink/TestRollingFileSystemSinkWithLocal.java @@ -21,9 +21,9 @@ import org.apache.hadoop.fs.FileUtil; import org.apache.hadoop.metrics2.MetricsSystem; -import org.junit.Test; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Test the {@link RollingFileSystemSink} class in the context of the local file @@ -114,9 +114,9 @@ public void testFailedWrite() { // publish the metrics ms.publishMetricsNow(); - assertTrue("No exception was generated while writing metrics " - + "even though the target directory was not writable", - MockSink.errored); + assertTrue(MockSink.errored, + "No exception was generated while writing metrics " + + "even though the target directory was not writable"); ms.stop(); ms.shutdown(); @@ -143,10 +143,10 @@ public void testSilentFailedWrite() { // publish the metrics ms.publishMetricsNow(); - assertFalse("An exception was generated while writing metrics " + assertFalse(MockSink.errored, + "An exception was generated while writing metrics " + "when the target directory was not writable, even though the " - + "sink is set to ignore errors", - MockSink.errored); + + "sink is set to ignore errors"); ms.stop(); ms.shutdown(); diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/TestUserGroupInformation.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/TestUserGroupInformation.java index 1327561cf4356..2fb5b6c22eb71 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/TestUserGroupInformation.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/TestUserGroupInformation.java @@ -33,12 +33,11 @@ import org.apache.hadoop.util.Shell; import org.apache.hadoop.util.StringUtils; import org.apache.hadoop.util.Time; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.mockito.Mockito; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.slf4j.Logger; @@ -81,20 +80,21 @@ import static org.apache.hadoop.test.MetricsAsserts.assertQuantileGauges; import static org.apache.hadoop.test.MetricsAsserts.getDoubleGauge; import static org.apache.hadoop.test.MetricsAsserts.getMetrics; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class TestUserGroupInformation { @@ -134,7 +134,7 @@ public AppConfigurationEntry[] getAppConfigurationEntry(String name) { } /** configure ugi */ - @BeforeClass + @BeforeAll public static void setup() { javax.security.auth.login.Configuration.setConfiguration( new DummyLoginConfiguration()); @@ -145,29 +145,32 @@ public static void setup() { System.setProperty("hadoop.home.dir", (home != null ? home : ".")); } - @Before + @BeforeEach public void setupUgi() { conf = new Configuration(); UserGroupInformation.reset(); UserGroupInformation.setConfiguration(conf); } - @After + @AfterEach public void resetUgi() { UserGroupInformation.setLoginUser(null); } - @Test(timeout = 30000) + @Test + @Timeout(value = 30) public void testSimpleLogin() throws IOException { tryLoginAuthenticationMethod(AuthenticationMethod.SIMPLE, true); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testTokenLogin() throws IOException { tryLoginAuthenticationMethod(AuthenticationMethod.TOKEN, false); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testProxyLogin() throws IOException { tryLoginAuthenticationMethod(AuthenticationMethod.PROXY, false); } @@ -196,7 +199,8 @@ private void tryLoginAuthenticationMethod(AuthenticationMethod method, } } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testGetRealAuthenticationMethod() { UserGroupInformation ugi = UserGroupInformation.createRemoteUser("user1"); ugi.setAuthenticationMethod(AuthenticationMethod.SIMPLE); @@ -207,7 +211,8 @@ public void testGetRealAuthenticationMethod() { assertEquals(AuthenticationMethod.SIMPLE, ugi.getRealAuthenticationMethod()); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testCreateRemoteUser() { UserGroupInformation ugi = UserGroupInformation.createRemoteUser("user1"); assertEquals(AuthenticationMethod.SIMPLE, ugi.getAuthenticationMethod()); @@ -219,7 +224,8 @@ public void testCreateRemoteUser() { } /** Test login method */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testLogin() throws Exception { conf.set(HADOOP_USER_GROUP_METRICS_PERCENTILES_INTERVALS, String.valueOf(PERCENTILES_INTERVAL)); @@ -250,7 +256,8 @@ public UserGroupInformation run() throws IOException { * given user name - get all the groups. * Needs to happen before creating the test users */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testGetServerSideGroups() throws IOException, InterruptedException { // get the user name @@ -311,7 +318,8 @@ public Object run() throws IOException { } /** test constructor */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testConstructor() throws Exception { // security off, so default should just return simple name testConstructorSuccess("user1", "user1"); @@ -325,7 +333,8 @@ public void testConstructor() throws Exception { } /** test constructor */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testConstructorWithRules() throws Exception { // security off, but use rules if explicitly set conf.set(HADOOP_SECURITY_AUTH_TO_LOCAL, @@ -357,7 +366,8 @@ public void testConstructorWithRules() throws Exception { } /** test constructor */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testConstructorWithKerberos() throws Exception { // security on, default is remove default realm conf.set(HADOOP_SECURITY_AUTH_TO_LOCAL_MECHANISM, "hadoop"); @@ -387,7 +397,8 @@ public void testConstructorWithKerberos() throws Exception { } /** test constructor */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testConstructorWithKerberosRules() throws Exception { // security on, explicit rules SecurityUtil.setAuthenticationMethod(AuthenticationMethod.KERBEROS, conf); @@ -423,12 +434,13 @@ private void testConstructorFailures(String userName) { String expect = (userName == null || userName.isEmpty()) ? "Null user" : "Illegal principal name "+userName; String expect2 = "Malformed Kerberos name: "+userName; - assertTrue("Did not find "+ expect + " or " + expect2 + " in " + e, - e.toString().contains(expect) || e.toString().contains(expect2)); + assertTrue(e.toString().contains(expect) || e.toString().contains(expect2), + "Did not find "+ expect + " or " + expect2 + " in " + e); } } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testSetConfigWithRules() { String[] rules = { "RULE:[1:TEST1]", "RULE:[1:TEST2]", "RULE:[1:TEST3]" }; @@ -458,7 +470,8 @@ public void testSetConfigWithRules() { assertEquals(rules[2], KerberosName.getRules()); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testEnsureInitWithRules() throws IOException { String rules = "RULE:[1:RULE1]"; @@ -477,7 +490,8 @@ public void testEnsureInitWithRules() throws IOException { assertEquals(rules, KerberosName.getRules()); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testEquals() throws Exception { UserGroupInformation uugi = UserGroupInformation.createUserForTesting(USER_NAME, GROUP_NAMES); @@ -495,7 +509,8 @@ public void testEquals() throws Exception { assertEquals(uugi.hashCode(), ugi3.hashCode()); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testEqualsWithRealUser() throws Exception { UserGroupInformation realUgi1 = UserGroupInformation.createUserForTesting( "RealUser", GROUP_NAMES); @@ -508,7 +523,8 @@ public void testEqualsWithRealUser() throws Exception { assertFalse(remoteUgi.equals(proxyUgi1)); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testGettingGroups() throws Exception { UserGroupInformation uugi = UserGroupInformation.createUserForTesting(USER_NAME, GROUP_NAMES); @@ -519,8 +535,8 @@ public void testGettingGroups() throws Exception { assertEquals(GROUP1_NAME, uugi.getPrimaryGroupName()); } - @SuppressWarnings("unchecked") // from Mockito mocks - @Test (timeout = 30000) + @SuppressWarnings("unchecked")@Test + @Timeout(value = 30) public void testAddToken() throws Exception { UserGroupInformation ugi = UserGroupInformation.createRemoteUser("someone"); @@ -557,8 +573,8 @@ public void testAddToken() throws Exception { checkTokens(ugi, t1, t2, t3); } - @SuppressWarnings("unchecked") // from Mockito mocks - @Test (timeout = 30000) + @SuppressWarnings("unchecked")@Test + @Timeout(value = 30) public void testGetCreds() throws Exception { UserGroupInformation ugi = UserGroupInformation.createRemoteUser("someone"); @@ -583,8 +599,8 @@ public void testGetCreds() throws Exception { checkTokens(ugi, t1, t2); } - @SuppressWarnings("unchecked") // from Mockito mocks - @Test (timeout = 30000) + @SuppressWarnings("unchecked")@Test + @Timeout(value = 30) public void testAddCreds() throws Exception { UserGroupInformation ugi = UserGroupInformation.createRemoteUser("someone"); @@ -609,7 +625,8 @@ public void testAddCreds() throws Exception { assertSame(secret, ugi.getCredentials().getSecretKey(secretKey)); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testGetCredsNotSame() throws Exception { UserGroupInformation ugi = @@ -636,8 +653,8 @@ private void checkTokens(UserGroupInformation ugi, Token ... tokens) { assertEquals(tokens.length, ugiCreds.numberOfTokens()); } - @SuppressWarnings("unchecked") // from Mockito mocks - @Test (timeout = 30000) + @SuppressWarnings("unchecked")@Test + @Timeout(value = 30) public void testAddNamedToken() throws Exception { UserGroupInformation ugi = UserGroupInformation.createRemoteUser("someone"); @@ -657,8 +674,8 @@ public void testAddNamedToken() throws Exception { assertSame(t1, ugi.getCredentials().getToken(service2)); } - @SuppressWarnings("unchecked") // from Mockito mocks - @Test (timeout = 30000) + @SuppressWarnings("unchecked")@Test + @Timeout(value = 30) public void testUGITokens() throws Exception { UserGroupInformation ugi = UserGroupInformation.createUserForTesting("TheDoctor", @@ -704,7 +721,8 @@ public Collection> run() throws IOException { assertTrue(otherSet.contains(t2)); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testTokenIdentifiers() throws Exception { UserGroupInformation ugi = UserGroupInformation.createUserForTesting( "TheDoctor", new String[] { "TheTARDIS" }); @@ -732,7 +750,8 @@ public Collection run() throws IOException { assertEquals(2, otherSet.size()); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testTestAuthMethod() throws Exception { UserGroupInformation ugi = UserGroupInformation.getCurrentUser(); // verify the reverse mappings works @@ -744,40 +763,42 @@ public void testTestAuthMethod() throws Exception { } } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testUGIAuthMethod() throws Exception { final UserGroupInformation ugi = UserGroupInformation.getCurrentUser(); final AuthenticationMethod am = AuthenticationMethod.KERBEROS; ugi.setAuthenticationMethod(am); - Assert.assertEquals(am, ugi.getAuthenticationMethod()); + assertEquals(am, ugi.getAuthenticationMethod()); ugi.doAs(new PrivilegedExceptionAction() { @Override public Object run() throws IOException { - Assert.assertEquals(am, UserGroupInformation.getCurrentUser() + assertEquals(am, UserGroupInformation.getCurrentUser() .getAuthenticationMethod()); return null; } }); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testUGIAuthMethodInRealUser() throws Exception { final UserGroupInformation ugi = UserGroupInformation.getCurrentUser(); UserGroupInformation proxyUgi = UserGroupInformation.createProxyUser( "proxy", ugi); final AuthenticationMethod am = AuthenticationMethod.KERBEROS; ugi.setAuthenticationMethod(am); - Assert.assertEquals(am, ugi.getAuthenticationMethod()); - Assert.assertEquals(AuthenticationMethod.PROXY, + assertEquals(am, ugi.getAuthenticationMethod()); + assertEquals(AuthenticationMethod.PROXY, proxyUgi.getAuthenticationMethod()); - Assert.assertEquals(am, UserGroupInformation + assertEquals(am, UserGroupInformation .getRealAuthenticationMethod(proxyUgi)); proxyUgi.doAs(new PrivilegedExceptionAction() { @Override public Object run() throws IOException { - Assert.assertEquals(AuthenticationMethod.PROXY, UserGroupInformation + assertEquals(AuthenticationMethod.PROXY, UserGroupInformation .getCurrentUser().getAuthenticationMethod()); - Assert.assertEquals(am, UserGroupInformation.getCurrentUser() + assertEquals(am, UserGroupInformation.getCurrentUser() .getRealUser().getAuthenticationMethod()); return null; } @@ -785,17 +806,18 @@ public Object run() throws IOException { UserGroupInformation proxyUgi2 = new UserGroupInformation(proxyUgi.getSubject()); proxyUgi2.setAuthenticationMethod(AuthenticationMethod.PROXY); - Assert.assertEquals(proxyUgi, proxyUgi2); + assertEquals(proxyUgi, proxyUgi2); // Equality should work if authMethod is null UserGroupInformation realugi = UserGroupInformation.getCurrentUser(); UserGroupInformation proxyUgi3 = UserGroupInformation.createProxyUser( "proxyAnother", realugi); UserGroupInformation proxyUgi4 = new UserGroupInformation(proxyUgi3.getSubject()); - Assert.assertEquals(proxyUgi3, proxyUgi4); + assertEquals(proxyUgi3, proxyUgi4); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testLoginObjectInSubject() throws Exception { UserGroupInformation loginUgi = UserGroupInformation.getLoginUser(); UserGroupInformation anotherUgi = new UserGroupInformation(loginUgi @@ -805,10 +827,11 @@ public void testLoginObjectInSubject() throws Exception { LoginContext login2 = anotherUgi.getSubject().getPrincipals(User.class) .iterator().next().getLogin(); //login1 and login2 must be same instances - Assert.assertTrue(login1 == login2); + assertTrue(login1 == login2); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testLoginModuleCommit() throws Exception { UserGroupInformation loginUgi = UserGroupInformation.getLoginUser(); User user1 = loginUgi.getSubject().getPrincipals(User.class).iterator() @@ -819,7 +842,7 @@ public void testLoginModuleCommit() throws Exception { User user2 = loginUgi.getSubject().getPrincipals(User.class).iterator() .next(); // user1 and user2 must be same instances. - Assert.assertTrue(user1 == user2); + assertTrue(user1 == user2); } public static void verifyLoginMetrics(long success, int failure) @@ -857,7 +880,8 @@ private static void verifyGroupMetrics( * with it, but that Subject was not created by Hadoop (ie it has no * associated User principal) */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testUGIUnderNonHadoopContext() throws Exception { Subject nonHadoopSubject = new Subject(); Subject.doAs(nonHadoopSubject, new PrivilegedExceptionAction() { @@ -870,7 +894,8 @@ public Void run() throws IOException { }); } - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testGetUGIFromSubject() throws Exception { KerberosPrincipal p = new KerberosPrincipal("guest"); Subject subject = new Subject(); @@ -881,7 +906,8 @@ public void testGetUGIFromSubject() throws Exception { } /** Test hasSufficientTimeElapsed method */ - @Test (timeout = 30000) + @Test + @Timeout(value = 30) public void testHasSufficientTimeElapsed() throws Exception { // Make hasSufficientTimeElapsed public Method method = UserGroupInformation.class @@ -915,7 +941,8 @@ public void testHasSufficientTimeElapsed() throws Exception { method.setAccessible(false); } - @Test(timeout=10000) + @Test + @Timeout(value = 10) public void testSetLoginUser() throws IOException { UserGroupInformation ugi = UserGroupInformation.createRemoteUser("test-user"); UserGroupInformation.setLoginUser(ugi); @@ -982,8 +1009,8 @@ public Void run() throws Exception { Token t = mock(Token.class); when(t.getService()).thenReturn(new Text("t" + i)); UserGroupInformation.getCurrentUser().addToken(t); - assertNull("ConcurrentModificationException encountered", - thread.cme); + assertNull(thread.cme, + "ConcurrentModificationException encountered"); } } catch (ConcurrentModificationException cme) { cme.printStackTrace(); @@ -1139,7 +1166,7 @@ public void testGetNextRetryTime() throws Exception { String str = "5th retry, now:" + currentTime + ", retry:" + lastRetry; LOG.info(str); - assertEquals(str, endTime - reloginIntervalMs, lastRetry); + assertEquals(endTime - reloginIntervalMs, lastRetry, str); // make sure no more retries after (tgt endTime - login interval). UserGroupInformation.metrics.getRenewalFailures().incr(); @@ -1147,7 +1174,7 @@ public void testGetNextRetryTime() throws Exception { UserGroupInformation.getNextTgtRenewalTime(endTime, currentTime, rp); str = "overflow retry, now:" + currentTime + ", retry:" + lastRetry; LOG.info(str); - assertEquals(str, endTime - reloginIntervalMs, lastRetry); + assertEquals(endTime - reloginIntervalMs, lastRetry, str); } private void assertWithinBounds(final int numFailures, final long lastRetry, @@ -1160,12 +1187,13 @@ private void assertWithinBounds(final int numFailures, final long lastRetry, + ", lower bound:" + lower + ", upper bound:" + upper + ", retry:" + lastRetry); LOG.info(str); - assertTrue(str, lower <= lastRetry && lastRetry < upper); + assertTrue(lower <= lastRetry && lastRetry < upper, str); } // verify that getCurrentUser on the same and different subjects can be // concurrent. Ie. no synchronization. - @Test(timeout=8000) + @Test + @Timeout(value = 8) public void testConcurrentGetCurrentUser() throws Exception { final CyclicBarrier barrier = new CyclicBarrier(2); final CountDownLatch latch = new CountDownLatch(1); @@ -1181,7 +1209,7 @@ public void testConcurrentGetCurrentUser() throws Exception { Set principals = testUgi1.getSubject().getPrincipals(); User user = testUgi1.getSubject().getPrincipals(User.class).iterator().next(); - final User spyUser = Mockito.spy(user); + final User spyUser = spy(user); principals.remove(user); principals.add(spyUser); when(spyUser.getName()).thenAnswer(new Answer(){ @@ -1269,7 +1297,7 @@ ugi.new TicketCacheRenewalRunnable(tgt, // there should be no exception when calling this userCredsRunnable.run(); // isDestroyed should be called at least once - Mockito.verify(tgt, atLeastOnce()).isDestroyed(); + verify(tgt, atLeastOnce()).isDestroyed(); } @Test @@ -1306,8 +1334,8 @@ public void testImportTokensFromConfig() throws IOException { // Check if the tokens were loaded UserGroupInformation ugi = UserGroupInformation.getLoginUser(); Credentials outCred = ugi.getCredentials(); - assertEquals("Tokens: " + outCred.getAllTokens(), - 2, outCred.getAllTokens().size()); + assertEquals(2, outCred.getAllTokens().size(), + "Tokens: " + outCred.getAllTokens()); boolean found0 = false; boolean found1 = false; for (Token token : outCred.getAllTokens()) { @@ -1321,10 +1349,10 @@ public void testImportTokensFromConfig() throws IOException { found1 = true; } } - assertTrue("Expected token testTokenService0 not found: " + outCred, - found0); - assertTrue("Expected token testTokenService1 not found: " + outCred, - found1); + assertTrue(found0, + "Expected token testTokenService0 not found: " + outCred); + assertTrue(found1, + "Expected token testTokenService1 not found: " + outCred); // Try to add the same token through configuration and file Credentials cred1 = new Credentials(); @@ -1336,8 +1364,8 @@ public void testImportTokensFromConfig() throws IOException { UserGroupInformation ugi1 = UserGroupInformation.getLoginUser(); Credentials outCred1 = ugi1.getCredentials(); - assertEquals("Tokens: " + outCred1.getAllTokens(), - 1, outCred1.getAllTokens().size()); + assertEquals(1, outCred1.getAllTokens().size(), + "Tokens: " + outCred1.getAllTokens()); } @Test @@ -1356,8 +1384,8 @@ public void testImportTokensFromProperty() throws IOException { UserGroupInformation.reset(); UserGroupInformation ugi = UserGroupInformation.getLoginUser(); Credentials creds = ugi.getCredentials(); - assertEquals("Tokens: " + creds.getAllTokens(), - 1, creds.getAllTokens().size()); + assertEquals(1, creds.getAllTokens().size(), + "Tokens: " + creds.getAllTokens()); assertArrayEquals(creds.getToken(service).getIdentifier(), identity); // Cleanup diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/delegation/TestZKDelegationTokenSecretManager.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/delegation/TestZKDelegationTokenSecretManager.java index 25dae7e4fd5cc..58c3f54101841 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/delegation/TestZKDelegationTokenSecretManager.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/delegation/TestZKDelegationTokenSecretManager.java @@ -18,6 +18,12 @@ package org.apache.hadoop.security.token.delegation; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -53,18 +59,18 @@ import org.apache.zookeeper.data.Id; import org.apache.zookeeper.data.Stat; import org.apache.zookeeper.server.auth.DigestAuthenticationProvider; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.Timeout; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.Timeout; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.junit.Assert.fail; - - +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +@Timeout(300) public class TestZKDelegationTokenSecretManager { private static final Logger LOG = LoggerFactory.getLogger(TestZKDelegationTokenSecretManager.class); @@ -79,16 +85,13 @@ public class TestZKDelegationTokenSecretManager { protected TestingServer zkServer; - @Rule - public Timeout globalTimeout = new Timeout(300000, TimeUnit.MILLISECONDS); - - @Before + @BeforeEach public void setup() throws Exception { zkServer = new TestingServer(); zkServer.start(); } - @After + @AfterEach public void tearDown() throws Exception { if (zkServer != null) { zkServer.close(); @@ -111,11 +114,13 @@ protected Configuration getSecretConf(String connectString) { @SuppressWarnings("unchecked") @Test + @Order(1) public void testMultiNodeOperations() throws Exception { testMultiNodeOperationsImpl(false); } @Test + @Order(2) public void testMultiNodeOperationsWithZeroRetry() throws Exception { testMultiNodeOperationsImpl(true); } @@ -136,7 +141,7 @@ public void testMultiNodeOperationsImpl(boolean setZeroRetry) throws Exception { Token token = (Token) tm1.createToken( UserGroupInformation.getCurrentUser(), "foo"); - Assert.assertNotNull(token); + assertNotNull(token); tm2.verifyToken(token); tm2.renewToken(token, "foo"); tm1.verifyToken(token); @@ -150,7 +155,7 @@ public void testMultiNodeOperationsImpl(boolean setZeroRetry) throws Exception { token = (Token) tm2.createToken( UserGroupInformation.getCurrentUser(), "bar"); - Assert.assertNotNull(token); + assertNotNull(token); tm1.verifyToken(token); tm1.renewToken(token, "bar"); tm2.verifyToken(token); @@ -168,6 +173,7 @@ public void testMultiNodeOperationsImpl(boolean setZeroRetry) throws Exception { @SuppressWarnings("unchecked") @Test + @Order(5) public void testNodeUpAferAWhile() throws Exception { for (int i = 0; i < TEST_RETRIES; i++) { String connectString = zkServer.getConnectString(); @@ -177,15 +183,15 @@ public void testNodeUpAferAWhile() throws Exception { Token token1 = (Token) tm1.createToken( UserGroupInformation.getCurrentUser(), "foo"); - Assert.assertNotNull(token1); + assertNotNull(token1); Token token2 = (Token) tm1.createToken( UserGroupInformation.getCurrentUser(), "bar"); - Assert.assertNotNull(token2); + assertNotNull(token2); Token token3 = (Token) tm1.createToken( UserGroupInformation.getCurrentUser(), "boo"); - Assert.assertNotNull(token3); + assertNotNull(token3); tm1.verifyToken(token1); tm1.verifyToken(token2); @@ -212,7 +218,7 @@ public void testNodeUpAferAWhile() throws Exception { Token token4 = (Token) tm2.createToken( UserGroupInformation.getCurrentUser(), "xyz"); - Assert.assertNotNull(token4); + assertNotNull(token4); tm2.verifyToken(token4); tm1.verifyToken(token4); @@ -241,6 +247,7 @@ public void testNodeUpAferAWhile() throws Exception { @SuppressWarnings("unchecked") @Test + @Order(6) public void testMultiNodeCompeteForSeqNum() throws Exception { DelegationTokenManager tm1, tm2 = null; String connectString = zkServer.getConnectString(); @@ -253,19 +260,17 @@ public void testMultiNodeCompeteForSeqNum() throws Exception { Token token1 = (Token) tm1.createToken( UserGroupInformation.getCurrentUser(), "foo"); - Assert.assertNotNull(token1); + assertNotNull(token1); AbstractDelegationTokenIdentifier id1 = tm1.getDelegationTokenSecretManager().decodeTokenIdentifier(token1); - Assert.assertEquals( - "Token seq should be the same", 1, id1.getSequenceNumber()); + assertEquals(1, id1.getSequenceNumber(), "Token seq should be the same"); Token token2 = (Token) tm1.createToken( UserGroupInformation.getCurrentUser(), "foo"); - Assert.assertNotNull(token2); + assertNotNull(token2); AbstractDelegationTokenIdentifier id2 = tm1.getDelegationTokenSecretManager().decodeTokenIdentifier(token2); - Assert.assertEquals( - "Token seq should be the same", 2, id2.getSequenceNumber()); + assertEquals(2, id2.getSequenceNumber(), "Token seq should be the same"); tm2 = new DelegationTokenManager(conf, new Text("bla")); tm2.init(); @@ -273,19 +278,17 @@ public void testMultiNodeCompeteForSeqNum() throws Exception { Token token3 = (Token) tm2.createToken( UserGroupInformation.getCurrentUser(), "foo"); - Assert.assertNotNull(token3); + assertNotNull(token3); AbstractDelegationTokenIdentifier id3 = tm2.getDelegationTokenSecretManager().decodeTokenIdentifier(token3); - Assert.assertEquals( - "Token seq should be the same", 1001, id3.getSequenceNumber()); + assertEquals(1001, id3.getSequenceNumber(), "Token seq should be the same"); Token token4 = (Token) tm2.createToken( UserGroupInformation.getCurrentUser(), "foo"); - Assert.assertNotNull(token4); + assertNotNull(token4); AbstractDelegationTokenIdentifier id4 = tm2.getDelegationTokenSecretManager().decodeTokenIdentifier(token4); - Assert.assertEquals( - "Token seq should be the same", 1002, id4.getSequenceNumber()); + assertEquals(1002, id4.getSequenceNumber(), "Token seq should be the same"); verifyDestroy(tm1, conf); verifyDestroy(tm2, conf); @@ -293,6 +296,7 @@ public void testMultiNodeCompeteForSeqNum() throws Exception { @SuppressWarnings("unchecked") @Test + @Order(3) public void testRenewTokenSingleManager() throws Exception { for (int i = 0; i < TEST_RETRIES; i++) { DelegationTokenManager tm1 = null; @@ -304,7 +308,7 @@ public void testRenewTokenSingleManager() throws Exception { Token token = (Token) tm1.createToken(UserGroupInformation.getCurrentUser(), "foo"); - Assert.assertNotNull(token); + assertNotNull(token); tm1.renewToken(token, "foo"); tm1.verifyToken(token); verifyDestroy(tm1, conf); @@ -313,6 +317,7 @@ public void testRenewTokenSingleManager() throws Exception { @SuppressWarnings("unchecked") @Test + @Order(9) public void testCancelTokenSingleManager() throws Exception { for (int i = 0; i < TEST_RETRIES; i++) { DelegationTokenManager tm1 = null; @@ -324,7 +329,7 @@ public void testCancelTokenSingleManager() throws Exception { Token token = (Token) tm1.createToken(UserGroupInformation.getCurrentUser(), "foo"); - Assert.assertNotNull(token); + assertNotNull(token); tm1.cancelToken(token, "foo"); try { verifyTokenFail(tm1, token); @@ -350,6 +355,7 @@ protected void verifyDestroy(DelegationTokenManager tm, Configuration conf) @SuppressWarnings({ "unchecked", "rawtypes" }) @Test + @Order(7) public void testStopThreads() throws Exception { DelegationTokenManager tm1 = null; String connectString = zkServer.getConnectString(); @@ -371,7 +377,7 @@ public void testStopThreads() throws Exception { Token token = (Token) tm1.createToken(UserGroupInformation.getCurrentUser(), "foo"); - Assert.assertNotNull(token); + assertNotNull(token); tm1.destroy(); } @@ -420,8 +426,8 @@ public List getDefaultAcl() { private void verifyACL(CuratorFramework curatorFramework, String path, ACL expectedACL) throws Exception { List acls = curatorFramework.getACL().forPath(path); - Assert.assertEquals(1, acls.size()); - Assert.assertEquals(expectedACL, acls.get(0)); + assertEquals(1, acls.size()); + assertEquals(expectedACL, acls.get(0)); } // Since it is possible that there can be a delay for the cancel token message @@ -452,6 +458,7 @@ private void verifyTokenFailWithRetry(DelegationTokenManager tm, @SuppressWarnings({ "unchecked" }) @Test + @Order(8) public void testNodesLoadedAfterRestart() throws Exception { final String connectString = zkServer.getConnectString(); final Configuration conf = getSecretConf(connectString); @@ -470,11 +477,11 @@ public void testNodesLoadedAfterRestart() throws Exception { Token token = (Token) tm .createToken(UserGroupInformation.getCurrentUser(), "good"); - Assert.assertNotNull(token); + assertNotNull(token); Token cancelled = (Token) tm .createToken(UserGroupInformation.getCurrentUser(), "cancelled"); - Assert.assertNotNull(cancelled); + assertNotNull(cancelled); tm.verifyToken(token); tm.verifyToken(cancelled); @@ -511,12 +518,12 @@ public Boolean get() { smNew.decodeTokenIdentifier(cancelled); AbstractDelegationTokenSecretManager.DelegationTokenInformation dtinfo = zksmNew.getTokenInfo(id); - Assert.assertNull("canceled dt should be gone!", dtinfo); + assertNull(dtinfo, "canceled dt should be gone!"); // The good token should be loaded on startup, and removed after expiry. id = smNew.decodeTokenIdentifier(token); dtinfo = zksmNew.getTokenInfoFromMemory(id); - Assert.assertNotNull("good dt should be in memory!", dtinfo); + assertNotNull(dtinfo, "good dt should be in memory!"); // Wait for the good token to expire. Thread.sleep(5000); @@ -556,13 +563,14 @@ public void testCreatingParentContainersIfNeeded() throws Exception { // Check if the created NameSpace exists. Stat stat = curatorFramework.checkExists().forPath(workingPath); - Assert.assertNotNull(stat); + assertNotNull(stat); tm1.destroy(); curatorFramework.close(); } @Test + @Order(4) public void testCreateNameSpaceRepeatedly() throws Exception { String connectString = zkServer.getConnectString(); @@ -584,7 +592,7 @@ public void testCreateNameSpaceRepeatedly() throws Exception { // Check if the created NameSpace exists. Stat stat = curatorFramework.checkExists().forPath(workingPath); - Assert.assertNotNull(stat); + assertNotNull(stat); // Repeated creation will throw NodeExists exception LambdaTestUtils.intercept(KeeperException.class, @@ -624,10 +632,10 @@ public void testMultipleInit() throws Exception { List> futures = executorService.invokeAll( Arrays.asList(tm1Callable, tm2Callable)); for(Future future : futures) { - Assert.assertTrue(future.get()); + assertTrue(future.get()); } executorService.shutdownNow(); - Assert.assertTrue(executorService.awaitTermination(1, TimeUnit.SECONDS)); + assertTrue(executorService.awaitTermination(1, TimeUnit.SECONDS)); tm1.destroy(); tm2.destroy(); @@ -636,7 +644,7 @@ public void testMultipleInit() throws Exception { // Check if the created NameSpace exists. Stat stat = curatorFramework.checkExists().forPath(workingPath); - Assert.assertNotNull(stat); + assertNotNull(stat); curatorFramework.close(); } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/delegation/web/TestDelegationTokenAuthenticationHandlerWithMocks.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/delegation/web/TestDelegationTokenAuthenticationHandlerWithMocks.java index bc140fa7b1075..0f5991af06530 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/delegation/web/TestDelegationTokenAuthenticationHandlerWithMocks.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/delegation/web/TestDelegationTokenAuthenticationHandlerWithMocks.java @@ -18,6 +18,18 @@ package org.apache.hadoop.security.token.delegation.web; import static org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticator.DelegationTokenOperation.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.Mockito.contains; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.startsWith; import com.fasterxml.jackson.databind.ObjectMapper; @@ -30,13 +42,10 @@ import org.apache.hadoop.security.authentication.server.AuthenticationToken; import org.apache.hadoop.security.token.SecretManager; import org.apache.hadoop.security.token.Token; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.Timeout; -import org.mockito.Mockito; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -47,8 +56,8 @@ import java.io.StringWriter; import java.util.Map; import java.util.Properties; -import java.util.concurrent.TimeUnit; +@Timeout(120) public class TestDelegationTokenAuthenticationHandlerWithMocks { public static class MockDelegationTokenAuthenticationHandler @@ -93,10 +102,7 @@ public AuthenticationToken authenticate(HttpServletRequest request, private DelegationTokenAuthenticationHandler handler; - @Rule - public Timeout testTimeout = new Timeout(120000, TimeUnit.MILLISECONDS); - - @Before + @BeforeEach public void setUp() throws Exception { Properties conf = new Properties(); @@ -105,7 +111,7 @@ public void setUp() throws Exception { handler.initTokenManager(conf); } - @After + @AfterEach public void cleanUp() { handler.destroy(); } @@ -130,40 +136,40 @@ public void testManagementOperations() throws Exception { } private void testNonManagementOperation() throws Exception { - HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.when(request.getParameter( + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getParameter( DelegationTokenAuthenticator.OP_PARAM)).thenReturn(null); - Assert.assertTrue(handler.managementOperation(null, request, null)); - Mockito.when(request.getParameter( + assertTrue(handler.managementOperation(null, request, null)); + when(request.getParameter( DelegationTokenAuthenticator.OP_PARAM)).thenReturn("CREATE"); - Assert.assertTrue(handler.managementOperation(null, request, null)); + assertTrue(handler.managementOperation(null, request, null)); } private void testManagementOperationErrors() throws Exception { - HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - HttpServletResponse response = Mockito.mock(HttpServletResponse.class); - Mockito.when(request.getQueryString()).thenReturn( + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + when(request.getQueryString()).thenReturn( DelegationTokenAuthenticator.OP_PARAM + "=" + DelegationTokenAuthenticator.DelegationTokenOperation. GETDELEGATIONTOKEN.toString() ); - Mockito.when(request.getMethod()).thenReturn("FOO"); - Assert.assertFalse(handler.managementOperation(null, request, response)); - Mockito.verify(response).sendError( - Mockito.eq(HttpServletResponse.SC_BAD_REQUEST), - Mockito.startsWith("Wrong HTTP method")); - - Mockito.reset(response); - Mockito.when(request.getMethod()).thenReturn( + when(request.getMethod()).thenReturn("FOO"); + assertFalse(handler.managementOperation(null, request, response)); + verify(response).sendError( + eq(HttpServletResponse.SC_BAD_REQUEST), + startsWith("Wrong HTTP method")); + + reset(response); + when(request.getMethod()).thenReturn( DelegationTokenAuthenticator.DelegationTokenOperation. GETDELEGATIONTOKEN.getHttpMethod() ); - Assert.assertFalse(handler.managementOperation(null, request, response)); - Mockito.verify(response).setStatus( - Mockito.eq(HttpServletResponse.SC_UNAUTHORIZED)); - Mockito.verify(response).setHeader( - Mockito.eq(KerberosAuthenticator.WWW_AUTHENTICATE), - Mockito.eq("mock")); + assertFalse(handler.managementOperation(null, request, response)); + verify(response).setStatus( + eq(HttpServletResponse.SC_UNAUTHORIZED)); + verify(response).setHeader( + eq(KerberosAuthenticator.WWW_AUTHENTICATE), + eq("mock")); } private Token testGetToken(String renewer, @@ -171,17 +177,17 @@ private Token testGetToken(String renewer, DelegationTokenAuthenticator.DelegationTokenOperation op = DelegationTokenAuthenticator.DelegationTokenOperation. GETDELEGATIONTOKEN; - HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - HttpServletResponse response = Mockito.mock(HttpServletResponse.class); - Mockito.when(request.getQueryString()). + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + when(request.getQueryString()). thenReturn(DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString()); - Mockito.when(request.getMethod()).thenReturn(op.getHttpMethod()); + when(request.getMethod()).thenReturn(op.getHttpMethod()); - AuthenticationToken token = Mockito.mock(AuthenticationToken.class); - Mockito.when(token.getUserName()).thenReturn("user"); - Mockito.when(response.getWriter()).thenReturn(new PrintWriter( + AuthenticationToken token = mock(AuthenticationToken.class); + when(token.getUserName()).thenReturn("user"); + when(response.getWriter()).thenReturn(new PrintWriter( new StringWriter())); - Assert.assertFalse(handler.managementOperation(token, request, response)); + assertFalse(handler.managementOperation(token, request, response)); String queryString = DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString() + "&" + @@ -190,27 +196,27 @@ private Token testGetToken(String renewer, queryString += "&" + DelegationTokenAuthenticator.SERVICE_PARAM + "=" + service; } - Mockito.when(request.getQueryString()).thenReturn(queryString); - Mockito.reset(response); - Mockito.reset(token); - Mockito.when(token.getUserName()).thenReturn("user"); + when(request.getQueryString()).thenReturn(queryString); + reset(response); + reset(token); + when(token.getUserName()).thenReturn("user"); StringWriter writer = new StringWriter(); PrintWriter pwriter = new PrintWriter(writer); - Mockito.when(response.getWriter()).thenReturn(pwriter); - Assert.assertFalse(handler.managementOperation(token, request, response)); + when(response.getWriter()).thenReturn(pwriter); + assertFalse(handler.managementOperation(token, request, response)); if (renewer == null) { - Mockito.verify(token).getUserName(); + verify(token).getUserName(); } else { - Mockito.verify(token).getUserName(); + verify(token).getUserName(); } - Mockito.verify(response).setStatus(HttpServletResponse.SC_OK); - Mockito.verify(response).setContentType(MediaType.APPLICATION_JSON); + verify(response).setStatus(HttpServletResponse.SC_OK); + verify(response).setContentType(MediaType.APPLICATION_JSON); pwriter.close(); String responseOutput = writer.toString(); String tokenLabel = DelegationTokenAuthenticator. DELEGATION_TOKEN_JSON; - Assert.assertTrue(responseOutput.contains(tokenLabel)); - Assert.assertTrue(responseOutput.contains( + assertTrue(responseOutput.contains(tokenLabel)); + assertTrue(responseOutput.contains( DelegationTokenAuthenticator.DELEGATION_TOKEN_URL_STRING_JSON)); ObjectMapper jsonMapper = new ObjectMapper(); Map json = jsonMapper.readValue(responseOutput, Map.class); @@ -221,11 +227,11 @@ private Token testGetToken(String renewer, Token dt = new Token(); dt.decodeFromUrlString(tokenStr); handler.getTokenManager().verifyToken(dt); - Assert.assertEquals(expectedTokenKind, dt.getKind()); + assertEquals(expectedTokenKind, dt.getKind()); if (service != null) { - Assert.assertEquals(service, dt.getService().toString()); + assertEquals(service, dt.getService().toString()); } else { - Assert.assertEquals(0, dt.getService().getLength()); + assertEquals(0, dt.getService().getLength()); } return dt; } @@ -244,33 +250,33 @@ private void testCancelToken(Token token) DelegationTokenAuthenticator.DelegationTokenOperation op = DelegationTokenAuthenticator.DelegationTokenOperation. CANCELDELEGATIONTOKEN; - HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - HttpServletResponse response = Mockito.mock(HttpServletResponse.class); - Mockito.when(request.getQueryString()).thenReturn( + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + when(request.getQueryString()).thenReturn( DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString()); - Mockito.when(request.getMethod()). + when(request.getMethod()). thenReturn(op.getHttpMethod()); - Assert.assertFalse(handler.managementOperation(null, request, response)); - Mockito.verify(response).sendError( - Mockito.eq(HttpServletResponse.SC_BAD_REQUEST), - Mockito.contains("requires the parameter [token]")); + assertFalse(handler.managementOperation(null, request, response)); + verify(response).sendError( + eq(HttpServletResponse.SC_BAD_REQUEST), + contains("requires the parameter [token]")); - Mockito.reset(response); - Mockito.when(request.getQueryString()).thenReturn( + reset(response); + when(request.getQueryString()).thenReturn( DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString() + "&" + DelegationTokenAuthenticator.TOKEN_PARAM + "=" + token.encodeToUrlString() ); - Assert.assertFalse(handler.managementOperation(null, request, response)); - Mockito.verify(response).setStatus(HttpServletResponse.SC_OK); + assertFalse(handler.managementOperation(null, request, response)); + verify(response).setStatus(HttpServletResponse.SC_OK); try { handler.getTokenManager().verifyToken(token); - Assert.fail(); + fail(); } catch (SecretManager.InvalidToken ex) { //NOP } catch (Throwable ex) { - Assert.fail(); + fail(); } } @@ -288,42 +294,42 @@ private void testRenewToken(Token dToken, DelegationTokenAuthenticator.DelegationTokenOperation op = DelegationTokenAuthenticator.DelegationTokenOperation. RENEWDELEGATIONTOKEN; - HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - HttpServletResponse response = Mockito.mock(HttpServletResponse.class); - Mockito.when(request.getQueryString()). + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + when(request.getQueryString()). thenReturn(DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString()); - Mockito.when(request.getMethod()). + when(request.getMethod()). thenReturn(op.getHttpMethod()); - Assert.assertFalse(handler.managementOperation(null, request, response)); - Mockito.verify(response).setStatus( - Mockito.eq(HttpServletResponse.SC_UNAUTHORIZED)); - Mockito.verify(response).setHeader(Mockito.eq( + assertFalse(handler.managementOperation(null, request, response)); + verify(response).setStatus( + eq(HttpServletResponse.SC_UNAUTHORIZED)); + verify(response).setHeader(eq( KerberosAuthenticator.WWW_AUTHENTICATE), - Mockito.eq("mock") + eq("mock") ); - Mockito.reset(response); - AuthenticationToken token = Mockito.mock(AuthenticationToken.class); - Mockito.when(token.getUserName()).thenReturn(testRenewer); - Assert.assertFalse(handler.managementOperation(token, request, response)); - Mockito.verify(response).sendError( - Mockito.eq(HttpServletResponse.SC_BAD_REQUEST), - Mockito.contains("requires the parameter [token]")); + reset(response); + AuthenticationToken token = mock(AuthenticationToken.class); + when(token.getUserName()).thenReturn(testRenewer); + assertFalse(handler.managementOperation(token, request, response)); + verify(response).sendError( + eq(HttpServletResponse.SC_BAD_REQUEST), + contains("requires the parameter [token]")); - Mockito.reset(response); + reset(response); StringWriter writer = new StringWriter(); PrintWriter pwriter = new PrintWriter(writer); - Mockito.when(response.getWriter()).thenReturn(pwriter); + when(response.getWriter()).thenReturn(pwriter); - Mockito.when(request.getQueryString()). + when(request.getQueryString()). thenReturn(DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString() + "&" + DelegationTokenAuthenticator.TOKEN_PARAM + "=" + dToken.encodeToUrlString()); - Assert.assertFalse(handler.managementOperation(token, request, response)); - Mockito.verify(response).setStatus(HttpServletResponse.SC_OK); + assertFalse(handler.managementOperation(token, request, response)); + verify(response).setStatus(HttpServletResponse.SC_OK); pwriter.close(); - Assert.assertTrue(writer.toString().contains("long")); + assertTrue(writer.toString().contains("long")); handler.getTokenManager().verifyToken(dToken); } @@ -337,104 +343,104 @@ public void testAuthenticate() throws Exception { @SuppressWarnings("unchecked") private void testValidDelegationTokenQueryString() throws Exception { - HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - HttpServletResponse response = Mockito.mock(HttpServletResponse.class); + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); Token dToken = (Token) handler.getTokenManager().createToken( UserGroupInformation.getCurrentUser(), "user"); - Mockito.when(request.getQueryString()).thenReturn( + when(request.getQueryString()).thenReturn( DelegationTokenAuthenticator.DELEGATION_PARAM + "=" + dToken.encodeToUrlString()); AuthenticationToken token = handler.authenticate(request, response); - Assert.assertEquals(UserGroupInformation.getCurrentUser(). + assertEquals(UserGroupInformation.getCurrentUser(). getShortUserName(), token.getUserName()); - Assert.assertEquals(0, token.getExpires()); - Assert.assertEquals(handler.getType(), + assertEquals(0, token.getExpires()); + assertEquals(handler.getType(), token.getType()); - Assert.assertTrue(token.isExpired()); + assertTrue(token.isExpired()); } @SuppressWarnings("unchecked") private void testValidDelegationTokenHeader() throws Exception { - HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - HttpServletResponse response = Mockito.mock(HttpServletResponse.class); + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); Token dToken = (Token) handler.getTokenManager().createToken( UserGroupInformation.getCurrentUser(), "user"); - Mockito.when(request.getHeader(Mockito.eq( + when(request.getHeader(eq( DelegationTokenAuthenticator.DELEGATION_TOKEN_HEADER))).thenReturn( dToken.encodeToUrlString()); AuthenticationToken token = handler.authenticate(request, response); - Assert.assertEquals(UserGroupInformation.getCurrentUser(). + assertEquals(UserGroupInformation.getCurrentUser(). getShortUserName(), token.getUserName()); - Assert.assertEquals(0, token.getExpires()); - Assert.assertEquals(handler.getType(), + assertEquals(0, token.getExpires()); + assertEquals(handler.getType(), token.getType()); - Assert.assertTrue(token.isExpired()); + assertTrue(token.isExpired()); } private void testInvalidDelegationTokenQueryString() throws Exception { - HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - HttpServletResponse response = Mockito.mock(HttpServletResponse.class); - Mockito.when(request.getQueryString()).thenReturn( + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + when(request.getQueryString()).thenReturn( DelegationTokenAuthenticator.DELEGATION_PARAM + "=invalid"); StringWriter writer = new StringWriter(); - Mockito.when(response.getWriter()).thenReturn(new PrintWriter(writer)); - Assert.assertNull(handler.authenticate(request, response)); - Mockito.verify(response).setStatus(HttpServletResponse.SC_FORBIDDEN); - Assert.assertTrue(writer.toString().contains("AuthenticationException")); + when(response.getWriter()).thenReturn(new PrintWriter(writer)); + assertNull(handler.authenticate(request, response)); + verify(response).setStatus(HttpServletResponse.SC_FORBIDDEN); + assertTrue(writer.toString().contains("AuthenticationException")); } private void testInvalidDelegationTokenHeader() throws Exception { - HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - HttpServletResponse response = Mockito.mock(HttpServletResponse.class); - Mockito.when(request.getHeader(Mockito.eq( + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + when(request.getHeader(eq( DelegationTokenAuthenticator.DELEGATION_TOKEN_HEADER))).thenReturn( "invalid"); StringWriter writer = new StringWriter(); - Mockito.when(response.getWriter()).thenReturn(new PrintWriter(writer)); - Assert.assertNull(handler.authenticate(request, response)); - Assert.assertTrue(writer.toString().contains("AuthenticationException")); + when(response.getWriter()).thenReturn(new PrintWriter(writer)); + assertNull(handler.authenticate(request, response)); + assertTrue(writer.toString().contains("AuthenticationException")); } private String getToken() throws Exception { DelegationTokenAuthenticator.DelegationTokenOperation op = DelegationTokenAuthenticator.DelegationTokenOperation. GETDELEGATIONTOKEN; - HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - HttpServletResponse response = Mockito.mock(HttpServletResponse.class); - Mockito.when(request.getQueryString()). + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + when(request.getQueryString()). thenReturn(DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString()); - Mockito.when(request.getMethod()).thenReturn(op.getHttpMethod()); + when(request.getMethod()).thenReturn(op.getHttpMethod()); - AuthenticationToken token = Mockito.mock(AuthenticationToken.class); - Mockito.when(token.getUserName()).thenReturn("user"); - Mockito.when(response.getWriter()).thenReturn(new PrintWriter( + AuthenticationToken token = mock(AuthenticationToken.class); + when(token.getUserName()).thenReturn("user"); + when(response.getWriter()).thenReturn(new PrintWriter( new StringWriter())); - Assert.assertFalse(handler.managementOperation(token, request, response)); + assertFalse(handler.managementOperation(token, request, response)); - Mockito.when(request.getQueryString()). + when(request.getQueryString()). thenReturn(DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString() + "&" + DelegationTokenAuthenticator.RENEWER_PARAM + "=" + null); - Mockito.reset(response); - Mockito.reset(token); - Mockito.when(token.getUserName()).thenReturn("user"); + reset(response); + reset(token); + when(token.getUserName()).thenReturn("user"); StringWriter writer = new StringWriter(); PrintWriter pwriter = new PrintWriter(writer); - Mockito.when(response.getWriter()).thenReturn(pwriter); - Assert.assertFalse(handler.managementOperation(token, request, response)); - Mockito.verify(token).getUserName(); - Mockito.verify(response).setStatus(HttpServletResponse.SC_OK); - Mockito.verify(response).setContentType(MediaType.APPLICATION_JSON); + when(response.getWriter()).thenReturn(pwriter); + assertFalse(handler.managementOperation(token, request, response)); + verify(token).getUserName(); + verify(response).setStatus(HttpServletResponse.SC_OK); + verify(response).setContentType(MediaType.APPLICATION_JSON); pwriter.close(); String responseOutput = writer.toString(); String tokenLabel = DelegationTokenAuthenticator. DELEGATION_TOKEN_JSON; - Assert.assertTrue(responseOutput.contains(tokenLabel)); - Assert.assertTrue(responseOutput.contains( + assertTrue(responseOutput.contains(tokenLabel)); + assertTrue(responseOutput.contains( DelegationTokenAuthenticator.DELEGATION_TOKEN_URL_STRING_JSON)); ObjectMapper jsonMapper = new ObjectMapper(); Map json = jsonMapper.readValue(responseOutput, Map.class); @@ -453,23 +459,23 @@ public void testCannotGetTokenUsingToken() throws Exception { DelegationTokenAuthenticator.DelegationTokenOperation op = DelegationTokenAuthenticator.DelegationTokenOperation. GETDELEGATIONTOKEN; - HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.when(request.getMethod()).thenReturn(op.getHttpMethod()); - HttpServletResponse response = Mockito.mock(HttpServletResponse.class); - Mockito.when(response.getWriter()).thenReturn(new PrintWriter( + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getMethod()).thenReturn(op.getHttpMethod()); + HttpServletResponse response = mock(HttpServletResponse.class); + when(response.getWriter()).thenReturn(new PrintWriter( new StringWriter())); String tokenStr = getToken(); // Try get a new token using the fetched token, should get 401. - Mockito.when(request.getQueryString()). + when(request.getQueryString()). thenReturn(DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString() + "&" + DelegationTokenAuthenticator.RENEWER_PARAM + "=" + null + "&" + DelegationTokenAuthenticator.DELEGATION_PARAM + "=" + tokenStr); - Mockito.reset(response); + reset(response); StringWriter writer = new StringWriter(); PrintWriter pwriter = new PrintWriter(writer); - Mockito.when(response.getWriter()).thenReturn(pwriter); - Assert.assertFalse(handler.managementOperation(null, request, response)); - Mockito.verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); + when(response.getWriter()).thenReturn(pwriter); + assertFalse(handler.managementOperation(null, request, response)); + verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); } @Test @@ -477,23 +483,23 @@ public void testCannotRenewTokenUsingToken() throws Exception { DelegationTokenAuthenticator.DelegationTokenOperation op = DelegationTokenAuthenticator.DelegationTokenOperation. RENEWDELEGATIONTOKEN; - HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - Mockito.when(request.getMethod()).thenReturn(op.getHttpMethod()); - HttpServletResponse response = Mockito.mock(HttpServletResponse.class); - Mockito.when(response.getWriter()).thenReturn(new PrintWriter( + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getMethod()).thenReturn(op.getHttpMethod()); + HttpServletResponse response = mock(HttpServletResponse.class); + when(response.getWriter()).thenReturn(new PrintWriter( new StringWriter())); String tokenStr = getToken(); // Try renew a token using itself, should get 401. - Mockito.when(request.getQueryString()). + when(request.getQueryString()). thenReturn(DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString() + "&" + DelegationTokenAuthenticator.TOKEN_PARAM + "=" + tokenStr + "&" + DelegationTokenAuthenticator.DELEGATION_PARAM + "=" + tokenStr); - Mockito.reset(response); + reset(response); StringWriter writer = new StringWriter(); PrintWriter pwriter = new PrintWriter(writer); - Mockito.when(response.getWriter()).thenReturn(pwriter); - Assert.assertFalse(handler.managementOperation(null, request, response)); - Mockito.verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); + when(response.getWriter()).thenReturn(pwriter); + assertFalse(handler.managementOperation(null, request, response)); + verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); } @Test @@ -510,14 +516,14 @@ public void testWriterNotClosed() throws Exception { DelegationTokenAuthenticator.DelegationTokenOperation op = GETDELEGATIONTOKEN; - HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - HttpServletResponse response = Mockito.mock(HttpServletResponse.class); - Mockito.when(request.getQueryString()).thenReturn( + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + when(request.getQueryString()).thenReturn( DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString()); - Mockito.when(request.getMethod()).thenReturn(op.getHttpMethod()); + when(request.getMethod()).thenReturn(op.getHttpMethod()); - AuthenticationToken token = Mockito.mock(AuthenticationToken.class); - Mockito.when(token.getUserName()).thenReturn("user"); + AuthenticationToken token = mock(AuthenticationToken.class); + when(token.getUserName()).thenReturn("user"); final MutableBoolean closed = new MutableBoolean(); PrintWriter printWriterCloseCount = new PrintWriter(new StringWriter()) { @Override @@ -535,8 +541,8 @@ public void write(String str) { } }; - Mockito.when(response.getWriter()).thenReturn(printWriterCloseCount); - Assert.assertFalse(noAuthCloseHandler.managementOperation(token, request, + when(response.getWriter()).thenReturn(printWriterCloseCount); + assertFalse(noAuthCloseHandler.managementOperation(token, request, response)); } finally { noAuthCloseHandler.destroy(); diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/ServiceAssert.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/ServiceAssert.java index 9f7250d129888..cb9964a39a1c7 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/ServiceAssert.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/ServiceAssert.java @@ -19,12 +19,12 @@ package org.apache.hadoop.service; import org.apache.hadoop.service.Service; -import org.junit.Assert; +import org.junit.jupiter.api.Assertions; /** * A set of assertions about the state of any service */ -public class ServiceAssert extends Assert { +public class ServiceAssert extends Assertions { public static void assertServiceStateCreated(Service service) { assertServiceInState(service, Service.STATE.NOTINITED); @@ -43,9 +43,9 @@ public static void assertServiceStateStopped(Service service) { } public static void assertServiceInState(Service service, Service.STATE state) { - assertNotNull("Null service", service); - assertEquals("Service in wrong state: " + service, state, - service.getServiceState()); + assertNotNull(service, "Null service"); + assertEquals(state, service.getServiceState(), + "Service in wrong state: " + service); } /** @@ -58,7 +58,7 @@ public static void assertServiceInState(Service service, Service.STATE state) { public static void assertStateCount(BreakableService service, Service.STATE state, int expected) { - assertNotNull("Null service", service); + assertNotNull(service, "Null service"); int actual = service.getCount(state); if (expected != actual) { fail("Expected entry count for state [" + state +"] of " + service @@ -74,7 +74,7 @@ public static void assertStateCount(BreakableService service, */ public static void assertServiceConfigurationContains(Service service, String key) { - assertNotNull("No option "+ key + " in service configuration", - service.getConfig().get(key)); + assertNotNull(service.getConfig().get(key), + "No option "+ key + " in service configuration"); } } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/TestGlobalStateChangeListener.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/TestGlobalStateChangeListener.java index 7bee2d68c6d18..a698d82476e92 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/TestGlobalStateChangeListener.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/TestGlobalStateChangeListener.java @@ -23,8 +23,8 @@ import org.apache.hadoop.service.LoggingStateChangeListener; import org.apache.hadoop.service.Service; import org.apache.hadoop.service.ServiceStateChangeListener; -import org.junit.After; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; /** * Test global state changes. It is critical for all tests to clean up the @@ -58,7 +58,7 @@ private boolean unregister(ServiceStateChangeListener l) { /** * After every test case reset the list of global listeners. */ - @After + @AfterEach public void cleanup() { AbstractService.resetGlobalListeners(); } @@ -70,7 +70,7 @@ public void cleanup() { */ public void assertListenerState(BreakableStateChangeListener breakable, Service.STATE state) { - assertEquals("Wrong state in " + breakable, state, breakable.getLastState()); + assertEquals(state, breakable.getLastState(), "Wrong state in " + breakable); } /** @@ -80,8 +80,8 @@ public void assertListenerState(BreakableStateChangeListener breakable, */ public void assertListenerEventCount(BreakableStateChangeListener breakable, int count) { - assertEquals("Wrong event count in " + breakable, count, - breakable.getEventCount()); + assertEquals(count, breakable.getEventCount(), + "Wrong event count in " + breakable); } /** @@ -90,7 +90,7 @@ public void assertListenerEventCount(BreakableStateChangeListener breakable, @Test public void testRegisterListener() { register(); - assertTrue("listener not registered", unregister()); + assertTrue(unregister(), "listener not registered"); } /** @@ -100,9 +100,9 @@ public void testRegisterListener() { public void testRegisterListenerTwice() { register(); register(); - assertTrue("listener not registered", unregister()); + assertTrue(unregister(), "listener not registered"); //there should be no listener to unregister the second time - assertFalse("listener double registered", unregister()); + assertFalse(unregister(), "listener double registered"); } /** diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/TestServiceLifecycle.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/TestServiceLifecycle.java index f72e130d75011..ccbc0a009fbf5 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/TestServiceLifecycle.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/service/TestServiceLifecycle.java @@ -25,7 +25,7 @@ import org.apache.hadoop.service.Service; import org.apache.hadoop.service.ServiceStateChangeListener; import org.apache.hadoop.service.ServiceStateException; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -204,10 +204,10 @@ public void testStopFailingInitAndStop() throws Throwable { assertEquals(Service.STATE.INITED, svc.getFailureState()); Throwable failureCause = svc.getFailureCause(); - assertNotNull("Null failure cause in " + svc, failureCause); + assertNotNull(failureCause, "Null failure cause in " + svc); BreakableService.BrokenLifecycleEvent cause = (BreakableService.BrokenLifecycleEvent) failureCause; - assertNotNull("null state in " + cause + " raised by " + svc, cause.state); + assertNotNull(cause.state, "null state in " + cause + " raised by " + svc); assertEquals(Service.STATE.INITED, cause.state); } @@ -299,7 +299,7 @@ public synchronized void stateChanged(Service service) { private void assertEventCount(BreakableStateChangeListener listener, int expected) { - assertEquals(listener.toString(), expected, listener.getEventCount()); + assertEquals(expected, listener.getEventCount(), listener.toString()); } @Test @@ -343,7 +343,7 @@ public void testListenerWithNotifications() throws Throwable { long duration = System.currentTimeMillis() - start; assertEquals(Service.STATE.STOPPED, listener.notifyingState); assertServiceInState(service, Service.STATE.STOPPED); - assertTrue("Duration of " + duration + " too long", duration < 10000); + assertTrue(duration < 10000, "Duration of " + duration + " too long"); } @Test diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java index e54971e491c32..e4271f0407f52 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java @@ -64,8 +64,6 @@ import org.apache.log4j.Logger; import org.apache.log4j.PatternLayout; import org.apache.log4j.WriterAppender; -import org.junit.Assert; -import org.junit.Assume; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.slf4j.LoggerFactory; @@ -75,6 +73,12 @@ import static org.apache.hadoop.fs.contract.ContractTestUtils.createFile; import static org.apache.hadoop.util.functional.CommonCallableSupplier.submit; import static org.apache.hadoop.util.functional.CommonCallableSupplier.waitForCompletion; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assumptions.assumeTrue; /** * Test provides some very generic helpers which might be used across the tests @@ -274,7 +278,7 @@ public static String getRandomizedTempPath() { * Assert that a given file exists. */ public static void assertExists(File f) { - Assert.assertTrue("File " + f + " should exist", f.exists()); + assertTrue(f.exists(), "File " + f + " should exist"); } /** @@ -293,9 +297,9 @@ public static void assertGlobEquals(File dir, String pattern, } Set expectedSet = new TreeSet<>( Arrays.asList(expectedMatches)); - Assert.assertEquals("Bad files matching " + pattern + " in " + dir, - Joiner.on(",").join(expectedSet), - Joiner.on(",").join(found)); + assertEquals(Joiner.on(",").join(expectedSet), + Joiner.on(",").join(found), + "Bad files matching " + pattern + " in " + dir); } static final String E_NULL_THROWABLE = "Null Throwable"; @@ -325,7 +329,7 @@ public static void assertExceptionContains(String expectedText, Throwable t) { public static void assertExceptionContains(String expectedText, Throwable t, String message) { - Assert.assertNotNull(E_NULL_THROWABLE, t); + assertNotNull(t, E_NULL_THROWABLE); String msg = t.toString(); if (msg == null) { throw new AssertionError(E_NULL_THROWABLE_STRING, t); @@ -692,15 +696,15 @@ public Object answer(InvocationOnMock invocation) throws Throwable { } public static void assertDoesNotMatch(String output, String pattern) { - Assert.assertFalse("Expected output to match /" + pattern + "/" + - " but got:\n" + output, - Pattern.compile(pattern).matcher(output).find()); + assertFalse(Pattern.compile(pattern).matcher(output).find(), + "Expected output to match /" + pattern + "/" + + " but got:\n" + output); } public static void assertMatches(String output, String pattern) { - Assert.assertTrue("Expected output to match /" + pattern + "/" + - " but got:\n" + output, - Pattern.compile(pattern).matcher(output).find()); + assertTrue(Pattern.compile(pattern).matcher(output).find(), + "Expected output to match /" + pattern + "/" + + " but got:\n" + output); } public static void assertValueNear(long expected, long actual, long allowedError) { @@ -709,8 +713,9 @@ public static void assertValueNear(long expected, long actual, long allowedError public static void assertValueWithinRange(long expectedMin, long expectedMax, long actual) { - Assert.assertTrue("Expected " + actual + " to be in range (" + expectedMin + "," - + expectedMax + ")", expectedMin <= actual && actual <= expectedMax); + assertTrue(expectedMin <= actual && actual <= expectedMax, + "Expected " + actual + " to be in range (" + expectedMin + "," + + expectedMax + ")"); } /** @@ -741,7 +746,7 @@ public static boolean anyThreadMatching(Pattern pattern) { public static void assertNoThreadsMatching(String regex) { Pattern pattern = Pattern.compile(regex); if (anyThreadMatching(pattern)) { - Assert.fail("Leaked thread matches " + regex); + fail("Leaked thread matches " + regex); } } @@ -774,8 +779,8 @@ public static void waitForThreadTermination(String regex, * in the definition of native profile in pom.xml. */ public static void assumeInNativeProfile() { - Assume.assumeTrue( - Boolean.parseBoolean(System.getProperty("runningWithNative", "false"))); + assumeTrue(Boolean.parseBoolean( + System.getProperty("runningWithNative", "false"))); } /** diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/LambdaTestUtils.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/LambdaTestUtils.java index 0c55871cfd7e9..e0f3c87518993 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/LambdaTestUtils.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/LambdaTestUtils.java @@ -18,7 +18,11 @@ package org.apache.hadoop.test; -import org.junit.Assert; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -699,9 +703,9 @@ private static String robustToString(Object o) { public static void assertOptionalEquals(String message, T expected, Optional actual) { - Assert.assertNotNull(message, actual); - Assert.assertTrue(message +" -not present", actual.isPresent()); - Assert.assertEquals(message, expected, actual.get()); + assertNotNull(actual, message); + assertTrue(actual.isPresent(), message +" -not present"); + assertEquals(expected, actual.get(), message); } /** @@ -713,9 +717,9 @@ public static void assertOptionalEquals(String message, */ public static void assertOptionalUnset(String message, Optional actual) { - Assert.assertNotNull(message, actual); + assertNotNull(actual, message); actual.ifPresent( - t -> Assert.fail("Expected empty option, got " + t.toString())); + t -> fail("Expected empty option, got " + t.toString())); } /** @@ -766,7 +770,7 @@ public static void eval(VoidCallable closure) { public static T notNull(String message, Callable eval) throws Exception { T t = eval.call(); - Assert.assertNotNull(message, t); + assertNotNull(t, message); return t; } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MetricsAsserts.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MetricsAsserts.java index 60f752d160327..5ce280cd76ff5 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MetricsAsserts.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MetricsAsserts.java @@ -20,10 +20,15 @@ import static org.apache.hadoop.util.Preconditions.*; -import org.junit.Assert; - import static org.mockito.AdditionalMatchers.geq; -import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.anyString; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.argThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import org.mockito.stubbing.Answer; import org.mockito.invocation.InvocationOnMock; @@ -150,8 +155,8 @@ public static MetricsInfo anyInfo() { */ public static void assertGauge(String name, int expected, MetricsRecordBuilder rb) { - Assert.assertEquals("Bad value for metric " + name, - expected, getIntGauge(name, rb)); + assertEquals(expected, getIntGauge(name, rb), + "Bad value for metric " + name); } public static int getIntGauge(String name, MetricsRecordBuilder rb) { @@ -169,8 +174,8 @@ public static int getIntGauge(String name, MetricsRecordBuilder rb) { */ public static void assertCounter(String name, int expected, MetricsRecordBuilder rb) { - Assert.assertEquals("Bad value for metric " + name, - expected, getIntCounter(name, rb)); + assertEquals(expected, getIntCounter(name, rb), + "Bad value for metric " + name); } public static int getIntCounter(String name, MetricsRecordBuilder rb) { @@ -189,8 +194,8 @@ public static int getIntCounter(String name, MetricsRecordBuilder rb) { */ public static void assertGauge(String name, long expected, MetricsRecordBuilder rb) { - Assert.assertEquals("Bad value for metric " + name, - expected, getLongGauge(name, rb)); + assertEquals(expected, getLongGauge(name, rb), + "Bad value for metric " + name); } public static long getLongGauge(String name, MetricsRecordBuilder rb) { @@ -208,8 +213,8 @@ public static long getLongGauge(String name, MetricsRecordBuilder rb) { */ public static void assertGauge(String name, double expected, MetricsRecordBuilder rb) { - Assert.assertEquals("Bad value for metric " + name, - expected, getDoubleGauge(name, rb), EPSILON); + assertEquals(expected, getDoubleGauge(name, rb), EPSILON, + "Bad value for metric " + name); } public static double getDoubleGauge(String name, MetricsRecordBuilder rb) { @@ -227,8 +232,8 @@ public static double getDoubleGauge(String name, MetricsRecordBuilder rb) { */ public static void assertCounter(String name, long expected, MetricsRecordBuilder rb) { - Assert.assertEquals("Bad value for metric " + name, - expected, getLongCounter(name, rb)); + assertEquals(expected, getLongCounter(name, rb), + "Bad value for metric " + name); } public static long getLongCounter(String name, MetricsRecordBuilder rb) { @@ -260,8 +265,8 @@ public static String getStringMetric(String name, MetricsRecordBuilder rb) { */ public static void assertGauge(String name, float expected, MetricsRecordBuilder rb) { - Assert.assertEquals("Bad value for metric " + name, - expected, getFloatGauge(name, rb), EPSILON); + assertEquals(expected, getFloatGauge(name, rb), EPSILON, + "Bad value for metric " + name); } public static float getFloatGauge(String name, MetricsRecordBuilder rb) { @@ -275,8 +280,8 @@ public static float getFloatGauge(String name, MetricsRecordBuilder rb) { * Check that this metric was captured exactly once. */ private static void checkCaptured(ArgumentCaptor captor, String name) { - Assert.assertEquals("Expected exactly one metric for name " + name, - 1, captor.getAllValues().size()); + assertEquals(1, captor.getAllValues().size(), + "Expected exactly one metric for name " + name); } /** @@ -331,8 +336,8 @@ public static void assertCounter(String name, long expected, */ public static void assertCounterGt(String name, long greater, MetricsRecordBuilder rb) { - Assert.assertTrue("Bad value for metric " + name, - getLongCounter(name, rb) > greater); + assertTrue(getLongCounter(name, rb) > greater, + "Bad value for metric " + name); } /** @@ -354,8 +359,8 @@ public static void assertCounterGt(String name, long greater, */ public static void assertGaugeGt(String name, double greater, MetricsRecordBuilder rb) { - Assert.assertTrue("Bad value for metric " + name, - getDoubleGauge(name, rb) > greater); + assertTrue(getDoubleGauge(name, rb) > greater, + "Bad value for metric " + name); } /** @@ -367,8 +372,8 @@ public static void assertGaugeGt(String name, double greater, public static void assertGaugeGte(String name, double greater, MetricsRecordBuilder rb) { double curValue = getDoubleGauge(name, rb); - Assert.assertTrue("Bad value for metric " + name, - curValue >= greater); + assertTrue(curValue >= greater, + "Bad value for metric " + name); } /** @@ -444,7 +449,7 @@ public static void assertInverseQuantileGauges(String prefix, */ public static void assertTag(String name, String expected, MetricsRecordBuilder rb) { - Assert.assertEquals("Bad Tag for metric " + name, + assertEquals("Bad Tag for metric " + name, expected, getStringTag(name, rb)); } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MoreAsserts.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MoreAsserts.java index f6e6055d78e2c..3aa4b2924914d 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MoreAsserts.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/MoreAsserts.java @@ -21,8 +21,8 @@ import java.util.Iterator; import java.util.concurrent.CompletableFuture; -import org.assertj.core.api.Assertions; -import org.junit.Assert; +import org.junit.jupiter.api.Assertions; +import static org.assertj.core.api.Assertions.assertThat; /** * A few more asserts @@ -42,10 +42,10 @@ public static void assertEquals(String s, T[] expected, Iterator it = actual.iterator(); int i = 0; for (; i < expected.length && it.hasNext(); ++i) { - Assert.assertEquals("Element " + i + " for " + s, expected[i], it.next()); + Assertions.assertEquals(expected[i], it.next(), "Element " + i + " for " + s); } - Assert.assertTrue("Expected more elements", i == expected.length); - Assert.assertTrue("Expected less elements", !it.hasNext()); + Assertions.assertTrue(i == expected.length, "Expected more elements"); + Assertions.assertTrue(!it.hasNext(), "Expected less elements"); } /** @@ -62,26 +62,26 @@ public static void assertEquals(String s, Iterable expected, Iterator ita = actual.iterator(); int i = 0; while (ite.hasNext() && ita.hasNext()) { - Assert.assertEquals("Element " + i + " for " + s, ite.next(), ita.next()); + Assertions.assertEquals(ite.next(), ita.next(), "Element " + i + " for " + s); } - Assert.assertTrue("Expected more elements", !ite.hasNext()); - Assert.assertTrue("Expected less elements", !ita.hasNext()); + Assertions.assertTrue(!ite.hasNext(), "Expected more elements"); + Assertions.assertTrue(!ita.hasNext(), "Expected less elements"); } public static void assertFutureCompletedSuccessfully(CompletableFuture future) { - Assertions.assertThat(future.isDone()) + assertThat(future.isDone()) .describedAs("This future is supposed to be " + "completed successfully") .isTrue(); - Assertions.assertThat(future.isCompletedExceptionally()) + assertThat(future.isCompletedExceptionally()) .describedAs("This future is supposed to be " + "completed successfully") .isFalse(); } public static void assertFutureFailedExceptionally(CompletableFuture future) { - Assertions.assertThat(future.isCompletedExceptionally()) + assertThat(future.isCompletedExceptionally()) .describedAs("This future is supposed to be " + "completed exceptionally") .isTrue(); @@ -94,7 +94,7 @@ public static void assertFutureFailedExceptionally(CompletableFuture futu * @param message error message to print in case of mismatch. */ public static void assertEqual(T actual, T expected, String message) { - Assertions.assertThat(actual) + assertThat(actual) .describedAs("Mismatch in %s", message) .isEqualTo(expected); } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/tools/GetGroupsTestBase.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/tools/GetGroupsTestBase.java index a31700778dc42..b6244efe74d4f 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/tools/GetGroupsTestBase.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/tools/GetGroupsTestBase.java @@ -17,7 +17,7 @@ */ package org.apache.hadoop.tools; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -27,8 +27,8 @@ import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; public abstract class GetGroupsTestBase { @@ -38,7 +38,7 @@ public abstract class GetGroupsTestBase { protected abstract Tool getTool(PrintStream o); - @Before + @BeforeEach public void setUpUsers() throws IOException { // Make sure the current user's info is in the list of test users. UserGroupInformation currentUser = UserGroupInformation.getCurrentUser(); @@ -52,42 +52,40 @@ public void setUpUsers() throws IOException { public void testNoUserGiven() throws Exception { String actualOutput = runTool(conf, new String[0], true); UserGroupInformation currentUser = UserGroupInformation.getCurrentUser(); - assertEquals("No user provided should default to current user", - getExpectedOutput(currentUser), actualOutput); + assertEquals(getExpectedOutput(currentUser), actualOutput, + "No user provided should default to current user"); } @Test public void testExistingUser() throws Exception { String actualOutput = runTool(conf, new String[]{testUser1.getUserName()}, true); - assertEquals("Show only the output of the user given", - getExpectedOutput(testUser1), actualOutput); + assertEquals(getExpectedOutput(testUser1), actualOutput, + "Show only the output of the user given"); } @Test public void testMultipleExistingUsers() throws Exception { String actualOutput = runTool(conf, new String[]{testUser1.getUserName(), testUser2.getUserName()}, true); - assertEquals("Show the output for both users given", - getExpectedOutput(testUser1) + getExpectedOutput(testUser2), actualOutput); + assertEquals(getExpectedOutput(testUser1) + getExpectedOutput(testUser2), actualOutput, + "Show the output for both users given"); } @Test public void testNonExistentUser() throws Exception { String actualOutput = runTool(conf, new String[]{"does-not-exist"}, true); - assertEquals("Show the output for only the user given, with no groups", - getExpectedOutput(UserGroupInformation.createRemoteUser("does-not-exist")), - actualOutput); + assertEquals(getExpectedOutput(UserGroupInformation.createRemoteUser("does-not-exist")), + actualOutput, "Show the output for only the user given, with no groups"); } @Test public void testMultipleNonExistingUsers() throws Exception { String actualOutput = runTool(conf, new String[]{"does-not-exist1", "does-not-exist2"}, true); - assertEquals("Show the output for only the user given, with no groups", - getExpectedOutput(UserGroupInformation.createRemoteUser("does-not-exist1")) + + assertEquals(getExpectedOutput(UserGroupInformation.createRemoteUser("does-not-exist1")) + getExpectedOutput(UserGroupInformation.createRemoteUser("does-not-exist2")), - actualOutput); + actualOutput, "Show the output for only the user given, with no groups"); } @Test @@ -95,12 +93,12 @@ public void testExistingInterleavedWithNonExistentUsers() throws Exception { String actualOutput = runTool(conf, new String[]{"does-not-exist1", testUser1.getUserName(), "does-not-exist2", testUser2.getUserName()}, true); - assertEquals("Show the output for only the user given, with no groups", - getExpectedOutput(UserGroupInformation.createRemoteUser("does-not-exist1")) + + assertEquals(getExpectedOutput(UserGroupInformation.createRemoteUser("does-not-exist1")) + getExpectedOutput(testUser1) + getExpectedOutput(UserGroupInformation.createRemoteUser("does-not-exist2")) + getExpectedOutput(testUser2), - actualOutput); + actualOutput, + "Show the output for only the user given, with no groups"); } private static String getExpectedOutput(UserGroupInformation user) { diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/contract/hdfs/HDFSContract.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/contract/hdfs/HDFSContract.java index 74b9a35adfcdb..4ff588a0bad06 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/contract/hdfs/HDFSContract.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/fs/contract/hdfs/HDFSContract.java @@ -27,7 +27,7 @@ import org.apache.hadoop.hdfs.DFSConfigKeys; import org.apache.hadoop.hdfs.HdfsConfiguration; import org.apache.hadoop.hdfs.MiniDFSCluster; -import org.junit.Assert; +import org.junit.jupiter.api.Assertions; import java.io.IOException; @@ -73,14 +73,14 @@ public static MiniDFSCluster getCluster() { @Override public void init() throws IOException { super.init(); - Assert.assertTrue("contract options not loaded", - isSupported(ContractOptions.IS_CASE_SENSITIVE, false)); + Assertions.assertTrue(isSupported(ContractOptions.IS_CASE_SENSITIVE, false), + "contract options not loaded"); } @Override public FileSystem getTestFileSystem() throws IOException { //assumes cluster is not null - Assert.assertNotNull("cluster not created", cluster); + Assertions.assertNotNull(cluster, "cluster not created"); return cluster.getFileSystem(); } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestGetGroupsWithHA.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestGetGroupsWithHA.java index c3c22759d967a..d2f6ef75f5454 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestGetGroupsWithHA.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestGetGroupsWithHA.java @@ -26,14 +26,14 @@ import org.apache.hadoop.hdfs.tools.GetGroups; import org.apache.hadoop.tools.GetGroupsTestBase; import org.apache.hadoop.util.Tool; -import org.junit.After; -import org.junit.Before; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; public class TestGetGroupsWithHA extends GetGroupsTestBase { private MiniDFSCluster cluster; - @Before + @BeforeEach public void setUpNameNode() throws IOException { conf = new HdfsConfiguration(); cluster = new MiniDFSCluster.Builder(conf) @@ -42,7 +42,7 @@ public void setUpNameNode() throws IOException { HATestUtil.setFailoverConfigurations(cluster, conf); } - @After + @AfterEach public void tearDownNameNode() { if (cluster != null) { cluster.shutdown(); diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestGetGroups.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestGetGroups.java index 3d9631dfe1222..3430d924a5e9d 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestGetGroups.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/TestGetGroups.java @@ -24,8 +24,8 @@ import org.apache.hadoop.hdfs.MiniDFSCluster; import org.apache.hadoop.tools.GetGroupsTestBase; import org.apache.hadoop.util.Tool; -import org.junit.After; -import org.junit.Before; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; /** * Tests for the HDFS implementation of {@link GetGroups} @@ -34,13 +34,13 @@ public class TestGetGroups extends GetGroupsTestBase { private MiniDFSCluster cluster; - @Before + @BeforeEach public void setUpNameNode() throws IOException { conf = new HdfsConfiguration(); cluster = new MiniDFSCluster.Builder(conf).numDataNodes(0).build(); } - @After + @AfterEach public void tearDownNameNode() { if (cluster != null) { cluster.shutdown(); diff --git a/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/ITestS3ATemporaryCredentials.java b/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/ITestS3ATemporaryCredentials.java index f8455efc0a03a..50af71e41964c 100644 --- a/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/ITestS3ATemporaryCredentials.java +++ b/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/ITestS3ATemporaryCredentials.java @@ -30,7 +30,6 @@ import software.amazon.awssdk.services.sts.StsClient; import software.amazon.awssdk.services.sts.StsClientBuilder; import software.amazon.awssdk.services.sts.model.Credentials; -import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,7 +55,7 @@ import static org.apache.hadoop.fs.s3a.auth.delegation.DelegationConstants.*; import static org.apache.hadoop.fs.s3a.auth.delegation.SessionTokenBinding.CREDENTIALS_CONVERTED_TO_DELEGATION_TOKEN; import static org.apache.hadoop.test.LambdaTestUtils.intercept; -import static org.hamcrest.Matchers.containsString; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests use of temporary credentials (for example, AWS STS & S3). @@ -205,9 +204,9 @@ public void testSessionTokenPropagation() throws Exception { = (SessionTokenIdentifier) fs.getDelegationToken("") .decodeIdentifier(); String ids = identifier.toString(); - assertThat("origin in " + ids, - identifier.getOrigin(), - containsString(CREDENTIALS_CONVERTED_TO_DELEGATION_TOKEN)); + assertThat(identifier.getOrigin()). + contains(CREDENTIALS_CONVERTED_TO_DELEGATION_TOKEN). + as("origin in " + ids); // and validate the AWS bits to make sure everything has come across. assertCredentialsEqual("Reissued credentials in " + ids, @@ -236,13 +235,10 @@ public void testSessionTokenExpiry() throws Exception { Duration actualDuration = Duration.between(localTimestamp, expirationTimestamp); Duration offset = actualDuration.minus(TEST_SESSION_TOKEN_DURATION); - - assertThat( - "Duration of session " + actualDuration - + " out of expected range of with " + offset - + " this host's clock may be wrong.", - offset.getSeconds(), - Matchers.lessThanOrEqualTo(permittedExpiryOffset)); + assertThat(offset.getSeconds()).isLessThanOrEqualTo(permittedExpiryOffset). + as( "Duration of session " + actualDuration + + " out of expected range of with " + offset + + " this host's clock may be wrong."); } protected void updateConfigWithSessionCreds(final Configuration conf,