Author: pottlinger
Date: Thu May 28 22:09:41 2015
New Revision: 1682347
URL: http://svn.apache.org/r1682347
Log:
RAT-204: Unify resource handling.
* Module: apache-rat-plugin
** AbstractRatMojo: avoid unnecessary null check when logging number of newly
added custom licenses.
** Replace indexOf(..) with String.contains() to improve readability of test
assertions.
** RatTestHelpers: Fixed possible null access when removing a file recursively.
Modified:
creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/AbstractRatMojo.java
creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/util/ScmIgnoreParser.java
creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java
creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatTestHelpers.java
creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/util/ScmIgnoreParserTest.java
Modified:
creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/AbstractRatMojo.java
URL:
http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/AbstractRatMojo.java?rev=1682347&r1=1682346&r2=1682347&view=diff
==============================================================================
---
creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/AbstractRatMojo.java
(original)
+++
creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/AbstractRatMojo.java
Thu May 28 22:09:41 2015
@@ -434,8 +434,7 @@ public abstract class AbstractRatMojo ex
throws MojoExecutionException, MojoFailureException {
final List<ILicenseFamily> list = new ArrayList<ILicenseFamily>();
if (licenseFamilies != null) {
- int numberOfAddedApprovals = licenseFamilies == null ? 0 :
licenseFamilies.length;
- getLog().info("Added " + numberOfAddedApprovals + " custom
approved licenses.");
+ getLog().info("Added " + licenseFamilies.length + " custom
approved licenses.");
list.addAll(Arrays.asList(licenseFamilies));
}
if (licenseFamilyNames != null) {
Modified:
creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/util/ScmIgnoreParser.java
URL:
http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/util/ScmIgnoreParser.java?rev=1682347&r1=1682346&r2=1682347&view=diff
==============================================================================
---
creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/util/ScmIgnoreParser.java
(original)
+++
creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/util/ScmIgnoreParser.java
Thu May 28 22:09:41 2015
@@ -20,6 +20,7 @@ package org.apache.rat.mp.util;
*/
+import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.logging.Log;
import org.apache.rat.config.SourceCodeManagementSystems;
@@ -69,14 +70,7 @@ public final class ScmIgnoreParser {
log.warn("Cannot parse " + scmIgnore + " for exclusions. Will
skip this file.");
log.debug("Skip parsing " + scmIgnore + " due to " +
e.getMessage());
} finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e) {
- log.error("Cannot close " + scmIgnore + " after
exclusion parsing. Will give up.");
- }
- }
-
+ IOUtils.closeQuietly(reader);
}
}
return exclusionLines;
Modified:
creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java
URL:
http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java?rev=1682347&r1=1682346&r2=1682347&view=diff
==============================================================================
---
creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java
(original)
+++
creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java
Thu May 28 22:09:41 2015
@@ -17,6 +17,7 @@ package org.apache.rat.mp;
* limitations under the License.
*/
+import org.apache.commons.io.IOUtils;
import org.apache.maven.model.Build;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
@@ -43,11 +44,9 @@ public class RatCheckMojoTest extends Ab
/**
* Creates a new instance of {@link RatCheckMojo}.
*
- * @param pDir
- * The directory, where to look for a pom.xml file.
+ * @param pDir The directory, where to look for a pom.xml file.
* @return The configured Mojo.
- * @throws Exception
- * An error occurred while creating the Mojo.
+ * @throws Exception An error occurred while creating the Mojo.
*/
private RatCheckMojo newRatCheckMojo(String pDir) throws Exception {
return (RatCheckMojo) newRatMojo(pDir, "check", false);
@@ -56,16 +55,13 @@ public class RatCheckMojoTest extends Ab
/**
* Creates a new instance of {@link AbstractRatMojo}.
*
- * @param pDir
- * The directory, where to look for a pom.xml file.
- * @param pGoal
- * The goal, which the Mojo must implement.
+ * @param pDir The directory, where to look for a pom.xml file.
+ * @param pGoal The goal, which the Mojo must implement.
* @return The configured Mojo.
- * @throws Exception
- * An error occurred while creating the Mojo.
+ * @throws Exception An error occurred while creating the Mojo.
*/
private AbstractRatMojo newRatMojo(String pDir, String pGoal,
- boolean pCreateCopy) throws Exception {
+ boolean pCreateCopy) throws Exception {
final File baseDir = new File(getBasedir());
final File testBaseDir = getSourceDirectory(getBasedir(), pDir,
pCreateCopy, baseDir);
@@ -118,11 +114,9 @@ public class RatCheckMojoTest extends Ab
/**
* Reads the location of the rat text file from the Mojo.
*
- * @param pMojo
- * The configured Mojo.
+ * @param pMojo The configured Mojo.
* @return Value of the "reportFile" property.
- * @throws Exception
- * An error occurred while reading the property.
+ * @throws Exception An error occurred while reading the property.
*/
private File getRatTxtFile(RatCheckMojo pMojo) throws Exception {
return (File) getVariableValueFromObject(pMojo, "reportFile");
@@ -131,8 +125,7 @@ public class RatCheckMojoTest extends Ab
/**
* Runs a check, which should expose no problems.
*
- * @throws Exception
- * The test failed.
+ * @throws Exception The test failed.
*/
public void testIt1() throws Exception {
final RatCheckMojo mojo = newRatCheckMojo("it1");
@@ -144,8 +137,7 @@ public class RatCheckMojoTest extends Ab
/**
* Runs a check, which should detect a problem.
*
- * @throws Exception
- * The test failed.
+ * @throws Exception The test failed.
*/
public void testIt2() throws Exception {
final RatCheckMojo mojo = newRatCheckMojo("it2");
@@ -161,18 +153,27 @@ public class RatCheckMojoTest extends Ab
assertTrue("report filename was not contained in '" + msg + "'",
msg.contains(REPORTFILE));
assertFalse("no null allowed in '" + msg + "'", (msg.toUpperCase()
- .indexOf("NULL") > -1));
+ .contains("NULL")));
}
ensureRatReportIsCorrect(ratTxtFile, 1, 1);
}
private String getFirstLine(File pFile) throws IOException {
- final FileInputStream fis = new FileInputStream(pFile);
- final InputStreamReader reader = new InputStreamReader(fis, "UTF8");
- final BufferedReader breader = new BufferedReader(reader);
- final String result = breader.readLine();
- breader.close();
- return result;
+ FileInputStream fis = null;
+ InputStreamReader reader = null;
+ BufferedReader breader = null;
+ try {
+ fis = new FileInputStream(pFile);
+ reader = new InputStreamReader(fis, "UTF8");
+ breader = new BufferedReader(reader);
+ final String result = breader.readLine();
+ breader.close();
+ return result;
+ } finally {
+ IOUtils.closeQuietly(fis);
+ IOUtils.closeQuietly(reader);
+ IOUtils.closeQuietly(breader);
+ }
}
/**
@@ -183,7 +184,7 @@ public class RatCheckMojoTest extends Ab
true);
setVariableValueToObject(mojo, "addLicenseHeaders",
AddLicenseHeaders.TRUE.name());
setVariableValueToObject(mojo, "numUnapprovedLicenses",
- Integer.valueOf(1));
+ 1);
mojo.execute();
final File ratTxtFile = getRatTxtFile(mojo);
ensureRatReportIsCorrect(ratTxtFile, 1, 1);
@@ -193,12 +194,12 @@ public class RatCheckMojoTest extends Ab
"it3");
final String firstLineOrig = getFirstLine(new File(sourcesDir,
"src.apt"));
- assertTrue(firstLineOrig.indexOf("--") != -1);
- assertTrue(firstLineOrig.indexOf("~~") == -1);
+ assertTrue(firstLineOrig.contains("--"));
+ assertFalse(firstLineOrig.contains("~~"));
final String firstLineModified = getFirstLine(new File(sourcesDir,
"src.apt.new"));
- assertTrue(firstLineModified.indexOf("--") == -1);
- assertTrue(firstLineModified.indexOf("~~") != -1);
+ assertTrue(firstLineModified.contains("~~"));
+ assertFalse(firstLineModified.contains("--"));
}
}
Modified:
creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatTestHelpers.java
URL:
http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatTestHelpers.java?rev=1682347&r1=1682346&r2=1682347&view=diff
==============================================================================
---
creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatTestHelpers.java
(original)
+++
creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatTestHelpers.java
Thu May 28 22:09:41 2015
@@ -16,17 +16,8 @@ package org.apache.rat.mp;
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.FileReader;
-import java.io.IOException;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.Method;
-import java.lang.reflect.Proxy;
+import org.apache.commons.io.IOUtils;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.factory.DefaultArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
@@ -40,16 +31,25 @@ import org.apache.maven.settings.io.xpp3
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.util.DirectoryScanner;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.IOException;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+
/**
* Test helpers used when verifying mojo interaction in RAT integration tests.
*/
public final class RatTestHelpers {
/**
- * @param pDir
- * Removes the given directory recursively.
- * @throws IOException
- * in case of errors.
+ * @param pDir Removes the given directory recursively.
+ * @throws IOException in case of errors.
*/
public static void remove(File pDir) throws IOException {
if (pDir.isFile()) {
@@ -58,8 +58,10 @@ public final class RatTestHelpers {
}
} else if (pDir.isDirectory()) {
final File[] files = pDir.listFiles();
- for (File file : files) {
- remove(file);
+ if (files != null) {
+ for (File file : files) {
+ remove(file);
+ }
}
if (!pDir.delete()) {
throw new IOException("Unable to delete directory: " + pDir);
@@ -71,14 +73,11 @@ public final class RatTestHelpers {
/**
* Copies the given files recursively in order to get all integration test
- * files into a target director.
+ * files into a target directory.
*
- * @param pSource
- * source files.
- * @param pTarget
- * target directory
- * @throws IOException
- * in case of errors.
+ * @param pSource source files.
+ * @param pTarget target directory
+ * @throws IOException in case of errors.
*/
public static void copy(File pSource, File pTarget) throws IOException {
if (pSource.isDirectory()) {
@@ -88,7 +87,7 @@ public final class RatTestHelpers {
final DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir(pSource);
scanner.addDefaultExcludes();
- scanner.setIncludes(new String[] { "*" });
+ scanner.setIncludes(new String[]{"*"});
scanner.scan();
final String[] dirs = scanner.getIncludedDirectories();
@@ -105,7 +104,7 @@ public final class RatTestHelpers {
final FileInputStream fis = new FileInputStream(pSource);
final FileOutputStream fos = new FileOutputStream(pTarget);
final byte[] buffer = new byte[8192];
- for (;;) {
+ for (; ; ) {
int res = fis.read(buffer);
if (res == -1) {
break;
@@ -124,11 +123,9 @@ public final class RatTestHelpers {
/**
* Creates a new instance of {@link Renderer}.
*
- * @param container
- * current plexus container.
- * @return A configured instance of {@link DefaultRenderer}.
- * @throws Exception
- * Creating the object failed.
+ * @param container current plexus container.
+ * @return A configured instance of a Default renderer.
+ * @throws Exception Creating the object failed.
*/
public static Renderer newSiteRenderer(PlexusContainer container)
throws Exception {
@@ -139,8 +136,7 @@ public final class RatTestHelpers {
* Creates a new instance of {@link ArtifactFactory}.
*
* @return A configured instance of {@link DefaultArtifactFactory}.
- * @throws Exception
- * Creating the object failed.
+ * @throws Exception Creating the object failed.
*/
public static ArtifactFactory newArtifactFactory() throws Exception {
final InvocationHandler handler = new InvocationHandler() {
@@ -151,16 +147,15 @@ public final class RatTestHelpers {
}
};
return (ArtifactFactory) Proxy.newProxyInstance(Thread.currentThread()
- .getContextClassLoader(),
- new Class[] { ArtifactFactory.class }, handler);
+ .getContextClassLoader(),
+ new Class[]{ArtifactFactory.class}, handler);
}
/**
* Creates a new instance of {@link ArtifactResolver}.
*
* @return A configured instance of {@link DefaultArtifactResolver}.
- * @throws Exception
- * Creating the object failed.
+ * @throws Exception Creating the object failed.
*/
public static ArtifactResolver newArtifactResolver() throws Exception {
final InvocationHandler handler = new InvocationHandler() {
@@ -171,18 +166,16 @@ public final class RatTestHelpers {
}
};
return (ArtifactResolver) Proxy.newProxyInstance(Thread.currentThread()
- .getContextClassLoader(),
- new Class[] { ArtifactResolver.class }, handler);
+ .getContextClassLoader(),
+ new Class[]{ArtifactResolver.class}, handler);
}
/**
* Creates an instance of {@link ArtifactRepository}.
*
- * @param container
- * current plexus container.
+ * @param container current plexus container.
* @return A configured instance of {@link DefaultArtifactRepository}.
- * @throws Exception
- * Creating the object failed.
+ * @throws Exception Creating the object failed.
*/
public static ArtifactRepository newArtifactRepository(
PlexusContainer container) throws Exception {
@@ -204,7 +197,7 @@ public final class RatTestHelpers {
}
public static File makeSourceDirectory(String mvnBaseDir, File pFile,
- String pDir, boolean pCreateCopy) throws IOException {
+ String pDir, boolean pCreateCopy)
throws IOException {
if (!pCreateCopy) {
return pFile;
}
@@ -217,7 +210,7 @@ public final class RatTestHelpers {
}
public static File getSourceDirectory(String mvnBaseDir, String pDir,
- boolean pCreateCopy, final File baseDir) throws IOException {
+ boolean pCreateCopy, final File
baseDir) throws IOException {
return makeSourceDirectory(mvnBaseDir, new File(new File(baseDir,
"src/test/resources/unit"), pDir), pDir, pCreateCopy);
}
@@ -226,55 +219,55 @@ public final class RatTestHelpers {
* Reads the created report file and verifies, whether the detected numbers
* are matching.
*
- * @param pRatTxtFile
- * The file to read.
- * @param pNumALFiles
- * The number of files with AL.
- * @param pNumNoLicenseFiles
- * The number of files without license.
- * @throws IOException
- * An error occurred while reading the file or the file does
not
- * exist at all.
- * @throws IllegalArgumentException
- * In case of mismatches in file numbers passed in as
parameter.
+ * @param pRatTxtFile The file to read.
+ * @param pNumALFiles The number of files with AL.
+ * @param pNumNoLicenseFiles The number of files without license.
+ * @throws IOException An error occurred while reading the
file or the file does not
+ * exist at all.
+ * @throws IllegalArgumentException In case of mismatches in file numbers
passed in as parameter.
*/
public static void ensureRatReportIsCorrect(File pRatTxtFile,
- int pNumALFiles, int pNumNoLicenseFiles) throws IOException {
+ int pNumALFiles, int
pNumNoLicenseFiles) throws IOException {
if (!pRatTxtFile.exists()) {
throw new FileNotFoundException("Could not find " + pRatTxtFile);
}
+ BufferedReader reader = null;
+ try {
- BufferedReader reader = new BufferedReader(new
FileReader(pRatTxtFile));
- Integer numALFiles = null;
- Integer numNoLicenseFiles = null;
- for (;;) {
- String line = reader.readLine();
- if (line == null) {
- break;
- }
- int offset = line.indexOf("Apache Licensed: ");
- if (offset >= 0) {
- numALFiles = new Integer(line.substring(
- offset + "Apache Licensed: ".length()).trim());
- }
- offset = line.indexOf("Unknown Licenses");
- if (offset >= 0) {
- numNoLicenseFiles = new Integer(line.substring(0, offset)
- .trim());
+ reader = new BufferedReader(new FileReader(pRatTxtFile));
+ Integer numALFiles = null;
+ Integer numNoLicenseFiles = null;
+ for (; ; ) {
+ String line = reader.readLine();
+ if (line == null) {
+ break;
+ }
+ int offset = line.indexOf("Apache Licensed: ");
+ if (offset >= 0) {
+ numALFiles = new Integer(line.substring(
+ offset + "Apache Licensed: ".length()).trim());
+ }
+ offset = line.indexOf("Unknown Licenses");
+ if (offset >= 0) {
+ numNoLicenseFiles = new Integer(line.substring(0, offset)
+ .trim());
+ }
}
- }
- reader.close();
+ reader.close();
- if (!new Integer(pNumALFiles).equals(numALFiles)) {
- throw new IllegalArgumentException(
- "Amount of licensed files does not match. Expected "
- + pNumALFiles + ", got " + numALFiles);
- }
+ if (!new Integer(pNumALFiles).equals(numALFiles)) {
+ throw new IllegalArgumentException(
+ "Amount of licensed files does not match. Expected "
+ + pNumALFiles + ", got " + numALFiles);
+ }
- if (!new Integer(pNumNoLicenseFiles).equals(numNoLicenseFiles)) {
- throw new IllegalArgumentException(
- "Amount of licensed files does not match. Expected "
- + pNumALFiles + ", got " + numALFiles);
+ if (!new Integer(pNumNoLicenseFiles).equals(numNoLicenseFiles)) {
+ throw new IllegalArgumentException(
+ "Amount of licensed files does not match. Expected "
+ + pNumALFiles + ", got " + numALFiles);
+ }
+ } finally {
+ IOUtils.closeQuietly(reader);
}
}
Modified:
creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/util/ScmIgnoreParserTest.java
URL:
http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/util/ScmIgnoreParserTest.java?rev=1682347&r1=1682346&r2=1682347&view=diff
==============================================================================
---
creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/util/ScmIgnoreParserTest.java
(original)
+++
creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/util/ScmIgnoreParserTest.java
Thu May 28 22:09:41 2015
@@ -16,6 +16,7 @@ package org.apache.rat.mp.util;
* limitations under the License.
*/
+import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.logging.Log;
import org.junit.Rule;
import org.junit.Test;
@@ -99,9 +100,7 @@ public class ScmIgnoreParserTest {
} catch (IOException e) {
throw e;
} finally {
- if (bw != null) {
- bw.close();
- }
+ IOUtils.closeQuietly(bw);
}
}