bookkeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From iv...@apache.org
Subject bookkeeper git commit: BOOKKEEPER-814: clean up temp files that generated by test cases. (zhaijia via ivank)
Date Wed, 10 Dec 2014 18:43:33 GMT
Repository: bookkeeper
Updated Branches:
  refs/heads/master e79f8736a -> 0e3d95c94


BOOKKEEPER-814: clean up temp files that generated by test cases. (zhaijia via ivank)


Project: http://git-wip-us.apache.org/repos/asf/bookkeeper/repo
Commit: http://git-wip-us.apache.org/repos/asf/bookkeeper/commit/0e3d95c9
Tree: http://git-wip-us.apache.org/repos/asf/bookkeeper/tree/0e3d95c9
Diff: http://git-wip-us.apache.org/repos/asf/bookkeeper/diff/0e3d95c9

Branch: refs/heads/master
Commit: 0e3d95c9411289b89961e434ce760e3726290761
Parents: e79f873
Author: Ivan Kelly <ivank@apache.org>
Authored: Wed Dec 10 19:43:24 2014 +0100
Committer: Ivan Kelly <ivank@apache.org>
Committed: Wed Dec 10 19:43:24 2014 +0100

----------------------------------------------------------------------
 CHANGES.txt                                     |   2 +
 .../org/apache/bookkeeper/util/IOUtils.java     |  42 +++++++
 .../apache/bookkeeper/util/LocalBookKeeper.java |  57 +++++++--
 .../bookie/BookieInitializationTest.java        | 124 +++++++------------
 .../bookkeeper/bookie/BookieJournalTest.java    | 112 +++++++----------
 .../bookkeeper/bookie/CompactionTest.java       |   8 +-
 .../apache/bookkeeper/bookie/CookieTest.java    |  95 +++++++-------
 .../apache/bookkeeper/bookie/EntryLogTest.java  |  42 ++++---
 .../bookkeeper/bookie/LedgerCacheTest.java      |  36 +++---
 .../bookie/TestLedgerDirsManager.java           |  26 +++-
 .../apache/bookkeeper/bookie/UpgradeTest.java   |  87 ++++++-------
 .../replication/AuditorBookieTest.java          |   2 -
 .../test/BookKeeperClusterTestCase.java         |  17 ++-
 .../bookkeeper/test/BookieClientTest.java       |   6 +-
 .../bookkeeper/test/BookieZKExpireTest.java     |   5 +-
 .../bookkeeper/test/ConcurrentLedgerTest.java   |  15 ++-
 .../bookkeeper/test/ReadOnlyBookieTest.java     |   5 +-
 .../bookkeeper/test/TestBackwardCompat.java     |  58 +++++----
 .../apache/bookkeeper/test/ZooKeeperUtil.java   |   5 +-
 .../apache/bookkeeper/util/TestDiskChecker.java |  34 +++--
 20 files changed, 426 insertions(+), 352 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 2edba29..910b1ed 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -26,6 +26,8 @@ Trunk (unreleased changes)
 
       BOOKKEEPER-795: Race condition causes writes to hang if ledger is fenced (sijie via ivank)
 
+      BOOKKEEPER-814: clean up temp files that generated by test cases. (zhaijia via ivank)
+
     IMPROVEMENTS:
 
       BOOKKEEPER-800: Expose whether a ledger is closed or not (ivank)

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/IOUtils.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/IOUtils.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/IOUtils.java
index 5e85594..0ecbf57 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/IOUtils.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/IOUtils.java
@@ -20,10 +20,12 @@
  */
 package org.apache.bookkeeper.util;
 
+import java.io.File;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.channels.WritableByteChannel;
 
+import org.apache.commons.io.FileUtils;
 import org.slf4j.Logger;
 
 /**
@@ -101,4 +103,44 @@ public class IOUtils {
             bc.write(buf);
         } while (buf.remaining() > 0);
     }
+
+
+    /**
+     * Create a temp directory with given <i>prefix</i> and <i>suffix</i>.
+     *
+     * @param prefix
+     *          prefix of the directory name
+     * @param suffix
+     *          suffix of the directory name
+     * @return directory created
+     * @throws IOException
+     */
+    public static File createTempDir(String prefix, String suffix)
+            throws IOException {
+        File tmpDir = File.createTempFile(prefix, suffix);
+        if (!tmpDir.delete()) {
+            throw new IOException("Couldn't delete directory " + tmpDir);
+        }
+        if (!tmpDir.mkdir()) {
+            throw new IOException("Couldn't create directory " + tmpDir);
+        }
+        return tmpDir;
+    }
+
+    /**
+     * Create a temp directory with given <i>prefix</i> and <i>suffix</i>.
+     *
+     * @param prefix
+     *          prefix of the directory name
+     * @param suffix
+     *          suffix of the directory name
+     * @return directory created
+     * @throws IOException
+     */
+    public static File createTempFileAndDeleteOnExit(String prefix, String suffix)
+            throws IOException {
+        File tmpDir = File.createTempFile(prefix, suffix);
+        tmpDir.deleteOnExit();
+        return tmpDir;
+    }
 }

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
index 4897883..0937bb5 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
@@ -27,7 +27,10 @@ import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
+import java.util.ArrayList;
+import java.util.List;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.bookkeeper.bookie.BookieException;
 import org.apache.bookkeeper.conf.ServerConfiguration;
 import org.apache.bookkeeper.proto.BookieServer;
@@ -86,10 +89,7 @@ public class LocalBookKeeper {
         LOG.info("Starting ZK server");
         //ServerStats.registerAsConcrete();
         //ClientBase.setupTestEnv();
-        ZkTmpDir = File.createTempFile("zookeeper", "test");
-        if (!ZkTmpDir.delete() || !ZkTmpDir.mkdir()) {
-            throw new IOException("Couldn't create zk directory " + ZkTmpDir);
-        }
+        ZkTmpDir = IOUtils.createTempDir("zookeeper", "localbookkeeper");
 
         try {
             zks = new ZooKeeperServer(ZkTmpDir, ZkTmpDir, ZooKeeperDefaultPort);
@@ -126,8 +126,42 @@ public class LocalBookKeeper {
         }
     }
 
-    private void runBookies(ServerConfiguration baseConf) throws IOException,
-            KeeperException, InterruptedException, BookieException,
+    private static void cleanupDirectories(List<File> dirs) throws IOException {
+        for (File dir : dirs) {
+            FileUtils.deleteDirectory(dir);
+        }
+    }
+
+    private List<File> runBookies(ServerConfiguration baseConf, String dirSuffix)
+            throws IOException, KeeperException, InterruptedException, BookieException,
+            UnavailableException, CompatibilityException {
+        List<File> tempDirs = new ArrayList<File>();
+        try {
+            runBookies(baseConf, tempDirs, dirSuffix);
+            return tempDirs;
+        } catch (IOException ioe) {
+            cleanupDirectories(tempDirs);
+            throw ioe;
+        } catch (KeeperException ke) {
+            cleanupDirectories(tempDirs);
+            throw ke;
+        } catch (InterruptedException ie) {
+            cleanupDirectories(tempDirs);
+            throw ie;
+        } catch (BookieException be) {
+            cleanupDirectories(tempDirs);
+            throw be;
+        } catch (UnavailableException ue) {
+            cleanupDirectories(tempDirs);
+            throw ue;
+        } catch (CompatibilityException ce) {
+            cleanupDirectories(tempDirs);
+            throw ce;
+        }
+    }
+
+    private void runBookies(ServerConfiguration baseConf, List<File> tempDirs, String dirSuffix)
+            throws IOException, KeeperException, InterruptedException, BookieException,
             UnavailableException, CompatibilityException {
         LOG.info("Starting Bookie(s)");
         // Create Bookie Servers (B1, B2, B3)
@@ -179,9 +213,14 @@ public class LocalBookKeeper {
 
         lb.runZookeeper(1000);
         lb.initializeZookeper();
-        lb.runBookies(conf);
-        while (true) {
-            Thread.sleep(5000);
+        List<File> tmpDirs = lb.runBookies(conf, "test");
+        try {
+            while (true) {
+                Thread.sleep(5000);
+            }
+        } catch (InterruptedException ie) {
+            cleanupDirectories(tmpDirs);
+            throw ie;
         }
     }
 

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieInitializationTest.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieInitializationTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieInitializationTest.java
index f19583f..8da9eb4 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieInitializationTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieInitializationTest.java
@@ -39,15 +39,15 @@ import org.apache.bookkeeper.conf.ServerConfiguration;
 import org.apache.bookkeeper.client.BookKeeperAdmin;
 import org.apache.bookkeeper.proto.BookieServer;
 import org.apache.bookkeeper.test.ZooKeeperUtil;
+import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
 import org.apache.bookkeeper.util.DiskChecker.DiskErrorException;
+import org.apache.bookkeeper.util.IOUtils;
 import org.apache.commons.io.FileUtils;
 import org.apache.zookeeper.WatchedEvent;
 import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.data.Stat;
 import org.apache.zookeeper.KeeperException;
-import org.junit.After;
-import org.junit.Before;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -55,26 +55,22 @@ import org.slf4j.LoggerFactory;
 /**
  * Testing bookie initialization cases
  */
-public class BookieInitializationTest {
+public class BookieInitializationTest extends BookKeeperClusterTestCase {
     private static final Logger LOG = LoggerFactory
             .getLogger(BookieInitializationTest.class);
-    ZooKeeperUtil zkutil;
-    ZooKeeper zkc = null;
-    ZooKeeper newzk = null;
 
-    @Before
-    public void setupZooKeeper() throws Exception {
-        zkutil = new ZooKeeperUtil();
-        zkutil.startServer();
-        zkc = zkutil.getZooKeeperClient();
+    ZooKeeper newzk = null;
+    
+    public BookieInitializationTest() {
+        super(0);
     }
 
-    @After
-    public void tearDownZooKeeper() throws Exception {
-        if (newzk != null) {
+    @Override
+    public void tearDown() throws Exception {
+        if (null != newzk) {
             newzk.close();
         }
-        zkutil.killServer();
+        super.tearDown();
     }
 
     private static class MockBookie extends Bookie {
@@ -94,9 +90,7 @@ public class BookieInitializationTest {
      */
     @Test(timeout = 20000)
     public void testExitCodeZK_REG_FAIL() throws Exception {
-        File tmpDir = File.createTempFile("bookie", "test");
-        tmpDir.delete();
-        tmpDir.mkdir();
+        File tmpDir = createTempDir("bookie", "test");
 
         final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
                 .setZkServers(null).setJournalDirName(tmpDir.getPath())
@@ -122,9 +116,7 @@ public class BookieInitializationTest {
 
     @Test(timeout = 20000)
     public void testBookieRegistrationWithSameZooKeeperClient() throws Exception {
-        File tmpDir = File.createTempFile("bookie", "test");
-        tmpDir.delete();
-        tmpDir.mkdir();
+        File tmpDir = createTempDir("bookie", "test");
 
         final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
                 .setZkServers(null).setJournalDirName(tmpDir.getPath())
@@ -153,9 +145,7 @@ public class BookieInitializationTest {
      */
     @Test(timeout = 20000)
     public void testBookieRegistration() throws Exception {
-        File tmpDir = File.createTempFile("bookie", "test");
-        tmpDir.delete();
-        tmpDir.mkdir();
+        File tmpDir = createTempDir("bookie", "test");
 
         final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
                 .setZkServers(null).setJournalDirName(tmpDir.getPath())
@@ -219,9 +209,7 @@ public class BookieInitializationTest {
      */
     @Test(timeout = 30000)
     public void testRegNodeExistsAfterSessionTimeOut() throws Exception {
-        File tmpDir = File.createTempFile("bookie", "test");
-        tmpDir.delete();
-        tmpDir.mkdir();
+        File tmpDir = createTempDir("bookie", "test");
 
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration().setZkServers(null)
                 .setJournalDirName(tmpDir.getPath()).setLedgerDirNames(
@@ -274,9 +262,7 @@ public class BookieInitializationTest {
      */
     @Test(timeout = 20000)
     public void testDuplicateBookieServerStartup() throws Exception {
-        File tmpDir = File.createTempFile("bookie", "test");
-        tmpDir.delete();
-        tmpDir.mkdir();
+        File tmpDir = createTempDir("bookie", "test");
 
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
         int port = 12555;
@@ -304,14 +290,12 @@ public class BookieInitializationTest {
      */
     @Test(timeout = 20000)
     public void testStartBookieWithoutZKServer() throws Exception {
-        zkutil.killServer();
+        zkUtil.killServer();
 
-        File tmpDir = File.createTempFile("bookie", "test");
-        tmpDir.delete();
-        tmpDir.mkdir();
+        File tmpDir = createTempDir("bookie", "test");
 
         final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-                .setZkServers(zkutil.getZooKeeperConnectString())
+                .setZkServers(zkUtil.getZooKeeperConnectString())
                 .setZkTimeout(5000).setJournalDirName(tmpDir.getPath())
                 .setLedgerDirNames(new String[] { tmpDir.getPath() });
         try {
@@ -319,9 +303,7 @@ public class BookieInitializationTest {
             fail("Should throw ConnectionLossException as ZKServer is not running!");
         } catch (KeeperException.ConnectionLossException e) {
             // expected behaviour
-        } finally {
-            FileUtils.deleteDirectory(tmpDir);
-        }
+        } 
     }
 
     /**
@@ -330,70 +312,60 @@ public class BookieInitializationTest {
      */
     @Test(timeout = 20000)
     public void testStartBookieWithoutZKInitialized() throws Exception {
-        File tmpDir = File.createTempFile("bookie", "test");
-        tmpDir.delete();
-        tmpDir.mkdir();
+        File tmpDir = createTempDir("bookie", "test");
         final String ZK_ROOT = "/ledgers2";
 
         final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-            .setZkServers(zkutil.getZooKeeperConnectString())
+            .setZkServers(zkUtil.getZooKeeperConnectString())
             .setZkTimeout(5000).setJournalDirName(tmpDir.getPath())
             .setLedgerDirNames(new String[] { tmpDir.getPath() });
         conf.setZkLedgersRootPath(ZK_ROOT);
         try {
-            try {
-                new Bookie(conf);
-                fail("Should throw NoNodeException");
-            } catch (Exception e) {
-                // shouldn't be able to start
-            }
-            ClientConfiguration clientConf = new ClientConfiguration();
-            clientConf.setZkServers(zkutil.getZooKeeperConnectString());
-            clientConf.setZkLedgersRootPath(ZK_ROOT);
-            BookKeeperAdmin.format(clientConf, false, false);
-
-            Bookie b = new Bookie(conf);
-            b.shutdown();
-        } finally {
-            FileUtils.deleteDirectory(tmpDir);
+            new Bookie(conf);
+            fail("Should throw NoNodeException");
+        } catch (Exception e) {
+            // shouldn't be able to start
         }
+        ClientConfiguration clientConf = new ClientConfiguration();
+        clientConf.setZkServers(zkUtil.getZooKeeperConnectString());
+        clientConf.setZkLedgersRootPath(ZK_ROOT);
+        BookKeeperAdmin.format(clientConf, false, false);
+
+        Bookie b = new Bookie(conf);
+        b.shutdown();
     }
 
     /**
      * Check disk full. Expected to throw NoWritableLedgerDirException
      * during bookie initialisation.
      */
-    @Test(timeout = 30000, expected = NoWritableLedgerDirException.class)
+    @Test(timeout = 30000)
     public void testWithDiskFull() throws Exception {
-        File tempDir = File.createTempFile("DiskCheck", "test");
-        tempDir.delete();
-        tempDir.mkdir();
-        long usableSpace = tempDir.getUsableSpace();
-        long totalSpace = tempDir.getTotalSpace();
+        File tmpDir = createTempDir("DiskCheck", "test");
+        long usableSpace = tmpDir.getUsableSpace();
+        long totalSpace = tmpDir.getTotalSpace();
         final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-                .setZkServers(zkutil.getZooKeeperConnectString())
-                .setZkTimeout(5000).setJournalDirName(tempDir.getPath())
-                .setLedgerDirNames(new String[] { tempDir.getPath() });
+                .setZkServers(zkUtil.getZooKeeperConnectString())
+                .setZkTimeout(5000).setJournalDirName(tmpDir.getPath())
+                .setLedgerDirNames(new String[] { tmpDir.getPath() });
         conf.setDiskUsageThreshold((1f - ((float) usableSpace / (float) totalSpace)) - 0.05f);
         conf.setDiskUsageWarnThreshold((1f - ((float) usableSpace / (float) totalSpace)) - 0.25f);
         try {
             new Bookie(conf);
-        } finally {
-            FileUtils.deleteDirectory(tempDir);
+        } catch (Exception e) {
+            // expected
         }
     }
 
     /**
      * Check disk error for file. Expected to throw DiskErrorException.
      */
-    @Test(timeout = 30000, expected = DiskErrorException.class)
+    @Test(timeout = 30000)
     public void testWithDiskError() throws Exception {
-        File parent = File.createTempFile("DiskCheck", "test");
-        parent.delete();
-        parent.mkdir();
+        File parent = createTempDir("DiskCheck", "test");
         File child = File.createTempFile("DiskCheck", "test", parent);
         final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-                .setZkServers(zkutil.getZooKeeperConnectString())
+                .setZkServers(zkUtil.getZooKeeperConnectString())
                 .setZkTimeout(5000).setJournalDirName(child.getPath())
                 .setLedgerDirNames(new String[] { child.getPath() });
         try {
@@ -401,8 +373,8 @@ public class BookieInitializationTest {
             // Simulating disk errors by directly calling #init
             LedgerDirsManager ldm = new LedgerDirsManager(conf, conf.getLedgerDirs());
             ldm.init();
-        } finally {
-            FileUtils.deleteDirectory(parent);
+        } catch (Exception e) {
+            // expected
         }
     }
 
@@ -410,7 +382,7 @@ public class BookieInitializationTest {
         // create a zookeeper client
         LOG.debug("Instantiate ZK Client");
         final CountDownLatch latch = new CountDownLatch(1);
-        newzk = new ZooKeeper(zkutil.getZooKeeperConnectString(), 10000,
+        newzk = new ZooKeeper(zkUtil.getZooKeeperConnectString(), 10000,
                 new Watcher() {
                     @Override
                     public void process(WatchedEvent event) {

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieJournalTest.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieJournalTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieJournalTest.java
index 0d6e592..9baa4e7 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieJournalTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieJournalTest.java
@@ -29,6 +29,8 @@ import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Arrays;
 import java.util.Random;
 
@@ -38,9 +40,11 @@ import org.apache.bookkeeper.conf.ServerConfiguration;
 import org.apache.bookkeeper.conf.TestBKConfiguration;
 import org.apache.bookkeeper.util.IOUtils;
 import org.apache.bookkeeper.util.ZeroBuffer;
+import org.apache.commons.io.FileUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.junit.Test;
+import org.junit.After;
 import static org.junit.Assert.*;
 
 public class BookieJournalTest {
@@ -48,6 +52,22 @@ public class BookieJournalTest {
 
     final Random r = new Random(System.currentTimeMillis());
 
+    final List<File> tempDirs = new ArrayList<File>();
+
+    File createTempDir(String prefix, String suffix) throws IOException {
+        File dir = IOUtils.createTempDir(prefix, suffix);
+        tempDirs.add(dir);
+        return dir;
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        for (File dir : tempDirs) {
+            FileUtils.deleteDirectory(dir);
+        }
+        tempDirs.clear();
+    }
+
     private void writeIndexFileForLedger(File indexDir, long ledgerId,
                                          byte[] masterKey)
             throws Exception {
@@ -312,14 +332,10 @@ public class BookieJournalTest {
      */
     @Test(timeout=60000)
     public void testPreV2Journal() throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));
 
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File ledgerDir = createTempDir("bookie", "ledger");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(ledgerDir));
 
         writePreV2Journal(Bookie.getCurrentDirectory(journalDir), 100);
@@ -346,14 +362,10 @@ public class BookieJournalTest {
 
     @Test
     public void testV4Journal() throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));
 
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File ledgerDir = createTempDir("bookie", "ledger");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(ledgerDir));
 
         writeV4Journal(Bookie.getCurrentDirectory(journalDir), 100, "testPasswd".getBytes());
@@ -380,16 +392,12 @@ public class BookieJournalTest {
 
     @Test
     public void testV5Journal() throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));
 
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File ledgerDir = createTempDir("bookie", "ledger");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(ledgerDir));
-
+        
         writeV5Journal(Bookie.getCurrentDirectory(journalDir), 2 * JournalChannel.SECTOR_SIZE,
                 "testV5Journal".getBytes());
 
@@ -422,14 +430,10 @@ public class BookieJournalTest {
      */
     @Test(timeout=60000)
     public void testAllJunkJournal() throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));
 
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File ledgerDir = createTempDir("bookie", "ledger");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(ledgerDir));
 
         writeJunkJournal(Bookie.getCurrentDirectory(journalDir));
@@ -460,14 +464,10 @@ public class BookieJournalTest {
      */
     @Test(timeout=60000)
     public void testEmptyJournal() throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));
 
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File ledgerDir = createTempDir("bookie", "ledger");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(ledgerDir));
 
         writePreV2Journal(Bookie.getCurrentDirectory(journalDir), 0);
@@ -486,14 +486,10 @@ public class BookieJournalTest {
      */
     @Test(timeout=60000)
     public void testHeaderOnlyJournal() throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));
 
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File ledgerDir = createTempDir("bookie", "ledger");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(ledgerDir));
 
         writeV2Journal(Bookie.getCurrentDirectory(journalDir), 0);
@@ -512,14 +508,10 @@ public class BookieJournalTest {
      */
     @Test(timeout=60000)
     public void testJunkEndedJournal() throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));
 
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File ledgerDir = createTempDir("bookie", "ledger");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(ledgerDir));
 
         JournalChannel jc = writeV2Journal(Bookie.getCurrentDirectory(journalDir), 0);
@@ -551,14 +543,10 @@ public class BookieJournalTest {
      */
     @Test(timeout=60000)
     public void testTruncatedInLenJournal() throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));
 
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File ledgerDir = createTempDir("bookie", "ledger");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(ledgerDir));
 
         JournalChannel jc = writeV2Journal(
@@ -599,14 +587,10 @@ public class BookieJournalTest {
      */
     @Test(timeout=60000)
     public void testTruncatedInEntryJournal() throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));
 
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File ledgerDir = createTempDir("bookie", "ledger");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(ledgerDir));
 
         JournalChannel jc = writeV2Journal(
@@ -672,15 +656,11 @@ public class BookieJournalTest {
      */
     private void testPartialFileInfoPreV3Journal(boolean truncateMasterKey)
         throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));
 
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
-        Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));
+        File ledgerDir = createTempDir("bookie", "ledger");
+        Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(ledgerDir));
 
         writePreV2Journal(Bookie.getCurrentDirectory(journalDir), 100);
         writePartialIndexFileForLedger(Bookie.getCurrentDirectory(ledgerDir),
@@ -732,14 +712,10 @@ public class BookieJournalTest {
      */
     private void testPartialFileInfoPostV3Journal(boolean truncateMasterKey)
         throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));
 
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File ledgerDir = createTempDir("bookie", "ledger");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(ledgerDir));
 
         byte[] masterKey = "testPasswd".getBytes();

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/CompactionTest.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/CompactionTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/CompactionTest.java
index ef4cea8..956595e 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/CompactionTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/CompactionTest.java
@@ -369,9 +369,7 @@ public class CompactionTest extends BookKeeperClusterTestCase {
         final Set<Long> ledgers = Collections.newSetFromMap(new ConcurrentHashMap<Long, Boolean>());
         LedgerManager manager = getLedgerManager(ledgers);
 
-        File tmpDir = File.createTempFile("bkTest", ".dir");
-        tmpDir.delete();
-        tmpDir.mkdir();
+        File tmpDir = createTempDir("bkTest", ".dir");
         File curDir = Bookie.getCurrentDirectory(tmpDir);
         Bookie.checkDirectoryStructure(curDir);
         conf.setLedgerDirNames(new String[] {tmpDir.toString()});
@@ -520,9 +518,7 @@ public class CompactionTest extends BookKeeperClusterTestCase {
     public void testWhenNoLogsToCompact() throws Exception {
         tearDown(); // I dont want the test infrastructure
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
-        File tmpDir = File.createTempFile("bkTest", ".dir");
-        tmpDir.delete();
-        tmpDir.mkdir();
+        File tmpDir = createTempDir("bkTest", ".dir");
         File curDir = Bookie.getCurrentDirectory(tmpDir);
         Bookie.checkDirectoryStructure(curDir);
         conf.setLedgerDirNames(new String[] { tmpDir.toString() });

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/CookieTest.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/CookieTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/CookieTest.java
index 27577d2..9497c29 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/CookieTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/CookieTest.java
@@ -21,6 +21,10 @@
 
 package org.apache.bookkeeper.bookie;
 
+import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
+import org.apache.bookkeeper.test.PortManager;
+import org.apache.bookkeeper.util.IOUtils;
+
 import static org.apache.bookkeeper.bookie.UpgradeTest.newV1JournalDirectory;
 import static org.apache.bookkeeper.bookie.UpgradeTest.newV1LedgerDirectory;
 import static org.apache.bookkeeper.bookie.UpgradeTest.newV2JournalDirectory;
@@ -38,44 +42,28 @@ import org.apache.bookkeeper.conf.ClientConfiguration;
 import org.apache.bookkeeper.conf.ServerConfiguration;
 import org.apache.bookkeeper.conf.TestBKConfiguration;
 import org.apache.bookkeeper.meta.ZkVersion;
-import org.apache.bookkeeper.test.PortManager;
-import org.apache.bookkeeper.test.ZooKeeperUtil;
 import org.apache.bookkeeper.versioning.Version;
 import org.apache.bookkeeper.versioning.Versioned;
 import org.apache.commons.io.FileUtils;
-import org.apache.zookeeper.ZooKeeper;
-import org.junit.After;
-import org.junit.Before;
 import org.junit.Test;
 
-public class CookieTest {
-    ZooKeeperUtil zkutil;
-    ZooKeeper zkc = null;
+public class CookieTest extends BookKeeperClusterTestCase {
     final int bookiePort = PortManager.nextFreePort();
 
-    @Before
-    public void setupZooKeeper() throws Exception {
-        zkutil = new ZooKeeperUtil();
-        zkutil.startServer();
-        zkc = zkutil.getZooKeeperClient();
-    }
-
-    @After
-    public void tearDownZooKeeper() throws Exception {
-        zkutil.killServer();
+    public CookieTest() {
+        super(0);
     }
 
-    private static String newDirectory() throws IOException {
+    private String newDirectory() throws IOException {
         return newDirectory(true);
     }
 
-    private static String newDirectory(boolean createCurDir) throws IOException {
-        File d = File.createTempFile("bookie", "tmpdir");
-        d.delete();
-        d.mkdirs();
+    private String newDirectory(boolean createCurDir) throws IOException {
+        File d = IOUtils.createTempDir("cookie", "tmpdir");
         if (createCurDir) {
             new File(d, "current").mkdirs();
         }
+        tmpDirs.add(d);
         return d.getPath();
     }
 
@@ -85,7 +73,7 @@ public class CookieTest {
     @Test(timeout=60000)
     public void testCleanStart() throws Exception {
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-            .setZkServers(zkutil.getZooKeeperConnectString())
+            .setZkServers(zkUtil.getZooKeeperConnectString())
             .setJournalDirName(newDirectory(false))
             .setLedgerDirNames(new String[] { newDirectory(false) })
             .setBookiePort(bookiePort);
@@ -114,7 +102,7 @@ public class CookieTest {
         String journalDir = newDirectory();
         String ledgerDir = newDirectory();
         ServerConfiguration conf2 = TestBKConfiguration.newServerConfiguration()
-            .setZkServers(zkutil.getZooKeeperConnectString())
+            .setZkServers(zkUtil.getZooKeeperConnectString())
             .setJournalDirName(journalDir)
             .setLedgerDirNames(new String[] { ledgerDir })
             .setBookiePort(bookiePort);
@@ -142,7 +130,7 @@ public class CookieTest {
             newDirectory(), newDirectory(), newDirectory() };
         String journalDir = newDirectory();
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-            .setZkServers(zkutil.getZooKeeperConnectString())
+            .setZkServers(zkUtil.getZooKeeperConnectString())
             .setJournalDirName(journalDir)
             .setLedgerDirNames(ledgerDirs)
             .setBookiePort(bookiePort);
@@ -183,7 +171,7 @@ public class CookieTest {
         String ledgerDir0 = newDirectory();
         String journalDir = newDirectory();
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-            .setZkServers(zkutil.getZooKeeperConnectString())
+            .setZkServers(zkUtil.getZooKeeperConnectString())
             .setJournalDirName(journalDir)
             .setLedgerDirNames(new String[] { ledgerDir0 })
             .setBookiePort(bookiePort);
@@ -215,7 +203,7 @@ public class CookieTest {
         String ledgerDir0 = newDirectory();
         String journalDir = newDirectory();
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-            .setZkServers(zkutil.getZooKeeperConnectString())
+            .setZkServers(zkUtil.getZooKeeperConnectString())
             .setJournalDirName(journalDir)
             .setLedgerDirNames(new String[] { ledgerDir0 , newDirectory() })
             .setBookiePort(bookiePort);
@@ -240,7 +228,7 @@ public class CookieTest {
     @Test(timeout=60000)
     public void testBookiePortChanged() throws Exception {
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-            .setZkServers(zkutil.getZooKeeperConnectString())
+            .setZkServers(zkUtil.getZooKeeperConnectString())
             .setJournalDirName(newDirectory())
             .setLedgerDirNames(new String[] { newDirectory() , newDirectory() })
             .setBookiePort(bookiePort);
@@ -266,7 +254,7 @@ public class CookieTest {
     @Test(timeout=60000)
     public void testNewBookieStartingWithAnotherBookiesPort() throws Exception {
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-            .setZkServers(zkutil.getZooKeeperConnectString())
+            .setZkServers(zkUtil.getZooKeeperConnectString())
             .setJournalDirName(newDirectory())
             .setLedgerDirNames(new String[] { newDirectory() , newDirectory() })
             .setBookiePort(bookiePort);
@@ -275,7 +263,7 @@ public class CookieTest {
         b.shutdown();
 
         conf = TestBKConfiguration.newServerConfiguration()
-            .setZkServers(zkutil.getZooKeeperConnectString())
+            .setZkServers(zkUtil.getZooKeeperConnectString())
             .setJournalDirName(newDirectory())
             .setLedgerDirNames(new String[] { newDirectory() , newDirectory() })
             .setBookiePort(bookiePort);
@@ -293,14 +281,14 @@ public class CookieTest {
     @Test(timeout=60000)
     public void testVerifyCookieWithFormat() throws Exception {
         ClientConfiguration adminConf = new ClientConfiguration()
-            .setZkServers(zkutil.getZooKeeperConnectString());
+            .setZkServers(zkUtil.getZooKeeperConnectString());
 
         adminConf.setProperty("bookkeeper.format", true);
         // Format the BK Metadata and generate INSTANCEID
         BookKeeperAdmin.format(adminConf, false, true);
 
         ServerConfiguration bookieConf = TestBKConfiguration.newServerConfiguration()
-                .setZkServers(zkutil.getZooKeeperConnectString())
+                .setZkServers(zkUtil.getZooKeeperConnectString())
                 .setJournalDirName(newDirectory(false))
                 .setLedgerDirNames(new String[] { newDirectory(false) })
                 .setBookiePort(bookiePort);
@@ -330,10 +318,15 @@ public class CookieTest {
      */
     @Test(timeout=60000)
     public void testV2data() throws Exception {
+        File journalDir = newV2JournalDirectory();
+        tmpDirs.add(journalDir);
+        File ledgerDir = newV2LedgerDirectory();
+        tmpDirs.add(ledgerDir);
+
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-            .setZkServers(zkutil.getZooKeeperConnectString())
-            .setJournalDirName(newV2JournalDirectory())
-            .setLedgerDirNames(new String[] { newV2LedgerDirectory() })
+            .setZkServers(zkUtil.getZooKeeperConnectString())
+            .setJournalDirName(journalDir.getPath())
+            .setLedgerDirNames(new String[] { ledgerDir.getPath() })
             .setBookiePort(bookiePort);
         try {
             Bookie b = new Bookie(conf);
@@ -350,10 +343,15 @@ public class CookieTest {
      */
     @Test(timeout=60000)
     public void testV1data() throws Exception {
+        File journalDir = newV1JournalDirectory();
+        tmpDirs.add(journalDir);
+        File ledgerDir = newV1LedgerDirectory();
+        tmpDirs.add(ledgerDir);
+
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-            .setZkServers(zkutil.getZooKeeperConnectString())
-            .setJournalDirName(newV1JournalDirectory())
-            .setLedgerDirNames(new String[] { newV1LedgerDirectory() })
+            .setZkServers(zkUtil.getZooKeeperConnectString())
+            .setJournalDirName(journalDir.getPath())
+            .setLedgerDirNames(new String[]{ledgerDir.getPath()})
             .setBookiePort(bookiePort);
         try {
             Bookie b = new Bookie(conf);
@@ -374,7 +372,7 @@ public class CookieTest {
                 newDirectory() };
         String journalDir = newDirectory();
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-                .setZkServers(zkutil.getZooKeeperConnectString())
+                .setZkServers(zkUtil.getZooKeeperConnectString())
                 .setJournalDirName(journalDir).setLedgerDirNames(ledgerDirs)
                 .setBookiePort(bookiePort);
         Bookie b = new Bookie(conf); // should work fine
@@ -398,7 +396,7 @@ public class CookieTest {
                 newDirectory() };
         String journalDir = newDirectory();
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-                .setZkServers(zkutil.getZooKeeperConnectString())
+                .setZkServers(zkUtil.getZooKeeperConnectString())
                 .setJournalDirName(journalDir).setLedgerDirNames(ledgerDirs)
                 .setBookiePort(bookiePort);
         conf.setUseHostNameAsBookieID(true);
@@ -419,10 +417,15 @@ public class CookieTest {
      */
     @Test(timeout = 60000)
     public void testV2dataWithHostNameAsBookieID() throws Exception {
+        File journalDir = newV2JournalDirectory();
+        tmpDirs.add(journalDir);
+        File ledgerDir = newV2LedgerDirectory();
+        tmpDirs.add(ledgerDir);
+
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-                .setZkServers(zkutil.getZooKeeperConnectString())
-                .setJournalDirName(newV2JournalDirectory())
-                .setLedgerDirNames(new String[] { newV2LedgerDirectory() })
+                .setZkServers(zkUtil.getZooKeeperConnectString())
+                .setJournalDirName(journalDir.getPath())
+                .setLedgerDirNames(new String[] { ledgerDir.getPath() })
                 .setBookiePort(bookiePort);
         try {
             conf.setUseHostNameAsBookieID(true);
@@ -443,7 +446,7 @@ public class CookieTest {
         String[] ledgerDirs = new String[] { newDirectory(), newDirectory(), newDirectory() };
         String journalDir = newDirectory();
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-                .setZkServers(zkutil.getZooKeeperConnectString()).setJournalDirName(journalDir)
+                .setZkServers(zkUtil.getZooKeeperConnectString()).setJournalDirName(journalDir)
                 .setLedgerDirNames(ledgerDirs).setBookiePort(bookiePort);
         Bookie b = new Bookie(conf); // should work fine
         b.start();
@@ -470,7 +473,7 @@ public class CookieTest {
         String[] ledgerDirs = new String[] { newDirectory(), newDirectory(), newDirectory() };
         String journalDir = newDirectory();
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-                .setZkServers(zkutil.getZooKeeperConnectString()).setJournalDirName(journalDir)
+                .setZkServers(zkUtil.getZooKeeperConnectString()).setJournalDirName(journalDir)
                 .setLedgerDirNames(ledgerDirs).setBookiePort(bookiePort);
         Bookie b = new Bookie(conf); // should work fine
         b.start();

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/EntryLogTest.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/EntryLogTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/EntryLogTest.java
index 8f59b54..488f4bf 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/EntryLogTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/EntryLogTest.java
@@ -25,14 +25,17 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.bookkeeper.bookie.GarbageCollectorThread.EntryLogMetadata;
 import org.apache.bookkeeper.bookie.GarbageCollectorThread.ExtractionScanner;
 import org.apache.bookkeeper.conf.ServerConfiguration;
 import org.apache.bookkeeper.conf.TestBKConfiguration;
+import org.apache.bookkeeper.util.IOUtils;
+import org.apache.commons.io.FileUtils;
 import org.junit.After;
 import org.junit.Assert;
-import org.junit.Before;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -42,15 +45,25 @@ import static org.junit.Assert.*;
 public class EntryLogTest {
     private final static Logger LOG = LoggerFactory.getLogger(EntryLogTest.class);
 
-    @Before
-    public void setUp() throws Exception {
+    final List<File> tempDirs = new ArrayList<File>();
+
+    File createTempDir(String prefix, String suffix) throws IOException {
+        File dir = IOUtils.createTempDir(prefix, suffix);
+        tempDirs.add(dir);
+        return dir;
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        for (File dir : tempDirs) {
+            FileUtils.deleteDirectory(dir);
+        }
+        tempDirs.clear();
     }
 
     @Test(timeout=60000)
     public void testCorruptEntryLog() throws Exception {
-        File tmpDir = File.createTempFile("bkTest", ".dir");
-        tmpDir.delete();
-        tmpDir.mkdir();
+        File tmpDir = createTempDir("bkTest", ".dir");
         File curDir = Bookie.getCurrentDirectory(tmpDir);
         Bookie.checkDirectoryStructure(curDir);
 
@@ -100,9 +113,7 @@ public class EntryLogTest {
 
     @Test(timeout=60000)
     public void testMissingLogId() throws Exception {
-        File tmpDir = File.createTempFile("entryLogTest", ".dir");
-        tmpDir.delete();
-        tmpDir.mkdir();
+        File tmpDir = createTempDir("entryLogTest", ".dir");
         File curDir = Bookie.getCurrentDirectory(tmpDir);
         Bookie.checkDirectoryStructure(curDir);
 
@@ -165,8 +176,7 @@ public class EntryLogTest {
     /** Test that EntryLogger Should fail with FNFE, if entry logger directories does not exist*/
     public void testEntryLoggerShouldThrowFNFEIfDirectoriesDoesNotExist()
             throws Exception {
-        File tmpDir = File.createTempFile("bkTest", ".dir");
-        tmpDir.delete();
+        File tmpDir = createTempDir("bkTest", ".dir");
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
         conf.setLedgerDirNames(new String[] { tmpDir.toString() });
         EntryLogger entryLogger = null;
@@ -188,10 +198,8 @@ public class EntryLogTest {
      */
     @Test(timeout=60000)
     public void testAddEntryFailureOnDiskFull() throws Exception {
-        File ledgerDir1 = File.createTempFile("bkTest", ".dir");
-        ledgerDir1.delete();
-        File ledgerDir2 = File.createTempFile("bkTest", ".dir");
-        ledgerDir2.delete();
+        File ledgerDir1 = createTempDir("bkTest", ".dir");
+        File ledgerDir2 = createTempDir("bkTest", ".dir");
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
         conf.setLedgerDirNames(new String[] { ledgerDir1.getAbsolutePath(),
                 ledgerDir2.getAbsolutePath() });
@@ -216,8 +224,4 @@ public class EntryLogTest {
         Assert.assertTrue(0 == generateEntry(3, 1).compareTo(ledgerStorage.getEntry(3, 1)));
     }
 
-    @After
-    public void tearDown() throws Exception {
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/LedgerCacheTest.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/LedgerCacheTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/LedgerCacheTest.java
index b62c695..19b6bcb 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/LedgerCacheTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/LedgerCacheTest.java
@@ -31,8 +31,11 @@ import org.apache.bookkeeper.conf.TestBKConfiguration;
 import org.apache.bookkeeper.meta.LedgerManagerFactory;
 import org.apache.bookkeeper.util.BookKeeperConstants;
 import org.apache.bookkeeper.util.SnapshotMap;
+import org.apache.bookkeeper.util.IOUtils;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.LinkedBlockingQueue;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.commons.io.FileUtils;
 import org.junit.After;
@@ -57,16 +60,14 @@ public class LedgerCacheTest {
     ServerConfiguration conf;
     File txnDir, ledgerDir;
 
+    private final List<File> tempDirs = new ArrayList<File>();
+
     private Bookie bookie;
 
     @Before
     public void setUp() throws Exception {
-        txnDir = File.createTempFile("ledgercache", "txn");
-        txnDir.delete();
-        txnDir.mkdir();
-        ledgerDir = File.createTempFile("ledgercache", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        txnDir = IOUtils.createTempDir("ledgercache", "txn");
+        ledgerDir = IOUtils.createTempDir("ledgercache", "ledger");
         // create current dir
         new File(ledgerDir, BookKeeperConstants.CURRENT_DIR).mkdir();
 
@@ -92,6 +93,15 @@ public class LedgerCacheTest {
         ledgerManagerFactory.uninitialize();
         FileUtils.deleteDirectory(txnDir);
         FileUtils.deleteDirectory(ledgerDir);
+        for (File dir : tempDirs) {
+            FileUtils.deleteDirectory(dir);
+        }
+    }
+
+    File createTempDir(String prefix, String suffix) throws IOException {
+        File dir = IOUtils.createTempDir(prefix, suffix);
+        tempDirs.add(dir);
+        return dir;
     }
 
     private void newLedgerCache() throws IOException {
@@ -251,10 +261,8 @@ public class LedgerCacheTest {
      */
     @Test(timeout=30000)
     public void testLedgerCacheFlushFailureOnDiskFull() throws Exception {
-        File ledgerDir1 = File.createTempFile("bkTest", ".dir");
-        ledgerDir1.delete();
-        File ledgerDir2 = File.createTempFile("bkTest", ".dir");
-        ledgerDir2.delete();
+        File ledgerDir1 = createTempDir("bkTest", ".dir");
+        File ledgerDir2 = createTempDir("bkTest", ".dir");
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
         conf.setLedgerDirNames(new String[] { ledgerDir1.getAbsolutePath(), ledgerDir2.getAbsolutePath() });
 
@@ -301,14 +309,10 @@ public class LedgerCacheTest {
     @Test(timeout=30000)
     public void testIndexPageEvictionWriteOrder() throws Exception {
         final int numLedgers = 10;
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));
 
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File ledgerDir = createTempDir("bookie", "ledger");
         Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(ledgerDir));
 
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/TestLedgerDirsManager.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/TestLedgerDirsManager.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/TestLedgerDirsManager.java
index fb48f3f..416062d 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/TestLedgerDirsManager.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/TestLedgerDirsManager.java
@@ -21,11 +21,17 @@
 package org.apache.bookkeeper.bookie;
 
 import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.commons.io.FileUtils;
 
 import org.apache.bookkeeper.bookie.LedgerDirsManager.NoWritableLedgerDirException;
 import org.apache.bookkeeper.conf.ServerConfiguration;
 import org.apache.bookkeeper.conf.TestBKConfiguration;
+import org.apache.bookkeeper.util.IOUtils;
 import org.junit.Before;
+import org.junit.After;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -39,11 +45,17 @@ public class TestLedgerDirsManager {
     File curDir;
     LedgerDirsManager dirsManager;
 
+    final List<File> tempDirs = new ArrayList<File>();
+
+    File createTempDir(String prefix, String suffix) throws IOException {
+        File dir = IOUtils.createTempDir(prefix, suffix);
+        tempDirs.add(dir);
+        return dir;
+    }
+
     @Before
     public void setUp() throws Exception {
-        File tmpDir = File.createTempFile("bkTest", ".dir");
-        tmpDir.delete();
-        tmpDir.mkdir();
+        File tmpDir = createTempDir("bkTest", ".dir");
         curDir = Bookie.getCurrentDirectory(tmpDir);
         Bookie.checkDirectoryStructure(curDir);
 
@@ -53,6 +65,14 @@ public class TestLedgerDirsManager {
         dirsManager = new LedgerDirsManager(conf, conf.getLedgerDirs());
     }
 
+    @After
+    public void tearDown() throws Exception {
+        for (File dir : tempDirs) {
+            FileUtils.deleteDirectory(dir);
+        }
+        tempDirs.clear();
+    }
+
     @Test(timeout=60000)
     public void testPickWritableDirExclusive() throws Exception {
         try {

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/UpgradeTest.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/UpgradeTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/UpgradeTest.java
index 64d994c..84af7a7 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/UpgradeTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/UpgradeTest.java
@@ -39,31 +39,19 @@ import org.apache.bookkeeper.client.LedgerHandle;
 import org.apache.bookkeeper.conf.ServerConfiguration;
 import org.apache.bookkeeper.conf.TestBKConfiguration;
 import org.apache.bookkeeper.test.PortManager;
-import org.apache.bookkeeper.test.ZooKeeperUtil;
-import org.apache.zookeeper.ZooKeeper;
-import org.junit.After;
-import org.junit.Before;
+import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
+import org.apache.bookkeeper.util.IOUtils;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class UpgradeTest {
+public class UpgradeTest extends BookKeeperClusterTestCase {
     private final static Logger LOG = LoggerFactory.getLogger(FileInfo.class);
 
-    ZooKeeperUtil zkutil;
-    ZooKeeper zkc = null;
     final static int bookiePort = PortManager.nextFreePort();
 
-    @Before
-    public void setupZooKeeper() throws Exception {
-        zkutil = new ZooKeeperUtil();
-        zkutil.startServer();
-        zkc = zkutil.getZooKeeperClient();
-    }
-
-    @After
-    public void tearDownZooKeeper() throws Exception {
-        zkutil.killServer();
+    public UpgradeTest() {
+        super(0);
     }
 
     static void writeLedgerDir(File dir,
@@ -115,23 +103,19 @@ public class UpgradeTest {
         return jc;
     }
 
-    static String newV1JournalDirectory() throws Exception {
-        File d = File.createTempFile("bookie", "tmpdir");
-        d.delete();
-        d.mkdirs();
+    static File newV1JournalDirectory() throws Exception {
+        File d = IOUtils.createTempDir("bookie", "tmpdir");
         writeJournal(d, 100, "foobar".getBytes()).close();
-        return d.getPath();
+        return d;
     }
-
-    static String newV1LedgerDirectory() throws Exception {
-        File d = File.createTempFile("bookie", "tmpdir");
-        d.delete();
-        d.mkdirs();
+              
+    static File newV1LedgerDirectory() throws Exception {
+        File d = IOUtils.createTempDir("bookie", "tmpdir");
         writeLedgerDir(d, "foobar".getBytes());
-        return d.getPath();
+        return d;
     }
 
-    static void createVersion2File(String dir) throws Exception {
+    static void createVersion2File(File dir) throws Exception {
         File versionFile = new File(dir, "VERSION");
 
         FileOutputStream fos = new FileOutputStream(versionFile);
@@ -147,14 +131,14 @@ public class UpgradeTest {
         }
     }
 
-    static String newV2JournalDirectory() throws Exception {
-        String d = newV1JournalDirectory();
+    static File newV2JournalDirectory() throws Exception {
+        File d = newV1JournalDirectory();
         createVersion2File(d);
         return d;
     }
 
-    static String newV2LedgerDirectory() throws Exception {
-        String d = newV1LedgerDirectory();
+    static File newV2LedgerDirectory() throws Exception {
+        File d = newV1LedgerDirectory();
         createVersion2File(d);
         return d;
     }
@@ -199,28 +183,35 @@ public class UpgradeTest {
 
     @Test(timeout=60000)
     public void testUpgradeV1toCurrent() throws Exception {
-        String journalDir = newV1JournalDirectory();
-        String ledgerDir = newV1LedgerDirectory();
-        testUpgradeProceedure(zkutil.getZooKeeperConnectString(), journalDir, ledgerDir);
+        File journalDir = newV1JournalDirectory();
+        tmpDirs.add(journalDir);
+        File ledgerDir = newV1LedgerDirectory();
+        tmpDirs.add(ledgerDir);
+        testUpgradeProceedure(zkUtil.getZooKeeperConnectString(), journalDir.getPath(), ledgerDir.getPath());
     }
 
     @Test(timeout=60000)
     public void testUpgradeV2toCurrent() throws Exception {
-        String journalDir = newV2JournalDirectory();
-        String ledgerDir = newV2LedgerDirectory();
-        testUpgradeProceedure(zkutil.getZooKeeperConnectString(), journalDir, ledgerDir);
+        File journalDir = newV2JournalDirectory();
+        tmpDirs.add(journalDir);
+        File ledgerDir = newV2LedgerDirectory();
+        tmpDirs.add(ledgerDir);
+        testUpgradeProceedure(zkUtil.getZooKeeperConnectString(), journalDir.getPath(), ledgerDir.getPath());
     }
 
     @Test(timeout=60000)
     public void testUpgradeCurrent() throws Exception {
-        String journalDir = newV2JournalDirectory();
-        String ledgerDir = newV2LedgerDirectory();
-        testUpgradeProceedure(zkutil.getZooKeeperConnectString(), journalDir, ledgerDir);
+        File journalDir = newV2JournalDirectory();
+        tmpDirs.add(journalDir);
+        File ledgerDir = newV2LedgerDirectory();
+        tmpDirs.add(ledgerDir);
+        testUpgradeProceedure(zkUtil.getZooKeeperConnectString(), journalDir.getPath(), ledgerDir.getPath());
+
         // Upgrade again
         ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
-            .setZkServers(zkutil.getZooKeeperConnectString())
-            .setJournalDirName(journalDir)
-            .setLedgerDirNames(new String[] { ledgerDir })
+            .setZkServers(zkUtil.getZooKeeperConnectString())
+            .setJournalDirName(journalDir.getPath())
+            .setLedgerDirNames(new String[] { ledgerDir.getPath() })
             .setBookiePort(bookiePort);
         FileSystemUpgrade.upgrade(conf); // should work fine with current directory
         Bookie b = new Bookie(conf);
@@ -233,8 +224,8 @@ public class UpgradeTest {
         PrintStream origerr = System.err;
         PrintStream origout = System.out;
 
-        File output = File.createTempFile("bookie", "stdout");
-        File erroutput = File.createTempFile("bookie", "stderr");
+        File output = IOUtils.createTempFileAndDeleteOnExit("bookie", "stdout");
+        File erroutput = IOUtils.createTempFileAndDeleteOnExit("bookie", "stderr");
         System.setOut(new PrintStream(output));
         System.setErr(new PrintStream(erroutput));
         try {
@@ -247,7 +238,7 @@ public class UpgradeTest {
                 assertTrue("Wrong exception " + iae.getMessage(),
                            iae.getMessage().contains("without configuration"));
             }
-            File f = File.createTempFile("bookie", "tmpconf");
+            File f = IOUtils.createTempFileAndDeleteOnExit("bookie", "tmpconf");
             try {
                 // test without upgrade op
                 FileSystemUpgrade.main(new String[] { "--conf", f.getPath() });

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AuditorBookieTest.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AuditorBookieTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AuditorBookieTest.java
index 6fbfdb0..b8020c6 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AuditorBookieTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AuditorBookieTest.java
@@ -160,7 +160,6 @@ public class AuditorBookieTest extends BookKeeperClusterTestCase {
         int indexOfDownBookie = bs.indexOf(auditor);
         bs.remove(indexOfDownBookie);
         bsConfs.remove(indexOfDownBookie);
-        tmpDirs.remove(indexOfDownBookie);
         List<String> children = zkc.getChildren(electionPath, false);
         for (String child : children) {
             byte[] data = zkc.getData(electionPath + '/' + child, false, null);
@@ -188,7 +187,6 @@ public class AuditorBookieTest extends BookKeeperClusterTestCase {
                 .get(indexOfDownBookie);
         bs.remove(indexOfDownBookie);
         bsConfs.remove(indexOfDownBookie);
-        tmpDirs.remove(indexOfDownBookie);
         auditorElectors.remove(addr);
         startBookie(serverConfiguration);
         // starting corresponding auditor elector

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookKeeperClusterTestCase.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookKeeperClusterTestCase.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookKeeperClusterTestCase.java
index 9662777..fbb79b0 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookKeeperClusterTestCase.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookKeeperClusterTestCase.java
@@ -46,6 +46,7 @@ import org.apache.bookkeeper.replication.AutoRecoveryMain;
 import org.apache.bookkeeper.replication.Auditor;
 import org.apache.bookkeeper.replication.ReplicationException.CompatibilityException;
 import org.apache.bookkeeper.replication.ReplicationException.UnavailableException;
+import org.apache.bookkeeper.util.IOUtils;
 import org.apache.commons.io.FileUtils;
 import org.apache.zookeeper.KeeperException;
 import org.apache.zookeeper.ZooKeeper;
@@ -108,9 +109,17 @@ public abstract class BookKeeperClusterTestCase {
         stopBKCluster();
         // stop zookeeper service
         stopZKCluster();
+        // cleanup temp dirs
+        cleanupTempDirs();
         LOG.info("Tearing down test {}", getClass());
     }
 
+    protected File createTempDir(String prefix, String suffix) throws IOException {
+        File dir = IOUtils.createTempDir(prefix, suffix);
+        tmpDirs.add(dir);
+        return dir;
+    }
+
     /**
      * Start zookeeper cluster
      *
@@ -169,16 +178,16 @@ public abstract class BookKeeperClusterTestCase {
             }
         }
         bs.clear();
+    }
+
+    protected void cleanupTempDirs() throws Exception {
         for (File f : tmpDirs) {
             FileUtils.deleteDirectory(f);
         }
     }
 
     protected ServerConfiguration newServerConfiguration() throws Exception {
-        File f = File.createTempFile("bookie", "test");
-        tmpDirs.add(f);
-        f.delete();
-        f.mkdir();
+        File f = createTempDir("bookie", "test");
 
         int port = PortManager.nextFreePort();
         return newServerConfiguration(port, zkUtil.getZooKeeperConnectString(),

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieClientTest.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieClientTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieClientTest.java
index c860bef..285cf71 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieClientTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieClientTest.java
@@ -37,6 +37,7 @@ import org.apache.bookkeeper.proto.BookieServer;
 import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback;
 import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.WriteCallback;
 import org.apache.bookkeeper.util.OrderedSafeExecutor;
+import org.apache.bookkeeper.util.IOUtils;
 import org.jboss.netty.buffer.ChannelBuffer;
 import org.jboss.netty.buffer.ChannelBuffers;
 import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
@@ -60,10 +61,7 @@ public class BookieClientTest {
 
     @Before
     public void setUp() throws Exception {
-        tmpDir = File.createTempFile("bookie", "test");
-        tmpDir.delete();
-        tmpDir.mkdir();
-
+        tmpDir = IOUtils.createTempDir("bookieClient", "test");
         // Since this test does not rely on the BookKeeper client needing to
         // know via ZooKeeper which Bookies are available, okay, so pass in null
         // for the zkServers input parameter when constructing the BookieServer.

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieZKExpireTest.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieZKExpireTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieZKExpireTest.java
index 93256a7..a3bd4d6 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieZKExpireTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieZKExpireTest.java
@@ -31,6 +31,7 @@ import org.apache.bookkeeper.conf.ServerConfiguration;
 import java.util.HashSet;
 import org.apache.bookkeeper.proto.BookieServer;
 import org.apache.bookkeeper.bookie.Bookie;
+import org.apache.bookkeeper.util.IOUtils;
 
 public class BookieZKExpireTest extends BookKeeperClusterTestCase {
 
@@ -46,9 +47,7 @@ public class BookieZKExpireTest extends BookKeeperClusterTestCase {
     public void testBookieServerZKExpireBehaviour() throws Exception {
         BookieServer server = null;
         try {
-            File f = File.createTempFile("bookieserver", "test");
-            f.delete();
-            f.mkdir();
+            File f = createTempDir("bookieserver", "test");
 
             HashSet<Thread> threadset = new HashSet<Thread>();
             int threadCount = Thread.activeCount();

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ConcurrentLedgerTest.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ConcurrentLedgerTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ConcurrentLedgerTest.java
index caf1c3d..94319df 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ConcurrentLedgerTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ConcurrentLedgerTest.java
@@ -26,6 +26,8 @@ import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.concurrent.Semaphore;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.bookkeeper.bookie.Bookie;
 import org.apache.bookkeeper.bookie.BookieException;
@@ -52,6 +54,14 @@ public class ConcurrentLedgerTest {
     int recvTimeout = 10000;
     Semaphore throttle;
     ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
+    final List<File> tempDirs = new ArrayList<File>();
+
+    private File createTempDir(String prefix, String suffix, File parent) throws IOException {
+        File dir = File.createTempFile(prefix, suffix, parent);
+        dir.delete();
+        tempDirs.add(dir);
+        return dir;
+    }
 
     @Before
     public void setUp() throws Exception {
@@ -63,11 +73,10 @@ public class ConcurrentLedgerTest {
         if (ledgerDirName != null) {
             ledgerDir = new File(ledgerDirName);
         }
-        File tmpFile = File.createTempFile("book", ".txn", txnDir);
-        tmpFile.delete();
+        File tmpFile = createTempDir("book", ".txn", txnDir);
         txnDir = new File(tmpFile.getParent(), tmpFile.getName()+".dir");
         txnDir.mkdirs();
-        tmpFile = File.createTempFile("book", ".ledger", ledgerDir);
+        tmpFile = createTempDir("book", ".ledger", ledgerDir);
         ledgerDir = new File(tmpFile.getParent(), tmpFile.getName()+".dir");
         ledgerDir.mkdirs();
 

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ReadOnlyBookieTest.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ReadOnlyBookieTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ReadOnlyBookieTest.java
index 8ccd07d..771a8a1 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ReadOnlyBookieTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ReadOnlyBookieTest.java
@@ -30,6 +30,7 @@ import org.apache.bookkeeper.client.BookKeeper.DigestType;
 import org.apache.bookkeeper.client.LedgerEntry;
 import org.apache.bookkeeper.client.LedgerHandle;
 import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.util.IOUtils;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -227,10 +228,8 @@ public class ReadOnlyBookieTest extends BookKeeperClusterTestCase {
 
         File[] ledgerDirs = new File[numOfLedgerDirs];
         for (int i = 0; i < numOfLedgerDirs; i++) {
-            File dir = File.createTempFile("bookie", "test");
+            File dir = createTempDir("bookie", "test");
             tmpDirs.add(dir);
-            dir.delete();
-            dir.mkdir();
             ledgerDirs[i] = dir;
         }
 

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/TestBackwardCompat.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/TestBackwardCompat.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/TestBackwardCompat.java
index 89939b4..5564130 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/TestBackwardCompat.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/TestBackwardCompat.java
@@ -24,17 +24,22 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.io.IOException;
 import java.io.File;
 import java.net.InetAddress;
 import java.util.Arrays;
 import java.util.Enumeration;
+import java.util.ArrayList;
+import java.util.List;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.bookkeeper.bookie.Bookie;
 import org.apache.bookkeeper.bookie.BookieException;
 import org.apache.bookkeeper.bookie.FileSystemUpgrade;
 import org.apache.bookkeeper.client.BookKeeperAdmin;
 import org.apache.bookkeeper.conf.ClientConfiguration;
 import org.apache.bookkeeper.conf.TestBKConfiguration;
+import org.apache.bookkeeper.util.IOUtils;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -54,6 +59,15 @@ public class TestBackwardCompat {
             Thread.sleep(500);
         }
     }
+
+    final List<File> tempDirs = new ArrayList<File>();
+
+    File createTempDir(String prefix, String suffix) throws IOException {
+        File dir = IOUtils.createTempDir(prefix, suffix);
+        tempDirs.add(dir);
+        return dir;
+    }
+
     @Before
     public void startZooKeeperServer() throws Exception {
         zkUtil.startServer();
@@ -62,6 +76,10 @@ public class TestBackwardCompat {
     @After
     public void stopZooKeeperServer() throws Exception {
         zkUtil.killServer();
+        for (File dir : tempDirs) {
+            FileUtils.deleteDirectory(dir);
+        }
+        tempDirs.clear();
     }
 
     /**
@@ -459,12 +477,8 @@ public class TestBackwardCompat {
      */
     @Test(timeout=60000)
     public void testOldCookieAccessingNewCluster() throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
+        File ledgerDir = createTempDir("bookie", "ledger");
 
         int port = PortManager.nextFreePort();
 
@@ -517,12 +531,8 @@ public class TestBackwardCompat {
      */
     @Test(timeout=60000)
     public void testCompat400() throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
+        File ledgerDir = createTempDir("bookie", "ledger");
 
         int port = PortManager.nextFreePort();
         // start server, upgrade
@@ -605,12 +615,8 @@ public class TestBackwardCompat {
      */
     @Test(timeout=60000)
     public void testCompat410() throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
+        File ledgerDir = createTempDir("bookie", "ledger");
 
         int port = PortManager.nextFreePort();
         // start server, upgrade
@@ -727,12 +733,8 @@ public class TestBackwardCompat {
      */
     @Test(timeout = 60000)
     public void testCompatReads() throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
+        File ledgerDir = createTempDir("bookie", "ledger");
 
         int port = PortManager.nextFreePort();
         // start server, upgrade
@@ -781,12 +783,8 @@ public class TestBackwardCompat {
      */
     @Test(timeout = 60000)
     public void testCompatWrites() throws Exception {
-        File journalDir = File.createTempFile("bookie", "journal");
-        journalDir.delete();
-        journalDir.mkdir();
-        File ledgerDir = File.createTempFile("bookie", "ledger");
-        ledgerDir.delete();
-        ledgerDir.mkdir();
+        File journalDir = createTempDir("bookie", "journal");
+        File ledgerDir = createTempDir("bookie", "ledger");
 
         int port = PortManager.nextFreePort();
         // start server, upgrade

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java
index fd61d2d..769abbc 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java
@@ -26,6 +26,7 @@ import java.io.IOException;
 
 import java.net.InetSocketAddress;
 
+import org.apache.bookkeeper.util.IOUtils;
 import org.apache.bookkeeper.util.ZkUtils;
 import org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase;
 import org.apache.commons.io.FileUtils;
@@ -76,9 +77,7 @@ public class ZooKeeperUtil {
         LOG.debug("Running ZK server");
         // ServerStats.registerAsConcrete();
         ClientBase.setupTestEnv();
-        ZkTmpDir = File.createTempFile("zookeeper", "test");
-        ZkTmpDir.delete();
-        ZkTmpDir.mkdir();
+        ZkTmpDir = IOUtils.createTempDir("zookeeper", "test");
 
         // start the server and client.
         restartServer();

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/0e3d95c9/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/TestDiskChecker.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/TestDiskChecker.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/TestDiskChecker.java
index c0bdfa6..ac1cb9f 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/TestDiskChecker.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/TestDiskChecker.java
@@ -21,10 +21,14 @@ import static org.junit.Assert.assertTrue;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.bookkeeper.util.DiskChecker.DiskErrorException;
 import org.apache.bookkeeper.util.DiskChecker.DiskOutOfSpaceException;
 import org.apache.bookkeeper.util.DiskChecker.DiskWarnThresholdException;
+import org.apache.commons.io.FileUtils;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -36,17 +40,33 @@ public class TestDiskChecker {
 
     DiskChecker diskChecker;
 
+    final List<File> tempDirs = new ArrayList<File>();
+
     @Before
     public void setup() {
         diskChecker = new DiskChecker(0.95f, 0.95f);
     }
 
+    @After
+    public void tearDown() throws Exception {
+        for (File dir : tempDirs) {
+            FileUtils.deleteDirectory(dir);
+        }
+        tempDirs.clear();
+    }
+
+    File createTempDir(String prefix, String suffix) throws IOException {
+        File dir = IOUtils.createTempDir(prefix, suffix);
+        tempDirs.add(dir);
+        return dir;
+    }
+
     /**
      * Check the disk full
      */
     @Test(expected = DiskOutOfSpaceException.class)
     public void testCheckDiskFull() throws IOException {
-        File file = File.createTempFile("DiskCheck", "test");
+        File file = createTempDir("DiskCheck", "test");
         long usableSpace = file.getUsableSpace();
         long totalSpace = file.getTotalSpace();
         float threshold =
@@ -57,7 +77,7 @@ public class TestDiskChecker {
 
     @Test(expected = DiskWarnThresholdException.class)
     public void testDiskWarnThresholdException() throws IOException {
-        File file = File.createTempFile("DiskCheck", "test");
+        File file = createTempDir("DiskCheck", "test");
         long usableSpace = file.getUsableSpace();
         long totalSpace = file.getTotalSpace();
         float diskSpaceThreshold =
@@ -74,7 +94,7 @@ public class TestDiskChecker {
      */
     @Test(timeout = 30000, expected = DiskOutOfSpaceException.class)
     public void testCheckDiskFullOnNonExistFile() throws IOException {
-        File file = File.createTempFile("DiskCheck", "test");
+        File file = createTempDir("DiskCheck", "test");
         long usableSpace = file.getUsableSpace();
         long totalSpace = file.getTotalSpace();
         float threshold = (1f - ((float) usableSpace / (float) totalSpace)) - 0.05f;
@@ -88,9 +108,7 @@ public class TestDiskChecker {
      */
     @Test(timeout = 30000, expected = DiskErrorException.class)
     public void testCheckDiskErrorForFile() throws Exception {
-        File parent = File.createTempFile("DiskCheck", "test");
-        parent.delete();
-        parent.mkdir();
+        File parent = createTempDir("DiskCheck", "test");
         File child = File.createTempFile("DiskCheck", "test", parent);
         diskChecker.checkDir(child);
     }
@@ -100,9 +118,7 @@ public class TestDiskChecker {
      */
     @Test(timeout=60000)
     public void testCheckDiskErrorForDir() throws Exception {
-        File parent = File.createTempFile("DiskCheck", "test");
-        parent.delete();
-        parent.mkdir();
+        File parent = createTempDir("DiskCheck", "test");
         File child = File.createTempFile("DiskCheck", "test", parent);
         child.delete();
         child.mkdir();


Mime
View raw message