View Javadoc
1   package org.apache.rat.mp;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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   * Test case for the {@link RatCheckMojo} and {@link RatReportMojo}.
41   */
42  public class RatCheckMojoTest extends AbstractMojoTestCase {
43  
44      /**
45       * Creates a new instance of {@link RatCheckMojo}.
46       *
47       * @param pDir The directory, where to look for a pom.xml file.
48       * @return The configured Mojo.
49       * @throws Exception An error occurred while creating the Mojo.
50       */
51      private RatCheckMojo newRatCheckMojo(String pDir) throws Exception {
52          return (RatCheckMojo) newRatMojo(pDir, "check", false);
53      }
54  
55      /**
56       * Creates a new instance of {@link AbstractRatMojo}.
57       *
58       * @param pDir  The directory, where to look for a pom.xml file.
59       * @param pGoal The goal, which the Mojo must implement.
60       * @return The configured Mojo.
61       * @throws Exception An error occurred while creating the Mojo.
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      * Reads the location of the rat text file from the Mojo.
116      *
117      * @param pMojo The configured Mojo.
118      * @return Value of the "reportFile" property.
119      * @throws Exception An error occurred while reading the property.
120      */
121     private File getRatTxtFile(RatCheckMojo pMojo) throws Exception {
122         return (File) getVariableValueFromObject(pMojo, "reportFile");
123     }
124 
125     /**
126      * Runs a check, which should expose no problems.
127      *
128      * @throws Exception The test failed.
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      * Runs a check, which should detect a problem.
139      *
140      * @throws Exception The test failed.
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             // default value is "${project.build.directory}/rat.txt"
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      * Tests adding license headers.
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 }