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