Browse Source

implement getting resource file as String

feature-password-validator-2
binsky 3 years ago
parent
commit
a3bb841632
  1. 5
      src/main/java/ResourceApi.java
  2. 5
      src/test/java/ResourceApiTest.java

5
src/main/java/ResourceApi.java

@ -5,6 +5,11 @@ import java.io.InputStreamReader;
public class ResourceApi { public class ResourceApi {
public String getFileFromResourceAsString(String fileName) throws IOException {
InputStream inputStream = getFileFromResourceAsStream(fileName);
return getStringFromInputStream(inputStream);
}
public InputStream getFileFromResourceAsStream(String fileName) { public InputStream getFileFromResourceAsStream(String fileName) {
//ClassLoader classLoader = getClass().getClassLoader(); //ClassLoader classLoader = getClass().getClassLoader();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

5
src/test/java/ResourceApiTest.java

@ -40,4 +40,9 @@ class ResourceApiTest {
InputStream is = new ByteArrayInputStream(testString.getBytes(StandardCharsets.UTF_8)); InputStream is = new ByteArrayInputStream(testString.getBytes(StandardCharsets.UTF_8));
assertEquals(resourceApi.getStringFromInputStream(is), testString); assertEquals(resourceApi.getStringFromInputStream(is), testString);
} }
@Test
void getFileFromResourceAsString() throws IOException {
assertTrue(resourceApi.getFileFromResourceAsString("german_wordlist.txt").contains("Alleinherrschaft"));
}
} }
Loading…
Cancel
Save