From 9df52c36ce539989dd7488a8420d82afde6dd5b7 Mon Sep 17 00:00:00 2001
From: Jonathan Hsieh <jon@cloudera.com>
Date: Tue, 3 May 2011 10:14:09 -0700
Subject: [PATCH] FLUME-627: Make all temp files created by File.createTempFile or FileUtil.mktmpdir live in one dir.
 - Replace instances of File.createTempFile with FileUtil.createTempFile.

---
 .../src/main/java/com/cloudera/util/FileUtil.java  |   51 ++++++++++++++++---
 .../flume/handlers/avro/TestAvroDataFile.java      |    5 +-
 .../flume/handlers/exec/TestExecNioSource.java     |   14 +++---
 .../flume/handlers/hdfs/TestReadLogEvents.java     |    3 +-
 .../flume/handlers/rolling/TestRollRollTags.java   |    5 +-
 .../cloudera/flume/handlers/text/TestFileNIO.java  |   17 +++---
 .../flume/handlers/text/TestMultiLineCursor.java   |   25 +++++----
 .../flume/handlers/text/TestTailSource.java        |   23 ++++----
 .../flume/handlers/text/TestTailSourceCursor.java  |   41 ++++++++-------
 .../text/output/TestOutputFormatFactory.java       |   19 ++++---
 .../flume/master/TestFlumeConfigMaster.java        |    2 +-
 .../flume/master/TestMemoryBackedConfigStore.java  |    7 ++-
 .../com/cloudera/flume/master/TestZKClient.java    |    8 ++-
 .../com/cloudera/flume/shell/TestFlumeShell.java   |    5 +-
 .../java/com/cloudera/util/TestDeleteOnExit.java   |    2 +-
 .../test/java/com/cloudera/util/TestFileUtil.java  |   55 ++++++++++++++++++++
 .../cloudera/util/dirwatcher/TestDirWatcher.java   |    2 +-
 17 files changed, 194 insertions(+), 90 deletions(-)
 create mode 100644 flume-core/src/test/java/com/cloudera/util/TestFileUtil.java

diff --git a/flume-core/src/main/java/com/cloudera/util/FileUtil.java b/flume-core/src/main/java/com/cloudera/util/FileUtil.java
index 341a65a..db0ce4b 100644
--- a/flume-core/src/main/java/com/cloudera/util/FileUtil.java
+++ b/flume-core/src/main/java/com/cloudera/util/FileUtil.java
@@ -21,6 +21,8 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 
 import com.google.common.base.Preconditions;
 
@@ -29,13 +31,41 @@ import com.google.common.base.Preconditions;
  */
 public class FileUtil {
 
+  static File baseDir;
+  static {
+    String baseDirStr = System.getenv("FLUME_TMP_DIR");
+    if (baseDirStr == null || baseDirStr.equals("")) {
+      // if not in environment, default to a date based dir name.
+      String dateFmt = "yyyyMMdd-HHmmssSSSZ";
+      SimpleDateFormat sdf = new SimpleDateFormat(dateFmt);
+      baseDirStr = "flume-" + sdf.format(new Date(Clock.unixTime()));
+    }
+    File tmpDir = new File(System.getProperty("java.io.tmpdir"));
+    baseDir = new File(tmpDir, baseDirStr);
+    makeDirs(baseDir);
+  }
+
+  static File getBaseDir() {
+    return baseDir;
+  }
+
   /**
-   * Makes a unique temporary directory. It is the responsibility of the caller
-   * to remove this directory.
+   * This wraps the standard create temp file and forces everything into the
+   * same baseDir. This makes clean up after testing really trivial.
+   * 
+   * @param filePrefix
+   * @param fileSuffix
+   * @return
+   * @throws IOException
    */
-  public static File mktempdir() throws IOException {
+  public static File createTempFile(String filePrefix, String fileSuffix)
+      throws IOException {
+    return File.createTempFile(filePrefix, fileSuffix, baseDir);
+  }
+
+  public static File mktempdir(String filePrefix) throws IOException {
     // setup a directory with a bad entry
-    File tmpdir = File.createTempFile("dir", ".tmp");
+    File tmpdir = File.createTempFile(filePrefix, ".tmp", baseDir);
     boolean status = tmpdir.delete();
     if (!status) {
       throw new IOException("failed to delete tmp file");
@@ -48,6 +78,14 @@ public class FileUtil {
   }
 
   /**
+   * Makes a unique temporary directory. It is the responsibility of the caller
+   * to remove this directory.
+   */
+  public static File mktempdir() throws IOException {
+    return mktempdir("dir");
+  }
+
+  /**
    * rm -r equivalent -- remove file/dir recursively.
    * 
    * Note that this function is extremely simplistic, and will traverse symlink
@@ -80,11 +118,10 @@ public class FileUtil {
     in.close();
     out.close();
   }
-  
+
   /**
    * Make a directory and its parents, returning true if and only if the dir
-   * exists.
-   * Does not guarantee that we can write to that directory!
+   * exists. Does not guarantee that we can write to that directory!
    */
   public static boolean makeDirs(File f) {
     if (f.exists()) {
diff --git a/flume-core/src/test/java/com/cloudera/flume/handlers/avro/TestAvroDataFile.java b/flume-core/src/test/java/com/cloudera/flume/handlers/avro/TestAvroDataFile.java
index 1d9c993..c08063a 100644
--- a/flume-core/src/test/java/com/cloudera/flume/handlers/avro/TestAvroDataFile.java
+++ b/flume-core/src/test/java/com/cloudera/flume/handlers/avro/TestAvroDataFile.java
@@ -42,6 +42,7 @@ import com.cloudera.flume.core.EventUtil;
 import com.cloudera.flume.handlers.debug.MemorySinkSource;
 import com.cloudera.flume.handlers.text.FormatFactory;
 import com.cloudera.flume.handlers.text.output.OutputFormat;
+import com.cloudera.util.FileUtil;
 
 /**
  * This test takes a small canned batch of events, writes them to a avro data
@@ -58,7 +59,7 @@ public class TestAvroDataFile {
     MemorySinkSource mem = MemorySinkSource.cannedData("test ", 5);
 
     // setup sink.
-    File f = File.createTempFile("avrodata", ".avro");
+    File f = FileUtil.createTempFile("avrodata", ".avro");
     f.deleteOnExit();
     LOG.info("filename before escaping: " + f.getAbsolutePath());
     String custom = "text(\""
@@ -95,7 +96,7 @@ public class TestAvroDataFile {
     MemorySinkSource mem = MemorySinkSource.cannedData("test ", 5);
 
     // setup sink.
-    File f = File.createTempFile("avrodata", ".avro");
+    File f = FileUtil.createTempFile("avrodata", ".avro");
     f.deleteOnExit();
     FileOutputStream fos = new FileOutputStream(f);
     LOG.info("filename before escaping: " + f.getAbsolutePath());
diff --git a/flume-core/src/test/java/com/cloudera/flume/handlers/exec/TestExecNioSource.java b/flume-core/src/test/java/com/cloudera/flume/handlers/exec/TestExecNioSource.java
index dbc2d98..c3cc439 100644
--- a/flume-core/src/test/java/com/cloudera/flume/handlers/exec/TestExecNioSource.java
+++ b/flume-core/src/test/java/com/cloudera/flume/handlers/exec/TestExecNioSource.java
@@ -275,7 +275,7 @@ public class TestExecNioSource {
   public void testAggModeMultiWrite() throws IOException, FlumeSpecException,
       InterruptedException {
 
-    File temp = File.createTempFile("slowwrite", ".sh");
+    File temp = FileUtil.createTempFile("slowwrite", ".sh");
     temp.deleteOnExit();
     BufferedWriter out = new BufferedWriter(new FileWriter(temp));
     // echo, sleep 1s, echo
@@ -339,7 +339,7 @@ public class TestExecNioSource {
    */
   @Test
   public void testReadStdErr() throws IOException, InterruptedException {
-    File temp = File.createTempFile("flmtst", null);
+    File temp = FileUtil.createTempFile("flmtst", null);
     temp.deleteOnExit();
 
     BufferedWriter out = new BufferedWriter(new FileWriter(temp));
@@ -364,7 +364,7 @@ public class TestExecNioSource {
    */
   @Test
   public void testClose() throws IOException, InterruptedException {
-    File temp = File.createTempFile("flmtst", null);
+    File temp = FileUtil.createTempFile("flmtst", null);
     temp.deleteOnExit();
 
     BufferedWriter out = new BufferedWriter(new FileWriter(temp));
@@ -466,7 +466,7 @@ public class TestExecNioSource {
    */
   @Test
   public void testAggregate() throws IOException, InterruptedException {
-    File temp = File.createTempFile("flmtst", null);
+    File temp = FileUtil.createTempFile("flmtst", null);
     temp.deleteOnExit();
 
     BufferedWriter out = new BufferedWriter(new FileWriter(temp));
@@ -492,7 +492,7 @@ public class TestExecNioSource {
    */
   @Test
   public void testRestart() throws IOException, InterruptedException {
-    File temp = File.createTempFile("flmtst", null);
+    File temp = FileUtil.createTempFile("flmtst", null);
     temp.deleteOnExit();
 
     BufferedWriter out = new BufferedWriter(new FileWriter(temp));
@@ -539,7 +539,7 @@ public class TestExecNioSource {
 
   @Test
   public void testNoRandomPrepend() throws IOException, InterruptedException {
-    File f = File.createTempFile("prepend", "bar");
+    File f = FileUtil.createTempFile("prepend", "bar");
     f.deleteOnExit();
     FileWriter fw = new FileWriter(f);
 
@@ -567,7 +567,7 @@ public class TestExecNioSource {
 
   @Test
   public void testTruncateLine() throws IOException, InterruptedException {
-    File f = File.createTempFile("truncate", ".bar");
+    File f = FileUtil.createTempFile("truncate", ".bar");
     f.deleteOnExit();
     FileWriter fw = new FileWriter(f);
 
diff --git a/flume-core/src/test/java/com/cloudera/flume/handlers/hdfs/TestReadLogEvents.java b/flume-core/src/test/java/com/cloudera/flume/handlers/hdfs/TestReadLogEvents.java
index 45dbce9..5ac20b6 100644
--- a/flume-core/src/test/java/com/cloudera/flume/handlers/hdfs/TestReadLogEvents.java
+++ b/flume-core/src/test/java/com/cloudera/flume/handlers/hdfs/TestReadLogEvents.java
@@ -25,6 +25,7 @@ import org.junit.Test;
 
 import com.cloudera.flume.core.Event;
 import com.cloudera.flume.core.EventImpl;
+import com.cloudera.util.FileUtil;
 
 /**
  * This tests a read of an existing logs.hdfs file.
@@ -35,7 +36,7 @@ public class TestReadLogEvents {
 
   @Test
   public void testReadLogEvents() throws IOException, InterruptedException {
-    File tmp = File.createTempFile("test", "tmp");
+    File tmp = FileUtil.createTempFile("test", "tmp");
     tmp.deleteOnExit();
     SeqfileEventSink sink = new SeqfileEventSink(tmp);
     sink.open();
diff --git a/flume-core/src/test/java/com/cloudera/flume/handlers/rolling/TestRollRollTags.java b/flume-core/src/test/java/com/cloudera/flume/handlers/rolling/TestRollRollTags.java
index 121b7e1..13f164b 100644
--- a/flume-core/src/test/java/com/cloudera/flume/handlers/rolling/TestRollRollTags.java
+++ b/flume-core/src/test/java/com/cloudera/flume/handlers/rolling/TestRollRollTags.java
@@ -37,6 +37,7 @@ import com.cloudera.flume.core.MaskDecorator;
 import com.cloudera.flume.handlers.debug.MemorySinkSource;
 import com.cloudera.flume.handlers.endtoend.ValueDecorator;
 import com.cloudera.util.BenchmarkHarness;
+import com.cloudera.util.FileUtil;
 
 /**
  * This test case demonstrates a usecase where a tag conflict occurs.
@@ -112,7 +113,7 @@ public class TestRollRollTags {
   @Test
   public void testAgentCollector() throws FlumeSpecException, IOException, InterruptedException {
     BenchmarkHarness.setupLocalWriteDir();
-    File path = File.createTempFile("collector", ".tmp");
+    File path = FileUtil.createTempFile("collector", ".tmp");
     path.deleteOnExit();
 
     EventSink snk = new CompositeSink(LogicalNodeContext.testingContext(),
@@ -129,7 +130,7 @@ public class TestRollRollTags {
   public void testAgentCollectorFixed() throws FlumeSpecException, IOException,
       InterruptedException {
     BenchmarkHarness.setupLocalWriteDir();
-    File path = File.createTempFile("collector", ".tmp");
+    File path = FileUtil.createTempFile("collector", ".tmp");
     path.deleteOnExit();
 
     EventSink snk = new CompositeSink(LogicalNodeContext.testingContext(),
diff --git a/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestFileNIO.java b/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestFileNIO.java
index 5272603..90bf5eb 100644
--- a/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestFileNIO.java
+++ b/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestFileNIO.java
@@ -34,6 +34,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.cloudera.util.Clock;
+import com.cloudera.util.FileUtil;
 
 /**
  * This tests on NIO semantics for file io in order to find out if is sufficient
@@ -74,10 +75,10 @@ public class TestFileNIO {
 
   @Test
   public void testRenameSemantics() throws IOException {
-    File f1 = File.createTempFile("moved", "");
+    File f1 = FileUtil.createTempFile("moved", "");
     f1.delete();
     f1.deleteOnExit();
-    File f2 = File.createTempFile("orig", "");
+    File f2 = FileUtil.createTempFile("orig", "");
     f2.deleteOnExit();
 
     f2.renameTo(f1);
@@ -126,7 +127,7 @@ public class TestFileNIO {
 
   @Test
   public void testFileChannel() throws IOException, InterruptedException {
-    File f = File.createTempFile("test", ".test");
+    File f = FileUtil.createTempFile("test", ".test");
     f.deleteOnExit();
 
     CountDownLatch done = slowWrite(f, 20);
@@ -160,7 +161,7 @@ public class TestFileNIO {
   @Test
   public void testFileChannelBlockRead() throws IOException,
       InterruptedException {
-    File f = File.createTempFile("test", ".test");
+    File f = FileUtil.createTempFile("test", ".test");
     f.deleteOnExit();
 
     CountDownLatch done = slowWrite(f, 0);
@@ -191,7 +192,7 @@ public class TestFileNIO {
 
   @Test
   public void testFileChannelByLine() throws IOException, InterruptedException {
-    File f = File.createTempFile("test", ".test");
+    File f = FileUtil.createTempFile("test", ".test");
     f.deleteOnExit();
 
     CountDownLatch done = slowWrite(f, 0);
@@ -244,7 +245,7 @@ public class TestFileNIO {
   @Test
   public void testFileChannelByLineWithBreaks() throws IOException,
       InterruptedException {
-    File f = File.createTempFile("test", ".test");
+    File f = FileUtil.createTempFile("test", ".test");
     f.deleteOnExit();
 
     CountDownLatch done = slowWrite(f, 0);
@@ -293,7 +294,7 @@ public class TestFileNIO {
   @Test
   public void testFileChannelByLineWithBreaksWhileWriting() throws IOException,
       InterruptedException {
-    File f = File.createTempFile("test", ".test");
+    File f = FileUtil.createTempFile("test", ".test");
     f.deleteOnExit();
 
     CountDownLatch done = slowWrite(f, 100);
@@ -341,7 +342,7 @@ public class TestFileNIO {
   @Test
   public void testInterruptFileChannelRead() throws IOException,
       InterruptedException {
-    File f = File.createTempFile("test", ".test");
+    File f = FileUtil.createTempFile("test", ".test");
     f.deleteOnExit();
 
     final CountDownLatch writeDone = slowWrite(f, 0);
diff --git a/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestMultiLineCursor.java b/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestMultiLineCursor.java
index aad5b18..f42164a 100644
--- a/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestMultiLineCursor.java
+++ b/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestMultiLineCursor.java
@@ -43,6 +43,7 @@ import com.cloudera.flume.core.Event;
 import com.cloudera.flume.handlers.text.CustomDelimCursor.ByteBufferAsCharSequence;
 import com.cloudera.flume.handlers.text.CustomDelimCursor.DelimMode;
 import com.cloudera.util.Clock;
+import com.cloudera.util.FileUtil;
 import com.cloudera.util.OSUtils;
 
 public class TestMultiLineCursor {
@@ -108,7 +109,7 @@ public class TestMultiLineCursor {
   }
 
   File createDataFile(int count) throws IOException {
-    File f = File.createTempFile("tail", ".tmp");
+    File f = FileUtil.createTempFile("tail", ".tmp");
     f.deleteOnExit();
     FileWriter fw = new FileWriter(f);
     for (int i = 0; i < count; i++) {
@@ -153,7 +154,7 @@ public class TestMultiLineCursor {
       InterruptedException {
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
-    File f2 = File.createTempFile("move", ".tmp");
+    File f2 = FileUtil.createTempFile("move", ".tmp");
     f2.delete();
     f2.deleteOnExit();
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(100);
@@ -190,7 +191,7 @@ public class TestMultiLineCursor {
       InterruptedException {
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
-    File f2 = File.createTempFile("move", ".tmp");
+    File f2 = FileUtil.createTempFile("move", ".tmp");
     f2.delete();
     f2.deleteOnExit();
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(100);
@@ -229,7 +230,7 @@ public class TestMultiLineCursor {
 
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
-    File f2 = File.createTempFile("move", ".tmp");
+    File f2 = FileUtil.createTempFile("move", ".tmp");
     f2.delete();
     f2.deleteOnExit();
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(100);
@@ -269,7 +270,7 @@ public class TestMultiLineCursor {
 
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
-    File f2 = File.createTempFile("move", ".tmp");
+    File f2 = FileUtil.createTempFile("move", ".tmp");
     f2.delete();
     f2.deleteOnExit();
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(100);
@@ -308,7 +309,7 @@ public class TestMultiLineCursor {
 
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
-    File f2 = File.createTempFile("move", ".tmp");
+    File f2 = FileUtil.createTempFile("move", ".tmp");
     f2.delete();
     f2.deleteOnExit();
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(100);
@@ -349,7 +350,7 @@ public class TestMultiLineCursor {
 
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
-    File f2 = File.createTempFile("move", ".tmp");
+    File f2 = FileUtil.createTempFile("move", ".tmp");
     f2.delete();
     f2.deleteOnExit();
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(100);
@@ -407,7 +408,7 @@ public class TestMultiLineCursor {
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(10);
-    File f = File.createTempFile("appear", ".tmp");
+    File f = FileUtil.createTempFile("appear", ".tmp");
     f.delete();
     f.deleteOnExit();
     Cursor c = new CustomDelimCursor(q, f, "blah", DelimMode.EXCLUDE);
@@ -438,7 +439,7 @@ public class TestMultiLineCursor {
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(10);
-    File f = File.createTempFile("appear", ".tmp");
+    File f = FileUtil.createTempFile("appear", ".tmp");
     f.delete();
     f.deleteOnExit();
     Cursor c = new CustomDelimCursor(q, f, "blah", DelimMode.EXCLUDE);
@@ -569,7 +570,7 @@ public class TestMultiLineCursor {
 
   @Test
   public void testDelimIncludeNext() throws IOException, InterruptedException {
-    File f = File.createTempFile("tail", ".tmp");
+    File f = FileUtil.createTempFile("tail", ".tmp");
     f.deleteOnExit();
     FileWriter fw = new FileWriter(f);
     fw.append("2011-03-07 00:26:53,918 [exec-thread] WARN commands.SetChokeLimitForm: PhysicalNode: physNode not present yet!\n"
@@ -615,7 +616,7 @@ public class TestMultiLineCursor {
 
   @Test
   public void testDelimIncludePrev() throws IOException, InterruptedException {
-    File f = File.createTempFile("tail", ".tmp");
+    File f = FileUtil.createTempFile("tail", ".tmp");
     f.deleteOnExit();
     FileWriter fw = new FileWriter(f);
     fw.append("<?xml version=\"1.0\"?><?xml-stylesheet type=\"text/xsl\"  href=\"logs.xsl\"?>"
@@ -647,7 +648,7 @@ public class TestMultiLineCursor {
 
   @Test
   public void testDelimExclude() throws IOException, InterruptedException {
-    File f = File.createTempFile("tail", ".tmp");
+    File f = FileUtil.createTempFile("tail", ".tmp");
     f.deleteOnExit();
     FileWriter fw = new FileWriter(f);
     fw.append("a\nb\n\n" + "c\nd\ne\n\n\n" + "f\ng\n\n\n\n" + "h");
diff --git a/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestTailSource.java b/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestTailSource.java
index 78a6d9a..2c7f119 100644
--- a/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestTailSource.java
+++ b/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestTailSource.java
@@ -50,6 +50,7 @@ import com.cloudera.flume.handlers.debug.MemorySinkSource;
 import com.cloudera.flume.reporter.ReportManager;
 import com.cloudera.flume.reporter.aggregator.CounterSink;
 import com.cloudera.util.Clock;
+import com.cloudera.util.FileUtil;
 
 /**
  * This tests the functionality of tail source using the EventSource api.
@@ -106,7 +107,7 @@ public class TestTailSource {
     long sleepTime;
     long eventCount;
 
-    f = File.createTempFile("temp", ".tmp");
+    f = FileUtil.createTempFile("temp", ".tmp");
     f.setReadable(false, false);
 
     f.deleteOnExit();
@@ -173,7 +174,7 @@ public class TestTailSource {
   @Test
   public void testTailSource() throws IOException, FlumeSpecException,
       InterruptedException {
-    File f = File.createTempFile("temp", ".tmp");
+    File f = FileUtil.createTempFile("temp", ".tmp");
     f.deleteOnExit();
     final CompositeSink snk = new CompositeSink(new ReportTestingContext(),
         "{ delay(50) => counter(\"count\") }");
@@ -201,9 +202,9 @@ public class TestTailSource {
   @Test
   public void testTailSourceMove() throws IOException, FlumeSpecException,
       InterruptedException {
-    File f = File.createTempFile("temp", ".tmp");
+    File f = FileUtil.createTempFile("temp", ".tmp");
     f.deleteOnExit();
-    File f2 = File.createTempFile("moved", ".tmp");
+    File f2 = FileUtil.createTempFile("moved", ".tmp");
     f2.delete();
     f2.deleteOnExit();
     final CompositeSink snk = new CompositeSink(new ReportTestingContext(),
@@ -249,9 +250,9 @@ public class TestTailSource {
   @Test
   public void testMultiTailSource() throws IOException, FlumeSpecException,
       InterruptedException {
-    File f = File.createTempFile("multitemp1", ".tmp");
+    File f = FileUtil.createTempFile("multitemp1", ".tmp");
     f.deleteOnExit();
-    File f2 = File.createTempFile("multitemp2", ".tmp");
+    File f2 = FileUtil.createTempFile("multitemp2", ".tmp");
     f2.deleteOnExit();
     final CompositeSink snk = new CompositeSink(new ReportTestingContext(),
         "{ delay(50) => counter(\"count\") }");
@@ -293,9 +294,9 @@ public class TestTailSource {
   @Test
   public void testMultiTailSourceFileName() throws IOException,
       FlumeSpecException, InterruptedException {
-    File f = File.createTempFile("multitemp1", ".tmp");
+    File f = FileUtil.createTempFile("multitemp1", ".tmp");
     f.deleteOnExit();
-    File f2 = File.createTempFile("multitemp2", ".tmp");
+    File f2 = FileUtil.createTempFile("multitemp2", ".tmp");
     f2.deleteOnExit();
     final MemorySinkSource snk = new MemorySinkSource();
     final EventSource src = TailSource.multiTailBuilder().build(
@@ -360,7 +361,7 @@ public class TestTailSource {
   @Test
   public void testTailSourceStartFromEnd() throws IOException,
       FlumeSpecException, InterruptedException {
-    File f = File.createTempFile("temp", ".tmp");
+    File f = FileUtil.createTempFile("temp", ".tmp");
     f.deleteOnExit();
 
     // pre-existing file
@@ -404,7 +405,7 @@ public class TestTailSource {
    */
   @Test
   public void testFileOutputStream() throws IOException {
-    File tmp = File.createTempFile("tmp-", ".tmp");
+    File tmp = FileUtil.createTempFile("tmp-", ".tmp");
     FileOutputStream f = new FileOutputStream(tmp);
     f.write("0123456789".getBytes());
     f.close();
@@ -439,7 +440,7 @@ public class TestTailSource {
     FileOutputStream os;
     Thread thread;
 
-    tmpFile = File.createTempFile("tmp-", ".tmp");
+    tmpFile = FileUtil.createTempFile("tmp-", ".tmp");
     tmpFile.deleteOnExit();
 
     source = TailSource.builder().build(LogicalNodeContext.testingContext(),
diff --git a/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestTailSourceCursor.java b/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestTailSourceCursor.java
index b4cda8c..9cba457 100644
--- a/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestTailSourceCursor.java
+++ b/flume-core/src/test/java/com/cloudera/flume/handlers/text/TestTailSourceCursor.java
@@ -44,6 +44,7 @@ import org.mortbay.log.Log;
 
 import com.cloudera.flume.core.Event;
 import com.cloudera.util.Clock;
+import com.cloudera.util.FileUtil;
 import com.cloudera.util.OSUtils;
 
 /**
@@ -59,7 +60,7 @@ public class TestTailSourceCursor {
   }
 
   File createDataFile(int count) throws IOException {
-    File f = File.createTempFile("tail", ".tmp");
+    File f = FileUtil.createTempFile("tail", ".tmp");
     f.deleteOnExit();
     FileWriter fw = new FileWriter(f);
     for (int i = 0; i < count; i++) {
@@ -104,7 +105,7 @@ public class TestTailSourceCursor {
       InterruptedException {
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
-    File f2 = File.createTempFile("move", ".tmp");
+    File f2 = FileUtil.createTempFile("move", ".tmp");
     f2.delete();
     f2.deleteOnExit();
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(100);
@@ -141,7 +142,7 @@ public class TestTailSourceCursor {
       InterruptedException {
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
-    File f2 = File.createTempFile("move", ".tmp");
+    File f2 = FileUtil.createTempFile("move", ".tmp");
     f2.delete();
     f2.deleteOnExit();
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(100);
@@ -180,7 +181,7 @@ public class TestTailSourceCursor {
 
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
-    File f2 = File.createTempFile("move", ".tmp");
+    File f2 = FileUtil.createTempFile("move", ".tmp");
     f2.delete();
     f2.deleteOnExit();
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(100);
@@ -220,7 +221,7 @@ public class TestTailSourceCursor {
 
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
-    File f2 = File.createTempFile("move", ".tmp");
+    File f2 = FileUtil.createTempFile("move", ".tmp");
     f2.delete();
     f2.deleteOnExit();
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(100);
@@ -259,7 +260,7 @@ public class TestTailSourceCursor {
 
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
-    File f2 = File.createTempFile("move", ".tmp");
+    File f2 = FileUtil.createTempFile("move", ".tmp");
     f2.delete();
     f2.deleteOnExit();
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(100);
@@ -300,7 +301,7 @@ public class TestTailSourceCursor {
 
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
-    File f2 = File.createTempFile("move", ".tmp");
+    File f2 = FileUtil.createTempFile("move", ".tmp");
     f2.delete();
     f2.deleteOnExit();
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(100);
@@ -357,7 +358,7 @@ public class TestTailSourceCursor {
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(10);
-    File f = File.createTempFile("appear", ".tmp");
+    File f = FileUtil.createTempFile("appear", ".tmp");
     f.delete();
     f.deleteOnExit();
     Cursor c = new Cursor(q, f);
@@ -388,7 +389,7 @@ public class TestTailSourceCursor {
     // normal implementation uses synchronous queue, but we use array blocking
     // queue for single threaded testing
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(10);
-    File f = File.createTempFile("appear", ".tmp");
+    File f = FileUtil.createTempFile("appear", ".tmp");
     f.delete();
     f.deleteOnExit();
     Cursor c = new Cursor(q, f);
@@ -525,9 +526,9 @@ public class TestTailSourceCursor {
    */
   @Test
   public void testFileDescriptor() throws IOException {
-    File f = File.createTempFile("fdes", ".tmp");
+    File f = FileUtil.createTempFile("fdes", ".tmp");
     f.deleteOnExit();
-    File f2 = File.createTempFile("fdes", ".tmp");
+    File f2 = FileUtil.createTempFile("fdes", ".tmp");
     f2.delete();
     f2.deleteOnExit();
 
@@ -553,9 +554,9 @@ public class TestTailSourceCursor {
    */
   @Test
   public void testFISFollowsFD() throws IOException {
-    File f = File.createTempFile("fdes", ".tmp");
+    File f = FileUtil.createTempFile("fdes", ".tmp");
     f.deleteOnExit();
-    File f2 = File.createTempFile("fdes", ".tmp");
+    File f2 = FileUtil.createTempFile("fdes", ".tmp");
     f2.delete();
     f2.deleteOnExit();
 
@@ -615,9 +616,9 @@ public class TestTailSourceCursor {
    */
   @Test
   public void testChannelFollowsHandle() throws IOException {
-    File f = File.createTempFile("first", ".tmp");
+    File f = FileUtil.createTempFile("first", ".tmp");
     f.deleteOnExit();
-    File f2 = File.createTempFile("second", ".tmp");
+    File f2 = FileUtil.createTempFile("second", ".tmp");
     f2.delete();
     f2.deleteOnExit();
 
@@ -651,9 +652,9 @@ public class TestTailSourceCursor {
    */
   @Test
   public void testFileDescriptorEquals() throws IOException {
-    File f = File.createTempFile("first", ".tmp");
+    File f = FileUtil.createTempFile("first", ".tmp");
     f.deleteOnExit();
-    File f2 = File.createTempFile("second", ".tmp");
+    File f2 = FileUtil.createTempFile("second", ".tmp");
     f2.delete();
     f2.deleteOnExit();
 
@@ -674,7 +675,7 @@ public class TestTailSourceCursor {
    */
   @Test
   public void testTruncate() throws IOException {
-    File f = File.createTempFile("first", ".tmp");
+    File f = FileUtil.createTempFile("first", ".tmp");
     f.deleteOnExit();
 
     FileWriter fw = new FileWriter(f);
@@ -714,9 +715,9 @@ public class TestTailSourceCursor {
     // Windows rename semantics different than unix
     Assume.assumeTrue(!OSUtils.isWindowsOS());
 
-    File f = File.createTempFile("tailexhaust", ".txt");
+    File f = FileUtil.createTempFile("tailexhaust", ".txt");
     f.deleteOnExit();
-    File f2 = File.createTempFile("tailexhaust", ".txt");
+    File f2 = FileUtil.createTempFile("tailexhaust", ".txt");
     f2.deleteOnExit();
     BlockingQueue<Event> q = new ArrayBlockingQueue<Event>(100);
     Cursor c = new Cursor(q, f);
diff --git a/flume-core/src/test/java/com/cloudera/flume/handlers/text/output/TestOutputFormatFactory.java b/flume-core/src/test/java/com/cloudera/flume/handlers/text/output/TestOutputFormatFactory.java
index a651cb1..db56413 100644
--- a/flume-core/src/test/java/com/cloudera/flume/handlers/text/output/TestOutputFormatFactory.java
+++ b/flume-core/src/test/java/com/cloudera/flume/handlers/text/output/TestOutputFormatFactory.java
@@ -37,6 +37,7 @@ import com.cloudera.flume.core.EventImpl;
 import com.cloudera.flume.core.EventSink;
 import com.cloudera.flume.handlers.text.FormatFactory;
 import com.cloudera.flume.handlers.text.FormatFactory.OutputFormatBuilder;
+import com.cloudera.util.FileUtil;
 
 /**
  * This tests building of outputformats via the FormatFactory mechanism. These
@@ -143,7 +144,7 @@ public class TestOutputFormatFactory {
   public void testSyslogText() throws FlumeSpecException, IOException,
       InterruptedException {
 
-    File tmp = File.createTempFile("syslogText", ".txt");
+    File tmp = FileUtil.createTempFile("syslogText", ".txt");
     tmp.deleteOnExit();
 
     EventSink snk = FlumeBuilder.buildSink(new Context(), "text(\""
@@ -166,7 +167,7 @@ public class TestOutputFormatFactory {
   public void testDefaultText() throws FlumeSpecException, IOException,
       InterruptedException {
 
-    File tmp = File.createTempFile("defaultText", ".txt");
+    File tmp = FileUtil.createTempFile("defaultText", ".txt");
     tmp.deleteOnExit();
 
     EventSink snk = FlumeBuilder.buildSink(new Context(), "text(\""
@@ -190,7 +191,7 @@ public class TestOutputFormatFactory {
   public void testLog4jText() throws FlumeSpecException, IOException,
       InterruptedException {
 
-    File tmp = File.createTempFile("log4jText", ".txt");
+    File tmp = FileUtil.createTempFile("log4jText", ".txt");
     tmp.deleteOnExit();
 
     EventSink snk = FlumeBuilder.buildSink(new Context(), "text(\""
@@ -213,7 +214,7 @@ public class TestOutputFormatFactory {
   public void testJsonText() throws FlumeSpecException, IOException,
       InterruptedException {
 
-    File tmp = File.createTempFile("jsonText", ".txt");
+    File tmp = FileUtil.createTempFile("jsonText", ".txt");
     tmp.deleteOnExit();
 
     EventSink snk = FlumeBuilder.buildSink(new Context(), "text(\""
@@ -236,7 +237,7 @@ public class TestOutputFormatFactory {
   public void testSyslogDfs() throws FlumeSpecException, IOException,
       InterruptedException {
 
-    File tmp = File.createTempFile("syslogDfs", ".txt");
+    File tmp = FileUtil.createTempFile("syslogDfs", ".txt");
     tmp.deleteOnExit();
 
     EventSink snk = FlumeBuilder.buildSink(new Context(),
@@ -260,7 +261,7 @@ public class TestOutputFormatFactory {
   public void testDefaultDfs() throws FlumeSpecException, IOException,
       InterruptedException {
 
-    File tmp = File.createTempFile("defaultDfs", ".txt");
+    File tmp = FileUtil.createTempFile("defaultDfs", ".txt");
     tmp.deleteOnExit();
 
     EventSink snk = FlumeBuilder.buildSink(new Context(),
@@ -284,7 +285,7 @@ public class TestOutputFormatFactory {
   public void testLog4jDfs() throws FlumeSpecException, IOException,
       InterruptedException {
 
-    File tmp = File.createTempFile("log4jDfs", ".txt");
+    File tmp = FileUtil.createTempFile("log4jDfs", ".txt");
     tmp.deleteOnExit();
 
     EventSink snk = FlumeBuilder.buildSink(new Context(),
@@ -308,7 +309,7 @@ public class TestOutputFormatFactory {
   public void testJsonDfs() throws FlumeSpecException, IOException,
       InterruptedException {
 
-    File tmp = File.createTempFile("jsonDfs", ".txt");
+    File tmp = FileUtil.createTempFile("jsonDfs", ".txt");
     tmp.deleteOnExit();
 
     EventSink snk = FlumeBuilder.buildSink(new Context(),
@@ -338,7 +339,7 @@ public class TestOutputFormatFactory {
     BufferedReader reader;
     int matchedLines;
 
-    tmpFile = File.createTempFile("wackadoodleOutputFormatTest", ".tmp");
+    tmpFile = FileUtil.createTempFile("wackadoodleOutputFormatTest", ".tmp");
     tmpFile.deleteOnExit();
 
     FormatFactory.get().registerFormat(new OutputFormatBuilder() {
diff --git a/flume-core/src/test/java/com/cloudera/flume/master/TestFlumeConfigMaster.java b/flume-core/src/test/java/com/cloudera/flume/master/TestFlumeConfigMaster.java
index 05f4f99..9353c22 100644
--- a/flume-core/src/test/java/com/cloudera/flume/master/TestFlumeConfigMaster.java
+++ b/flume-core/src/test/java/com/cloudera/flume/master/TestFlumeConfigMaster.java
@@ -82,7 +82,7 @@ public class TestFlumeConfigMaster {
     fcm.serve();
     ConfigurationManager cm = fcm.getSpecMan();
     cm.setConfig("test1", "test-flow", "console", "console");
-    File f = File.createTempFile("test", ".tmp");
+    File f = FileUtil.createTempFile("test", ".tmp");
     f.deleteOnExit();
     cm.saveConfigFile(f.getAbsolutePath());
     assertTrue(f.exists());
diff --git a/flume-core/src/test/java/com/cloudera/flume/master/TestMemoryBackedConfigStore.java b/flume-core/src/test/java/com/cloudera/flume/master/TestMemoryBackedConfigStore.java
index 96f10e9..12405ac 100644
--- a/flume-core/src/test/java/com/cloudera/flume/master/TestMemoryBackedConfigStore.java
+++ b/flume-core/src/test/java/com/cloudera/flume/master/TestMemoryBackedConfigStore.java
@@ -27,6 +27,7 @@ import java.util.List;
 
 import org.junit.Test;
 import com.cloudera.flume.conf.FlumeConfigData;
+import com.cloudera.util.FileUtil;
 
 public class TestMemoryBackedConfigStore {
   /**
@@ -35,7 +36,7 @@ public class TestMemoryBackedConfigStore {
    */
   @Test
   public void testGetSetSaveLoad() throws IOException {
-    File tmp = File.createTempFile("test-flume", "");
+    File tmp = FileUtil.createTempFile("test-flume", "");
     tmp.delete();
     tmp.deleteOnExit();
     MemoryBackedConfigStore store = new MemoryBackedConfigStore();
@@ -59,7 +60,7 @@ public class TestMemoryBackedConfigStore {
    */
   @Test
   public void testNodes() throws IOException {
-    File tmp = File.createTempFile("test-flume", "");
+    File tmp = FileUtil.createTempFile("test-flume", "");
     tmp.delete();
     tmp.deleteOnExit();
     MemoryBackedConfigStore store = new MemoryBackedConfigStore();
@@ -86,7 +87,7 @@ public class TestMemoryBackedConfigStore {
    */
   @Test
   public void testUnmapAllNodes() throws IOException {
-    File tmp = File.createTempFile("test-flume", "");
+    File tmp = FileUtil.createTempFile("test-flume", "");
     tmp.delete();
     tmp.deleteOnExit();
     MemoryBackedConfigStore store = new MemoryBackedConfigStore();
diff --git a/flume-core/src/test/java/com/cloudera/flume/master/TestZKClient.java b/flume-core/src/test/java/com/cloudera/flume/master/TestZKClient.java
index e5ce158..97a4e33 100644
--- a/flume-core/src/test/java/com/cloudera/flume/master/TestZKClient.java
+++ b/flume-core/src/test/java/com/cloudera/flume/master/TestZKClient.java
@@ -36,6 +36,8 @@ import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.cloudera.util.FileUtil;
+
 /**
  * A variety of tests both for ZKClient and the ZKServer
  * 
@@ -108,7 +110,7 @@ public class TestZKClient {
    */
   @Test
   public void testZKClient() throws Exception {
-    File temp = File.createTempFile("flume-zk-test", "");
+    File temp = FileUtil.createTempFile("flume-zk-test", "");
     temp.delete();
     temp.mkdir();
     temp.deleteOnExit();
@@ -140,7 +142,7 @@ public class TestZKClient {
       InterruptedException {
     ZKClient client = null;
     try {
-      File temp = File.createTempFile("flume-zk-test", "");
+      File temp = FileUtil.createTempFile("flume-zk-test", "");
       temp.delete();
       temp.mkdir();
       temp.deleteOnExit();
@@ -164,7 +166,7 @@ public class TestZKClient {
   @Test
   public void testZKSequentialChildren() throws InterruptedException,
       IOException, KeeperException {
-    File temp = File.createTempFile("flume-zk-test", "");
+    File temp = FileUtil.createTempFile("flume-zk-test", "");
     temp.delete();
     temp.mkdir();
     temp.deleteOnExit();
diff --git a/flume-core/src/test/java/com/cloudera/flume/shell/TestFlumeShell.java b/flume-core/src/test/java/com/cloudera/flume/shell/TestFlumeShell.java
index 1d996b3..f81b190 100644
--- a/flume-core/src/test/java/com/cloudera/flume/shell/TestFlumeShell.java
+++ b/flume-core/src/test/java/com/cloudera/flume/shell/TestFlumeShell.java
@@ -52,6 +52,7 @@ import com.cloudera.flume.reporter.ReportManager;
 import com.cloudera.flume.reporter.aggregator.AccumulatorSink;
 import com.cloudera.flume.util.FlumeShell;
 import com.cloudera.util.Clock;
+import com.cloudera.util.FileUtil;
 
 /**
  * Test a few basic commands on the flume shell.
@@ -134,7 +135,7 @@ public class TestFlumeShell extends SetupMasterTestEnv {
     retval = sh.executeLine("exec config foo 'null' 'console'");
     assertEquals(0, retval);
 
-    File saveFile = File.createTempFile("test-flume", "");
+    File saveFile = FileUtil.createTempFile("test-flume", "");
     saveFile.delete();
     saveFile.deleteOnExit();
 
@@ -160,7 +161,7 @@ public class TestFlumeShell extends SetupMasterTestEnv {
         + FlumeConfiguration.DEFAULT_ADMIN_PORT);
     assertEquals(0, retval);
 
-    File saveFile = File.createTempFile("test-flume", "");
+    File saveFile = FileUtil.createTempFile("test-flume", "");
     saveFile.deleteOnExit();
     BufferedWriter out = new BufferedWriter(new FileWriter(saveFile));
     out.write("foo : null | console;\n");
diff --git a/flume-core/src/test/java/com/cloudera/util/TestDeleteOnExit.java b/flume-core/src/test/java/com/cloudera/util/TestDeleteOnExit.java
index 87125ad..aa1d741 100644
--- a/flume-core/src/test/java/com/cloudera/util/TestDeleteOnExit.java
+++ b/flume-core/src/test/java/com/cloudera/util/TestDeleteOnExit.java
@@ -30,7 +30,7 @@ public class TestDeleteOnExit {
   @Test
   public void testDeleteOnExit() throws IOException {
 
-    File f = File.createTempFile("foo", ".tmp");
+    File f = FileUtil.createTempFile("foo", ".tmp");
     System.out.println(f.getAbsolutePath());
     f.deleteOnExit();
     f.renameTo(new File(f.getParentFile(), "pre" + f.getName()));
diff --git a/flume-core/src/test/java/com/cloudera/util/TestFileUtil.java b/flume-core/src/test/java/com/cloudera/util/TestFileUtil.java
new file mode 100644
index 0000000..3b2630c
--- /dev/null
+++ b/flume-core/src/test/java/com/cloudera/util/TestFileUtil.java
@@ -0,0 +1,55 @@
+/**
+ * Licensed to Cloudera, Inc. under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  Cloudera, Inc. licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.cloudera.util;
+
+import java.io.File;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.junit.Test;
+
+/**
+ * Test the to make sure FileUtil generated temp dirs are all part of the given
+ * baseDir
+ */
+public class TestFileUtil extends TestCase {
+  @Test
+  public void testCreateTempFile() throws IOException {
+    File f = FileUtil.createTempFile("foo", ".bar");
+    f.deleteOnExit();
+    String matchPat = FileUtil.getBaseDir() + ".*foo.*\\.bar";
+    assertTrue(f.getAbsolutePath().matches(matchPat));
+  }
+
+  @Test
+  public void testCreateTempDir() throws IOException {
+    File d = null;
+    try {
+      d = FileUtil.mktempdir();
+      assertTrue(d.exists());
+      assertTrue(d.isDirectory());
+    } finally {
+      if (d != null) {
+        FileUtil.rmr(d);
+      }
+    }
+
+  }
+
+}
diff --git a/flume-core/src/test/java/com/cloudera/util/dirwatcher/TestDirWatcher.java b/flume-core/src/test/java/com/cloudera/util/dirwatcher/TestDirWatcher.java
index 517d1bd..c4a4296 100644
--- a/flume-core/src/test/java/com/cloudera/util/dirwatcher/TestDirWatcher.java
+++ b/flume-core/src/test/java/com/cloudera/util/dirwatcher/TestDirWatcher.java
@@ -120,7 +120,7 @@ public class TestDirWatcher {
     List<File> temps = new ArrayList<File>();
     for (int i = 0; i < 20; i++) {
 
-      File temp = File.createTempFile("foo", "bar");
+      File temp = FileUtil.createTempFile("foo", "bar");
       BufferedWriter out = new BufferedWriter(new FileWriter(temp));
       out.write(temp.getName());
       out.close();
-- 
1.7.1

