1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.rat.anttasks;
18
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
28 import java.nio.charset.Charset;
29 import java.nio.charset.StandardCharsets;
30 import java.util.Arrays;
31 import java.util.Optional;
32
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35
36 import org.apache.commons.io.IOUtils;
37 import org.apache.rat.ReportConfiguration;
38 import org.apache.rat.ReportConfigurationTest;
39 import org.apache.tools.ant.BuildException;
40 import org.apache.tools.ant.Target;
41 import org.apache.tools.ant.Task;
42 import org.apache.tools.ant.UnknownElement;
43 import org.junit.Assume;
44 import org.junit.Ignore;
45 import org.junit.Test;
46 import org.w3c.dom.Document;
47
48 public class ReportTest extends AbstractRatAntTaskTest {
49 private static final File antFile = new File("src/test/resources/antunit/report-junit.xml").getAbsoluteFile();
50
51 @Override
52 protected File getAntFile() {
53 return antFile;
54 }
55
56 @Test
57 public void testWithReportSentToAnt() throws Exception {
58 buildRule.executeTarget("testWithReportSentToAnt");
59 assertLogMatches("AL +\\Q" + getAntFileName() + "\\E");
60 }
61
62 @Test
63 public void testWithReportSentToFile() throws Exception {
64 final File reportFile = new File(getTempDir(), "selftest.report");
65 final String alLine = "AL +\\Q" + getAntFileName() + "\\E";
66
67 if (!getTempDir().mkdirs() && !getTempDir().isDirectory()) {
68 throw new IOException("Could not create temporary directory " + getTempDir());
69 }
70 if (reportFile.isFile() && !reportFile.delete()) {
71 throw new IOException("Unable to remove report file " + reportFile);
72 }
73 buildRule.executeTarget("testWithReportSentToFile");
74 assertLogDoesNotMatch(alLine);
75 assertTrue("Expected report file " + reportFile, reportFile.isFile());
76 assertFileMatches(reportFile, alLine);
77 }
78
79 @Test
80 public void testWithALUnknown() throws Exception {
81 buildRule.executeTarget("testWithALUnknown");
82 assertLogDoesNotMatch("AL +\\Q" + getAntFileName() + "\\E");
83 assertLogMatches("\\!\\?\\?\\?\\?\\? +\\Q" + getAntFileName() + "\\E");
84 }
85
86 @Test
87 public void testCustomLicense() throws Exception {
88 buildRule.executeTarget("testCustomLicense");
89 assertLogDoesNotMatch(" AS +\\Q" + getAntFileName() + "\\E");
90 assertLogMatches(" newFa +\\Q" + getAntFileName() + "\\E");
91 }
92
93 @Test
94 public void testCustomMatcher() throws Exception {
95 buildRule.executeTarget("testCustomMatcher");
96 assertLogDoesNotMatch(" AS +\\Q" + getAntFileName() + "\\E");
97 assertLogMatches(" YASL1 +\\Q" + getAntFileName() + "\\E");
98 }
99
100 @Test
101 public void testInlineCustomMatcher() throws Exception {
102 buildRule.executeTarget("testInlineCustomMatcher");
103 assertLogDoesNotMatch(" AS +\\Q" + getAntFileName() + "\\E");
104 assertLogMatches(" YASL1 +\\Q" + getAntFileName() + "\\E");
105 }
106
107 @Test
108 public void testCustomMatcherBuilder() throws Exception {
109 buildRule.executeTarget("testCustomMatcherBuilder");
110 assertLogDoesNotMatch(" AS +\\Q" + getAntFileName() + "\\E");
111 assertLogMatches(" YASL1 +\\Q" + getAntFileName() + "\\E");
112 }
113
114 @Test
115 public void testNoResources() throws Exception {
116 try {
117 buildRule.executeTarget("testNoResources");
118 fail("Expected Exceptoin");
119 } catch (BuildException e) {
120 final String expect = "You must specify at least one file";
121 assertTrue("Expected " + expect + ", got " + e.getMessage(), e.getMessage().contains(expect));
122 }
123 }
124
125 @Test
126 public void testCopyrightBuild() throws Exception {
127 try {
128 String fileName = new File(getAntFile().getParent(), "index.apt").getPath().replace('\\', '/');
129 buildRule.executeTarget("testCopyrightBuild");
130 assertLogMatches("YASL1 +\\Q" + fileName + "\\E");
131 assertLogDoesNotMatch("AL +\\Q" + fileName + "\\E");
132 } catch (BuildException e) {
133 final String expect = "You must specify at least one file";
134 assertTrue("Expected " + expect + ", got " + e.getMessage(), e.getMessage().contains(expect));
135 }
136 }
137
138 private Report getReport(String target) {
139 Target testDefault = buildRule.getProject().getTargets().get(target);
140 Optional<Task> optT = Arrays.stream(testDefault.getTasks()).filter(t -> t.getTaskName().equals("rat:report"))
141 .findFirst();
142 assertTrue(optT.isPresent());
143 optT.get().maybeConfigure();
144 return (Report) ((UnknownElement) optT.get()).getRealThing();
145 }
146
147 @Test
148 public void testDefault() {
149 Report report = getReport("testDefault");
150 ReportConfiguration config = report.getConfiguration();
151 ReportConfigurationTest.validateDefault(config);
152 }
153
154 @Test
155 public void testNoLicenseMatchers() throws Exception {
156 try {
157 buildRule.executeTarget("testNoLicenseMatchers");
158 fail("Expected Exception");
159 } catch (BuildException e) {
160 final String expect = "at least one license";
161 assertTrue("Expected " + expect + ", got " + e.getMessage(), e.getMessage().contains(expect));
162 }
163 }
164
165 private String getAntFileName() {
166 return getAntFile().getPath().replace('\\', '/');
167 }
168
169 private String getFirstLine(File pFile) throws IOException {
170 FileInputStream fis = null;
171 InputStreamReader reader = null;
172 BufferedReader breader = null;
173 try {
174 fis = new FileInputStream(pFile);
175 reader = new InputStreamReader(fis, StandardCharsets.UTF_8);
176 breader = new BufferedReader(reader);
177 final String result = breader.readLine();
178 breader.close();
179 return result;
180 } finally {
181 IOUtils.closeQuietly(fis);
182 IOUtils.closeQuietly(reader);
183 IOUtils.closeQuietly(breader);
184 }
185 }
186
187 @Test
188 public void testAddLicenseHeaders() throws Exception {
189 buildRule.executeTarget("testAddLicenseHeaders");
190
191 final File origFile = new File("target/anttasks/it-sources/index.apt");
192 final String origFirstLine = getFirstLine(origFile);
193 assertTrue(origFirstLine.contains("--"));
194 assertFalse(origFirstLine.contains("~~"));
195 final File modifiedFile = new File("target/anttasks/it-sources/index.apt.new");
196 final String modifiedFirstLine = getFirstLine(modifiedFile);
197 assertFalse(modifiedFirstLine.contains("--"));
198 assertTrue(modifiedFirstLine.contains("~~"));
199 }
200
201
202
203
204 @Test
205 @Ignore
206 public void testISO88591() throws Exception {
207
208
209
210
211
212
213 Assume.assumeTrue("Expected file.encoding=ISO-8859-1",
214 "ISO-8859-1".equals(System.getProperty("file.encoding")));
215 buildRule.executeTarget("testISO88591");
216 assertTrue("Log should contain the test umlauts",
217 buildRule.getLog().contains("\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u00DF"));
218 }
219
220
221
222
223 @Test
224 @Ignore
225 public void testISO88591WithFile() throws Exception {
226
227
228
229
230
231
232 Assume.assumeTrue("Expected file.encoding=ISO-8859-1",
233 "ISO-8859-1".equals(System.getProperty("file.encoding")));
234 Charset.defaultCharset();
235 String outputDir = System.getProperty("output.dir", "target/anttasks");
236 String selftestOutput = System.getProperty("report.file", outputDir + "/selftest.report");
237 buildRule.executeTarget("testISO88591WithReportFile");
238 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
239 boolean documentParsed = false;
240 try (FileInputStream fis = new FileInputStream(selftestOutput)) {
241 Document doc = db.parse(fis);
242 boolean byteSequencePresent = doc.getElementsByTagName("header-sample").item(0).getTextContent()
243 .contains("\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u00DF");
244 assertTrue("Report should contain test umlauts", byteSequencePresent);
245 documentParsed = true;
246 } catch (Exception ex) {
247 documentParsed = false;
248 }
249 assertTrue("Report file could not be parsed as XML", documentParsed);
250 }
251 }