DBZ-963 Removing hard-coded references to "target" dir;

Also injecting actual build dir via system variable.
Making DATA_DIR a simple string.
This commit is contained in:
Gunnar Morling 2018-11-08 09:44:25 +01:00
parent 3401cd23ea
commit 92704ce394
4 changed files with 53 additions and 45 deletions

View File

@ -4,6 +4,7 @@ Andreas Bergmeier
Andras Istvan Nagy
Andrew Tongen
Andrey Pustovetov
Anton Martynov
Attila Szucs
Barry LaFond
Ben Williams

View File

@ -5,6 +5,9 @@
*/
package io.debezium.util;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@ -19,16 +22,12 @@
import org.fest.assertions.Fail;
import org.junit.Before;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
import io.debezium.util.Stopwatch.Statistics;
import io.debezium.util.Stopwatch.StopwatchSet;
/**
* A set of utility methods for test cases.
*
*
* @author Randall Hauch
*/
public interface Testing {
@ -118,7 +117,7 @@ public static interface Network {
/**
* Find a port that is available. This method starts a {@link ServerSocket} and obtains the port on which the socket is
* listening, and then shuts down the socket so the port becomes available.
*
*
* @return the number of the now-available port
*/
public static int getAvailablePort() {
@ -132,9 +131,28 @@ public static int getAvailablePort() {
*/
public static interface Files {
public static final String DBZ_TEST_DATA_DIR_ENV_VAR_NAME = "DBZ_TEST_DATA_DIR";
public static final String DBZ_TEST_DATA_DIR_SYSTEM_PROPERTY_KEY = "dbz.test.data.dir";
static final String DATA_DIR = determineTestDataDir();
static String determineTestDataDir() {
String value = System.getProperty(DBZ_TEST_DATA_DIR_SYSTEM_PROPERTY_KEY);
if (value != null && (value = value.trim()).length() > 0) {
return value;
}
value = System.getenv(DBZ_TEST_DATA_DIR_ENV_VAR_NAME);
if (value != null && (value = value.trim()).length() > 0) {
return value;
}
return "target/data"; // default value
}
/**
* Obtain an InputStream to a named resource on the given classpath.
*
*
* @param pathOnClasspath the path of the resource on the classpath
* @param testClass the test class, used for accessing the class loader
* @return the string representation
@ -147,7 +165,7 @@ public static InputStream readResourceAsStream(String pathOnClasspath, Class<?>
/**
* Obtain an InputStream to a named resource on the classpath used to load this {@link Testing} class.
*
*
* @param pathOnClasspath the path of the resource on the classpath
* @return the string representation
*/
@ -157,7 +175,7 @@ public static InputStream readResourceAsStream(String pathOnClasspath) {
/**
* Read a classpath resource into a string.
*
*
* @param pathOnClasspath the path of the resource on the classpath
* @return the string representation
*/
@ -172,7 +190,7 @@ public static String readResourceAsString(String pathOnClasspath) {
/**
* Create a directory within the test data directory at the given relative path.
*
*
* @param relativePath the path of the directory within the test data directory; may not be null
* @return the reference to the existing readable and writable directory
*/
@ -186,27 +204,12 @@ public static File createTestingDirectory(String relativePath) {
* This value can be overridden by value of the {@code DBZ_TEST_DATA_DIR} system or environment variable.
*/
static String dataDir() {
if (DATA_DIR[0] != null) {
return DATA_DIR[0];
}
String value = System.getProperty("DBZ_TEST_DATA_DIR");
if (value != null && (value = value.trim()).length() > 0) {
return DATA_DIR[0] = value;
}
value = System.getenv("DBZ_TEST_DATA_DIR");
if (value != null && (value = value.trim()).length() > 0) {
return DATA_DIR[0] = value;
}
return DATA_DIR[0] = "target/data"; // default value
return DATA_DIR;
}
String[] DATA_DIR = {null};
/**
* Create a randomly-named file within the test data directory.
*
*
* @return the reference to the existing readable and writable file
*/
public static File createTestingFile() {
@ -215,7 +218,7 @@ public static File createTestingFile() {
/**
* Create a file within the test data directory at the given relative path.
*
*
* @param relativePath the path of the file within the test data directory; may not be null
* @return the reference to the existing readable and writable file
*/
@ -226,7 +229,7 @@ public static File createTestingFile(String relativePath) {
/**
* Create a file within the test data directory at the given relative path.
*
*
* @param relativePath the path of the file within the test data directory; may not be null
* @return the reference to the existing readable and writable file
*/
@ -240,7 +243,7 @@ public static File createTestingFile(Path relativePath) {
/**
* Create the path to a file within the test data directory at the given relative path.
*
*
* @param relativePath the path of the file within the test data directory; may not be null
* @return the reference to the existing readable and writable file
*/
@ -250,7 +253,7 @@ public static Path createTestingPath(String relativePath) {
/**
* Create a directory within the test data directory at the given relative path.
*
*
* @param relativePath the path of the directory within the test data directory; may not be null
* @param removeExistingContent true if any existing content should be removed
* @return the reference to the existing readable and writable directory
@ -264,7 +267,7 @@ public static File createTestingDirectory(String relativePath, boolean removeExi
/**
* A method that will delete a file or folder only if it is within the 'target' directory (for safety).
* Folders are removed recursively.
*
*
* @param path the path to the file or folder in the target directory
*/
public static void delete(String path) {
@ -274,7 +277,7 @@ public static void delete(String path) {
/**
* A method that will delete a file or folder only if it is within the 'target' directory (for safety).
* Folders are removed recursively.
*
*
* @param fileOrFolder the file or folder in the target directory
*/
public static void delete(File fileOrFolder) {
@ -284,7 +287,7 @@ public static void delete(File fileOrFolder) {
/**
* A method that will delete a file or folder only if it is within the 'target' directory (for safety).
* Folders are removed recursively.
*
*
* @param path the path to the file or folder in the target directory
*/
public static void delete(Path path) {
@ -304,7 +307,7 @@ public static void delete(Path path) {
/**
* Verify that the supplied file or directory is within the test data directory.
*
*
* @param file the file or directory; may not be null
* @return true if inside the test data directory, or false otherwise
*/
@ -314,7 +317,7 @@ public static boolean inTestDataDir(File file) {
/**
* Verify that the supplied file or directory is within the test data directory.
*
*
* @param path the path to the file or directory; may not be null
* @return true if inside the test data directory, or false otherwise
*/

View File

@ -5,31 +5,31 @@
*/
package io.debezium.util;
import static org.fest.assertions.Assertions.assertThat;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Test;
import static org.fest.assertions.Assertions.assertThat;
public class TestingTest implements Testing {
@Test
public void shouldKnowDirectoriesInsideTestData() {
assertThat(Testing.Files.inTestDataDir(new File("target/data/somefile").toPath())).isTrue();
assertThat(Testing.Files.inTestDataDir(Paths.get(Testing.Files.dataDir(), "somefile"))).isTrue();
assertThat(Testing.Files.inTestDataDir(new File("../debezium").toPath())).isFalse();
}
@Test
public void shouldRemoveDirectory() throws Exception {
Path path = Paths.get("target/data/test-dir");
Path path = Paths.get(Testing.Files.dataDir(), "test-dir");
assertThat(path.toFile().mkdirs()).isTrue();
Path file = Paths.get("target/data/test-dir/file.txt");
Path file = path.resolve("file.txt");
assertThat(file.toFile().createNewFile()).isTrue();
Testing.Files.delete(path);
// todo: assert that 'target/data/test-dir' is removed
assertThat(java.nio.file.Files.exists(path)).isFalse();
}
}

View File

@ -565,12 +565,16 @@
<systemProperties combine.children="append">
<property>
<name>java.io.tmpdir</name>
<value>${basedir}/target</value>
<value>${project.build.directory}</value>
</property>
<property>
<name>skipLongRunningTests</name>
<value>${skipLongRunningTests}</value>
</property>
<property>
<name>dbz.test.data.dir</name>
<value>${project.build.directory}/data</value>
</property>
</systemProperties>
<argLine>-Djava.awt.headless=true ${debug.argline} ${modules.argline}</argLine>
<!--runOrder>alphabetical</runOrder-->