1 package org.apache.rat.mp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import org.apache.commons.io.IOUtils;
21 import org.apache.maven.model.Build;
22 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
23 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
24 import org.apache.rat.config.AddLicenseHeaders;
25
26 import java.io.BufferedReader;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31
32 import static org.apache.rat.mp.RatTestHelpers.ensureRatReportIsCorrect;
33 import static org.apache.rat.mp.RatTestHelpers.getSourceDirectory;
34 import static org.apache.rat.mp.RatTestHelpers.newArtifactFactory;
35 import static org.apache.rat.mp.RatTestHelpers.newArtifactRepository;
36 import static org.apache.rat.mp.RatTestHelpers.newArtifactResolver;
37 import static org.apache.rat.mp.RatTestHelpers.newSiteRenderer;
38
39
40
41
42 public class RatCheckMojoTest extends AbstractMojoTestCase {
43
44
45
46
47
48
49
50
51 private RatCheckMojo newRatCheckMojo(String pDir) throws Exception {
52 return (RatCheckMojo) newRatMojo(pDir, "check", false);
53 }
54
55
56
57
58
59
60
61
62
63 private AbstractRatMojo newRatMojo(String pDir, String pGoal,
64 boolean pCreateCopy) throws Exception {
65 final File baseDir = new File(getBasedir());
66 final File testBaseDir = getSourceDirectory(getBasedir(), pDir,
67 pCreateCopy, baseDir);
68 File testPom = new File(testBaseDir, "pom.xml");
69 AbstractRatMojo mojo = (AbstractRatMojo) lookupMojo(pGoal, testPom);
70 assertNotNull(mojo);
71 final File buildDirectory = new File(new File(baseDir, "target/test"),
72 pDir);
73 setVariableValueToObject(mojo, "basedir", testBaseDir);
74 setVariableValueToObject(mojo, "addDefaultLicenseMatchers",
75 Boolean.TRUE);
76 setVariableValueToObject(mojo, "useDefaultExcludes", Boolean.TRUE);
77 setVariableValueToObject(mojo, "useMavenDefaultExcludes", Boolean.TRUE);
78 setVariableValueToObject(mojo, "useEclipseDefaultExcludes",
79 Boolean.TRUE);
80 setVariableValueToObject(mojo, "addLicenseHeaders", AddLicenseHeaders.FALSE.name());
81 final Build build = new Build();
82 build.setDirectory(buildDirectory.getPath());
83 final MavenProjectStub project = new MavenProjectStub() {
84 @Override
85 public Build getBuild() {
86 return build;
87 }
88 };
89 setVariableValueToObject(mojo, "project", project);
90 assertNotNull(
91 "Problem in test setup - you are missing a project in your mojo.",
92 project);
93 assertNotNull(
94 "The mojo is missing its MavenProject, which will result in an NPE during rat runs.",
95 mojo.getProject());
96 assertNotNull(
97 "No artifactRepos found, which will result in an NPE during rat runs.",
98 project.getRemoteArtifactRepositories());
99
100 if (mojo instanceof RatReportMojo) {
101 setVariableValueToObject(mojo, "localRepository",
102 newArtifactRepository(container));
103 setVariableValueToObject(mojo, "resolver", newArtifactResolver());
104 setVariableValueToObject(mojo, "factory", newArtifactFactory());
105 setVariableValueToObject(mojo, "siteRenderer",
106 newSiteRenderer(container));
107 } else if (mojo instanceof RatCheckMojo) {
108 final File ratTxtFile = new File(buildDirectory, "rat.txt");
109 setVariableValueToObject(mojo, "reportFile", ratTxtFile);
110 }
111 return mojo;
112 }
113
114
115
116
117
118
119
120
121 private File getRatTxtFile(RatCheckMojo pMojo) throws Exception {
122 return (File) getVariableValueFromObject(pMojo, "reportFile");
123 }
124
125
126
127
128
129
130 public void testIt1() throws Exception {
131 final RatCheckMojo mojo = newRatCheckMojo("it1");
132 final File ratTxtFile = getRatTxtFile(mojo);
133 mojo.execute();
134 ensureRatReportIsCorrect(ratTxtFile, 1, 0);
135 }
136
137
138
139
140
141
142 public void testIt2() throws Exception {
143 final RatCheckMojo mojo = newRatCheckMojo("it2");
144 final File ratTxtFile = getRatTxtFile(mojo);
145 try {
146 mojo.execute();
147 fail("Expected RatCheckException");
148 } catch (RatCheckException e) {
149 final String msg = e.getMessage();
150
151 final String REPORTFILE = "rat.txt";
152
153 assertTrue("report filename was not contained in '" + msg + "'",
154 msg.contains(REPORTFILE));
155 assertFalse("no null allowed in '" + msg + "'", (msg.toUpperCase()
156 .contains("NULL")));
157 }
158 ensureRatReportIsCorrect(ratTxtFile, 1, 1);
159 }
160
161 private String getFirstLine(File pFile) throws IOException {
162 FileInputStream fis = null;
163 InputStreamReader reader = null;
164 BufferedReader breader = null;
165 try {
166 fis = new FileInputStream(pFile);
167 reader = new InputStreamReader(fis, "UTF8");
168 breader = new BufferedReader(reader);
169 final String result = breader.readLine();
170 breader.close();
171 return result;
172 } finally {
173 IOUtils.closeQuietly(fis);
174 IOUtils.closeQuietly(reader);
175 IOUtils.closeQuietly(breader);
176 }
177 }
178
179
180
181
182 public void testIt3() throws Exception {
183 final RatCheckMojo mojo = (RatCheckMojo) newRatMojo("it3", "check",
184 true);
185 setVariableValueToObject(mojo, "addLicenseHeaders", AddLicenseHeaders.TRUE.name());
186 setVariableValueToObject(mojo, "numUnapprovedLicenses",
187 1);
188 mojo.execute();
189 final File ratTxtFile = getRatTxtFile(mojo);
190 ensureRatReportIsCorrect(ratTxtFile, 1, 1);
191
192 final File baseDir = new File(getBasedir());
193 final File sourcesDir = new File(new File(baseDir, "target/it-source"),
194 "it3");
195 final String firstLineOrig = getFirstLine(new File(sourcesDir,
196 "src.apt"));
197 assertTrue(firstLineOrig.contains("--"));
198 assertFalse(firstLineOrig.contains("~~"));
199 final String firstLineModified = getFirstLine(new File(sourcesDir,
200 "src.apt.new"));
201 assertTrue(firstLineModified.contains("~~"));
202 assertFalse(firstLineModified.contains("--"));
203 }
204
205 }