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 iw...@apache.org on 2022/04/14 22:24:25 UTC
[hadoop] branch branch-2.10 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.
iwasakims pushed a commit to branch branch-2.10
in repository https://gitbox.apache.org/repos/asf/hadoop.git
The following commit(s) were added to refs/heads/branch-2.10 by this push:
new 12c3b8f8df8 HADOOP-16811: Use JUnit TemporaryFolder Rule in TestFileUtils (#1811). Contributed by David Mollitor.
12c3b8f8df8 is described below
commit 12c3b8f8df8ff19e0342b14d480e768f3f2ad51b
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.
(cherry picked from commit 1afd54fbbb858a58112e6290b3063216eea82206)
Conflicts:
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestFileUtil.java
---
.../java/org/apache/hadoop/fs/TestFileUtil.java | 201 +++++++--------------
1 file changed, 68 insertions(+), 133 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 111a24479ae..074d77688a3 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
@@ -51,6 +51,7 @@ import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
+import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.test.GenericTestUtils;
import org.apache.hadoop.util.Shell;
@@ -61,22 +62,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;
@@ -113,21 +130,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();
@@ -148,6 +178,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
@@ -172,7 +207,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);
@@ -199,7 +233,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);
@@ -224,30 +257,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());
@@ -262,8 +273,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
@@ -292,7 +301,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);
@@ -319,7 +327,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());
@@ -333,15 +340,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.
*
@@ -363,36 +361,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);
@@ -414,7 +406,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.",
@@ -444,7 +436,7 @@ public class TestFileUtil {
boolean ret = FileUtil.fullyDelete(new MyFile(del));
validateAndSetWritablePermissions(true, ret);
}
-
+
@Test (timeout = 30000)
public void testFailFullyDeleteGrantPermissions() throws IOException {
setupDirsAndNonWritablePermissions();
@@ -481,7 +473,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();
@@ -536,10 +528,9 @@ public class TestFileUtil {
@Test (timeout = 30000)
public void testCopyMergeSingleDirectory() throws IOException {
- setupDirs();
boolean copyMergeResult = copyMerge("partitioned", "tmp/merged");
Assert.assertTrue("Expected successful copyMerge result.", copyMergeResult);
- File merged = new File(TEST_DIR, "tmp/merged");
+ File merged = new File(testFolder.getRoot(), "tmp/merged");
Assert.assertTrue("File tmp/merged must exist after copyMerge.",
merged.exists());
BufferedReader rdr = new BufferedReader(new FileReader(merged));
@@ -575,8 +566,8 @@ public class TestFileUtil {
final boolean result;
try {
- Path srcPath = new Path(TEST_DIR.getAbsolutePath(), src);
- Path dstPath = new Path(TEST_DIR.getAbsolutePath(), dst);
+ Path srcPath = new Path(testFolder.getRoot().getAbsolutePath(), src);
+ Path dstPath = new Path(testFolder.getRoot().getAbsolutePath(), dst);
boolean deleteSource = false;
String addString = null;
result = FileUtil.copyMerge(fs, srcPath, fs, dstPath, deleteSource, conf,
@@ -596,9 +587,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());
@@ -647,8 +636,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);
@@ -685,7 +672,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:
@@ -727,7 +713,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);
@@ -743,8 +728,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);
@@ -780,7 +764,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);
@@ -811,8 +794,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);
@@ -902,9 +883,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);
@@ -937,9 +915,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");
@@ -969,9 +944,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");
@@ -993,9 +965,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);
@@ -1032,9 +1001,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");
@@ -1052,9 +1018,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();
}
/**
@@ -1065,9 +1028,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");
@@ -1083,9 +1043,6 @@ public class TestFileUtil {
result1 = FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath());
Assert.assertEquals(1, result1);
-
- file.delete();
- link.delete();
}
/**
@@ -1097,19 +1054,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();
}
/**
@@ -1121,8 +1075,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");
@@ -1139,10 +1091,6 @@ public class TestFileUtil {
FileUtil.symLink(fileSecond.getAbsolutePath(), link.getAbsolutePath());
Assert.assertEquals(1, result);
-
- file.delete();
- fileSecond.delete();
- link.delete();
}
/**
@@ -1154,8 +1102,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");
@@ -1172,10 +1118,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)
@@ -1264,10 +1206,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"),
@@ -1356,9 +1294,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");
---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org