You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.5 KiB

  1. import org.junit.jupiter.api.BeforeAll;
  2. import org.junit.jupiter.api.Test;
  3. import java.io.*;
  4. import java.nio.charset.StandardCharsets;
  5. import static org.junit.jupiter.api.Assertions.*;
  6. class ResourceApiTest {
  7. private static ResourceApi resourceApi;
  8. @BeforeAll
  9. static void init() {
  10. resourceApi = new ResourceApi();
  11. }
  12. @Test
  13. void getFileFromResourceAsStream() throws IOException {
  14. assertThrowsExactly(IllegalArgumentException.class,
  15. () -> resourceApi.getFileFromResourceAsStream("does_not_exist"));
  16. InputStream is = resourceApi.getFileFromResourceAsStream("german_wordlist.txt");
  17. BufferedReader in = new BufferedReader(new InputStreamReader(is));
  18. String inputLine;
  19. StringBuffer response = new StringBuffer();
  20. while ((inputLine = in.readLine()) != null) {
  21. response.append(inputLine + "\n");
  22. }
  23. in.close();
  24. assertTrue(response.toString().contains("Alleinherrschaft"));
  25. }
  26. @Test
  27. void getStringFromInputStream() throws IOException {
  28. String testString = "I am a test string!\nAnother test line.\n";
  29. InputStream is = new ByteArrayInputStream(testString.getBytes(StandardCharsets.UTF_8));
  30. assertEquals(resourceApi.getStringFromInputStream(is), testString);
  31. }
  32. @Test
  33. void getFileFromResourceAsString() throws IOException {
  34. assertTrue(resourceApi.getFileFromResourceAsString("german_wordlist.txt").contains("Alleinherrschaft"));
  35. }
  36. }