-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MPMD-395] Build doesn't fail for invalid CPD format
This closes #150
- Loading branch information
Showing
7 changed files
with
241 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/main/java/org/apache/maven/plugins/pmd/exec/CpdReportConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package org.apache.maven.plugins.pmd.exec; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStreamWriter; | ||
import java.io.Writer; | ||
import java.util.function.Consumer; | ||
import java.util.function.Predicate; | ||
|
||
import net.sourceforge.pmd.cpd.CPDReport; | ||
import net.sourceforge.pmd.cpd.CPDReportRenderer; | ||
import net.sourceforge.pmd.cpd.Match; | ||
import net.sourceforge.pmd.cpd.XMLRenderer; | ||
import org.apache.maven.plugins.pmd.ExcludeDuplicationsFromFile; | ||
import org.apache.maven.reporting.MavenReportException; | ||
import org.codehaus.plexus.util.FileUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
class CpdReportConsumer implements Consumer<CPDReport> { | ||
private static final Logger LOG = LoggerFactory.getLogger(CpdReportConsumer.class); | ||
|
||
private final CpdRequest request; | ||
private final ExcludeDuplicationsFromFile excludeDuplicationsFromFile; | ||
|
||
CpdReportConsumer(CpdRequest request, ExcludeDuplicationsFromFile excludeDuplicationsFromFile) { | ||
this.request = request; | ||
this.excludeDuplicationsFromFile = excludeDuplicationsFromFile; | ||
} | ||
|
||
@Override | ||
public void accept(CPDReport report) { | ||
try { | ||
// always create XML format. we need to output it even if the file list is empty or we have no | ||
// duplications so that the "check" goals can check for violations | ||
writeXmlReport(report); | ||
|
||
// HTML format is handled by maven site report, XML format has already been rendered. | ||
// a renderer is only needed for other formats | ||
String format = request.getFormat(); | ||
if (!"html".equals(format) && !"xml".equals(format)) { | ||
writeFormattedReport(report); | ||
} | ||
} catch (IOException | MavenReportException e) { | ||
// Exceptions happening during this consumer are logged by PMD | ||
// through the CPDConfiguration's reporter. | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void writeXmlReport(CPDReport cpd) throws IOException { | ||
File targetFile = writeReport(cpd, new XMLRenderer(request.getOutputEncoding()), "xml"); | ||
if (request.isIncludeXmlInSite()) { | ||
File siteDir = new File(request.getReportOutputDirectory()); | ||
if (!siteDir.exists() && !siteDir.mkdirs()) { | ||
throw new IOException("Couldn't create report output directory: " + siteDir); | ||
} | ||
FileUtils.copyFile(targetFile, new File(siteDir, "cpd.xml")); | ||
} | ||
} | ||
|
||
private void writeFormattedReport(CPDReport cpd) throws IOException, MavenReportException { | ||
CPDReportRenderer r = CpdExecutor.createRenderer(request.getFormat(), request.getOutputEncoding()); | ||
writeReport(cpd, r, request.getFormat()); | ||
} | ||
|
||
private File writeReport(CPDReport cpd, CPDReportRenderer renderer, String extension) throws IOException { | ||
if (renderer == null) { | ||
return null; | ||
} | ||
|
||
File targetDir = new File(request.getTargetDirectory()); | ||
if (!targetDir.exists() && !targetDir.mkdirs()) { | ||
throw new IOException("Couldn't create report output directory: " + targetDir); | ||
} | ||
|
||
File targetFile = new File(targetDir, "cpd." + extension); | ||
try (Writer writer = new OutputStreamWriter(new FileOutputStream(targetFile), request.getOutputEncoding())) { | ||
renderer.render(cpd.filterMatches(filterMatches()), writer); | ||
writer.flush(); | ||
} | ||
return targetFile; | ||
} | ||
|
||
private Predicate<Match> filterMatches() { | ||
return (Match match) -> { | ||
LOG.debug( | ||
"Filtering duplications. Using {} configured exclusions.", | ||
excludeDuplicationsFromFile.countExclusions()); | ||
|
||
if (excludeDuplicationsFromFile.isExcludedFromFailure(match)) { | ||
LOG.debug("Excluded {} duplications.", match); | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/test/resources/unit/CpdReportTest/with-cpd-errors/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<!-- | ||
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>unit.CpdReportTest</groupId> | ||
<artifactId>cpd-configuration-with-pmd-errors</artifactId> | ||
<packaging>jar</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<inceptionYear>2006</inceptionYear> | ||
<name>Maven CPD Plugin Test for failing build when CPD errors occurred</name> | ||
<url>http://maven.apache.org</url> | ||
<build> | ||
<finalName>with-cpd-errors</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/CpdReportTest/with-cpd-errors/target/site</outputDirectory> | ||
<targetDirectory>${basedir}/target/test/unit/CpdReportTest/with-cpd-errors/target</targetDirectory> | ||
<localRepository>${localRepository}</localRepository> | ||
<format>xml</format> | ||
<linkXRef>false</linkXRef> | ||
<xrefLocation>${basedir}/target/test/unit/CpdReportTest/with-cpd-errors/target/site/xref</xrefLocation> | ||
<minimumTokens>100</minimumTokens> | ||
<compileSourceRoots> | ||
<compileSourceRoot>${basedir}/src/test/resources/unit/CpdReportTest/with-cpd-errors/src/main/java</compileSourceRoot> | ||
</compileSourceRoots> | ||
<inputEncoding>UTF-8</inputEncoding> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
26 changes: 26 additions & 0 deletions
26
src/test/resources/unit/CpdReportTest/with-cpd-errors/src/main/java/sample/BadFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package sample; | ||
|
||
public class BadFile { | ||
public void foo() { | ||
// this is a bad character � it's U+FFFD REPLACEMENT CHARACTER | ||
int a�b = 1; | ||
} | ||
} |
Oops, something went wrong.