diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/filecache/FileCacheDeleteValidationTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/filecache/FileCacheDeleteValidationTest.java index 4db0d93b45465..4dca3dbc10e8f 100644 --- a/flink-runtime/src/test/java/org/apache/flink/runtime/filecache/FileCacheDeleteValidationTest.java +++ b/flink-runtime/src/test/java/org/apache/flink/runtime/filecache/FileCacheDeleteValidationTest.java @@ -19,6 +19,7 @@ package org.apache.flink.runtime.filecache; import java.io.File; +import java.io.IOException; import java.util.concurrent.Future; import org.apache.flink.core.fs.Path; @@ -27,10 +28,12 @@ import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import com.google.common.base.Charsets; import com.google.common.io.Files; +import org.junit.rules.TemporaryFolder; import static org.junit.Assert.fail; import static org.junit.Assert.assertTrue; @@ -56,12 +59,15 @@ public class FileCacheDeleteValidationTest { + "Da flammt ein blitzendes Verheeren Dem Pfade vor des Donnerschlags. Doch\n" + "deine Boten, Herr, verehren Das sanfte Wandeln deines Tags.\n"; + @Rule + public final TemporaryFolder temporaryFolder = new TemporaryFolder(); + private FileCache fileCache; private File f; @Before - public void setup() { - String[] tmpDirectories = System.getProperty("java.io.tmpdir").split(",|" + File.pathSeparator); + public void setup() throws IOException { + String[] tmpDirectories = new String[]{temporaryFolder.newFolder().getAbsolutePath()}; try { fileCache = new FileCache(tmpDirectories); } @@ -70,7 +76,7 @@ public void setup() { fail("Cannot create FileCache: " + e.getMessage()); } - f = new File(System.getProperty("java.io.tmpdir"), "cacheFile"); + f = temporaryFolder.newFile("cacheFile"); try { Files.write(testFileContent, f, Charsets.UTF_8); } diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/io/disk/iomanager/IOManagerTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/io/disk/iomanager/IOManagerTest.java index 39cb8ee7ae085..156098ea665a0 100644 --- a/flink-runtime/src/test/java/org/apache/flink/runtime/io/disk/iomanager/IOManagerTest.java +++ b/flink-runtime/src/test/java/org/apache/flink/runtime/io/disk/iomanager/IOManagerTest.java @@ -21,7 +21,9 @@ import org.apache.flink.core.memory.MemorySegment; import org.apache.flink.runtime.io.disk.iomanager.FileIOChannel.ID; import org.apache.flink.runtime.io.network.buffer.Buffer; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.IOException; @@ -34,12 +36,15 @@ public class IOManagerTest { + @Rule + public final TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Test - public void channelEnumerator() { + public void channelEnumerator() throws IOException { IOManager ioMan = null; try { - File tempPath = new File(System.getProperty("java.io.tmpdir")); + File tempPath = temporaryFolder.newFolder(); String[] tempDirs = new String[]{ new File(tempPath, "a").getAbsolutePath(), @@ -126,4 +131,4 @@ public BulkBlockChannelReader createBulkBlockChannelReader(ID channelID, List tempFiles; - + private final FiniteDuration timeout; protected int taskManagerNumSlots = 1; protected int numTaskManagers = 1; - + + @Rule + public final TemporaryFolder temporaryFolder = new TemporaryFolder(); + + /** The mini cluster that runs the test programs */ protected LocalFlinkMiniCluster executor; public AbstractTestBase(Configuration config) { this.config = Objects.requireNonNull(config); - this.tempFiles = new ArrayList(); timeout = AkkaUtils.getTimeout(config); } @@ -74,7 +78,6 @@ public void startCluster() throws Exception { public void stopCluster() throws Exception { stopCluster(executor, timeout); - deleteAllTempFiles(); } //------------------ @@ -103,12 +106,12 @@ public void setNumTaskManagers(int numTaskManagers) { // -------------------------------------------------------------------------------------------- public String getTempDirPath(String dirName) throws IOException { - File f = createAndRegisterTempFile(dirName); + File f = temporaryFolder.newFolder(dirName); return f.toURI().toString(); } public String getTempFilePath(String fileName) throws IOException { - File f = createAndRegisterTempFile(fileName); + File f = temporaryFolder.newFile(fileName); return f.toURI().toString(); } @@ -119,35 +122,7 @@ public String createTempFile(String fileName, String contents) throws IOExceptio } public File createAndRegisterTempFile(String fileName) throws IOException { - File baseDir = new File(System.getProperty("java.io.tmpdir")); - File f = new File(baseDir, this.getClass().getName() + "-" + fileName); - - if (f.exists()) { - deleteRecursively(f); - } - - File parentToDelete = f; - while (true) { - File parent = parentToDelete.getParentFile(); - if (parent == null) { - throw new IOException("Missed temp dir while traversing parents of a temp file."); - } - if (parent.equals(baseDir)) { - break; - } - parentToDelete = parent; - } - - Files.createParentDirs(f); - this.tempFiles.add(parentToDelete); - return f; + return temporaryFolder.newFile(fileName); } - private void deleteAllTempFiles() throws IOException { - for (File f : this.tempFiles) { - if (f.exists()) { - deleteRecursively(f); - } - } - } }