Repository: flink
Updated Branches:
  refs/heads/master 70475b367 -> f113d7945


[FLINK-5817] [tests] Use TemporaryFold to create temp files and folds for test

This closes #3341


Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/709fa1d9
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/709fa1d9
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/709fa1d9

Branch: refs/heads/master
Commit: 709fa1d95b7dbbcfdd1124de7d6e073834ca75cf
Parents: d05fc37
Author: wenlong.lwl <[email protected]>
Authored: Fri Feb 17 17:14:54 2017 +0800
Committer: Stephan Ewen <[email protected]>
Committed: Sat Feb 18 19:19:34 2017 +0100

----------------------------------------------------------------------
 .../FileCacheDeleteValidationTest.java          | 12 +++--
 .../io/disk/iomanager/IOManagerTest.java        | 11 ++--
 .../flink/test/util/AbstractTestBase.java       | 53 ++++++--------------
 3 files changed, 31 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/709fa1d9/flink-runtime/src/test/java/org/apache/flink/runtime/filecache/FileCacheDeleteValidationTest.java
----------------------------------------------------------------------
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 4db0d93..4dca3db 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.apache.flink.api.common.JobID;
 
 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 class FileCacheDeleteValidationTest {
                        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);
                }

http://git-wip-us.apache.org/repos/asf/flink/blob/709fa1d9/flink-runtime/src/test/java/org/apache/flink/runtime/io/disk/iomanager/IOManagerTest.java
----------------------------------------------------------------------
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 39cb8ee..156098e 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 @@ package org.apache.flink.runtime.io.disk.iomanager;
 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 @@ import static org.junit.Assert.assertTrue;
 
 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 class IOManagerTest {
                        throw new UnsupportedOperationException();
                }
        }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/flink/blob/709fa1d9/flink-test-utils-parent/flink-test-utils/src/main/java/org/apache/flink/test/util/AbstractTestBase.java
----------------------------------------------------------------------
diff --git 
a/flink-test-utils-parent/flink-test-utils/src/main/java/org/apache/flink/test/util/AbstractTestBase.java
 
b/flink-test-utils-parent/flink-test-utils/src/main/java/org/apache/flink/test/util/AbstractTestBase.java
index 316fd21..544d473 100644
--- 
a/flink-test-utils-parent/flink-test-utils/src/main/java/org/apache/flink/test/util/AbstractTestBase.java
+++ 
b/flink-test-utils-parent/flink-test-utils/src/main/java/org/apache/flink/test/util/AbstractTestBase.java
@@ -20,14 +20,17 @@ package org.apache.flink.test.util;
 
 import com.google.common.base.Charsets;
 import com.google.common.io.Files;
+
 import org.apache.flink.configuration.Configuration;
 import org.apache.flink.runtime.minicluster.LocalFlinkMiniCluster;
+
+import org.junit.Rule;
+import org.junit.rules.TemporaryFolder;
+
 import scala.concurrent.duration.FiniteDuration;
 
 import java.io.File;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
 import java.util.Objects;
 
 import org.apache.flink.runtime.akka.AkkaUtils;
@@ -39,22 +42,23 @@ public abstract class AbstractTestBase extends 
TestBaseUtils {
        
        /** Configuration to start the testing cluster with */
        protected final Configuration config;
-       
-       private final List<File> 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<File>();
 
                timeout = AkkaUtils.getTimeout(config);
        }
@@ -74,7 +78,6 @@ public abstract class AbstractTestBase extends TestBaseUtils {
 
        public void stopCluster() throws Exception {
                stopCluster(executor, timeout);
-               deleteAllTempFiles();
        }
 
        //------------------
@@ -103,12 +106,12 @@ public abstract class AbstractTestBase extends 
TestBaseUtils {
        // 
--------------------------------------------------------------------------------------------
 
        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 abstract class AbstractTestBase extends 
TestBaseUtils {
        }
 
        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);
-                       }
-               }
-       }
 }

Reply via email to