From 61c8397e48af999df6f7db39c375b8bd7dda54eb Mon Sep 17 00:00:00 2001
From: "Lin.Liang" <546648227@qq.com>
Date: Tue, 16 Jul 2019 09:41:44 +0800
Subject: [PATCH] Fix temp file problem in log test cases (#908)

---
 .../alibaba/csp/sentinel/RecordLogTest.java   | 29 ++++++++++---
 .../slots/logger/EagleEyeLogUtilTest.java     | 41 ++++++++++++-------
 2 files changed, 51 insertions(+), 19 deletions(-)

diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/RecordLogTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/RecordLogTest.java
index e2e4bd99..50f9b63f 100755
--- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/RecordLogTest.java
+++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/RecordLogTest.java
@@ -15,14 +15,14 @@
  */
 package com.alibaba.csp.sentinel;
 
-import java.io.File;
-
 import com.alibaba.csp.sentinel.log.LogBase;
 import com.alibaba.csp.sentinel.log.RecordLog;
 import com.alibaba.csp.sentinel.util.PidUtil;
-
+import org.junit.Assert;
 import org.junit.Test;
 
+import java.io.File;
+
 import static org.junit.Assert.assertTrue;
 
 /**
@@ -44,21 +44,27 @@ public class RecordLogTest {
         }
     }
 
-    @Test
+    //Change LogBase It is not not work when integration Testing
+    //Because LogBase.LOG_DIR can be just static init for once and it will not be changed
+    //@Test
     public void testChangeLogBase() {
+
         String userHome = System.getProperty("user.home");
         String newLogBase = userHome + File.separator + "tmpLogDir" + System.currentTimeMillis();
         System.setProperty(LogBase.LOG_DIR, newLogBase);
 
         RecordLog.info("testChangeLogBase");
         String logFileName = RecordLog.getLogBaseDir();
+        Assert.assertTrue(newLogBase.equals(logFileName));
         File[] files = new File(logFileName).listFiles();
         assertTrue(files != null && files.length > 0);
+        deleteLogDir(new File(newLogBase));
+
+
     }
 
     @Test
     public void testLogBaseDir() {
-        RecordLog.info("testLogBaseDir");
         assertTrue(RecordLog.getLogBaseDir().startsWith(System.getProperty("user.home")));
     }
 
@@ -88,4 +94,17 @@ public class RecordLogTest {
         }
     }
 
+    private void deleteLogDir(File logDirFile) {
+        if (logDirFile != null && logDirFile.isDirectory()) {
+            if (logDirFile.listFiles() != null) {
+                for (File file : logDirFile.listFiles()) {
+                    file.delete();
+                }
+            }
+            logDirFile.delete();
+        }
+    }
+
+
+
 }
\ No newline at end of file
diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtilTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtilTest.java
index fd9e9f43..f9e7dd70 100644
--- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtilTest.java
+++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtilTest.java
@@ -1,19 +1,16 @@
 package com.alibaba.csp.sentinel.slots.logger;
 
-import java.io.File;
-import java.util.concurrent.Callable;
-import java.util.concurrent.TimeUnit;
-
 import com.alibaba.csp.sentinel.log.LogBase;
 import com.alibaba.csp.sentinel.log.RecordLog;
-
-import org.hamcrest.CoreMatchers;
-import org.hamcrest.Matchers;
 import org.hamcrest.io.FileMatchers;
+import org.junit.Assert;
 import org.junit.Test;
 
+import java.io.File;
+import java.util.concurrent.Callable;
+import java.util.concurrent.TimeUnit;
+
 import static org.awaitility.Awaitility.await;
-import static org.junit.Assert.*;
 
 /**
  * @author Carpenter Lee
@@ -34,7 +31,9 @@ public class EagleEyeLogUtilTest {
             }, FileMatchers.anExistingFile());
     }
 
-    @Test
+    //Change LogBase It is not not work when integration Testing
+    //Because LogBase.LOG_DIR can be just static init for once and it will not be changed
+    //@Test
     public void testChangeLogBase() throws Exception {
         String userHome = System.getProperty("user.home");
         String newLogBase = userHome + File.separator + "tmpLogDir" + System.currentTimeMillis();
@@ -42,13 +41,27 @@ public class EagleEyeLogUtilTest {
 
         EagleEyeLogUtil.log("resourceName", "BlockException", "app1", "origin", 1);
 
+
         final File file = new File(RecordLog.getLogBaseDir() + EagleEyeLogUtil.FILE_NAME);
         await().timeout(2, TimeUnit.SECONDS)
-            .until(new Callable<File>() {
-                @Override
-                public File call() throws Exception {
-                    return file;
+                .until(new Callable<File>() {
+                    @Override
+                    public File call() throws Exception {
+                        return file;
+                    }
+                }, FileMatchers.anExistingFile());
+        Assert.assertTrue(file.getAbsolutePath().startsWith(newLogBase));
+        deleteLogDir(new File(RecordLog.getLogBaseDir()));
+    }
+
+    private void deleteLogDir(File logDirFile) {
+        if (logDirFile != null && logDirFile.isDirectory()) {
+            if (logDirFile.listFiles() != null) {
+                for (File file : logDirFile.listFiles()) {
+                    file.delete();
                 }
-            }, FileMatchers.anExistingFile());
+            }
+            logDirFile.delete();
+        }
     }
 }
\ No newline at end of file