Browse Source
Merge commit '8cfd7481978d77f5c5823248d77b9f5132fb4566' into HEAD
feature-create-new-vault
Merge commit '8cfd7481978d77f5c5823248d77b9f5132fb4566' into HEAD
feature-create-new-vault
jenkins
3 years ago
5 changed files with 137625 additions and 0 deletions
-
43src/main/java/PasswordValidator.java
-
37src/main/java/ResourceApi.java
-
137465src/main/resources/german_wordlist.txt
-
32src/test/java/PasswordValidatorTest.java
-
48src/test/java/ResourceApiTest.java
@ -0,0 +1,37 @@ |
|||||
|
import java.io.BufferedReader; |
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStream; |
||||
|
import java.io.InputStreamReader; |
||||
|
|
||||
|
public class ResourceApi { |
||||
|
|
||||
|
public String getFileFromResourceAsString(String fileName) throws IOException { |
||||
|
InputStream inputStream = getFileFromResourceAsStream(fileName); |
||||
|
return getStringFromInputStream(inputStream); |
||||
|
} |
||||
|
|
||||
|
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; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public String getStringFromInputStream(InputStream inputStream) throws IOException { |
||||
|
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); |
||||
|
String inputLine; |
||||
|
StringBuffer response = new StringBuffer(); |
||||
|
|
||||
|
while ((inputLine = in.readLine()) != null) { |
||||
|
response.append(inputLine + "\n"); |
||||
|
} |
||||
|
in.close(); |
||||
|
|
||||
|
return response.toString(); |
||||
|
} |
||||
|
} |
137465
src/main/resources/german_wordlist.txt
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,48 @@ |
|||||
|
import org.junit.jupiter.api.BeforeAll; |
||||
|
import org.junit.jupiter.api.Test; |
||||
|
|
||||
|
import java.io.*; |
||||
|
import java.nio.charset.StandardCharsets; |
||||
|
|
||||
|
import static org.junit.jupiter.api.Assertions.*; |
||||
|
|
||||
|
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")); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void getStringFromInputStream() throws IOException { |
||||
|
String testString = "I am a test string!\nAnother test line.\n"; |
||||
|
InputStream is = new ByteArrayInputStream(testString.getBytes(StandardCharsets.UTF_8)); |
||||
|
assertEquals(resourceApi.getStringFromInputStream(is), testString); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void getFileFromResourceAsString() throws IOException { |
||||
|
assertTrue(resourceApi.getFileFromResourceAsString("german_wordlist.txt").contains("Alleinherrschaft")); |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue