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 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   * Test case for the {@link RatCheckMojo} and {@link RatReportMojo}.
39   */
40  public class RatCheckMojoTest extends AbstractMojoTestCase {
41  
42      /**
43       * Creates a new instance of {@link RatCheckMojo}.
44       *
45       * @param pDir
46       *            The directory, where to look for a pom.xml file.
47       * @return The configured Mojo.
48       * @throws Exception
49       *             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
59       *            The directory, where to look for a pom.xml file.
60       * @param pGoal
61       *            The goal, which the Mojo must implement.
62       * @return The configured Mojo.
63       * @throws Exception
64       *             An error occurred while creating the Mojo.
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      * Reads the location of the rat text file from the Mojo.
119      *
120      * @param pMojo
121      *            The configured Mojo.
122      * @return Value of the "reportFile" property.
123      * @throws Exception
124      *             An error occurred while reading the property.
125      */
126     private File getRatTxtFile(RatCheckMojo pMojo) throws Exception {
127         return (File) getVariableValueFromObject(pMojo, "reportFile");
128     }
129 
130     /**
131      * Runs a check, which should expose no problems.
132      *
133      * @throws Exception
134      *             The test failed.
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      * Runs a check, which should detect a problem.
145      *
146      * @throws Exception
147      *             The test failed.
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             // default value is "${project.build.directory}/rat.txt"
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      * Tests adding license headers.
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 }