-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from HHS/move-test-resource-helper
move test resource helper
- Loading branch information
Showing
2 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
104
src/test/java/util/resourcehelper/AT_TestResourceHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |