diff options
author | miracleshiy <857701789@qq.com> | 2023-05-17 21:37:17 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-17 16:37:17 +0300 |
commit | 3c2e7e297e9c6c00de5c831285b7b7d79d4138c9 (patch) | |
tree | f35110d85efcd90ff590c08d3e472e24decbae22 | |
parent | bdadc6667ae1e09b2fb062987671f8e70830e3c5 (diff) | |
download | pf4j-3c2e7e297e9c6c00de5c831285b7b7d79d4138c9.tar.gz pf4j-3c2e7e297e9c6c00de5c831285b7b7d79d4138c9.zip |
Fix:when ignoredComments=false, FileUtils.readLines() will return empty list. (#527)
-rw-r--r-- | pf4j/src/main/java/org/pf4j/util/FileUtils.java | 2 | ||||
-rw-r--r-- | pf4j/src/test/java/org/pf4j/util/FileUtilsTest.java | 30 |
2 files changed, 31 insertions, 1 deletions
diff --git a/pf4j/src/main/java/org/pf4j/util/FileUtils.java b/pf4j/src/main/java/org/pf4j/util/FileUtils.java index 285a280..bde31f4 100644 --- a/pf4j/src/main/java/org/pf4j/util/FileUtils.java +++ b/pf4j/src/main/java/org/pf4j/util/FileUtils.java @@ -57,7 +57,7 @@ public final class FileUtils { try (BufferedReader reader = new BufferedReader(new FileReader(file))) { String line; while ((line = reader.readLine()) != null) { - if (ignoreComments && !line.startsWith("#") && !lines.contains(line)) { + if (!(ignoreComments && line.startsWith("#")) && !lines.contains(line)) { lines.add(line); } } diff --git a/pf4j/src/test/java/org/pf4j/util/FileUtilsTest.java b/pf4j/src/test/java/org/pf4j/util/FileUtilsTest.java index 050b3f1..756b3b9 100644 --- a/pf4j/src/test/java/org/pf4j/util/FileUtilsTest.java +++ b/pf4j/src/test/java/org/pf4j/util/FileUtilsTest.java @@ -19,9 +19,11 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.pf4j.test.PluginZip; +import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -65,4 +67,32 @@ public class FileUtilsTest { assertEquals(folder, FileUtils.expandIfZip(folder)); } + @Test + public void readLinesIgnoreCommentTest() throws IOException { + File file = createSampleFile("test"); + + // ignoreComments = true + List<String> ignoreCommentsLines = FileUtils.readLines(file.toPath(), true); + assertEquals("1 content", ignoreCommentsLines.get(0)); + assertEquals(2, ignoreCommentsLines.size()); + + // ignoreComments = false + List<String> lines = FileUtils.readLines(file.toPath(), false); + assertEquals("# 1 comment", lines.get(0)); + assertEquals(4, lines.size()); + file.deleteOnExit(); + } + + public File createSampleFile(String fileName) throws IOException { + File file = File.createTempFile(fileName, ".txt"); + file.deleteOnExit(); + + try (Writer writer = new OutputStreamWriter(Files.newOutputStream(file.toPath()))) { + writer.write("# 1 comment\n"); + writer.write("1 content\n"); + writer.write("2 content\n"); + writer.write("# 2 comment\n"); + } + return file; + } } |