1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.config.exclusion;
20
21 import org.apache.rat.ConfigurationException;
22 import org.apache.rat.utils.ExtendedIterator;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.io.CleanupMode;
25 import org.junit.jupiter.api.io.TempDir;
26
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.io.PrintWriter;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.io.File;
34 import java.util.function.Predicate;
35
36 import static java.lang.String.format;
37 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
38 import static org.junit.jupiter.api.Assertions.assertFalse;
39 import static org.junit.jupiter.api.Assertions.assertThrows;
40 import static org.junit.jupiter.api.Assertions.assertTrue;
41
42 public class ExclusionUtilsTest {
43
44 @TempDir(cleanup = CleanupMode.NEVER)
45 private File testDir;
46
47 private final int fileCount = 0;
48
49 private static final String[] COMMENTS = {
50 "# comment that is", "## comment that is",
51 "## comment that is ## ",
52 " // comment that is ## ", "", //
53 " /** comment that is **/ ", null
54 };
55
56 private static final String[] NOT_COMMENTS = {
57 "This is a normal line", "**/ignoreMe/*", "C:\\No Space In FileNames Please"
58 };
59
60 private File createFile(Iterable<String> contents) {
61 File f = new File(testDir, "file" + fileCount);
62 try (PrintWriter pw = new PrintWriter(new FileWriter(f))) {
63 for (String content : contents) {
64 if (content != null) {
65 pw.println(content);
66 }
67 }
68 } catch (IOException e) {
69 throw new RuntimeException(e);
70 }
71 return f;
72 }
73
74 @Test
75 public void CommentFilterTest() {
76 Predicate<String> filter = ExclusionUtils.commentFilter(ExclusionUtils.COMMENT_PREFIXES);
77 for (String comment : COMMENTS) {
78 assertFalse(filter.test(comment), () -> format(" '%s' should be a comment", comment));
79 }
80 for (String comment : NOT_COMMENTS) {
81 assertTrue(filter.test(comment), () -> format(" '%s' should not be a comment", comment));
82 }
83 }
84
85 @Test
86 public void fileCommentFilterTest() {
87 List<String> notComments = new ArrayList<>(Arrays.asList(NOT_COMMENTS));
88 ExtendedIterator<String> iter = ExtendedIterator.create(Arrays.asList(COMMENTS).iterator())
89 .andThen(Arrays.asList(NOT_COMMENTS).iterator());
90 Predicate<String> filter = ExclusionUtils.commentFilter(ExclusionUtils.COMMENT_PREFIXES);
91 File f = createFile(() -> iter);
92 for (String s : ExclusionUtils.asIterable(f, filter)) {
93 assertTrue(notComments.remove(s), () -> format(" '%s' should be a comment", s));
94 }
95 }
96
97 @Test
98 public void parseFromNonExistingFileOrDirectoryOrNull() {
99 Predicate<String> filter = ExclusionUtils.commentFilter(ExclusionUtils.COMMENT_PREFIXES);
100 assertThrows(ConfigurationException.class, () -> ExclusionUtils.asIterable(new File("./mustNotExist-RAT-171"), filter));
101 assertThrows(ConfigurationException.class, () -> ExclusionUtils.asIterable(null, filter));
102 assertThrows(ConfigurationException.class, () -> ExclusionUtils.asIterable(new File("."), filter));
103 }
104
105 @Test
106 public void asIterableWithPredicateTest() {
107 String[] contents = { "# comment", "Hello World", " # comment 2", "Good bye cruel World"};
108 String[] comments = { "# comment", " # comment 2" };
109
110 File f = createFile(Arrays.asList(contents));
111 List<String> found = new ArrayList<>();
112 for (String s : ExclusionUtils.asIterable(f, s -> !s.contains("World"))) {
113 found.add(s);
114 }
115 assertArrayEquals(comments, found.toArray());
116 }
117
118 @Test
119 public void asIterableWithStringTest() {
120 String[] contents = { "# comment", "Hello World", " # comment 2", "Good bye cruel World"};
121 String[] nonComments = { "Hello World", "Good bye cruel World" };
122
123 File f = createFile(Arrays.asList(contents));
124 List<String> found = new ArrayList<>();
125 for (String s : ExclusionUtils.asIterable(f, "#")) {
126 found.add(s);
127 }
128 assertArrayEquals(nonComments, found.toArray());
129 }
130 }