Skip to content

Commit

Permalink
Merge pull request #8 from HHS/move-test-resource-helper
Browse files Browse the repository at this point in the history
move test resource helper
  • Loading branch information
shawnhatch authored Feb 1, 2024
2 parents 7c82a29 + 69eacd1 commit 6be24bf
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/main/java/util/resourcehelper/TestResourceHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package util.resourcehelper;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;

public class TestResourceHelper {

private TestResourceHelper() {
}

public static Path getResourceDir(Class<?> classRef) {
return Path.of(getURI(classRef.getClassLoader().getResource("")));
}

protected static URI getURI(URL url) {
try {
return url.toURI();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

public static Path makeOutputDir(Path dirPath) {
dirPath.toFile().mkdirs();

return dirPath;
}

public static Path makeOutputDir(Path basepath, String subDir) {
Path dirPath = basepath.resolve(subDir);

return makeOutputDir(dirPath);
}

public static void createOutputFile(Path filePath, String fileName) {

File isAfile = filePath.resolve(fileName).toFile();

if (isAfile.exists()) {
isAfile.delete();
}

createFile(isAfile);
}

protected static void createFile(File file) {
try {
file.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
104 changes: 104 additions & 0 deletions src/test/java/util/resourcehelper/AT_TestResourceHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package util.resourcehelper;

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.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;

import org.junit.jupiter.api.Test;

import util.annotations.UnitTestMethod;

public class AT_TestResourceHelper {

@Test
@UnitTestMethod(target = TestResourceHelper.class, name = "getResourceDir", args = { Class.class })
public void testGetResourceDir() throws MalformedURLException {
Path path = TestResourceHelper.getResourceDir(this.getClass());

assertNotNull(path);
assertTrue(path.toFile().exists());

// preconditions
// bad url syntax
URL url = new URL("file:${my.properties}");

RuntimeException runtimeException = assertThrows(RuntimeException.class, () -> {
TestResourceHelper.getURI(url);
});

assertTrue(runtimeException.getCause() instanceof URISyntaxException);
}

@Test
@UnitTestMethod(target = TestResourceHelper.class, name = "makeOutputDir", args = { Path.class })
public void testMakeTestOutputDir() {
Path path = TestResourceHelper.getResourceDir(this.getClass()).resolve("additional-folder");

Path newPath = TestResourceHelper.makeOutputDir(path);

assertNotNull(newPath);
assertTrue(newPath.toFile().exists());
}

@Test
@UnitTestMethod(target = TestResourceHelper.class, name = "makeOutputDir", args = { Path.class, String.class })
public void testMakeTestOutputDir_SubDir() {
Path path = TestResourceHelper.getResourceDir(this.getClass()).resolve("additional-folder");

Path newPath = TestResourceHelper.makeOutputDir(path, "subFolder");

assertNotNull(newPath);
assertTrue(newPath.toFile().exists());
}

private class IOExceptionFile extends File {

public IOExceptionFile(String pathname) {
super(pathname);
}

public IOExceptionFile(File file) {
super(file, pathSeparator);
}

@Override
public boolean createNewFile() throws IOException {

throw new IOException();
}

}

@Test
@UnitTestMethod(target = TestResourceHelper.class, name = "createOutputFile", args = { Path.class,
String.class })
public void testCreateTestOutputFile() throws IOException {
Path path = TestResourceHelper.getResourceDir(this.getClass());

Path newPath = TestResourceHelper.makeOutputDir(path);

String fileName = "foo.txt";
TestResourceHelper.createOutputFile(newPath, fileName);

assertTrue(newPath.resolve(fileName).toFile().exists());

// test for delete/recreat file if it exists
TestResourceHelper.createOutputFile(newPath, fileName);

// preconditions
// force an IOException
RuntimeException runtimeException = assertThrows(RuntimeException.class, () -> {
IOExceptionFile testFile = new IOExceptionFile(newPath.resolve(fileName).toFile());
TestResourceHelper.createFile(testFile);
});

assertTrue(runtimeException.getCause() instanceof IOException);
}
}

0 comments on commit 6be24bf

Please sign in to comment.