Commit 89bffbcc authored by Daniel Bluhm's avatar Daniel Bluhm
Browse files

Rework TestData into TestDataPath, Add TestConfig



Cleaner division of responsiblities, TestDataPath is essentially a more
convenient hook into the java.nio classes
Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent 75af2bba
/*******************************************************************************
* Copyright (c) 2020- UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Daniel Bluhm - Initial implementation
*******************************************************************************/
package org.eclipse.ice.tests.data;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Testing utility for loading configuration files from test data directory.
* @author Daniel Bluhm
*/
public class TestConfig extends Properties {
/**
* Serial ID.
*/
private static final long serialVersionUID = -4388914400856890552L;
/**
* Logger.
*/
public static final Logger logger = LoggerFactory.getLogger(TestConfig.class);
/**
* Test data directory path resolver.
*/
private TestDataPath dataPath;
public TestConfig(TestDataPath dataPath) {
this.dataPath = dataPath;
}
/**
* Retrieve a test configuration from the test data directory and load the
* properties contained therein.
* @param propertyFilename name of the property file to load.
* @return loaded Properties, empty if any errors occur.
*/
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 {
logger.warn(
"Could not find properties file {} in test data directory",
propPath.toString()
);
}
}
/**
* Load a test config from the test data directory
* @param propertyFilename
* @return
*/
public static TestConfig from(String propertyFilename) {
TestConfig config = new TestConfig(new TestDataPath());
config.load(propertyFilename);
return config;
}
}
\ No newline at end of file
/*******************************************************************************
* Copyright (c) 2020- UT-Battelle, LLC. All rights reserved. This program and
* the accompanying materials are made available under the terms of the Eclipse
* Public License v1.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Daniel Bluhm - Initial implementation
*******************************************************************************/
package org.eclipse.ice.tests.data;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
/**
* Utility methods for retrieving data from the test data directory.
* @author Daniel Bluhm
*/
public class TestData {
/**
* Path of the default test data directory.
*/
static final Path DEFAULT_TEST_DATA_DIR =
Path.of(System.getProperty("user.home")).resolve("ICETests");
/**
* Environment variable for test data directory location.
*/
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 or null
* if the file does not exist.
*/
public Optional<Path> resolve(String filename) {
Optional<Path> returnValue = Optional.empty();
Path testDataDir = null;
String alternateTestDataDir = env.get(TEST_DATA_DIR_ENV_VAR);
if (alternateTestDataDir == null) {
testDataDir = defaultTestDataDir;
} else {
testDataDir = fs.getPath(alternateTestDataDir);
}
if (Files.exists(testDataDir) && Files.isDirectory(testDataDir)) {
returnValue = Optional.of(testDataDir.resolve(filename));
}
return returnValue;
}
}
\ No newline at end of file
/*******************************************************************************
* Copyright (c) 2020- UT-Battelle, LLC. All rights reserved. This program and
* the accompanying materials are made available under the terms of the Eclipse
* Public License v1.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Daniel Bluhm - Initial implementation
*******************************************************************************/
package org.eclipse.ice.tests.data;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utility methods for retrieving data from the test data directory.
* @author Daniel Bluhm
*/
public class TestDataPath {
/**
* Logger.
*/
public final Logger logger = LoggerFactory.getLogger(TestConfig.class);
/**
* Path of the default test data directory.
*/
static final Path DEFAULT_TEST_DATA_DIR =
Path.of(System.getProperty("user.home")).resolve("ICETests");
/**
* Environment variable for test data directory location.
*/
static final String TEST_DATA_DIR_ENV_VAR = "TEST_DATA_DIR";
/**
* Test data directory.
*/
private Path testDataDir;
/**
* FileSystem on which paths are retrieved.
*/
private FileSystem fs;
/**
* No args constructor.
*/
public TestDataPath() {
fs = FileSystems.getDefault();
this.testDataDir = determineTestDataDirectory(System.getenv());
}
/**
* Constructor for use with a different FS and environment. Primarily for
* testing.
* @param fs alternate FileSystem
* @param env alternate environment
*/
public TestDataPath(FileSystem fs, Map<String, String> env) {
this.fs = fs;
this.testDataDir = determineTestDataDirectory(env);
}
/**
* Determine test data directory from environment.
* @param env environment from which an alternate test data directory may be
* loaded.
*/
private Path determineTestDataDirectory(Map<String, String> env) {
String alternateTestDataDir = env.get(TEST_DATA_DIR_ENV_VAR);
if (alternateTestDataDir == null) {
return fs.getPath(DEFAULT_TEST_DATA_DIR.toString());
} else {
return fs.getPath(alternateTestDataDir);
}
}
/**
* Create the test data directory if it doesn't already exist.
* @throws IOException on file errors
*/
public void create() throws IOException {
Files.createDirectories(testDataDir);
}
/**
* Create a directory inside of test data directory if it doesn't already
* exist. All parent directories are created if necessary.
* @param dirname of directory to create
* @throws IOException on file errors
*/
public void createDir(String dirname) throws IOException {
Files.createDirectories(testDataDir.resolve(dirname));
}
/**
* Create a directory inside of test data directory if it doesn't already
* exist. All parent directories are created if necessary. Generally
* intended for use by paths resolved from the test data directory but all
* other paths will work as well.
* @param dirname of directory to create
* @throws IOException on file errors
*/
public void createDir(Path dir) throws IOException {
Files.createDirectories(dir);
}
/**
* Return whether the test data directory exists.
* @return true if test data directory exists
*/
public boolean exists() {
return Files.exists(testDataDir);
}
/**
* Return whether the file exists in the test data directory.
* @param filename to test
* @return true if filename exists in test data directory.
*/
public boolean exists(String filename) {
return Files.exists(testDataDir.resolve(filename));
}
/**
* Return whether the path exists. Generally intended for paths resolved
* from the testDataDirectory but will work for any path.
* @param path to test
* @return true if path exists.
*/
public boolean exists(Path path) {
return Files.exists(path);
}
/**
* 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 or null
* if the test data directory does not exist.
*/
public Path resolve(String filename) {
return testDataDir.resolve(filename);
}
/**
* Return a new input stream for the file inside of the test data directory.
* @param filename file for which an input stream will be opened.
* @return InputStream for file in test data directory.
* @throws IOException on file not fouind
*/
public InputStream input(String filename) throws IOException {
return Files.newInputStream(testDataDir.resolve(filename));
}
/**
* Return a new input stream for the file inside of the test data directory.
* Generally intended for paths resolved from test data directory but will
* work for any Path.
* @param path for which an input stream will be opened.
* @return InputStream for file in test data directory.
* @throws IOException on file not fouind
*/
public InputStream input(Path path) throws IOException {
return Files.newInputStream(path);
}
/**
* Return a new output stream for a file inside of the test data directory.
* If the file does not exist, a new file will be created.
* @param filename file for which an output stream will be opened.
* @return OutputStream for file in test data Directory.
* @throws IOException on file open errors
*/
public OutputStream output(String filename) throws IOException {
return Files.newOutputStream(testDataDir.resolve(filename));
}
/**
* Return a new output stream for a file inside of the test data directory.
* If the file does not exist, a new file will be created.
* Generally intended for paths resolved from test data directory but will
* work for any Path.
* @param path for which an output stream will be opened.
* @return OutputStream for file in test data Directory.
* @throws IOException on file open errors
*/
public OutputStream output(Path path) throws IOException {
return Files.newOutputStream(path);
}
/**
* Return a new reader for a file inside of the test data directory.
* @param filename to open for reading.
* @return Reader for file in test data directory.
* @throws IOException on file not found
*/
public Reader reader(String filename) throws IOException {
return Files.newBufferedReader(testDataDir.resolve(filename));
}
/**
* Return a new reader for a file inside of the test data directory.
* @param path to open for reading.
* @return Reader for file in test data directory.
* @throws IOException on file not found
*/
public Reader reader(Path path) throws IOException {
return Files.newBufferedReader(path);
}
/**
* Return a new writer for a file inside of the test data directory.
* A new file will be created if it does not already exist.
* @param filename file to open for writing.
* @return Writer for file in test data directory.
* @throws IOException on file open errors
*/
public Writer writer(String filename) throws IOException {
return Files.newBufferedWriter(testDataDir.resolve(filename));
}
/**
* Return a new writer for a file inside of the test data directory.
* A new file will be created if it does not already exist.
* @param path to open for writing.
* @return Writer for file in test data directory.
* @throws IOException on file open errors
*/
public Writer writer(Path path) throws IOException {
return Files.newBufferedWriter(path);
}
}
\ No newline at end of file
......@@ -14,61 +14,35 @@ 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.io.PrintWriter;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
/**
* Test TestData utility class.
* Test TestConfig class loading and usage.
* @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;
}
class TestConfigTest {
@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").get().toString()
);
}
private static final String PROP_CONTENTS = "test=abcd1234";
private static final String PROP_FILENAME = "test.properties";
@Test
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").get().toString()
);
private static Path setupPropFile(TestDataPath dataPath) throws IOException {
dataPath.create();
try(PrintWriter writer = new PrintWriter(dataPath.writer(PROP_FILENAME))) {
writer.println(PROP_CONTENTS);
}
return dataPath.resolve(PROP_FILENAME);
}
@Test
void testNonExistentReturnsNull() throws IOException {
final String unlikelyFile = "a_file_that_could_not_possibly_exist";
TestData data = new TestData();
assertEquals(Optional.empty(), data.resolve(unlikelyFile));
void testLoadAndAccess() throws IOException {
TestDataPath dataPath = TestDataPathTest.inMemTestDataPath(null);
TestConfig config = new TestConfig(dataPath);
setupPropFile(dataPath);
config.load(PROP_FILENAME);
String test = config.getProperty("test");
assertEquals("abcd1234", test);
}
}
/*******************************************************************************
* Copyright (c) 2020- UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Daniel Bluhm - Initial implementation
*******************************************************************************/
package org.eclipse.ice.tests.data;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
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 TestDataPath utility class.
* @author Daniel Bluhm
*/
class TestDataPathTest {
public static TestDataPath inMemTestDataPath(
Map<String, String> env, 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);
}
@Test
void testResolve() throws IOException {
TestDataPath data = inMemTestDataPath(null);
assertEquals(
TestDataPath.DEFAULT_TEST_DATA_DIR.resolve("test").toString(),
data.resolve("test").toString()
);
}
@Test
void testResolveWithOverridenDefault() throws IOException {
final Path alt = Path.of("/home/test/test_data");
TestDataPath data = inMemTestDataPath(
Map.of(TestDataPath.TEST_DATA_DIR_ENV_VAR, alt.toString())
);
assertEquals(