add util function getResourceAsText

This commit is contained in:
az 2021-10-08 16:55:34 +02:00
parent 39db40cd5e
commit 3653620369
3 changed files with 21 additions and 1 deletions

View file

@ -16,7 +16,7 @@ import java.net.InetAddress
*/
fun getCallingMethodName(): String? {
val offsetVal = 1
val exclude = arrayOf("def", "record", "invoke", "invoke0", "handle", "def\$default", "addResultToEval")
val exclude = arrayOf("def", "record", "invoke", "invoke0", "handle", "def\$default", "addResultToEval", "handle\$default")
// suffixes are also ignored as method names but will be added as suffix in the evaluation results
val suffixes = arrayOf("optional", "requireAll", "requireLast", "inContainer")
@ -57,6 +57,12 @@ fun hostUserHome(): String = System.getProperty("user.home") + fileSeparator()
fun newline(): String = System.getProperty("line.separator")
fun fileSeparator(): String = File.separator
fun getResourceAsText(path: String): String {
val resource = Thread.currentThread().contextClassLoader.getResource(path)
requireNotNull(resource) { "Resource $path not found" }
return resource.readText()
}
/**
* Returns default local Prov instance.

View file

@ -5,6 +5,7 @@ import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.net.UnknownHostException
internal class UtilsKtTest {
@ -41,6 +42,18 @@ internal class UtilsKtTest {
{ remote("invalid_host", "user") })
}
@Test
fun getResourceAsText_successful() {
assertEquals("resource text\n", getResourceAsText("resource-test"))
}
@Test
fun getResourceAsText_throws_exception_for_missing_file() {
assertThrows<IllegalArgumentException> {
getResourceAsText("not existing resource")
}
}
@Test
@Disabled // run manually after having updated user
fun test_remote() {

View file

@ -0,0 +1 @@
resource text