Skip to content

Commit de2ff17

Browse files
committed
[MPMD-305] Add back support for txt format for CPD
Closes #30
1 parent 096cb20 commit de2ff17

File tree

5 files changed

+89
-4
lines changed

5 files changed

+89
-4
lines changed

src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public abstract class AbstractPmdReport
8686
/**
8787
* Set the output format type, in addition to the HTML report. Must be one of: "none", "csv", "xml", "txt" or the
8888
* full class name of the PMD renderer to use. See the net.sourceforge.pmd.renderers package javadoc for available
89-
* renderers. XML is required if the pmd:check goal is being used.
89+
* renderers. XML is produced in any case, since this format is needed
90+
* for the check goals (pmd:check, pmd:cpd-check).
9091
*/
9192
@Parameter( property = "format", defaultValue = "xml" )
9293
protected String format = "xml";

src/main/java/org/apache/maven/plugins/pmd/CpdReport.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import net.sourceforge.pmd.cpd.Language;
5252
import net.sourceforge.pmd.cpd.LanguageFactory;
5353
import net.sourceforge.pmd.cpd.Match;
54+
import net.sourceforge.pmd.cpd.SimpleRenderer;
5455
import net.sourceforge.pmd.cpd.XMLRenderer;
5556
import net.sourceforge.pmd.cpd.renderer.CPDRenderer;
5657

@@ -312,7 +313,7 @@ else if ( "jsp".equals( language ) )
312313
// so the "check" goals can check for violations
313314
writeXmlReport( cpd );
314315

315-
// html format is handled by maven site report, xml format as already bean rendered
316+
// html format is handled by maven site report, xml format has already bean rendered
316317
if ( !isHtml() && !isXml() )
317318
{
318319
writeFormattedReport( cpd );
@@ -449,6 +450,10 @@ else if ( "csv".equals( format ) )
449450
{
450451
renderer = new CSVRenderer();
451452
}
453+
else if ( "txt".equals( format ) )
454+
{
455+
renderer = new SimpleRenderer();
456+
}
452457
else if ( !"".equals( format ) && !"none".equals( format ) )
453458
{
454459
try

src/main/java/org/apache/maven/plugins/pmd/PmdReport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,9 @@ private void executePmd()
499499
Report report = renderer.asReport();
500500
writeXmlReport( report );
501501

502-
// write any other format except for xml and html. xml as been just produced.
502+
// write any other format except for xml and html. xml has just been produced.
503503
// html format is produced by the maven site formatter. Excluding html here
504-
// avoids usind PMD's own html formatter, which doesn't fit into the maven site
504+
// avoids using PMD's own html formatter, which doesn't fit into the maven site
505505
// considering the html/css styling
506506
if ( !isHtml() && !isXml() )
507507
{

src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,34 @@ public void testDefaultConfiguration()
9999
assertTrue( lowerCaseContains( str, "tmp = tmp + str.substring( i, i + 1);" ) );
100100
}
101101

102+
/**
103+
* Test CPDReport with the text renderer given as "format=txt"
104+
*
105+
* @throws Exception
106+
*/
107+
public void testTxtFormat()
108+
throws Exception
109+
{
110+
File testPom =
111+
new File( getBasedir(),
112+
"src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml" );
113+
CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
114+
mojo.execute();
115+
116+
// check if the CPD files were generated
117+
File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.xml" );
118+
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
119+
generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.txt" );
120+
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
121+
122+
// check the contents of cpd.txt
123+
String str = readFile( generatedFile );
124+
// Contents that should NOT be in the report
125+
assertFalse( lowerCaseContains( str, "public static void main( String[] args )" ) );
126+
// Contents that should be in the report
127+
assertTrue( lowerCaseContains( str, "public void duplicateMethod( int i )" ) );
128+
}
129+
102130
/**
103131
* Test CPDReport using custom configuration
104132
*
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
20+
<project>
21+
<modelVersion>4.0.0</modelVersion>
22+
<groupId>custom.configuration</groupId>
23+
<artifactId>custom-configuration</artifactId>
24+
<packaging>jar</packaging>
25+
<version>1.0-SNAPSHOT</version>
26+
<inceptionYear>2006</inceptionYear>
27+
<name>Maven CPD Plugin Txt Format Configuration Test</name>
28+
<url>http://maven.apache.org</url>
29+
<build>
30+
<finalName>txt-format-configuration</finalName>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-pmd-plugin</artifactId>
35+
<configuration>
36+
<project implementation="org.apache.maven.plugins.pmd.stubs.DefaultConfigurationMavenProjectStub"/>
37+
<outputDirectory>${basedir}/target/test/unit/custom-configuration/target/site</outputDirectory>
38+
<targetDirectory>${basedir}/target/test/unit/custom-configuration/target</targetDirectory>
39+
<format>txt</format>
40+
<linkXRef>false</linkXRef>
41+
<minimumTokens>30</minimumTokens>
42+
43+
<compileSourceRoots>
44+
<compileSourceRoot>${basedir}/src/test/resources/unit/custom-configuration/</compileSourceRoot>
45+
</compileSourceRoots>
46+
<sourceEncoding>UTF-8</sourceEncoding>
47+
</configuration>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</project>

0 commit comments

Comments
 (0)