From 36536203697b47263c6d10a37249398732cb38c5 Mon Sep 17 00:00:00 2001 From: az Date: Fri, 8 Oct 2021 16:55:34 +0200 Subject: [PATCH] add util function getResourceAsText --- .../domaindrivenarchitecture/provs/core/Utils.kt | 8 +++++++- .../provs/core/UtilsKtTest.kt | 13 +++++++++++++ src/test/resources/resource-test | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/resource-test diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Utils.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Utils.kt index 55336e7..6a58c4b 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Utils.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/core/Utils.kt @@ -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. diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/core/UtilsKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/core/UtilsKtTest.kt index a0f7865..6afa4ee 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/core/UtilsKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/core/UtilsKtTest.kt @@ -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 { + getResourceAsText("not existing resource") + } + } + @Test @Disabled // run manually after having updated user fun test_remote() { diff --git a/src/test/resources/resource-test b/src/test/resources/resource-test new file mode 100644 index 0000000..e9dbfb9 --- /dev/null +++ b/src/test/resources/resource-test @@ -0,0 +1 @@ +resource text