From 5761f28db9b420ecfe0ed551c8549eebdb9ef88c Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 4 Oct 2021 15:34:42 +0200 Subject: [PATCH] Add FileUtil tests --- .../main/java/sop/cli/picocli/FileUtil.java | 22 ++- .../java/sop/cli/picocli/FileUtilTest.java | 134 ++++++++++++++++++ 2 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 sop-java-picocli/src/test/java/sop/cli/picocli/FileUtilTest.java diff --git a/sop-java-picocli/src/main/java/sop/cli/picocli/FileUtil.java b/sop-java-picocli/src/main/java/sop/cli/picocli/FileUtil.java index 30e2ae0c..f55af503 100644 --- a/sop-java-picocli/src/main/java/sop/cli/picocli/FileUtil.java +++ b/sop-java-picocli/src/main/java/sop/cli/picocli/FileUtil.java @@ -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)); } diff --git a/sop-java-picocli/src/test/java/sop/cli/picocli/FileUtilTest.java b/sop-java-picocli/src/test/java/sop/cli/picocli/FileUtilTest.java new file mode 100644 index 00000000..aad57290 --- /dev/null +++ b/sop-java-picocli/src/test/java/sop/cli/picocli/FileUtilTest.java @@ -0,0 +1,134 @@ +/* + * Copyright 2021 Paul Schaub. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package sop.cli.picocli; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Files; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import sop.exception.SOPGPException; + +public class FileUtilTest { + + @BeforeAll + public static void setup() { + FileUtil.setEnvironmentVariableResolver(new FileUtil.EnvironmentVariableResolver() { + @Override + public String resolveEnvironmentVariable(String name) { + if (name.equals("test123")) { + return "test321"; + } + return null; + } + }); + } + + @Test + public void getFile_ThrowsForNull() { + assertThrows(NullPointerException.class, () -> FileUtil.getFile(null)); + } + + @Test + public void getFile_prfxEnvAlreadyExists() throws IOException { + File tempFile = new File("@ENV:test"); + tempFile.createNewFile(); + tempFile.deleteOnExit(); + + assertThrows(SOPGPException.AmbiguousInput.class, () -> FileUtil.getFile("@ENV:test")); + } + + @Test + public void getFile_EnvironmentVariable() { + File file = FileUtil.getFile("@ENV:test123"); + assertEquals("test321", file.getName()); + } + + @Test + public void getFile_nonExistentEnvVariable() { + assertThrows(IllegalArgumentException.class, () -> FileUtil.getFile("@ENV:INVALID")); + } + + @Test + public void getFile_prfxFdAlreadyExists() throws IOException { + File tempFile = new File("@FD:1"); + tempFile.createNewFile(); + tempFile.deleteOnExit(); + + assertThrows(SOPGPException.AmbiguousInput.class, () -> FileUtil.getFile("@FD:1")); + } + + @Test + public void getFile_prfxFdNotSupported() { + assertThrows(IllegalArgumentException.class, () -> FileUtil.getFile("@FD:2")); + } + + @Test + public void createNewFileOrThrow_throwsForNull() { + assertThrows(NullPointerException.class, () -> FileUtil.createNewFileOrThrow(null)); + } + + @Test + public void createNewFileOrThrow_success() throws IOException { + File dir = Files.createTempDirectory("test").toFile(); + dir.deleteOnExit(); + File file = new File(dir, "file"); + + assertFalse(file.exists()); + FileUtil.createNewFileOrThrow(file); + assertTrue(file.exists()); + } + + @Test + public void createNewFileOrThrow_alreadyExists() throws IOException { + File dir = Files.createTempDirectory("test").toFile(); + dir.deleteOnExit(); + File file = new File(dir, "file"); + + FileUtil.createNewFileOrThrow(file); + assertTrue(file.exists()); + assertThrows(SOPGPException.OutputExists.class, () -> FileUtil.createNewFileOrThrow(file)); + } + + @Test + public void getFileInputStream_success() throws IOException { + File dir = Files.createTempDirectory("test").toFile(); + dir.deleteOnExit(); + File file = new File(dir, "file"); + + FileUtil.createNewFileOrThrow(file); + FileInputStream inputStream = FileUtil.getFileInputStream(file.getAbsolutePath()); + assertNotNull(inputStream); + } + + @Test + public void getFileInputStream_fileNotFound() throws IOException { + File dir = Files.createTempDirectory("test").toFile(); + dir.deleteOnExit(); + File file = new File(dir, "file"); + + assertThrows(SOPGPException.MissingInput.class, + () -> FileUtil.getFileInputStream(file.getAbsolutePath())); + } +}