Browse Source

implement getting resource file as input stream

feature-password-validator-2
binsky 3 years ago
parent
commit
bed0764835
  1. 16
      src/main/java/ResourceApi.java
  2. 137465
      src/main/resources/german_wordlist.txt
  3. 39
      src/test/java/ResourceApiTest.java

16
src/main/java/ResourceApi.java

@ -0,0 +1,16 @@
import java.io.InputStream;
public class ResourceApi {
public InputStream getFileFromResourceAsStream(String fileName) {
//ClassLoader classLoader = getClass().getClassLoader();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(fileName);
if (inputStream == null) {
throw new IllegalArgumentException("file not found! " + fileName);
} else {
return inputStream;
}
}
}

137465
src/main/resources/german_wordlist.txt
File diff suppressed because it is too large
View File

39
src/test/java/ResourceApiTest.java

@ -0,0 +1,39 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ResourceApiTest {
private static ResourceApi resourceApi;
@BeforeAll
static void init() {
resourceApi = new ResourceApi();
}
@Test
void getFileFromResourceAsStream() throws IOException {
assertThrowsExactly(IllegalArgumentException.class,
() -> resourceApi.getFileFromResourceAsStream("does_not_exist"));
InputStream is = resourceApi.getFileFromResourceAsStream("german_wordlist.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine + "\n");
}
in.close();
assertTrue(response.toString().contains("Alleinherrschaft"));
}
}
Loading…
Cancel
Save