Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move test resource helper #8

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading