You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by ay...@apache.org on 2020/01/25 15:12:35 UTC
[hadoop] branch trunk updated: HADOOP-16811: Use JUnit
TemporaryFolder Rule in TestFileUtils (#1811). Contributed by David
Mollitor.
This is an automated email from the ASF dual-hosted git repository.
ayushsaxena pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/hadoop.git
The following commit(s) were added to refs/heads/trunk by this push:
new 1afd54f HADOOP-16811: Use JUnit TemporaryFolder Rule in TestFileUtils (#1811). Contributed by David Mollitor.
1afd54f is described below
commit 1afd54fbbb858a58112e6290b3063216eea82206
Author: belugabehr <12...@users.noreply.github.com>
AuthorDate: Sat Jan 25 10:12:21 2020 -0500
HADOOP-16811: Use JUnit TemporaryFolder Rule in TestFileUtils (#1811). Contributed by David Mollitor.
---
.../java/org/apache/hadoop/fs/TestFileUtil.java | 221 ++++++---------------
1 file changed, 65 insertions(+), 156 deletions(-)
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 eb9098e..1ca1f24 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
@@ -66,22 +66,38 @@ 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.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestFileUtil {
private static final Logger LOG = LoggerFactory.getLogger(TestFileUtil.class);
- private static final File TEST_DIR = GenericTestUtils.getTestDir("fu");
+ @Rule
+ public TemporaryFolder testFolder = new TemporaryFolder();
+
private static final String FILE = "x";
private static final String LINK = "y";
private static final String DIR = "dir";
- private final File del = new File(TEST_DIR, "del");
- private final File tmp = new File(TEST_DIR, "tmp");
- private final File dir1 = new File(del, DIR + "1");
- private final File dir2 = new File(del, DIR + "2");
- private final File partitioned = new File(TEST_DIR, "partitioned");
+
+ private static final String FILE_1_NAME = "file1";
+
+ private File del;
+ private File tmp;
+ private File dir1;
+ private File dir2;
+ private File partitioned;
+
+ private File xSubDir;
+ private File xSubSubDir;
+ private File ySubDir;
+
+ private File file2;
+ private File file22;
+ private File file3;
+ private File zlink;
private InetAddress inet1;
private InetAddress inet2;
@@ -118,21 +134,34 @@ public class TestFileUtil {
* file: part-r-00000, contents: "foo"
* file: part-r-00001, contents: "bar"
*/
- @Ignore
- private void setupDirs() throws IOException {
- Assert.assertFalse(del.exists());
- Assert.assertFalse(tmp.exists());
- Assert.assertFalse(partitioned.exists());
- del.mkdirs();
- tmp.mkdirs();
- partitioned.mkdirs();
+ @Before
+ public void setup() throws IOException {
+ del = testFolder.newFolder("del");
+ tmp = testFolder.newFolder("tmp");
+ partitioned = testFolder.newFolder("partitioned");
+
+ zlink = new File(del, "zlink");
+
+ xSubDir = new File(del, "xSubDir");
+ xSubSubDir = new File(xSubDir, "xSubSubDir");
+ ySubDir = new File(del, "ySubDir");
+
+
+ file2 = new File(xSubDir, "file2");
+ file22 = new File(xSubSubDir, "file22");
+ file3 = new File(ySubDir, "file3");
+
+ dir1 = new File(del, DIR + "1");
+ dir2 = new File(del, DIR + "2");
+
+ FileUtils.forceMkdir(dir1);
+ FileUtils.forceMkdir(dir2);
+
new File(del, FILE).createNewFile();
File tmpFile = new File(tmp, FILE);
tmpFile.createNewFile();
- // create directories
- dir1.mkdirs();
- dir2.mkdirs();
+ // create files
new File(dir1, FILE).createNewFile();
new File(dir2, FILE).createNewFile();
@@ -153,6 +182,11 @@ public class TestFileUtil {
FileUtil.symLink(del.toString(), dir1.toString() + "/cycle");
}
+ @After
+ public void tearDown() throws IOException {
+ testFolder.delete();
+ }
+
/**
* Creates a new file in the specified directory, with the specified name and
* the specified file contents. This method will add a newline terminator to
@@ -177,7 +211,6 @@ public class TestFileUtil {
@Test (timeout = 30000)
public void testListFiles() throws IOException {
- setupDirs();
//Test existing files case
File[] files = FileUtil.listFiles(partitioned);
Assert.assertEquals(2, files.length);
@@ -204,7 +237,6 @@ public class TestFileUtil {
@Test (timeout = 30000)
public void testListAPI() throws IOException {
- setupDirs();
//Test existing files case
String[] files = FileUtil.list(partitioned);
Assert.assertEquals("Unexpected number of pre-existing files", 2, files.length);
@@ -229,30 +261,8 @@ public class TestFileUtil {
}
}
- @Before
- public void before() throws IOException {
- cleanupImpl();
- }
-
- @After
- public void tearDown() throws IOException {
- cleanupImpl();
- }
-
- private void cleanupImpl() throws IOException {
- FileUtil.fullyDelete(del, true);
- Assert.assertTrue(!del.exists());
-
- FileUtil.fullyDelete(tmp, true);
- Assert.assertTrue(!tmp.exists());
-
- FileUtil.fullyDelete(partitioned, true);
- Assert.assertTrue(!partitioned.exists());
- }
-
@Test (timeout = 30000)
public void testFullyDelete() throws IOException {
- setupDirs();
boolean ret = FileUtil.fullyDelete(del);
Assert.assertTrue(ret);
Assert.assertFalse(del.exists());
@@ -267,8 +277,6 @@ public class TestFileUtil {
*/
@Test (timeout = 30000)
public void testFullyDeleteSymlinks() throws IOException {
- setupDirs();
-
File link = new File(del, LINK);
Assert.assertEquals(5, del.list().length);
// Since tmpDir is symlink to tmp, fullyDelete(tmpDir) should not
@@ -297,7 +305,6 @@ public class TestFileUtil {
*/
@Test (timeout = 30000)
public void testFullyDeleteDanglingSymlinks() throws IOException {
- setupDirs();
// 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);
@@ -324,7 +331,6 @@ public class TestFileUtil {
@Test (timeout = 30000)
public void testFullyDeleteContents() throws IOException {
- setupDirs();
boolean ret = FileUtil.fullyDeleteContents(del);
Assert.assertTrue(ret);
Assert.assertTrue(del.exists());
@@ -338,15 +344,6 @@ public class TestFileUtil {
Assert.assertTrue(new File(tmp, FILE).exists());
}
- private final File xSubDir = new File(del, "xSubDir");
- private final File xSubSubDir = new File(xSubDir, "xSubSubDir");
- private final File ySubDir = new File(del, "ySubDir");
- private static final String file1Name = "file1";
- private final File file2 = new File(xSubDir, "file2");
- private final File file22 = new File(xSubSubDir, "file22");
- private final File file3 = new File(ySubDir, "file3");
- private final File zlink = new File(del, "zlink");
-
/**
* Creates a directory which can not be deleted completely.
*
@@ -368,36 +365,30 @@ public class TestFileUtil {
* @throws IOException
*/
private void setupDirsAndNonWritablePermissions() throws IOException {
- Assert.assertFalse("The directory del should not have existed!",
- del.exists());
- del.mkdirs();
- new MyFile(del, file1Name).createNewFile();
+ new MyFile(del, FILE_1_NAME).createNewFile();
// "file1" is non-deletable by default, see MyFile.delete().
xSubDir.mkdirs();
file2.createNewFile();
-
+
xSubSubDir.mkdirs();
file22.createNewFile();
-
+
revokePermissions(file22);
revokePermissions(xSubSubDir);
-
+
revokePermissions(file2);
revokePermissions(xSubDir);
-
+
ySubDir.mkdirs();
file3.createNewFile();
- Assert.assertFalse("The directory tmp should not have existed!",
- tmp.exists());
- tmp.mkdirs();
File tmpFile = new File(tmp, FILE);
tmpFile.createNewFile();
FileUtil.symLink(tmpFile.toString(), zlink.toString());
}
-
+
private static void grantPermissions(final File f) {
FileUtil.setReadable(f, true);
FileUtil.setWritable(f, true);
@@ -419,7 +410,7 @@ public class TestFileUtil {
Assert.assertFalse("The return value should have been false.", ret);
Assert.assertTrue("The file file1 should not have been deleted.",
- new File(del, file1Name).exists());
+ new File(del, FILE_1_NAME).exists());
Assert.assertEquals(
"The directory xSubDir *should* not have been deleted.",
@@ -447,7 +438,7 @@ public class TestFileUtil {
boolean ret = FileUtil.fullyDelete(new MyFile(del));
validateAndSetWritablePermissions(true, ret);
}
-
+
@Test (timeout = 30000)
public void testFailFullyDeleteGrantPermissions() throws IOException {
setupDirsAndNonWritablePermissions();
@@ -484,7 +475,7 @@ public class TestFileUtil {
public boolean delete() {
LOG.info("Trying to delete myFile " + getAbsolutePath());
boolean bool = false;
- if (getName().equals(file1Name)) {
+ if (getName().equals(FILE_1_NAME)) {
bool = false;
} else {
bool = super.delete();
@@ -534,7 +525,7 @@ public class TestFileUtil {
// this time the directories with revoked permissions *should* be deleted:
validateAndSetWritablePermissions(false, ret);
}
-
+
/**
* Test that getDU is able to handle cycles caused due to symbolic links
* and that directory sizes are not added to the final calculated size
@@ -542,9 +533,7 @@ public class TestFileUtil {
*/
@Test (timeout = 30000)
public void testGetDU() throws Exception {
- setupDirs();
-
- long du = FileUtil.getDU(TEST_DIR);
+ long du = FileUtil.getDU(testFolder.getRoot());
// Only two files (in partitioned). Each has 3 characters + system-specific
// line separator.
final long expected = 2 * (3 + System.getProperty("line.separator").length());
@@ -593,8 +582,6 @@ public class TestFileUtil {
@Test (timeout = 30000)
public void testUnTar() throws IOException {
- setupDirs();
-
// make a simple tar:
final File simpleTar = new File(del, FILE);
OutputStream os = new FileOutputStream(simpleTar);
@@ -631,7 +618,6 @@ public class TestFileUtil {
@Test (timeout = 30000)
public void testReplaceFile() throws IOException {
- setupDirs();
final File srcFile = new File(tmp, "src");
// src exists, and target does not exist:
@@ -673,7 +659,6 @@ public class TestFileUtil {
@Test (timeout = 30000)
public void testCreateLocalTempFile() throws IOException {
- setupDirs();
final File baseFile = new File(tmp, "base");
File tmp1 = FileUtil.createLocalTempFile(baseFile, "foo", false);
File tmp2 = FileUtil.createLocalTempFile(baseFile, "foo", true);
@@ -689,8 +674,7 @@ public class TestFileUtil {
@Test (timeout = 30000)
public void testUnZip() throws IOException {
- setupDirs();
- // make a simple zip
+ // make sa simple zip
final File simpleZip = new File(del, FILE);
OutputStream os = new FileOutputStream(simpleZip);
ZipOutputStream tos = new ZipOutputStream(os);
@@ -726,7 +710,6 @@ public class TestFileUtil {
@Test (timeout = 30000)
public void testUnZip2() throws IOException {
- setupDirs();
// make a simple zip
final File simpleZip = new File(del, FILE);
OutputStream os = new FileOutputStream(simpleZip);
@@ -757,8 +740,6 @@ public class TestFileUtil {
* Test method copy(FileSystem srcFS, Path src, File dst, boolean deleteSource, Configuration conf)
*/
public void testCopy5() throws IOException {
- setupDirs();
-
URI uri = tmp.toURI();
Configuration conf = new Configuration();
FileSystem fs = FileSystem.newInstance(uri, conf);
@@ -848,9 +829,6 @@ public class TestFileUtil {
@Test (timeout = 30000)
public void testSymlink() throws Exception {
- Assert.assertFalse(del.exists());
- del.mkdirs();
-
byte[] data = "testSymLink".getBytes();
File file = new File(del, FILE);
@@ -883,9 +861,6 @@ public class TestFileUtil {
*/
@Test (timeout = 30000)
public void testSymlinkRenameTo() throws Exception {
- Assert.assertFalse(del.exists());
- del.mkdirs();
-
File file = new File(del, FILE);
file.createNewFile();
File link = new File(del, "_link");
@@ -915,9 +890,6 @@ public class TestFileUtil {
*/
@Test (timeout = 30000)
public void testSymlinkDelete() throws Exception {
- Assert.assertFalse(del.exists());
- del.mkdirs();
-
File file = new File(del, FILE);
file.createNewFile();
File link = new File(del, "_link");
@@ -939,9 +911,6 @@ public class TestFileUtil {
*/
@Test (timeout = 30000)
public void testSymlinkLength() throws Exception {
- Assert.assertFalse(del.exists());
- del.mkdirs();
-
byte[] data = "testSymLinkData".getBytes();
File file = new File(del, FILE);
@@ -978,9 +947,6 @@ public class TestFileUtil {
*/
@Test
public void testSymlinkWithNullInput() throws IOException {
- Assert.assertFalse(del.exists());
- del.mkdirs();
-
File file = new File(del, FILE);
File link = new File(del, "_link");
@@ -998,9 +964,6 @@ public class TestFileUtil {
// The operation should fail and returns 1
result = FileUtil.symLink(null, link.getAbsolutePath());
Assert.assertEquals(1, result);
-
- file.delete();
- link.delete();
}
/**
@@ -1011,9 +974,6 @@ public class TestFileUtil {
*/
@Test
public void testSymlinkFileAlreadyExists() throws IOException {
- Assert.assertFalse(del.exists());
- del.mkdirs();
-
File file = new File(del, FILE);
File link = new File(del, "_link");
@@ -1029,9 +989,6 @@ public class TestFileUtil {
result1 = FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath());
Assert.assertEquals(1, result1);
-
- file.delete();
- link.delete();
}
/**
@@ -1043,19 +1000,16 @@ public class TestFileUtil {
*/
@Test
public void testSymlinkSameFile() throws IOException {
- Assert.assertFalse(del.exists());
- del.mkdirs();
-
File file = new File(del, FILE);
+ file.delete();
+
// Create a symbolic link
// The operation should succeed
int result =
FileUtil.symLink(file.getAbsolutePath(), file.getAbsolutePath());
Assert.assertEquals(0, result);
-
- file.delete();
}
/**
@@ -1067,8 +1021,6 @@ public class TestFileUtil {
*/
@Test
public void testSymlink2DifferentFile() throws IOException {
- Assert.assertFalse(del.exists());
- del.mkdirs();
File file = new File(del, FILE);
File fileSecond = new File(del, FILE + "_1");
File link = new File(del, "_link");
@@ -1085,10 +1037,6 @@ public class TestFileUtil {
FileUtil.symLink(fileSecond.getAbsolutePath(), link.getAbsolutePath());
Assert.assertEquals(1, result);
-
- file.delete();
- fileSecond.delete();
- link.delete();
}
/**
@@ -1100,8 +1048,6 @@ public class TestFileUtil {
*/
@Test
public void testSymlink2DifferentLinks() throws IOException {
- Assert.assertFalse(del.exists());
- del.mkdirs();
File file = new File(del, FILE);
File link = new File(del, "_link");
File linkSecond = new File(del, "_link_1");
@@ -1118,10 +1064,6 @@ public class TestFileUtil {
FileUtil.symLink(file.getAbsolutePath(), linkSecond.getAbsolutePath());
Assert.assertEquals(0, result);
-
- file.delete();
- link.delete();
- linkSecond.delete();
}
private void doUntarAndVerify(File tarFile, File untarDir)
@@ -1166,10 +1108,6 @@ public class TestFileUtil {
@Test (timeout = 30000)
public void testCreateJarWithClassPath() throws Exception {
- // setup test directory for files
- Assert.assertFalse(tmp.exists());
- Assert.assertTrue(tmp.mkdirs());
-
// create files expected to match a wildcard
List<File> wildcardMatches = Arrays.asList(new File(tmp, "wildcard1.jar"),
new File(tmp, "wildcard2.jar"), new File(tmp, "wildcard3.JAR"),
@@ -1258,9 +1196,6 @@ public class TestFileUtil {
assertTrue("no jars should be returned for a bogus path",
jars.isEmpty());
- // setup test directory for files
- assertFalse(tmp.exists());
- assertTrue(tmp.mkdirs());
// create jar files to be returned
File jar1 = new File(tmp, "wildcard1.jar");
@@ -1366,7 +1301,6 @@ public class TestFileUtil {
@Test(timeout = 8000)
public void testCreateSymbolicLinkUsingJava() throws IOException {
- setupDirs();
final File simpleTar = new File(del, FILE);
OutputStream os = new FileOutputStream(simpleTar);
TarArchiveOutputStream tos = new TarArchiveOutputStream(os);
@@ -1460,9 +1394,6 @@ public class TestFileUtil {
*/
@Test
public void testReadSymlink() throws IOException {
- Assert.assertFalse(del.exists());
- del.mkdirs();
-
File file = new File(del, FILE);
File link = new File(del, "_link");
@@ -1471,9 +1402,6 @@ public class TestFileUtil {
String result = FileUtil.readLink(link);
Assert.assertEquals(file.getAbsolutePath(), result);
-
- file.delete();
- link.delete();
}
/**
@@ -1484,9 +1412,6 @@ public class TestFileUtil {
*/
@Test
public void testReadSymlinkWithAFileAsInput() throws IOException {
- Assert.assertFalse(del.exists());
- del.mkdirs();
-
File file = new File(del, FILE);
String result = FileUtil.readLink(file);
@@ -1500,8 +1425,6 @@ public class TestFileUtil {
*/
@Test
public void testWriteBytesFileSystem() throws IOException {
- setupDirs();
-
URI uri = tmp.toURI();
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(uri, conf);
@@ -1522,8 +1445,6 @@ public class TestFileUtil {
*/
@Test
public void testWriteStringsFileSystem() throws IOException {
- setupDirs();
-
URI uri = tmp.toURI();
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(uri, conf);
@@ -1544,8 +1465,6 @@ public class TestFileUtil {
*/
@Test
public void testWriteStringFileSystem() throws IOException {
- setupDirs();
-
URI uri = tmp.toURI();
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(uri, conf);
@@ -1567,8 +1486,6 @@ public class TestFileUtil {
*/
@Test
public void testWriteStringNoCharSetFileSystem() throws IOException {
- setupDirs();
-
URI uri = tmp.toURI();
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(uri, conf);
@@ -1588,8 +1505,6 @@ public class TestFileUtil {
*/
@Test
public void testWriteBytesFileContext() throws IOException {
- setupDirs();
-
URI uri = tmp.toURI();
Configuration conf = new Configuration();
FileContext fc = FileContext.getFileContext(uri, conf);
@@ -1610,8 +1525,6 @@ public class TestFileUtil {
*/
@Test
public void testWriteStringsFileContext() throws IOException {
- setupDirs();
-
URI uri = tmp.toURI();
Configuration conf = new Configuration();
FileContext fc = FileContext.getFileContext(uri, conf);
@@ -1632,8 +1545,6 @@ public class TestFileUtil {
*/
@Test
public void testWriteStringFileContext() throws IOException {
- setupDirs();
-
URI uri = tmp.toURI();
Configuration conf = new Configuration();
FileContext fc = FileContext.getFileContext(uri, conf);
@@ -1655,8 +1566,6 @@ public class TestFileUtil {
*/
@Test
public void testWriteStringNoCharSetFileContext() throws IOException {
- setupDirs();
-
URI uri = tmp.toURI();
Configuration conf = new Configuration();
FileContext fc = FileContext.getFileContext(uri, conf);
---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org