Skip to content

Commit 69da555

Browse files
Add Verify#exists and Verify#noteExists
1 parent 5792612 commit 69da555

File tree

1 file changed

+73
-47
lines changed
  • hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs

1 file changed

+73
-47
lines changed

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java

Lines changed: 73 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public void testListAPI() throws IOException {
266266
public void testFullyDelete() throws IOException {
267267
boolean ret = FileUtil.fullyDelete(del);
268268
Assert.assertTrue(ret);
269-
Assert.assertFalse(del.exists());
269+
Verify.notExists(del);
270270
validateTmpDir();
271271
}
272272

@@ -284,7 +284,7 @@ public void testFullyDeleteSymlinks() throws IOException {
284284
// delete contents of tmp. See setupDirs for details.
285285
boolean ret = FileUtil.fullyDelete(link);
286286
Assert.assertTrue(ret);
287-
Assert.assertFalse(link.exists());
287+
Verify.notExists(link);
288288
assertDelListLength(4);
289289
validateTmpDir();
290290

@@ -293,7 +293,7 @@ public void testFullyDeleteSymlinks() throws IOException {
293293
// delete contents of tmp. See setupDirs for details.
294294
ret = FileUtil.fullyDelete(linkDir);
295295
Assert.assertTrue(ret);
296-
Assert.assertFalse(linkDir.exists());
296+
Verify.notExists(linkDir);
297297
assertDelListLength(3);
298298
validateTmpDir();
299299
}
@@ -310,7 +310,7 @@ public void testFullyDeleteDanglingSymlinks() throws IOException {
310310
// to make y as a dangling link to file tmp/x
311311
boolean ret = FileUtil.fullyDelete(tmp);
312312
Assert.assertTrue(ret);
313-
Assert.assertFalse(tmp.exists());
313+
Verify.notExists(tmp);
314314

315315
// dangling symlink to file
316316
File link = new File(del, LINK);
@@ -334,15 +334,15 @@ public void testFullyDeleteDanglingSymlinks() throws IOException {
334334
public void testFullyDeleteContents() throws IOException {
335335
boolean ret = FileUtil.fullyDeleteContents(del);
336336
Assert.assertTrue(ret);
337-
Assert.assertTrue(del.exists());
337+
Verify.exists(del);
338338
Assert.assertEquals(0, Objects.requireNonNull(del.listFiles()).length);
339339
validateTmpDir();
340340
}
341341

342342
private void validateTmpDir() {
343-
Assert.assertTrue(tmp.exists());
343+
Verify.exists(tmp);
344344
Assert.assertEquals(1, Objects.requireNonNull(tmp.listFiles()).length);
345-
Assert.assertTrue(new File(tmp, FILE).exists());
345+
Verify.exists(new File(tmp, FILE));
346346
}
347347

348348
/**
@@ -385,7 +385,8 @@ private void setupDirsAndNonWritablePermissions() throws IOException {
385385
Verify.mkdirs(ySubDir);
386386
Verify.createNewFile(file3);
387387

388-
File tmpFile = Verify.createNewFile(new File(tmp, FILE));
388+
File tmpFile = new File(tmp, FILE);
389+
tmpFile.createNewFile();
389390
FileUtil.symLink(tmpFile.toString(), zlink.toString());
390391
}
391392

@@ -461,7 +462,7 @@ public void testFailFullyDeleteDirSymlinks() throws IOException {
461462
boolean ret = FileUtil.fullyDelete(linkDir);
462463
// fail symlink deletion
463464
Assert.assertFalse(ret);
464-
Assert.assertTrue(linkDir.exists());
465+
Verify.exists(linkDir);
465466
assertDelListLength(5);
466467
// tmp dir should exist
467468
validateTmpDir();
@@ -470,7 +471,7 @@ public void testFailFullyDeleteDirSymlinks() throws IOException {
470471
ret = FileUtil.fullyDelete(linkDir);
471472
// success symlink deletion
472473
Assert.assertTrue(ret);
473-
Assert.assertFalse(linkDir.exists());
474+
Verify.notExists(linkDir);
474475
assertDelListLength(4);
475476
// tmp dir should exist
476477
validateTmpDir();
@@ -533,6 +534,29 @@ public static File delete(File file) {
533534
assertTrue("Unable to delete " + file, file.delete());
534535
return file;
535536
}
537+
538+
/**
539+
* Invokes {@link File#exists()} on the given {@link File} instance.
540+
*
541+
* @param file The file to call {@link File#exists()} on.
542+
* @return The result of {@link File#exists()}.
543+
*/
544+
public static File exists(File file) {
545+
assertTrue("Expected file " + file + " doesn't exist", file.exists());
546+
return file;
547+
}
548+
549+
/**
550+
* Invokes {@link File#exists()} on the given {@link File} instance to check if the
551+
* {@link File} doesn't exists.
552+
*
553+
* @param file The file to call {@link File#exists()} on.
554+
* @return The negation of the result of {@link File#exists()}.
555+
*/
556+
public static File notExists(File file) {
557+
assertFalse("Expected file " + file + " must not exist", file.exists());
558+
return file;
559+
}
536560
}
537561

538562
/**
@@ -687,7 +711,7 @@ public void testUnTar() throws Exception {
687711
// successfully untar it into an existing dir:
688712
FileUtil.unTar(simpleTar, tmp);
689713
// check result:
690-
assertTrue(new File(tmp, "/bar/foo").exists());
714+
Verify.exists(new File(tmp, "/bar/foo"));
691715
assertEquals(12, new File(tmp, "/bar/foo").length());
692716

693717
final File regularFile =
@@ -700,21 +724,21 @@ public void testReplaceFile() throws IOException {
700724
// src exists, and target does not exist:
701725
final File srcFile = Verify.createNewFile(new File(tmp, "src"));
702726
final File targetFile = new File(tmp, "target");
703-
assertTrue(!targetFile.exists());
727+
Verify.notExists(targetFile);
704728
FileUtil.replaceFile(srcFile, targetFile);
705-
assertTrue(!srcFile.exists());
706-
assertTrue(targetFile.exists());
729+
Verify.notExists(srcFile);
730+
Verify.exists(targetFile);
707731

708732
// src exists and target is a regular file:
709733
Verify.createNewFile(srcFile);
710-
assertTrue(srcFile.exists());
734+
Verify.exists(srcFile);
711735
FileUtil.replaceFile(srcFile, targetFile);
712-
assertTrue(!srcFile.exists());
713-
assertTrue(targetFile.exists());
736+
Verify.notExists(srcFile);
737+
Verify.exists(targetFile);
714738

715739
// src exists, and target is a non-empty directory:
716740
Verify.createNewFile(srcFile);
717-
assertTrue(srcFile.exists());
741+
Verify.exists(srcFile);
718742
Verify.delete(targetFile);
719743
Verify.mkdirs(targetFile);
720744
File obstacle = Verify.createNewFile(new File(targetFile, "obstacle"));
@@ -726,9 +750,9 @@ public void testReplaceFile() throws IOException {
726750
// okay
727751
}
728752
// check up the post-condition: nothing is deleted:
729-
assertTrue(srcFile.exists());
753+
Verify.exists(srcFile);
730754
assertTrue(targetFile.exists() && targetFile.isDirectory());
731-
assertTrue(obstacle.exists());
755+
Verify.exists(obstacle);
732756
}
733757

734758
@Test (timeout = 30000)
@@ -768,7 +792,7 @@ public void testUnZip() throws Exception {
768792
// successfully unzip it into an existing dir:
769793
FileUtil.unZip(simpleZip, tmp);
770794
// check result:
771-
assertTrue(new File(tmp, "foo").exists());
795+
Verify.exists(new File(tmp, "foo"));
772796
assertEquals(12, new File(tmp, "foo").length());
773797

774798
final File regularFile =
@@ -819,24 +843,24 @@ public void testCopy5() throws IOException {
819843
final File dest = new File(del, "dest");
820844
boolean result = FileUtil.copy(fs, srcPath, dest, false, conf);
821845
assertTrue(result);
822-
assertTrue(dest.exists());
846+
Verify.exists(dest);
823847
assertEquals(content.getBytes().length
824848
+ System.getProperty("line.separator").getBytes().length, dest.length());
825-
assertTrue(srcFile.exists()); // should not be deleted
849+
Verify.exists(srcFile); // should not be deleted
826850

827851
// copy regular file, delete src:
828852
Verify.delete(dest);
829-
assertTrue(!dest.exists());
853+
Verify.notExists(dest);
830854
result = FileUtil.copy(fs, srcPath, dest, true, conf);
831855
assertTrue(result);
832-
assertTrue(dest.exists());
856+
Verify.exists(dest);
833857
assertEquals(content.getBytes().length
834858
+ System.getProperty("line.separator").getBytes().length, dest.length());
835-
assertTrue(!srcFile.exists()); // should be deleted
859+
Verify.notExists(srcFile); // should be deleted
836860

837861
// copy a dir:
838862
Verify.delete(dest);
839-
assertTrue(!dest.exists());
863+
Verify.notExists(dest);
840864
srcPath = new Path(partitioned.toURI());
841865
result = FileUtil.copy(fs, srcPath, dest, true, conf);
842866
assertTrue(result);
@@ -848,7 +872,7 @@ public void testCopy5() throws IOException {
848872
assertEquals(3
849873
+ System.getProperty("line.separator").getBytes().length, f.length());
850874
}
851-
assertTrue(!partitioned.exists()); // should be deleted
875+
Verify.notExists(partitioned); // should be deleted
852876
}
853877

854878
@Test (timeout = 30000)
@@ -929,14 +953,15 @@ public void testSymlink() throws Exception {
929953
*/
930954
@Test (timeout = 30000)
931955
public void testSymlinkRenameTo() throws Exception {
932-
File file = Verify.createNewFile(new File(del, FILE));
956+
File file = new File(del, FILE);
957+
file.createNewFile();
933958
File link = new File(del, "_link");
934959

935960
// create the symlink
936961
FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath());
937962

938-
Assert.assertTrue(file.exists());
939-
Assert.assertTrue(link.exists());
963+
Verify.exists(file);
964+
Verify.exists(link);
940965

941966
File link2 = new File(del, "_link2");
942967

@@ -946,30 +971,31 @@ public void testSymlinkRenameTo() throws Exception {
946971
// Make sure the file still exists
947972
// (NOTE: this would fail on Java6 on Windows if we didn't
948973
// copy the file in FileUtil#symlink)
949-
Assert.assertTrue(file.exists());
974+
Verify.exists(file);
950975

951-
Assert.assertTrue(link2.exists());
952-
Assert.assertFalse(link.exists());
976+
Verify.exists(link2);
977+
Verify.notExists(link);
953978
}
954979

955980
/**
956981
* Test that deletion of a symlink works as expected.
957982
*/
958983
@Test (timeout = 30000)
959984
public void testSymlinkDelete() throws Exception {
960-
File file = Verify.createNewFile(new File(del, FILE));
985+
File file = new File(del, FILE);
986+
file.createNewFile();
961987
File link = new File(del, "_link");
962988

963989
// create the symlink
964990
FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath());
965991

966-
Assert.assertTrue(file.exists());
967-
Assert.assertTrue(link.exists());
992+
Verify.exists(file);
993+
Verify.exists(link);
968994

969995
// make sure that deleting a symlink works properly
970-
Assert.assertTrue(link.delete());
971-
Assert.assertFalse(link.exists());
972-
Assert.assertTrue(file.exists());
996+
Verify.delete(link);
997+
Verify.notExists(link);
998+
Verify.exists(file);
973999
}
9741000

9751001
/**
@@ -997,12 +1023,12 @@ public void testSymlinkLength() throws Exception {
9971023
Assert.assertEquals(data.length, link.length());
9981024

9991025
Verify.delete(file);
1000-
Assert.assertFalse(file.exists());
1026+
Verify.notExists(file);
10011027

10021028
Assert.assertEquals(0, link.length());
10031029

10041030
Verify.delete(link);
1005-
Assert.assertFalse(link.exists());
1031+
Verify.notExists(link);
10061032
}
10071033

10081034
/**
@@ -1141,21 +1167,21 @@ private void doUntarAndVerify(File tarFile, File untarDir)
11411167

11421168
String parentDir = untarDir.getCanonicalPath() + Path.SEPARATOR + "name";
11431169
File testFile = new File(parentDir + Path.SEPARATOR + "version");
1144-
Assert.assertTrue(testFile.exists());
1170+
Verify.exists(testFile);
11451171
Assert.assertTrue(testFile.length() == 0);
11461172
String imageDir = parentDir + Path.SEPARATOR + "image";
11471173
testFile = new File(imageDir + Path.SEPARATOR + "fsimage");
1148-
Assert.assertTrue(testFile.exists());
1174+
Verify.exists(testFile);
11491175
Assert.assertTrue(testFile.length() == 157);
11501176
String currentDir = parentDir + Path.SEPARATOR + "current";
11511177
testFile = new File(currentDir + Path.SEPARATOR + "fsimage");
1152-
Assert.assertTrue(testFile.exists());
1178+
Verify.exists(testFile);
11531179
Assert.assertTrue(testFile.length() == 4331);
11541180
testFile = new File(currentDir + Path.SEPARATOR + "edits");
1155-
Assert.assertTrue(testFile.exists());
1181+
Verify.exists(testFile);
11561182
Assert.assertTrue(testFile.length() == 1033);
11571183
testFile = new File(currentDir + Path.SEPARATOR + "fstime");
1158-
Assert.assertTrue(testFile.exists());
1184+
Verify.exists(testFile);
11591185
Assert.assertTrue(testFile.length() == 8);
11601186
}
11611187

0 commit comments

Comments
 (0)