Commit 711d9a6c authored by Daniel Bluhm's avatar Daniel Bluhm

Fix test utilities

Closes #469 (hopefully)
Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent da70bee9
......@@ -6,9 +6,10 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
......@@ -18,17 +19,15 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.ice.tests.data</name>
<name>org.eclipse.ice.tests.util.data</name>
<comment></comment>
<projects>
</projects>
......
......@@ -55,20 +55,13 @@ public class TestConfig extends Properties {
*/
public void load(String propertyFilename) {
Path propPath = dataPath.resolve(propertyFilename);
if (dataPath.exists(propPath)) {
try(InputStream propStream = dataPath.input(propPath)) {
super.load(propStream);
} catch (IOException e) {
logger.warn(
"Could not load properties from {}: {}",
propPath.toString(),
e.getMessage()
);
}
} else {
try(InputStream propStream = dataPath.input(propPath)) {
super.load(propStream);
} catch (IOException e) {
logger.warn(
"Could not find properties file {} in test data directory",
propPath.toString()
"Could not load properties from {}:",
propPath,
e
);
}
}
......
......@@ -58,7 +58,7 @@ public class TestDataPath {
* No args constructor.
*/
public TestDataPath() {
fs = FileSystems.getDefault();
this.fs = FileSystems.getDefault();
this.testDataDir = determineTestDataDirectory(System.getenv());
}
......@@ -87,6 +87,22 @@ public class TestDataPath {
}
}
/**
* Return the testDataDirectory as string.
*/
@Override
public String toString() {
return this.testDataDir.toString();
}
/**
* Get the test data directory.
* @return test data directory
*/
public Path getTestDataDirectory() {
return this.testDataDir;
}
/**
* Create the test data directory if it doesn't already exist.
* @throws IOException on file errors
......
......@@ -38,11 +38,36 @@ class TestConfigTest {
@Test
void testLoadAndAccess() throws IOException {
TestDataPath dataPath = TestDataPathTest.inMemTestDataPath(null);
TestDataPath dataPath = TestDataPathTest.inMemTestDataPath();
TestConfig config = new TestConfig(dataPath);
setupPropFile(dataPath);
config.load(PROP_FILENAME);
String test = config.getProperty("test");
assertEquals("abcd1234", test);
assertEquals("abcd1234", config.getProperty("test"));
}
@Test
void testExceptionNotThrownOnFileNotExist() throws IOException {
TestDataPath dataPath = TestDataPathTest.inMemTestDataPath();
TestConfig config = new TestConfig(dataPath);
config.load(PROP_FILENAME);
assertEquals("default", config.getProperty("test", "default"));
}
@Test
void testExceptionNotThrownOnBadPropFile() throws IOException {
TestDataPath dataPath = TestDataPathTest.inMemTestDataPath();
TestConfig config = new TestConfig(dataPath);
dataPath.create();
try(PrintWriter writer = new PrintWriter(dataPath.writer(PROP_FILENAME))) {
writer.println("Not valid property contents");
}
config.load(PROP_FILENAME);
assertEquals("default", config.getProperty("test", "default"));
}
@Test
void testFrom() {
TestConfig config = TestConfig.from("");
assertEquals("default", config.getProperty("test", "default"));
}
}
......@@ -33,18 +33,20 @@ import com.google.common.jimfs.Jimfs;
*/
class TestDataPathTest {
private static final String TEST_DATA_PATH = "/home/test/test_data";
private static final Map<String, String> ALTERNATE_ENVIRONMENT = Map.of(
TestDataPath.TEST_DATA_PATH_ENV_VAR, TEST_DATA_PATH
);
public static TestDataPath inMemTestDataPath(
Map<String, String> env, String... dirs
String... dirs
) throws IOException {
FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
for (String dir : dirs) {
Path toMake = fs.getPath(dir);
Files.createDirectories(toMake);
}
if (env == null) {
env = Collections.emptyMap();
}
return new TestDataPath(fs, env);
return new TestDataPath(fs, ALTERNATE_ENVIRONMENT);
}
@Test
......@@ -57,29 +59,35 @@ class TestDataPathTest {
}
@Test
void testResolve() throws IOException {
TestDataPath data = inMemTestDataPath(null);
void testDefaultInPlaceWhenNoOverrideGiven() throws IOException {
TestDataPath data = new TestDataPath();
assertEquals(
TestDataPath.DEFAULT_TEST_DATA_DIR.resolve("test").toString(),
data.resolve("test").toString()
TestDataPath.DEFAULT_TEST_DATA_DIR,
data.getTestDataDirectory()
);
}
@Test
void testResolveWithOverridenDefault() throws IOException {
final Path alt = Path.of("/home/test/test_data");
TestDataPath data = inMemTestDataPath(
Map.of(TestDataPath.TEST_DATA_PATH_ENV_VAR, alt.toString())
void testOverridenDefault() throws IOException {
TestDataPath data = inMemTestDataPath();
assertEquals(
TEST_DATA_PATH,
data.toString()
);
}
@Test
void testResolve() throws IOException {
TestDataPath data = inMemTestDataPath();
assertEquals(
alt.resolve("test").toString(),
TEST_DATA_PATH + "/test",
data.resolve("test").toString()
);
}
@Test
void testCreate() throws IOException {
TestDataPath data = inMemTestDataPath(null);
TestDataPath data = inMemTestDataPath();
data.create();
assertTrue(Files.exists(data.resolve("")));
}
......@@ -87,14 +95,14 @@ class TestDataPathTest {
@Test
void testCreateDirString() throws IOException {
final String test = "test";
TestDataPath data = inMemTestDataPath(null);
TestDataPath data = inMemTestDataPath();
data.createDir(test);
assertTrue(Files.exists(data.resolve(test)));
}
@Test
void testCreateDirPath() throws IOException {
TestDataPath data = inMemTestDataPath(null);
TestDataPath data = inMemTestDataPath();
final Path test = data.resolve("test");
data.createDir(test);
assertTrue(Files.exists(test));
......@@ -102,56 +110,54 @@ class TestDataPathTest {
@Test
void testExists() throws IOException {
TestDataPath data = inMemTestDataPath(
null, TestDataPath.DEFAULT_TEST_DATA_DIR.toString()
);
TestDataPath data = inMemTestDataPath();
assertFalse(data.exists());
Files.createDirectories(data.getTestDataDirectory());
assertTrue(data.exists());
}
@Test
void testExistsString() throws IOException {
TestDataPath data = inMemTestDataPath(
null,
TestDataPath.DEFAULT_TEST_DATA_DIR.resolve("test").toString()
);
TestDataPath data = inMemTestDataPath();
assertFalse(data.exists("test"));
Files.createDirectories(data.getTestDataDirectory().resolve("test"));
assertTrue(data.exists("test"));
}
@Test
void testExistsPath() throws IOException {
TestDataPath data = inMemTestDataPath(
null,
TestDataPath.DEFAULT_TEST_DATA_DIR.resolve("test").toString()
);
TestDataPath data = inMemTestDataPath();
final Path test = data.resolve("test");
assertFalse(data.exists(test));
Files.createDirectories(data.getTestDataDirectory().resolve(test));
assertTrue(data.exists(test));
}
@Test
void testInputString() throws IOException {
TestDataPath data = inMemTestDataPath(null);
TestDataPath data = inMemTestDataPath();
assertThrows(IOException.class, () -> {data.input("test");});
data.create();
Files.createFile(data.resolve("test"));
Files.createFile(data.getTestDataDirectory().resolve("test"));
InputStream input = data.input("test");
assertEquals(0, input.available());
}
@Test
void testInputPath() throws IOException {
TestDataPath data = inMemTestDataPath(null);
TestDataPath data = inMemTestDataPath();
assertThrows(IOException.class, () -> {
data.input(data.resolve("test"));
data.input(data.getTestDataDirectory().resolve("test"));
});
data.create();
Files.createFile(data.resolve("test"));
InputStream input = data.input(data.resolve("test"));
Files.createFile(data.getTestDataDirectory().resolve("test"));
InputStream input = data.input(data.getTestDataDirectory().resolve("test"));
assertEquals(0, input.available());
}
@Test
void testOutputString() throws IOException {
TestDataPath data = inMemTestDataPath(null);
TestDataPath data = inMemTestDataPath();
assertThrows(IOException.class, () -> {
data.output("test");
});
......@@ -162,40 +168,40 @@ class TestDataPathTest {
@Test
void testOutputPath() throws IOException {
TestDataPath data = inMemTestDataPath(null);
TestDataPath data = inMemTestDataPath();
assertThrows(IOException.class, () -> {
data.output(data.resolve("test"));
data.output(data.getTestDataDirectory().resolve("test"));
});
data.create();
data.output(data.resolve("test"));
Files.exists(data.resolve("test"));
data.output(data.getTestDataDirectory().resolve("test"));
Files.exists(data.getTestDataDirectory().resolve("test"));
}
@Test
void testReaderString() throws IOException {
TestDataPath data = inMemTestDataPath(null);
TestDataPath data = inMemTestDataPath();
assertThrows(IOException.class, () -> {data.reader("test");});
data.create();
Files.createFile(data.resolve("test"));
Files.createFile(data.getTestDataDirectory().resolve("test"));
Reader reader = data.reader("test");
assertEquals(-1, reader.read());
}
@Test
void testReaderPath() throws IOException {
TestDataPath data = inMemTestDataPath(null);
TestDataPath data = inMemTestDataPath();
assertThrows(IOException.class, () -> {
data.reader(data.resolve("test"));
data.reader(data.getTestDataDirectory().resolve("test"));
});
data.create();
Files.createFile(data.resolve("test"));
Reader reader = data.reader(data.resolve("test"));
Files.createFile(data.getTestDataDirectory().resolve("test"));
Reader reader = data.reader(data.getTestDataDirectory().resolve("test"));
assertEquals(-1, reader.read());
}
@Test
void testWriterString() throws IOException {
TestDataPath data = inMemTestDataPath(null);
TestDataPath data = inMemTestDataPath();
assertThrows(IOException.class, () -> {
data.writer("test");
});
......@@ -206,12 +212,12 @@ class TestDataPathTest {
@Test
void testWriterPath() throws IOException {
TestDataPath data = inMemTestDataPath(null);
TestDataPath data = inMemTestDataPath();
assertThrows(IOException.class, () -> {
data.writer(data.resolve("test"));
data.writer(data.getTestDataDirectory().resolve("test"));
});
data.create();
data.writer(data.resolve("test"));
Files.exists(data.resolve("test"));
data.writer(data.getTestDataDirectory().resolve("test"));
Files.exists(data.getTestDataDirectory().resolve("test"));
}
}
# SLF4J's SimpleLogger configuration file
# Simple implementation of Logger that sends all enabled log messages, for all
# defined loggers, to System.err.
# Default logging detail level for all instances of SimpleLogger.
# Must be one of ("trace", "debug", "info", "warn", or "error").
# If not specified, defaults to "info".
org.slf4j.simpleLogger.defaultLogLevel=error
# Logging detail level for a SimpleLogger instance named "xxxxx".
# Must be one of ("trace", "debug", "info", "warn", or "error").
# If not specified, the default logging detail level is used.
#org.slf4j.simpleLogger.log.xxxxx=
# Set to true if you want the current date and time to be included in output
# messages. Default is false, and will output the number of milliseconds elapsed
# since startup.
#org.slf4j.simpleLogger.showDateTime=false
# The date and time format to be used in the output messages. The pattern
# describing the date and time format is the same that is used in
# java.text.SimpleDateFormat. If the format is not specified or is invalid, the
# default format is used.
# The default format is yyyy-MM-dd HH:mm:ss:SSS Z.
#org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss:SSS Z
# Set to true if you want to output the current thread name.
# Defaults to true.
#org.slf4j.simpleLogger.showThreadName=true
# Set to true if you want the Logger instance name to be included in output
# messages. Defaults to true.
#org.slf4j.simpleLogger.showLogName=true
# Set to true if you want the last component of the name to be included in
# output messages. Defaults to false.
#org.slf4j.simpleLogger.showShortLogName=false
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment