1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 10:19:39 +02:00

Add FileUtil tests

This commit is contained in:
Paul Schaub 2021-10-04 15:34:42 +02:00
parent d170138ea8
commit 5761f28db9
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 155 additions and 1 deletions

View file

@ -33,6 +33,26 @@ public class FileUtil {
public static final String PRFX_ENV = "@ENV:";
public static final String PRFX_FD = "@FD:";
private static EnvironmentVariableResolver envResolver = System::getenv;
public static void setEnvironmentVariableResolver(EnvironmentVariableResolver envResolver) {
if (envResolver == null) {
throw new NullPointerException("Variable envResolver cannot be null.");
}
FileUtil.envResolver = envResolver;
}
public interface EnvironmentVariableResolver {
/**
* Resolve the value of the given environment variable.
* Return null if the variable is not present.
*
* @param name name of the variable
* @return variable value or null
*/
String resolveEnvironmentVariable(String name);
}
public static File getFile(String fileName) {
if (fileName == null) {
throw new NullPointerException("File name cannot be null.");
@ -45,7 +65,7 @@ public class FileUtil {
}
String envName = fileName.substring(PRFX_ENV.length());
String envValue = System.getenv(envName);
String envValue = envResolver.resolveEnvironmentVariable(envName);
if (envValue == null) {
throw new IllegalArgumentException(String.format(ERROR_ENV_FOUND, envName));
}