|
36 | 36 | import java.io.BufferedWriter;
|
37 | 37 | import java.io.File;
|
38 | 38 | import java.io.FileOutputStream;
|
| 39 | +import java.io.IOException; |
39 | 40 | import java.io.OutputStreamWriter;
|
| 41 | +import java.io.PrintWriter; |
40 | 42 | import java.nio.charset.StandardCharsets;
|
| 43 | +import java.nio.file.Files; |
41 | 44 | import java.util.Arrays;
|
42 | 45 | import java.util.Collection;
|
43 | 46 | import java.util.HashMap;
|
44 | 47 | import java.util.Map;
|
| 48 | +import java.util.Set; |
| 49 | +import java.util.stream.Collectors; |
| 50 | +import java.util.stream.Stream; |
45 | 51 |
|
| 52 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
46 | 53 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
47 | 54 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
48 | 55 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
@@ -164,6 +171,35 @@ public void shouldThrowOnLoadIfNoFileSpecified(String param) {
|
164 | 171 | "caughtException should mention 'Missing parameter'");
|
165 | 172 | }
|
166 | 173 |
|
| 174 | + @ParameterizedTest |
| 175 | + @MethodSource("parameters") |
| 176 | + public void shouldStripWhitespaceFromWhitelists(String param) throws IOException { |
| 177 | + plugin = new UserWhiteListPlugin(); |
| 178 | + HashMap<String, Object> pluginParameters = new HashMap<>(); |
| 179 | + pluginParameters.put(UserWhiteListPlugin.FIELD_PARAM, param); |
| 180 | + Set<String> entries = Set.of("Moomin", " Fillyjonk", " Snuffkin", "Snork Maiden ", "Groke "); |
| 181 | + |
| 182 | + File tmpFile = File.createTempFile("UserWhiteListPluginTestId", "txt"); |
| 183 | + try (PrintWriter writer = new PrintWriter(new OutputStreamWriter( |
| 184 | + new FileOutputStream(tmpFile), StandardCharsets.UTF_8))) { |
| 185 | + for (String entity : entries) { |
| 186 | + writer.println(entity); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + // Make sure there as some entries with trailing spaces in the file. |
| 191 | + Stream<String> stream = Files.lines(tmpFile.toPath()); |
| 192 | + assertTrue(stream.filter(s -> s.startsWith(" ") || s.endsWith(" ")). |
| 193 | + collect(Collectors.toSet()).size() > 0); |
| 194 | + |
| 195 | + pluginParameters.put(UserWhiteListPlugin.FILE_PARAM, tmpFile.toString()); |
| 196 | + plugin.load(pluginParameters); |
| 197 | + tmpFile.delete(); |
| 198 | + |
| 199 | + Set<String> expected = entries.stream().map(String::trim).collect(Collectors.toSet()); |
| 200 | + assertEquals(expected, plugin.getWhitelist()); |
| 201 | + } |
| 202 | + |
167 | 203 | @ParameterizedTest
|
168 | 204 | @MethodSource("parameters")
|
169 | 205 | public void shouldUnload(String param) {
|
|
0 commit comments