creadur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pottlin...@apache.org
Subject svn commit: r1672780 - in /creadur/rat/trunk: ./ apache-rat-core/src/main/java/org/apache/rat/config/ apache-rat-plugin/ apache-rat-plugin/src/main/java/org/apache/rat/mp/ apache-rat-plugin/src/main/java/org/apache/rat/mp/util/ apache-rat-plugin/src/te...
Date Fri, 10 Apr 2015 22:16:50 GMT
Author: pottlinger
Date: Fri Apr 10 22:16:50 2015
New Revision: 1672780

URL: http://svn.apache.org/r1672780
Log:
RAT-171: Add parsing of SCM ignores and enable exclusions in maven runs

* If configuration parameter 'parseSCMIgnoresAsExcludes' is set to true OR
  environment variable 'rat.parseSCMIgnoresAsExcludes' project basedir's SCM ignores are parsed
for exclusions.
* Added tests and changed visibility of some helper methods, moved stuff in util packages.
* ExclusionHelper may parse ignore files. Certain comment prefixes are ignored. There's no
syntax checking going on.
* Try out with: mvn -U -debug -Drat.parseSCMIgnoresAsExcludes=true

Added:
    creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/util/
    creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/util/ExclusionHelper.java
      - copied, changed from r1672493, creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/ExclusionHelper.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/util/
    creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/util/ExclusionHelperTest.java
      - copied, changed from r1672483, creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/ExclusionHelperTest.java
    creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/util/ScmIgnoreParserTest.java
Removed:
    creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/ExclusionHelper.java
    creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/ExclusionHelperTest.java
Modified:
    creadur/rat/trunk/RELEASE_NOTES.txt
    creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/config/SourceCodeManagementSystems.java
    creadur/rat/trunk/apache-rat-plugin/pom.xml
    creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/AbstractRatMojo.java

Modified: creadur/rat/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/creadur/rat/trunk/RELEASE_NOTES.txt?rev=1672780&r1=1672779&r2=1672780&view=diff
==============================================================================
--- creadur/rat/trunk/RELEASE_NOTES.txt (original)
+++ creadur/rat/trunk/RELEASE_NOTES.txt Fri Apr 10 22:16:50 2015
@@ -17,6 +17,10 @@ Rat 0.12 (SNAPSHOT)
     * [RAT-188] - Fix problems with AnimalSniffer-plugin when running in GUMP with Java 8.
                   Workaround for https://jira.codehaus.org/browse/MANIMALSNIFFER-45
                   (thanks to Mark Thomas)
+
+   New features
+    * [RAT-171] - Maven plugin allows scanning of source code management ignore files. Their
contents is added as exclusion/ignored during RAT runs.
+
    Improvement
     * [RAT-196] - Minor refactoring in pom.xml.
                   (thanks to Karl Heinz Marbaise) 
@@ -36,7 +40,7 @@ Rat 0.12 (SNAPSHOT)
     * [RAT-198] - Upgrade to plexus-utils 3.0.20.
                   (thanks to Karl Heinz Marbaise)
     * [RAT-172] - Exclude technical directories of source code management systems and their
ignore files from RAT scans. 
-                  Enabled for SVN,Git,Mercurial,Bazari and CVS.
+                  Enabled for SVN,Git,Mercurial,Bazar and CVS.
 
 Rat 0.11
 ========

Modified: creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/config/SourceCodeManagementSystems.java
URL: http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/config/SourceCodeManagementSystems.java?rev=1672780&r1=1672779&r2=1672780&view=diff
==============================================================================
--- creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/config/SourceCodeManagementSystems.java
(original)
+++ creadur/rat/trunk/apache-rat-core/src/main/java/org/apache/rat/config/SourceCodeManagementSystems.java
Fri Apr 10 22:16:50 2015
@@ -92,4 +92,14 @@ public enum SourceCodeManagementSystems
 
         return pluginExclusions;
     }
+
+    /**
+     * Maybe <code>null</code>, check before with
+     * @see #hasIgnoreFile()
+     *
+     * @return the ignore file of the SCM.
+     */
+    public String getIgnoreFile() {
+        return ignoreFile;
+    }
 }
\ No newline at end of file

Modified: creadur/rat/trunk/apache-rat-plugin/pom.xml
URL: http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-plugin/pom.xml?rev=1672780&r1=1672779&r2=1672780&view=diff
==============================================================================
--- creadur/rat/trunk/apache-rat-plugin/pom.xml (original)
+++ creadur/rat/trunk/apache-rat-plugin/pom.xml Fri Apr 10 22:16:50 2015
@@ -211,7 +211,7 @@
     <dependency>
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-utils</artifactId>
-      <version>3.0.20</version>
+      <version>3.0.21</version>
     </dependency>
   </dependencies>
   <reporting>

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=1672780&r1=1672779&r2=1672780&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
Fri Apr 10 22:16:50 2015
@@ -33,12 +33,12 @@ import org.apache.rat.analysis.util.Head
 import org.apache.rat.api.RatException;
 import org.apache.rat.config.SourceCodeManagementSystems;
 import org.apache.rat.license.ILicenseFamily;
+import org.apache.rat.mp.util.ScmIgnoreParser;
 import org.apache.rat.report.IReportable;
 import org.apache.rat.report.claim.ClaimStatistic;
 import org.codehaus.plexus.util.DirectoryScanner;
 
 import javax.xml.transform.TransformerConfigurationException;
-
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -53,18 +53,18 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
-import static org.apache.rat.mp.ExclusionHelper.addEclipseDefaults;
-import static org.apache.rat.mp.ExclusionHelper.addIdeaDefaults;
-import static org.apache.rat.mp.ExclusionHelper.addMavenDefaults;
-import static org.apache.rat.mp.ExclusionHelper.addPlexusAndScmDefaults;
+import static org.apache.rat.mp.util.ExclusionHelper.addEclipseDefaults;
+import static org.apache.rat.mp.util.ExclusionHelper.addIdeaDefaults;
+import static org.apache.rat.mp.util.ExclusionHelper.addMavenDefaults;
+import static org.apache.rat.mp.util.ExclusionHelper.addPlexusAndScmDefaults;
+
 /**
  * Abstract base class for Mojos, which are running Rat.
  */
 public abstract class AbstractRatMojo extends AbstractMojo {
-    
+
     /**
      * The base directory, in which to search for files.
-     *
      */
     @Parameter(property = "rat.basedir", defaultValue = "${basedir}", required = true)
     private File basedir;
@@ -107,7 +107,6 @@ public abstract class AbstractRatMojo ex
 
     /**
      * Whether to add the default list of license matchers.
-     *
      */
     @Parameter(property = "rat.addDefaultLicenseMatchers", defaultValue = "true")
     private boolean addDefaultLicenseMatchers;
@@ -115,7 +114,6 @@ public abstract class AbstractRatMojo ex
     /**
      * Specifies files, which are included in the report. By default, all files
      * are included.
-     *
      */
     @Parameter
     private String[] includes;
@@ -123,7 +121,6 @@ public abstract class AbstractRatMojo ex
     /**
      * Specifies files, which are excluded in the report. By default, no files
      * are excluded.
-     *
      */
     @Parameter
     private String[] excludes;
@@ -133,7 +130,7 @@ public abstract class AbstractRatMojo ex
      * excludes are:
      * <ul>
      * <li>meta data files for source code management / revision control systems,
-     *  see {@link SourceCodeManagementSystems}</li>
+     * see {@link SourceCodeManagementSystems}</li>
      * <li>temporary files used by Maven, see <a
      * href="#useMavenDefaultExcludes">useMavenDefaultExcludes</a></li>
      * <li>configuration files for Eclipse, see <a
@@ -155,6 +152,15 @@ public abstract class AbstractRatMojo ex
     private boolean useMavenDefaultExcludes;
 
     /**
+     * Whether to parse source code management system (SCM) ignore files and use their contents
as excludes.
+     * At the moment this works for the following SCMs:
+     *
+     * @see org.apache.rat.config.SourceCodeManagementSystems
+     */
+    @Parameter(property = "rat.parseSCMIgnoresAsExcludes", defaultValue = "false")
+    private boolean parseSCMIgnoresAsExcludes;
+
+    /**
      * Whether to use the Eclipse specific default excludes when scanning for
      * files. Eclipse specific default excludes are given by the constant
      * ECLIPSE_DEFAULT_EXCLUDES: The <code>.classpath</code> and
@@ -176,21 +182,21 @@ public abstract class AbstractRatMojo ex
     /**
      * Whether to exclude subprojects. This is recommended, if you want a
      * separate apache-rat-plugin report for each subproject.
-     *
      */
     @Parameter(property = "rat.excludeSubprojects", defaultValue = "true")
     private boolean excludeSubProjects;
 
     /**
      * Will skip the plugin execution, e.g. for technical builds that do not take licence
compliance into account.
+     *
      * @since 0.11
      */
     @Parameter(property = "rat.skip", defaultValue = "false")
     protected boolean skip;
 
     /**
-    * Holds the maven-internal project to allow resolution of artifact properties during
mojo runs.
-    */
+     * Holds the maven-internal project to allow resolution of artifact properties during
mojo runs.
+     */
     @Parameter(defaultValue = "${project}", required = true, readonly = true)
     private MavenProject project;
 
@@ -204,11 +210,9 @@ public abstract class AbstractRatMojo ex
     /**
      * Returns the set of {@link IHeaderMatcher header matchers} to use.
      *
-     * @throws MojoFailureException
-     *             An error in the plugin configuration was detected.
-     * @throws MojoExecutionException
-     *             An error occurred while calculating the result.
      * @return list of license matchers to use
+     * @throws MojoFailureException   An error in the plugin configuration was detected.
+     * @throws MojoExecutionException An error occurred while calculating the result.
      */
     protected List<IHeaderMatcher> getLicenseMatchers()
             throws MojoFailureException, MojoExecutionException {
@@ -265,10 +269,8 @@ public abstract class AbstractRatMojo ex
     /**
      * Adds the given string array to the list.
      *
-     * @param pList
-     *            The list to which the array elements are being added.
-     * @param pArray
-     *            The strings to add to the list.
+     * @param pList  The list to which the array elements are being added.
+     * @param pArray The strings to add to the list.
      */
     private static void add(List<String> pList, String[] pArray) {
         if (pArray != null) {
@@ -358,6 +360,12 @@ public abstract class AbstractRatMojo ex
         addEclipseDefaults(getLog(), useEclipseDefaultExcludes, results);
         addIdeaDefaults(getLog(), useIdeaDefaultExcludes, results);
 
+        if(parseSCMIgnoresAsExcludes) {
+            getLog().info("Will parse SCM ignores for exclusions...");
+            results.addAll(ScmIgnoreParser.getExclusionsFromSCM(getLog(), project.getBasedir()));
+            getLog().info("Finished adding exclusions from SCM ignore files.");
+        }
+
         if (excludeSubProjects && project != null
                 && project.getModules() != null) {
             for (final Object o : project.getModules()) {
@@ -385,53 +393,39 @@ public abstract class AbstractRatMojo ex
      * Creates the report as a string.
      *
      * @param styleSheet The style sheet to use when formatting the report
-     * @throws MojoFailureException
-     *             An error in the plugin configuration was detected.
-     * @throws MojoExecutionException
-     *             An error occurred while creating the report.
      * @return Report contents
+     * @throws MojoFailureException   An error in the plugin configuration was detected.
+     * @throws MojoExecutionException An error occurred while creating the report.
      */
-    protected String createReport( InputStream styleSheet )
-        throws MojoExecutionException, MojoFailureException
-    {
+    protected String createReport(InputStream styleSheet)
+            throws MojoExecutionException, MojoFailureException {
         StringWriter sw = new StringWriter();
         PrintWriter pw = null;
-        try
-        {
-            pw = new PrintWriter( sw );
-            createReport( new PrintWriter( sw ), styleSheet );
+        try {
+            pw = new PrintWriter(sw);
+            createReport(new PrintWriter(sw), styleSheet);
             final String result = sw.toString();
             pw.close();
             pw = null;
             sw.close();
             sw = null;
             return result;
-        }
-        catch ( IOException e )
-        {
-            throw new MojoExecutionException( e.getMessage(), e );
-        }
-        finally
-        {
-            IOUtils.closeQuietly( pw );
-            IOUtils.closeQuietly( sw );
+        } catch (IOException e) {
+            throw new MojoExecutionException(e.getMessage(), e);
+        } finally {
+            IOUtils.closeQuietly(pw);
+            IOUtils.closeQuietly(sw);
         }
     }
 
     /**
      * Writes the report to the given stream.
      *
-     * @param out
-     *            The target writer, to which the report is being written.
-     * @param style
-     *            The stylesheet to use, or <code>null</code> for raw XML
-     *
+     * @param out   The target writer, to which the report is being written.
+     * @param style The stylesheet to use, or <code>null</code> for raw XML
      * @return the current statistic.
-     *
-     * @throws MojoFailureException
-     *             An error in the plugin configuration was detected.
-     * @throws MojoExecutionException
-     *             Another error occurred while creating the report.
+     * @throws MojoFailureException   An error in the plugin configuration was detected.
+     * @throws MojoExecutionException Another error occurred while creating the report.
      */
     protected ClaimStatistic createReport(Writer out, InputStream style)
             throws MojoExecutionException, MojoFailureException {

Copied: creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/util/ExclusionHelper.java
(from r1672493, creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/ExclusionHelper.java)
URL: http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/util/ExclusionHelper.java?p2=creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/util/ExclusionHelper.java&p1=creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/ExclusionHelper.java&r1=1672493&r2=1672780&rev=1672780&view=diff
==============================================================================
--- creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/ExclusionHelper.java
(original)
+++ creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/util/ExclusionHelper.java
Fri Apr 10 22:16:50 2015
@@ -1,4 +1,4 @@
-package org.apache.rat.mp;
+package org.apache.rat.mp.util;
 
 import org.apache.maven.plugin.logging.Log;
 import org.apache.rat.config.SourceCodeManagementSystems;
@@ -30,6 +30,7 @@ import java.util.Set;
 
 /**
  * This class encapsulates the file/directory exclusion handling of RAT.
+ * Visibility is to allow testing and usage in the maven plugin itself.
  */
 public final class ExclusionHelper {
     /**
@@ -62,8 +63,8 @@ public final class ExclusionHelper {
                     "*.iws", //
                     ".idea/**/*"));
 
-    static void addPlexusAndScmDefaults(Log log, final boolean useDefaultExcludes,
-            final Set<String> excludeList1) {
+    public static void addPlexusAndScmDefaults(Log log, final boolean useDefaultExcludes,
+                                               final Set<String> excludeList1) {
         if (useDefaultExcludes) {
             log.debug("Adding plexus default exclusions...");
             Collections.addAll(excludeList1, DirectoryScanner.DEFAULTEXCLUDES);
@@ -76,8 +77,8 @@ public final class ExclusionHelper {
         }
     }
 
-    static void addMavenDefaults(Log log, boolean useMavenDefaultExcludes,
-            final Set<String> excludeList) {
+    public static void addMavenDefaults(Log log, boolean useMavenDefaultExcludes,
+                                        final Set<String> excludeList) {
         if (useMavenDefaultExcludes) {
             log.debug("Adding exclusions often needed by Maven projects...");
             excludeList.addAll(MAVEN_DEFAULT_EXCLUDES);
@@ -87,8 +88,8 @@ public final class ExclusionHelper {
         }
     }
 
-    static void addEclipseDefaults(Log log, boolean useEclipseDefaultExcludes,
-            final Set<String> excludeList) {
+    public static void addEclipseDefaults(Log log, boolean useEclipseDefaultExcludes,
+                                          final Set<String> excludeList) {
         if (useEclipseDefaultExcludes) {
             log.debug("Adding exclusions often needed by projects "
                     + "developed in Eclipse...");
@@ -100,8 +101,8 @@ public final class ExclusionHelper {
         }
     }
 
-    static void addIdeaDefaults(Log log, boolean useIdeaDefaultExcludes,
-            final Set<String> excludeList) {
+    public static void addIdeaDefaults(Log log, boolean useIdeaDefaultExcludes,
+                                       final Set<String> excludeList) {
         if (useIdeaDefaultExcludes) {
             log.debug("Adding exclusions often needed by projects "
                     + "developed in IDEA...");

Added: 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=1672780&view=auto
==============================================================================
--- creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/util/ScmIgnoreParser.java
(added)
+++ creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/util/ScmIgnoreParser.java
Fri Apr 10 22:16:50 2015
@@ -0,0 +1,116 @@
+package org.apache.rat.mp.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+import org.apache.maven.plugin.logging.Log;
+import org.apache.rat.config.SourceCodeManagementSystems;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Helper to parse SCM ignore files to add entries as excludes during RAT runs.
+ * Since we log errors it needs to reside inside of the maven plugin.
+ */
+public final class ScmIgnoreParser {
+    private ScmIgnoreParser() {
+        // prevent instantiation of utility class
+    }
+
+    private static List<String> COMMENT_PREFIXES = Arrays.asList("#", "##", "//", "/**",
"/*");
+
+    // FIXME: RAT-171 should we try to parse it into Patterns to sort out error cases?
+    /**
+     * Parses excludes from the given SCM ignore file.
+     *
+     * @param log       Maven log to show output during RAT runs.
+     * @param scmIgnore if <code>null</code> or invalid an empty list of exclusions
is returned.
+     * @return all exclusions (=non-comment lines) from the SCM ignore file.
+     */
+    public static List<String> getExcludesFromFile(final Log log, final File scmIgnore)
{
+
+        List<String> exclusionLines = new ArrayList<String>();
+
+        if (scmIgnore != null && scmIgnore.exists() && scmIgnore.isFile())
{
+            log.info("Parsing exclusions from " + scmIgnore);
+            try {
+                final BufferedReader reader = new BufferedReader(new FileReader(scmIgnore));
+                String line;
+                while ((line = reader.readLine()) != null) {
+                    if (!isComment(line)) {
+                        exclusionLines.add(line);
+                        log.debug("Added " + line);
+                    }
+                }
+            } catch (final IOException e) {
+                log.warn("Cannot parse " + scmIgnore + " for exclusions. Will skip this file.");
+                log.debug("Skip parsing " + scmIgnore + " due to " + e.getMessage());
+            }
+        }
+        return exclusionLines;
+    }
+
+
+    /**
+     * Parse ignore files from all known SCMs that have ignore files.
+     *
+     * @param log     Show information via maven logger.
+     * @param baseDir base directory from which to look for SCM ignores.
+     * @return Exclusions from the SCM ignore files.
+     */
+    public static List<String> getExclusionsFromSCM(final Log log, final File baseDir)
{
+        List<String> exclusions = new ArrayList<String>();
+        for (SourceCodeManagementSystems scm : SourceCodeManagementSystems.values()) {
+            if (scm.hasIgnoreFile()) {
+                exclusions.addAll(getExcludesFromFile(log, new File(baseDir, scm.getIgnoreFile())));
+            }
+        }
+        return exclusions;
+
+    }
+
+    /**
+     * Determines whether the given line is a comment or not based on scanning
+     * for prefixes
+     * {@see COMMENT_PREFIXES}.
+     *
+     * @param line line to verify.
+     * @return <code>true</code> if the given line is a commented out line.
+     */
+    static boolean isComment(final String line) {
+        if (line == null || line.length() <= 0) {
+            return false;
+        }
+
+        final String trimLine = line.trim();
+        for (String prefix : COMMENT_PREFIXES) {
+            if (trimLine.startsWith(prefix)) {
+                return true;
+            }
+        }
+        return false;
+    }
+}

Copied: creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/util/ExclusionHelperTest.java
(from r1672483, creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/ExclusionHelperTest.java)
URL: http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/util/ExclusionHelperTest.java?p2=creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/util/ExclusionHelperTest.java&p1=creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/ExclusionHelperTest.java&r1=1672483&r2=1672780&rev=1672780&view=diff
==============================================================================
--- creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/ExclusionHelperTest.java
(original)
+++ creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/util/ExclusionHelperTest.java
Fri Apr 10 22:16:50 2015
@@ -1,4 +1,4 @@
-package org.apache.rat.mp;
+package org.apache.rat.mp.util;
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
@@ -17,19 +17,6 @@ package org.apache.rat.mp;
  * limitations under the License.
  */
 
-import static org.apache.rat.mp.ExclusionHelper.ECLIPSE_DEFAULT_EXCLUDES;
-import static org.apache.rat.mp.ExclusionHelper.IDEA_DEFAULT_EXCLUDES;
-import static org.apache.rat.mp.ExclusionHelper.MAVEN_DEFAULT_EXCLUDES;
-import static org.apache.rat.mp.ExclusionHelper.addEclipseDefaults;
-import static org.apache.rat.mp.ExclusionHelper.addIdeaDefaults;
-import static org.apache.rat.mp.ExclusionHelper.addMavenDefaults;
-import static org.apache.rat.mp.ExclusionHelper.addPlexusAndScmDefaults;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.util.HashSet;
-import java.util.Set;
-
 import org.apache.maven.plugin.logging.Log;
 import org.apache.rat.config.SourceCodeManagementSystems;
 import org.junit.Test;
@@ -37,11 +24,24 @@ import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
 
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.apache.rat.mp.util.ExclusionHelper.ECLIPSE_DEFAULT_EXCLUDES;
+import static org.apache.rat.mp.util.ExclusionHelper.IDEA_DEFAULT_EXCLUDES;
+import static org.apache.rat.mp.util.ExclusionHelper.MAVEN_DEFAULT_EXCLUDES;
+import static org.apache.rat.mp.util.ExclusionHelper.addEclipseDefaults;
+import static org.apache.rat.mp.util.ExclusionHelper.addIdeaDefaults;
+import static org.apache.rat.mp.util.ExclusionHelper.addMavenDefaults;
+import static org.apache.rat.mp.util.ExclusionHelper.addPlexusAndScmDefaults;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
 @RunWith(MockitoJUnitRunner.class)
 public class ExclusionHelperTest {
 
     @Mock
-    Log log;
+    private Log log;
 
     @Test
     public void testNumberOfExclusions() {

Added: 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=1672780&view=auto
==============================================================================
--- creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/util/ScmIgnoreParserTest.java
(added)
+++ creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/util/ScmIgnoreParserTest.java
Fri Apr 10 22:16:50 2015
@@ -0,0 +1,109 @@
+package org.apache.rat.mp.util;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.maven.plugin.logging.Log;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.List;
+
+import static org.apache.rat.mp.util.ScmIgnoreParser.getExcludesFromFile;
+import static org.apache.rat.mp.util.ScmIgnoreParser.getExclusionsFromSCM;
+import static org.apache.rat.mp.util.ScmIgnoreParser.isComment;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ScmIgnoreParserTest {
+    @Rule
+    public TemporaryFolder testFolder = new TemporaryFolder();
+
+    @Mock
+    private Log log;
+
+    private static String IGNORE_EXAMPLE = "**/*.java\r\n## Justus commentos\r\nignoredDirectory";
+
+    @Test
+    public void parseAsNoComments() {
+        assertFalse(isComment(null));
+        assertFalse(isComment(""));
+        assertFalse(isComment("This is a  normal line"));
+        assertFalse(isComment("**/ignoreMe/*"));
+        assertFalse(isComment("C:\\No Space In FileNames Please"));
+    }
+
+    @Test
+    public void parseAsComments() {
+        assertTrue(isComment(" # comment that is"));
+        assertTrue(isComment("## comment that is"));
+        assertTrue(isComment("## comment that is ## "));
+        assertTrue(isComment("     // comment that is ## "));
+        assertTrue(isComment("     /** comment that is **/ "));
+        assertTrue(isComment("     /* comment that is */ "));
+    }
+
+
+    @Test
+    public void parseFromNonExistingFileOrDirectoryOrNull() {
+        assertTrue(getExcludesFromFile(log, new File("./mustNotExist-RAT-171")).isEmpty());
+        assertTrue(getExcludesFromFile(log, null).isEmpty());
+        assertTrue(getExcludesFromFile(log, new File(".")).isEmpty());
+    }
+
+    @Test
+    public void parseFromTargetDirectoryHopefullyWithoutSCMIgnores() {
+        assertTrue(getExclusionsFromSCM(log, new File("./target")).isEmpty());
+    }
+
+    @Test
+    public void parseFromEmptyIgnoreFile() throws IOException {
+        File ignore = testFolder.newFile();
+        assertTrue(ignore.exists());
+        writeToFile(IGNORE_EXAMPLE, ignore);
+
+        final List<String> excludes = getExcludesFromFile(log, ignore);
+        assertFalse(excludes.isEmpty());
+        assertEquals(2, excludes.size());
+    }
+
+    private static void writeToFile(String contents, File file) throws IOException {
+        BufferedWriter bw = null;
+        try {
+            FileWriter fw = new FileWriter(file.getAbsoluteFile());
+            bw = new BufferedWriter(fw);
+            bw.write(contents);
+        } catch (IOException e) {
+            throw e;
+        } finally {
+            if (bw != null) {
+                bw.close();
+            }
+        }
+    }
+
+
+}



Mime
View raw message