Commit 594a7f94 authored by Daniel Bluhm's avatar Daniel Bluhm
Browse files

Implement basic tests for TestData


Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent 2b3da9de
......@@ -40,5 +40,11 @@
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -11,8 +11,11 @@
package org.eclipse.ice.tests.data;
import java.io.File;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
/**
* Utility methods for retrieving data from the test data directory.
......@@ -22,32 +25,64 @@ public class TestData {
/**
* Path of the default test data directory.
*/
public static final Path DEFAULT_TEST_DATA_DIR =
static final Path DEFAULT_TEST_DATA_DIR =
Path.of(System.getProperty("user.home")).resolve("ICETests");
/**
* Environment variable for test data directory location.
*/
public static final String TEST_DATA_DIR_ENV_VAR = "TEST_DATA_DIR";
static final String TEST_DATA_DIR_ENV_VAR = "TEST_DATA_DIR";
/**
* Default test data directory.
*/
private Path defaultTestDataDir = DEFAULT_TEST_DATA_DIR;
/**
* FileSystem on which paths are retrieved.
*/
private FileSystem fs = FileSystems.getDefault();
/**
* Environment from which an alternate test data dir will be potentially
* pulled.
*/
private Map<String, String> env = System.getenv();
/**
* No args constructor.
*/
public TestData() { }
/**
* Constructor for use with a different FS and environment. Primarily for
* testing.
* @param fs alternate FileSystem
* @param env alternate environment
*/
public TestData(FileSystem fs, Map<String, String> env) {
this.fs = fs;
this.env = env;
this.defaultTestDataDir = fs.getPath(DEFAULT_TEST_DATA_DIR.toString());
}
/**
* Return a path to the specified test data file.
* @param filename for which a path will be returned.
* @return the path to the file within the ICE test data directory.
*/
public static Path path(String filename) {
public Path resolve(String filename) {
Path returnValue = null;
Path testDataDir = null;
String alternateTestDataDir = System.getenv(TEST_DATA_DIR_ENV_VAR);
String alternateTestDataDir = env.get(TEST_DATA_DIR_ENV_VAR);
if (alternateTestDataDir == null) {
testDataDir = DEFAULT_TEST_DATA_DIR;
testDataDir = defaultTestDataDir;
} else {
testDataDir = Path.of(alternateTestDataDir);
testDataDir = fs.getPath(alternateTestDataDir);
}
File testDataDirFile = testDataDir.toFile();
if (testDataDirFile.exists() && testDataDirFile.isDirectory()) {
if (Files.exists(testDataDir) && Files.isDirectory(testDataDir)) {
returnValue = testDataDir.resolve(filename);
}
return returnValue;
......
......@@ -13,21 +13,61 @@ package org.eclipse.ice.tests.data;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
/**
* Test TestData utility class.
* @author Daniel Bluhm
*/
class TestDataTest {
public FileSystem inMemFSWithDirs(String... dirs) throws IOException {
FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
for (String dir : dirs) {
Path toMake = fs.getPath(dir);
Files.createDirectories(toMake);
}
return fs;
}
@Test
void testDefaultTestDataDir() throws IOException {
TestData data = new TestData(inMemFSWithDirs(
TestData.DEFAULT_TEST_DATA_DIR.toString()
), Collections.emptyMap());
assertEquals(
TestData.DEFAULT_TEST_DATA_DIR.resolve("test").toString(),
data.resolve("test").toString()
);
}
@Test
void testDefaultTestDataDir() {
fail("Not yet implemented");
void testOverriddenTestDataDir() throws IOException {
final String alt = "/home/test/test_data";
TestData data = new TestData(inMemFSWithDirs(
TestData.DEFAULT_TEST_DATA_DIR.toString(),
alt
), Map.of(TestData.TEST_DATA_DIR_ENV_VAR, alt));
assertEquals(
alt + "/test",
data.resolve("test").toString()
);
}
@Test
void testOverriddenTestDataDir() {
fail("Not yet implemented");
void testNonExistentReturnsNull() throws IOException {
final String unlikelyFile = "a_file_that_could_not_possibly_exist";
TestData data = new TestData();
assertNull(data.resolve(unlikelyFile));
}
}
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