You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by vs...@apache.org on 2006/10/18 15:32:58 UTC

svn commit: r465247 - in /maven/plugins/trunk/maven-checkstyle-plugin/src: main/java/org/apache/maven/plugin/checkstyle/ site/fml/ test/java/org/apache/maven/plugin/checkstyle/ test/java/org/apache/maven/plugin/checkstyle/stubs/ test/plugin-configs/

Author: vsiveton
Date: Wed Oct 18 06:32:57 2006
New Revision: 465247

URL: http://svn.apache.org/viewvc?view=rev&rev=465247
Log:
MCHECKSTYLE-47: Allow running checkstyle in test sources

o handle testSourceDirectory
o added test case
o updated javadoc and documentation

Added:
    maven/plugins/trunk/maven-checkstyle-plugin/src/test/plugin-configs/test-source-directory-plugin-config.xml   (with props)
Modified:
    maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java
    maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportListener.java
    maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java
    maven/plugins/trunk/maven-checkstyle-plugin/src/site/fml/faq.fml
    maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/CheckstyleReportTest.java
    maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/stubs/MinMavenProjectStub.java

Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java?view=diff&rev=465247&r1=465246&r2=465247
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java (original)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java Wed Oct 18 06:32:57 2006
@@ -354,7 +354,7 @@
     private boolean failsOnError;
 
     /**
-     * Specifies the location of the source files to be used for Checkstyle
+     * Specifies the location of the source directory to be used for Checkstyle
      *
      * @parameter default-value="${project.build.sourceDirectory}"
      * @required
@@ -362,6 +362,22 @@
     private File sourceDirectory;
 
     /**
+     * Specifies the location of the test source directory to be used for Checkstyle
+     *
+     * @parameter default-value="${project.build.testSourceDirectory}"
+     */
+    private File testSourceDirectory;
+
+    /**
+     * Include or not the test source directory to be used for Checkstyle
+     *
+     * @parameter default-value="${false}"
+     */
+    private boolean includeTestSourceDirectory;
+
+    /**
+     * The Maven Project Object
+     *
      * @parameter default-value="${project}"
      * @required
      * @readonly
@@ -694,6 +710,12 @@
         try
         {
             classPathStrings = this.project.getCompileClasspathElements();
+
+            if ( includeTestSourceDirectory && ( testSourceDirectory != null ) && ( testSourceDirectory.exists() )
+                && ( testSourceDirectory.isDirectory() ) )
+            {
+                classPathStrings = this.project.getTestClasspathElements();
+            }
         }
         catch ( DependencyResolutionRequiredException e )
         {
@@ -743,6 +765,11 @@
         }
 
         CheckstyleReportListener sinkListener = new CheckstyleReportListener( sourceDirectory );
+        if ( includeTestSourceDirectory && ( testSourceDirectory != null ) && ( testSourceDirectory.exists() )
+            && ( testSourceDirectory.isDirectory() ) )
+        {
+            sinkListener.addSourceDirectory( testSourceDirectory );
+        }
 
         checker.addListener( sinkListener );
 
@@ -851,6 +878,11 @@
         }
 
         List files = FileUtils.getFiles( sourceDirectory, includes, excludesStr.toString() );
+        if ( includeTestSourceDirectory && ( testSourceDirectory != null ) && ( testSourceDirectory.exists() )
+            && ( testSourceDirectory.isDirectory() ) )
+        {
+            files.addAll( FileUtils.getFiles( testSourceDirectory, includes, excludesStr.toString() ) );
+        }
 
         return (File[]) files.toArray( EMPTY_FILE_ARRAY );
     }

Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportListener.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportListener.java?view=diff&rev=465247&r1=465246&r2=465247
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportListener.java (original)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportListener.java Wed Oct 18 06:32:57 2006
@@ -23,17 +23,22 @@
 import org.codehaus.plexus.util.StringUtils;
 
 import java.io.File;
+import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 
 /**
+ * Listener in charge of receiving events from the Checker.
+ *
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
+ * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
  * @version $Id$
  */
 public class CheckstyleReportListener
     extends AutomaticBean
     implements AuditListener
 {
-    private File sourceDirectory;
+    private List sourceDirectories;
 
     private CheckstyleResults results;
 
@@ -43,45 +48,88 @@
 
     private SeverityLevel severityLevel;
 
+    /**
+     * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
+     */
     public CheckstyleReportListener( File sourceDirectory )
     {
-        this.sourceDirectory = sourceDirectory;
+        this.sourceDirectories = new ArrayList();
+        this.sourceDirectories.add( sourceDirectory );
     }
 
+    /**
+     * @param sourceDirectory assume that is <code>sourceDirectory</code> is a not null directory and exists
+     */
+    public void addSourceDirectory( File sourceDirectory )
+    {
+        this.sourceDirectories.add( sourceDirectory );
+    }
+
+    /**
+     * @param severityLevel
+     */
     public void setSeverityLevelFilter( SeverityLevel severityLevel )
     {
         this.severityLevel = severityLevel;
     }
 
+    /**
+     * @return
+     */
     public SeverityLevel getSeverityLevelFilter()
     {
         return severityLevel;
     }
 
+    /**
+     * @see com.puppycrawl.tools.checkstyle.api.AuditListener#auditStarted(com.puppycrawl.tools.checkstyle.api.AuditEvent)
+     */
     public void auditStarted( AuditEvent event )
     {
         setResults( new CheckstyleResults() );
     }
 
+    /**
+     * @see com.puppycrawl.tools.checkstyle.api.AuditListener#auditFinished(com.puppycrawl.tools.checkstyle.api.AuditEvent)
+     */
     public void auditFinished( AuditEvent event )
     {
         //do nothing
     }
 
+    /**
+     * @see com.puppycrawl.tools.checkstyle.api.AuditListener#fileStarted(com.puppycrawl.tools.checkstyle.api.AuditEvent)
+     */
     public void fileStarted( AuditEvent event )
     {
-        currentFile = StringUtils.substring( event.getFileName(), sourceDirectory.getPath().length() + 1 );
-        currentFile = StringUtils.replace( currentFile, "\\", "/" );
+        for (Iterator it = sourceDirectories.iterator(); it.hasNext(); )
+        {
+            File sourceDirectory = (File)it.next();
+
+            currentFile = StringUtils.substring( event.getFileName(), sourceDirectory.getPath().length() + 1 );
+            currentFile = StringUtils.replace( currentFile, "\\", "/" );
 
-        events = getResults().getFileViolations( currentFile );
+            events = getResults().getFileViolations( currentFile );
+        }
+
+        if ( events == null )
+        {
+            events = new ArrayList();
+        }
     }
 
+    /**
+     * @see com.puppycrawl.tools.checkstyle.api.AuditListener#fileFinished(com.puppycrawl.tools.checkstyle.api.AuditEvent)
+     */
     public void fileFinished( AuditEvent event )
     {
         getResults().setFileViolations( currentFile, events );
         currentFile = null;
     }
 
+    /**
+     * @see com.puppycrawl.tools.checkstyle.api.AuditListener#addError(com.puppycrawl.tools.checkstyle.api.AuditEvent)
+     */
     public void addError( AuditEvent event )
     {
         if ( SeverityLevel.IGNORE.equals( event.getSeverityLevel() ) )
@@ -95,16 +143,25 @@
         }
     }
 
+    /**
+     * @see com.puppycrawl.tools.checkstyle.api.AuditListener#addException(com.puppycrawl.tools.checkstyle.api.AuditEvent, java.lang.Throwable)
+     */
     public void addException( AuditEvent event, Throwable throwable )
     {
         //Do Nothing
     }
 
+    /**
+     * @return
+     */
     public CheckstyleResults getResults()
     {
         return results;
     }
 
+    /**
+     * @param results
+     */
     public void setResults( CheckstyleResults results )
     {
         this.results = results;

Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java?view=diff&rev=465247&r1=465246&r2=465247
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java (original)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java Wed Oct 18 06:32:57 2006
@@ -31,7 +31,7 @@
 /**
  * Perform a violation check against the last Checkstyle run to see if there are any violations.
  * It reads the Checkstyle output file, counts the number of violations found and displays it on the
- * console.  
+ * console.
  *
  * @author <a href="mailto:joakim@erdfelt.net">Joakim Erdfelt</a>
  * @goal check
@@ -65,7 +65,9 @@
      */
     private boolean failOnViolation;
 
-
+    /**
+     * @see org.apache.maven.plugin.Mojo#execute()
+     */
     public void execute()
         throws MojoExecutionException, MojoFailureException
     {
@@ -97,10 +99,8 @@
                     throw new MojoFailureException(
                         "You have " + violations + " Checkstyle violation" + ( ( violations > 1 ) ? "s" : "" ) + "." );
                 }
-                else
-                {
-                    getLog().warn( "checkstyle:check violations detected but failOnViolation set to false" );
-                }
+
+                getLog().warn( "checkstyle:check violations detected but failOnViolation set to false" );
             }
         }
         catch ( IOException e )

Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/site/fml/faq.fml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/site/fml/faq.fml?view=diff&rev=465247&r1=465246&r2=465247
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/site/fml/faq.fml (original)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/site/fml/faq.fml Wed Oct 18 06:32:57 2006
@@ -22,19 +22,30 @@
       <question>How are the checkstyle properties set?</question>
       <answer>
         <p>
-        You can set the checkstyle properties to be used in the plugin configuration in your pom through the <strong>propertiesLocation</strong>
-        parameter. The properties file will be resolved by the plugin based on its value.
+          You can set the checkstyle properties to be used in the plugin configuration in your pom through the
+          <strong>propertiesLocation</strong> parameter. The properties file will be resolved by the plugin based
+          on its value.
         </p>
-        </answer>
+      </answer>
     </faq>
     <faq id="How do I set a custom ruleset">
       <question>How do I set a custom ruleset?</question>
       <answer>
         <p>
-        You can set a custom ruleset through the <strong>configLocation</strong> plugin parameter. If no value is specified, the
-        plugin will use a default ruleset, which is the sun_checks.xml, that is bundled with the plugin.
+          You can set a custom ruleset through the <strong>configLocation</strong> plugin parameter. If no value is
+          specified, the plugin will use a default ruleset, which is the sun_checks.xml, that is bundled with the
+          plugin.
         </p>
-        </answer>
+      </answer>
+    </faq>
+    <faq id="How do I include the test directory in Checkstyle">
+      <question>How do I include the test directory in Checkstyle?</question>
+      <answer>
+        <p>
+          You can include test directory in the checkstyle report by setting the
+          <strong>includeTestSourceDirectory</strong> plugin parameter to <strong>true</strong>.
+        </p>
+      </answer>
     </faq>
   </part>
 </faqs>

Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/CheckstyleReportTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/CheckstyleReportTest.java?view=diff&rev=465247&r1=465246&r2=465247
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/CheckstyleReportTest.java (original)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/CheckstyleReportTest.java Wed Oct 18 06:32:57 2006
@@ -141,6 +141,12 @@
         }
     }
 
+    public void testTestSourceDirectory()
+        throws Exception
+    {
+        File htmlFile = generateReport( "test-source-directory-plugin-config.xml" );
+    }
+
     private File generateReport( String pluginXml )
         throws Exception
     {

Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/stubs/MinMavenProjectStub.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/stubs/MinMavenProjectStub.java?view=diff&rev=465247&r1=465246&r2=465247
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/stubs/MinMavenProjectStub.java (original)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/test/java/org/apache/maven/plugin/checkstyle/stubs/MinMavenProjectStub.java Wed Oct 18 06:32:57 2006
@@ -22,6 +22,7 @@
 import org.apache.maven.artifact.DependencyResolutionRequiredException;
 import org.codehaus.plexus.PlexusTestCase;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Collections;
 import java.io.File;
@@ -32,17 +33,37 @@
 public class MinMavenProjectStub
     extends org.apache.maven.plugin.testing.stubs.MavenProjectStub
 {
+    /**
+     * @see org.apache.maven.project.MavenProject#getCompileClasspathElements()
+     */
     public List getCompileClasspathElements()
         throws DependencyResolutionRequiredException
     {
         return Collections.singletonList( PlexusTestCase.getBasedir() + "/target/classes" );
     }
 
+    /**
+     * @see org.apache.maven.project.MavenProject#getTestClasspathElements()
+     */
+    public List getTestClasspathElements()
+        throws DependencyResolutionRequiredException
+    {
+        List list = new ArrayList( getCompileClasspathElements() );
+        list.add( PlexusTestCase.getBasedir() + "/target/test-classes" );
+        return list;
+    }
+
+    /**
+     * @see org.apache.maven.project.MavenProject#getBasedir()
+     */
     public File getBasedir()
     {
         return new File( PlexusTestCase.getBasedir() );
     }
 
+    /**
+     * @see org.apache.maven.project.MavenProject#getReportPlugins()
+     */
     public List getReportPlugins()
     {
         ReportPlugin jxrPlugin = new ReportPlugin();
@@ -52,6 +73,9 @@
         return Collections.singletonList( jxrPlugin );
     }
 
+    /**
+     * @see org.apache.maven.project.MavenProject#getOrganization()
+     */
     public Organization getOrganization()
     {
         Organization organization = new Organization();
@@ -61,11 +85,17 @@
         return organization;
     }
 
+    /**
+     * @see org.apache.maven.project.MavenProject#getInceptionYear()
+     */
     public String getInceptionYear()
     {
         return "2006";
     }
 
+    /**
+     * @see org.apache.maven.project.MavenProject#getBuild()
+     */
     public Build getBuild()
     {
         Build build = new Build();

Added: maven/plugins/trunk/maven-checkstyle-plugin/src/test/plugin-configs/test-source-directory-plugin-config.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/test/plugin-configs/test-source-directory-plugin-config.xml?view=auto&rev=465247
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/test/plugin-configs/test-source-directory-plugin-config.xml (added)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/test/plugin-configs/test-source-directory-plugin-config.xml Wed Oct 18 06:32:57 2006
@@ -0,0 +1,46 @@
+<!--
+  ~ Copyright 2006 The Apache Software Foundation.
+  ~
+  ~ Licensed 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.
+  -->
+
+<project>
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-checkstyle-plugin</artifactId>
+        <configuration>
+          <outputDirectory>${basedir}/target/test-harness/checkstyle/no-source</outputDirectory>
+          <enableRulesSummary>true</enableRulesSummary>
+          <enableSeveritySummary>true</enableSeveritySummary>
+          <enableFilesSummary>true</enableFilesSummary>
+          <enableRSS>true</enableRSS>
+          <includes>**/*.java</includes>
+          <configLocation>config/sun_checks.xml</configLocation>
+          <format>sun</format>
+          <headerLocation>${basedir}/src/test/test-sources/LICENSE.txt</headerLocation>
+          <cacheFile>${basedir}/target/test-harness/checkstyle/no-source/checkstyle-cachefile</cacheFile>
+          <outputFile>${basedir}/target/test-harness/checkstyle/no-source/checkstyle-result.xml</outputFile>
+          <outputFileFormat>xml</outputFileFormat>
+          <failsOnError>false</failsOnError>
+          <sourceDirectory>${basedir}/src/test/test-sources</sourceDirectory>
+          <testSourceDirectory>${basedir}/src/test/test-sources</testSourceDirectory>
+          <project implementation="org.apache.maven.plugin.checkstyle.stubs.MinMavenProjectStub"/>
+          <consoleOutput>false</consoleOutput>
+          <linkXRef>true</linkXRef>
+          <xrefLocation>${basedir}/target/site/xref</xrefLocation>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Propchange: maven/plugins/trunk/maven-checkstyle-plugin/src/test/plugin-configs/test-source-directory-plugin-config.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-checkstyle-plugin/src/test/plugin-configs/test-source-directory-plugin-config.xml
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"