Skip to content

Commit

Permalink
[MPMD-305] Add back support for txt format for CPD
Browse files Browse the repository at this point in the history
Closes #30
  • Loading branch information
adangel committed Sep 17, 2020
1 parent 096cb20 commit de2ff17
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
Expand Up @@ -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";
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
Expand Up @@ -51,6 +51,7 @@
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;

Expand Down Expand Up @@ -312,7 +313,7 @@ else if ( "jsp".equals( language ) )
// 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 );
Expand Down Expand Up @@ -449,6 +450,10 @@ else if ( "csv".equals( format ) )
{
renderer = new CSVRenderer();
}
else if ( "txt".equals( format ) )
{
renderer = new SimpleRenderer();
}
else if ( !"".equals( format ) && !"none".equals( format ) )
{
try
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
Expand Up @@ -499,9 +499,9 @@ private void executePmd()
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() )
{
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
Expand Up @@ -99,6 +99,34 @@ public void testDefaultConfiguration()
assertTrue( lowerCaseContains( str, "tmp = tmp + str.substring( i, i + 1);" ) );
}

/**
* 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
*
Expand Down
@@ -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>

0 comments on commit de2ff17

Please sign in to comment.