1 package org.apache.rat.mp.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import org.apache.commons.io.IOUtils;
20 import org.apache.maven.plugin.logging.Log;
21 import org.junit.Rule;
22 import org.junit.Test;
23 import org.junit.rules.TemporaryFolder;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.runners.MockitoJUnitRunner;
27
28 import java.io.BufferedWriter;
29 import java.io.File;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.util.List;
33
34 import static org.apache.rat.mp.util.ScmIgnoreParser.getExcludesFromFile;
35 import static org.apache.rat.mp.util.ScmIgnoreParser.getExclusionsFromSCM;
36 import static org.apache.rat.mp.util.ScmIgnoreParser.isComment;
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertFalse;
39 import static org.junit.Assert.assertTrue;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class ScmIgnoreParserTest {
43 @Rule
44 public TemporaryFolder testFolder = new TemporaryFolder();
45
46 @Mock
47 private Log log;
48
49 private static String IGNORE_EXAMPLE = "**/*.java\r\n## Justus commentos\r\nignoredDirectory";
50
51 @Test
52 public void parseAsNoComments() {
53 assertFalse(isComment(null));
54 assertFalse(isComment(""));
55 assertFalse(isComment("This is a normal line"));
56 assertFalse(isComment("**/ignoreMe/*"));
57 assertFalse(isComment("C:\\No Space In FileNames Please"));
58 }
59
60 @Test
61 public void parseAsComments() {
62 assertTrue(isComment(" # comment that is"));
63 assertTrue(isComment("## comment that is"));
64 assertTrue(isComment("## comment that is ## "));
65 assertTrue(isComment(" // comment that is ## "));
66 assertTrue(isComment(" /** comment that is **/ "));
67 assertTrue(isComment(" /* comment that is */ "));
68 }
69
70
71 @Test
72 public void parseFromNonExistingFileOrDirectoryOrNull() {
73 assertTrue(getExcludesFromFile(log, new File("./mustNotExist-RAT-171")).isEmpty());
74 assertTrue(getExcludesFromFile(log, null).isEmpty());
75 assertTrue(getExcludesFromFile(log, new File(".")).isEmpty());
76 }
77
78 @Test
79 public void parseFromTargetDirectoryHopefullyWithoutSCMIgnores() {
80 assertTrue(getExclusionsFromSCM(log, new File("./target")).isEmpty());
81 }
82
83 @Test
84 public void parseFromEmptyIgnoreFile() throws IOException {
85 File ignore = testFolder.newFile();
86 assertTrue(ignore.exists());
87 writeToFile(IGNORE_EXAMPLE, ignore);
88
89 final List<String> excludes = getExcludesFromFile(log, ignore);
90 assertFalse(excludes.isEmpty());
91 assertEquals(2, excludes.size());
92 }
93
94 private static void writeToFile(String contents, File file) throws IOException {
95 BufferedWriter bw = null;
96 try {
97 FileWriter fw = new FileWriter(file.getAbsoluteFile());
98 bw = new BufferedWriter(fw);
99 bw.write(contents);
100 } catch (IOException e) {
101 throw e;
102 } finally {
103 IOUtils.closeQuietly(bw);
104 }
105 }
106
107
108 }