From 98d0032a9d00e3c8a309e905e397fe38be6f76cc Mon Sep 17 00:00:00 2001 From: tison Date: Thu, 10 Oct 2019 21:29:47 +0800 Subject: [PATCH] ZOOKEEPER-3571: Ensure test base directory before tests --- .../java/org/apache/zookeeper/ZKTestCase.java | 21 ++++++++++++++++++- .../org/apache/zookeeper/test/ClientBase.java | 7 +++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ZKTestCase.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ZKTestCase.java index b256a7a37a4..d9d481aa24d 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/ZKTestCase.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/ZKTestCase.java @@ -18,8 +18,11 @@ package org.apache.zookeeper; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.io.File; import java.time.LocalDateTime; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.rules.TestWatcher; import org.junit.runner.Description; @@ -33,10 +36,10 @@ * Basic utilities shared by all tests. Also logging of various events during * the test execution (start/stop/success/failure/etc...) */ -@SuppressWarnings("deprecation") @RunWith(JUnit4ZKTestRunner.class) public class ZKTestCase { + protected static final File testBaseDir = new File(System.getProperty("build.test.dir", "build")); private static final Logger LOG = LoggerFactory.getLogger(ZKTestCase.class); private String testName; @@ -45,6 +48,22 @@ protected String getTestName() { return testName; } + @BeforeClass + public static void before() { + if (!testBaseDir.exists()) { + assertTrue( + "Cannot properly create test base directory " + testBaseDir.getAbsolutePath(), + testBaseDir.mkdirs()); + } else if (!testBaseDir.isDirectory()) { + assertTrue( + "Cannot properly delete file with duplicate name of test base directory " + testBaseDir.getAbsolutePath(), + testBaseDir.delete()); + assertTrue( + "Cannot properly create test base directory " + testBaseDir.getAbsolutePath(), + testBaseDir.mkdirs()); + } + } + @Rule public TestWatcher watchman = new TestWatcher() { diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java index ef3291fbc8e..4235b6b6349 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java @@ -70,7 +70,6 @@ public abstract class ClientBase extends ZKTestCase { protected static final Logger LOG = LoggerFactory.getLogger(ClientBase.class); public static int CONNECTION_TIMEOUT = 30000; - static final File BASETEST = new File(System.getProperty("build.test.dir", "build")); protected String hostPort = "127.0.0.1:" + PortAssignment.unique(); protected int maxCnxns = 0; @@ -344,11 +343,11 @@ static void verifyThreadTerminated(Thread thread, long millis) throws Interrupte } public static File createEmptyTestDir() throws IOException { - return createTmpDir(BASETEST, false); + return createTmpDir(testBaseDir, false); } public static File createTmpDir() throws IOException { - return createTmpDir(BASETEST, true); + return createTmpDir(testBaseDir, true); } static File createTmpDir(File parentDir, boolean createInitFile) throws IOException { @@ -495,7 +494,7 @@ protected void setUpWithServerId(int serverId) throws Exception { setUpAll(); - tmpDir = createTmpDir(BASETEST, true); + tmpDir = createTmpDir(testBaseDir, true); startServer(serverId);