mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-26 12:58:02 +01:00
Add FileUtil tests
This commit is contained in:
parent
d170138ea8
commit
5761f28db9
2 changed files with 155 additions and 1 deletions
|
@ -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));
|
||||
}
|
||||
|
|
134
sop-java-picocli/src/test/java/sop/cli/picocli/FileUtilTest.java
Normal file
134
sop-java-picocli/src/test/java/sop/cli/picocli/FileUtilTest.java
Normal file
|
@ -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()));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue