This is an automated email from the ASF dual-hosted git repository.

adangel pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-pmd-plugin.git

commit de2ff17c612faba5db5412fe45255e5e8f6e370c
Author: Andreas Dangel <adan...@apache.org>
AuthorDate: Tue Sep 1 12:07:24 2020 +0200

    [MPMD-305] Add back support for txt format for CPD
    
    Closes #30
---
 .../maven/plugins/pmd/AbstractPmdReport.java       |  3 +-
 .../org/apache/maven/plugins/pmd/CpdReport.java    |  7 ++-
 .../org/apache/maven/plugins/pmd/PmdReport.java    |  4 +-
 .../apache/maven/plugins/pmd/CpdReportTest.java    | 28 ++++++++++++
 .../cpd-txt-format-configuration-plugin-config.xml | 51 ++++++++++++++++++++++
 5 files changed, 89 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java 
b/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java
index 48567a9..469cc35 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java
@@ -86,7 +86,8 @@ public abstract class AbstractPmdReport
     /**
      * Set the output format type, in addition to the HTML report. Must be one 
of: "none", "csv", "xml", "txt" or the
      * full class name of the PMD renderer to use. See the 
net.sourceforge.pmd.renderers package javadoc for available
-     * renderers. XML is required if the pmd:check goal is being used.
+     * renderers. XML is produced in any case, since this format is needed
+     * for the check goals (pmd:check, pmd:cpd-check).
      */
     @Parameter( property = "format", defaultValue = "xml" )
     protected String format = "xml";
diff --git a/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java 
b/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
index d9fb606..6163094 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
@@ -51,6 +51,7 @@ import net.sourceforge.pmd.cpd.JavaTokenizer;
 import net.sourceforge.pmd.cpd.Language;
 import net.sourceforge.pmd.cpd.LanguageFactory;
 import net.sourceforge.pmd.cpd.Match;
+import net.sourceforge.pmd.cpd.SimpleRenderer;
 import net.sourceforge.pmd.cpd.XMLRenderer;
 import net.sourceforge.pmd.cpd.renderer.CPDRenderer;
 
@@ -312,7 +313,7 @@ public class CpdReport
         // so the "check" goals can check for violations
         writeXmlReport( cpd );
 
-        // html format is handled by maven site report, xml format as already 
bean rendered
+        // html format is handled by maven site report, xml format has already 
bean rendered
         if ( !isHtml() && !isXml() )
         {
             writeFormattedReport( cpd );
@@ -449,6 +450,10 @@ public class CpdReport
         {
             renderer = new CSVRenderer();
         }
+        else if ( "txt".equals( format ) )
+        {
+            renderer = new SimpleRenderer();
+        }
         else if ( !"".equals( format ) && !"none".equals( format ) )
         {
             try
diff --git a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java 
b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
index 64a0068..f4d7f49 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
@@ -499,9 +499,9 @@ public class PmdReport
         Report report = renderer.asReport();
         writeXmlReport( report );
 
-        // write any other format except for xml and html. xml as been just 
produced.
+        // write any other format except for xml and html. xml has just been 
produced.
         // html format is produced by the maven site formatter. Excluding html 
here
-        // avoids usind PMD's own html formatter, which doesn't fit into the 
maven site
+        // avoids using PMD's own html formatter, which doesn't fit into the 
maven site
         // considering the html/css styling
         if ( !isHtml() && !isXml() )
         {
diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java 
b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
index e64d532..91d504a 100644
--- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
+++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
@@ -100,6 +100,34 @@ public class CpdReportTest
     }
 
     /**
+     * Test CPDReport with the text renderer given as "format=txt"
+     *
+     * @throws Exception
+     */
+    public void testTxtFormat()
+        throws Exception
+    {
+        File testPom =
+            new File( getBasedir(),
+                      
"src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml"
 );
+        CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
+        mojo.execute();
+
+        // check if the CPD files were generated
+        File generatedFile = new File( getBasedir(), 
"target/test/unit/custom-configuration/target/cpd.xml" );
+        assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+        generatedFile = new File( getBasedir(), 
"target/test/unit/custom-configuration/target/cpd.txt" );
+        assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+
+        // check the contents of cpd.txt
+        String str = readFile( generatedFile );
+        // Contents that should NOT be in the report
+        assertFalse( lowerCaseContains( str, "public static void main( 
String[] args )" ) );
+        // Contents that should be in the report
+        assertTrue( lowerCaseContains( str, "public void duplicateMethod( int 
i )" ) );
+    }
+
+    /**
      * Test CPDReport using custom configuration
      *
      * @throws Exception
diff --git 
a/src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml
 
b/src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml
new file mode 100644
index 0000000..5ea5415
--- /dev/null
+++ 
b/src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml
@@ -0,0 +1,51 @@
+<!--
+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.
+-->
+
+<project>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>custom.configuration</groupId>
+  <artifactId>custom-configuration</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <inceptionYear>2006</inceptionYear>
+  <name>Maven CPD Plugin Txt Format Configuration Test</name>
+  <url>http://maven.apache.org</url>
+  <build>
+    <finalName>txt-format-configuration</finalName>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-pmd-plugin</artifactId>
+        <configuration>
+          <project 
implementation="org.apache.maven.plugins.pmd.stubs.DefaultConfigurationMavenProjectStub"/>
+          
<outputDirectory>${basedir}/target/test/unit/custom-configuration/target/site</outputDirectory>
+          
<targetDirectory>${basedir}/target/test/unit/custom-configuration/target</targetDirectory>
+          <format>txt</format>
+          <linkXRef>false</linkXRef>
+          <minimumTokens>30</minimumTokens>
+
+          <compileSourceRoots>
+            
<compileSourceRoot>${basedir}/src/test/resources/unit/custom-configuration/</compileSourceRoot>
+          </compileSourceRoots>
+          <sourceEncoding>UTF-8</sourceEncoding>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Reply via email to