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 org.apache.commons.io.IOUtils;
20 import org.apache.tools.ant.BuildException;
21 import org.junit.Assert;
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
29 public class ReportTest extends AbstractRatAntTaskTest {
30 private static final File antFile = new File("src/test/resources/antunit/report-junit.xml").getAbsoluteFile();
31
32 @Override
33 protected File getAntFile() {
34 return antFile;
35 }
36
37 public void testWithReportSentToAnt() throws Exception {
38 executeTarget("testWithReportSentToAnt");
39 assertLogMatches("AL +\\Q" + getAntFileName() + "\\E");
40 }
41
42 public void testWithReportSentToFile() throws Exception {
43 final File reportFile = new File(getTempDir(), "selftest.report");
44 if (!getTempDir().mkdirs() && !getTempDir().isDirectory()) {
45 throw new IOException("Could not create temporary directory " + getTempDir());
46 }
47 final String alLine = "AL +\\Q" + getAntFileName() + "\\E";
48 if (reportFile.isFile() && !reportFile.delete()) {
49 throw new IOException("Unable to remove report file " + reportFile);
50 }
51 executeTarget("testWithReportSentToFile");
52 assertLogDoesNotMatch(alLine);
53 Assert.assertTrue("Expected report file " + reportFile, reportFile.isFile());
54 assertFileMatches(reportFile, alLine);
55 }
56
57 public void testWithALUnknown() throws Exception {
58 executeTarget("testWithALUnknown");
59 assertLogDoesNotMatch("AL +\\Q" + getAntFileName() + "\\E");
60 assertLogMatches("\\!\\?\\?\\?\\?\\? +\\Q" + getAntFileName() + "\\E");
61 }
62
63 public void testCustomMatcher() throws Exception {
64 executeTarget("testCustomMatcher");
65 assertLogDoesNotMatch("AL +\\Q" + getAntFileName() + "\\E");
66 assertLogMatches("EXMPL +\\Q" + getAntFileName() + "\\E");
67 }
68
69 public void testNoResources() throws Exception {
70 try {
71 executeTarget("testNoResources");
72 fail("Expected Exception");
73 } catch (BuildException e) {
74 final String expect = "You must specify at least one file";
75 assertTrue("Expected " + expect + ", got " + e.getMessage(),
76 e.getMessage().contains(expect));
77 }
78 }
79
80 public void testNoLicenseMatchers() throws Exception {
81 try {
82 executeTarget("testNoLicenseMatchers");
83 fail("Expected Exception");
84 } catch (BuildException e) {
85 final String expect = "at least one license";
86 assertTrue("Expected " + expect + ", got " + e.getMessage(),
87 e.getMessage().contains(expect));
88 }
89 }
90
91 private String getAntFileName() {
92 return getAntFile().getPath().replace('\\', '/');
93 }
94
95 private String getFirstLine(File pFile) throws IOException {
96 FileInputStream fis = null;
97 InputStreamReader reader = null;
98 BufferedReader breader = null;
99 try {
100 fis = new FileInputStream(pFile);
101 reader = new InputStreamReader(fis, "UTF8");
102 breader = new BufferedReader(reader);
103 final String result = breader.readLine();
104 breader.close();
105 return result;
106 } finally {
107 IOUtils.closeQuietly(fis);
108 IOUtils.closeQuietly(reader);
109 IOUtils.closeQuietly(breader);
110 }
111 }
112
113 public void testAddLicenseHeaders() throws Exception {
114 executeTarget("testAddLicenseHeaders");
115
116 final File origFile = new File("target/anttasks/it-sources/index.apt");
117 final String origFirstLine = getFirstLine(origFile);
118 assertTrue(origFirstLine.contains("--"));
119 assertFalse(origFirstLine.contains("~~"));
120 final File modifiedFile = new File("target/anttasks/it-sources/index.apt.new");
121 final String modifiedFirstLine = getFirstLine(modifiedFile);
122 assertFalse(modifiedFirstLine.contains("--"));
123 assertTrue(modifiedFirstLine.contains("~~"));
124 }
125 }