Commit 75af2bba authored by Daniel Bluhm's avatar Daniel Bluhm
Browse files

Modify TestData.reslove to return an Optional



To encourage correct handling of missing values.
Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent 594a7f94
/*******************************************************************************
* 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
* 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
* Contributors: Daniel Bluhm - Initial implementation
*******************************************************************************/
package org.eclipse.ice.tests.data;
......@@ -16,6 +14,7 @@ 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.
......@@ -52,7 +51,8 @@ public class TestData {
/**
* No args constructor.
*/
public TestData() { }
public TestData() {
}
/**
* Constructor for use with a different FS and environment. Primarily for
......@@ -69,10 +69,11 @@ public class TestData {
/**
* 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.
* @return the path to the file within the ICE test data directory or null
* if the file does not exist.
*/
public Path resolve(String filename) {
Path returnValue = null;
public Optional<Path> resolve(String filename) {
Optional<Path> returnValue = Optional.empty();
Path testDataDir = null;
String alternateTestDataDir = env.get(TEST_DATA_DIR_ENV_VAR);
......@@ -83,7 +84,7 @@ public class TestData {
}
if (Files.exists(testDataDir) && Files.isDirectory(testDataDir)) {
returnValue = testDataDir.resolve(filename);
returnValue = Optional.of(testDataDir.resolve(filename));
}
return returnValue;
}
......
......@@ -19,6 +19,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.api.Test;
......@@ -47,7 +48,7 @@ class TestDataTest {
), Collections.emptyMap());
assertEquals(
TestData.DEFAULT_TEST_DATA_DIR.resolve("test").toString(),
data.resolve("test").toString()
data.resolve("test").get().toString()
);
}
......@@ -60,7 +61,7 @@ class TestDataTest {
), Map.of(TestData.TEST_DATA_DIR_ENV_VAR, alt));
assertEquals(
alt + "/test",
data.resolve("test").toString()
data.resolve("test").get().toString()
);
}
......@@ -68,6 +69,6 @@ class TestDataTest {
void testNonExistentReturnsNull() throws IOException {
final String unlikelyFile = "a_file_that_could_not_possibly_exist";
TestData data = new TestData();
assertNull(data.resolve(unlikelyFile));
assertEquals(Optional.empty(), 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