From 690dcbe5b4c7d3e4218a6c69f86a907bf8bc1d2e Mon Sep 17 00:00:00 2001
From: Elliotte Rusty Harold project.compileSourceRoots
.
* @since 3.7
*/
- @Parameter( defaultValue = "${project.compileSourceRoots}" )
+ @Parameter(defaultValue = "${project.compileSourceRoots}")
private Listproject.testCompileSourceRoots
* @since 3.7
*/
- @Parameter( defaultValue = "${project.testCompileSourceRoots}" )
+ @Parameter(defaultValue = "${project.testCompileSourceRoots}")
private Listpmd:aggregate-pmd
and pmd:aggregate-cpd
* instead.
*/
- @Parameter( property = "aggregate", defaultValue = "false" )
+ @Parameter(property = "aggregate", defaultValue = "false")
@Deprecated
protected boolean aggregate;
@@ -167,7 +163,7 @@ public abstract class AbstractPmdReport
*
* @since 3.0
*/
- @Parameter( defaultValue = "false" )
+ @Parameter(defaultValue = "false")
protected boolean includeXmlInSite;
/**
@@ -178,7 +174,7 @@ public abstract class AbstractPmdReport
*
* @since 3.1
*/
- @Parameter( defaultValue = "false" )
+ @Parameter(defaultValue = "false")
protected boolean skipEmptyReport;
/**
@@ -189,7 +185,7 @@ public abstract class AbstractPmdReport
*
* @since 3.7
*/
- @Parameter( property = "pmd.excludeFromFailureFile", defaultValue = "" )
+ @Parameter(property = "pmd.excludeFromFailureFile", defaultValue = "")
protected String excludeFromFailureFile;
/**
@@ -201,7 +197,7 @@ public abstract class AbstractPmdReport
*
* @since 3.9.0
*/
- @Parameter( defaultValue = "true", property = "pmd.showPmdLog" )
+ @Parameter(defaultValue = "true", property = "pmd.showPmdLog")
protected boolean showPmdLog = true;
/**
@@ -248,14 +244,14 @@ public abstract class AbstractPmdReport
/**
* The projects in the reactor for aggregation report.
*/
- @Parameter( property = "reactorProjects", readonly = true )
+ @Parameter(property = "reactorProjects", readonly = true)
protected Listnull
.
*/
- private String getIncludes()
- {
+ private String getIncludes() {
Collectionnull
.
*/
- private String getExcludes()
- {
- Collectionpmd:aggregate-check
or
* pmd:aggregate-cpd-check
instead.
*/
- @Parameter( property = "aggregate", defaultValue = "false" )
+ @Parameter(property = "aggregate", defaultValue = "false")
@Deprecated
protected boolean aggregate;
/**
* Print details of check failures to build output.
*/
- @Parameter( property = "pmd.verbose", defaultValue = "false" )
+ @Parameter(property = "pmd.verbose", defaultValue = "false")
private boolean verbose;
/**
@@ -78,7 +75,7 @@ public abstract class AbstractPmdViolationCheckMojotest-compile
phase again.
- *
+ *
* @since 3.15.0
*/
-@Mojo( name = "aggregate-pmd-no-fork", aggregator = true, threadSafe = true,
- requiresDependencyResolution = ResolutionScope.TEST )
-@Execute( phase = LifecyclePhase.NONE )
-public class AggregatorPmdNoForkReport extends AggregatorPmdReport
-{
-}
+@Mojo(
+ name = "aggregate-pmd-no-fork",
+ aggregator = true,
+ threadSafe = true,
+ requiresDependencyResolution = ResolutionScope.TEST)
+@Execute(phase = LifecyclePhase.NONE)
+public class AggregatorPmdNoForkReport extends AggregatorPmdReport {}
diff --git a/src/main/java/org/apache/maven/plugins/pmd/AggregatorPmdReport.java b/src/main/java/org/apache/maven/plugins/pmd/AggregatorPmdReport.java
index 3495241c..8e635b4e 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/AggregatorPmdReport.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/AggregatorPmdReport.java
@@ -1,5 +1,3 @@
-package org.apache.maven.plugins.pmd;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -18,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+package org.apache.maven.plugins.pmd;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -27,17 +26,14 @@
/**
* Creates a PMD site report in an aggregator project based on the rulesets and configuration set in the plugin.
* It can also generate a pmd output file aside from the site report in any of the following formats: xml, csv or txt.
- *
+ *
* @since 3.15.0
*/
-@Mojo( name = "aggregate-pmd", aggregator = true, threadSafe = true,
- requiresDependencyResolution = ResolutionScope.TEST )
-@Execute( phase = LifecyclePhase.TEST_COMPILE )
-public class AggregatorPmdReport extends PmdReport
-{
+@Mojo(name = "aggregate-pmd", aggregator = true, threadSafe = true, requiresDependencyResolution = ResolutionScope.TEST)
+@Execute(phase = LifecyclePhase.TEST_COMPILE)
+public class AggregatorPmdReport extends PmdReport {
@Override
- protected boolean isAggregator()
- {
+ protected boolean isAggregator() {
return true;
}
}
diff --git a/src/main/java/org/apache/maven/plugins/pmd/AggregatorPmdViolationCheckMojo.java b/src/main/java/org/apache/maven/plugins/pmd/AggregatorPmdViolationCheckMojo.java
index 6fdcab3d..48cd8267 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/AggregatorPmdViolationCheckMojo.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/AggregatorPmdViolationCheckMojo.java
@@ -1,5 +1,3 @@
-package org.apache.maven.plugins.pmd;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -18,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+package org.apache.maven.plugins.pmd;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -28,13 +27,11 @@
*
* @since 3.15.0
*/
-@Mojo( name = "aggregate-pmd-check", defaultPhase = LifecyclePhase.VERIFY, aggregator = true, threadSafe = true )
-@Execute( goal = "aggregate-pmd" )
-public class AggregatorPmdViolationCheckMojo extends PmdViolationCheckMojo
-{
+@Mojo(name = "aggregate-pmd-check", defaultPhase = LifecyclePhase.VERIFY, aggregator = true, threadSafe = true)
+@Execute(goal = "aggregate-pmd")
+public class AggregatorPmdViolationCheckMojo extends PmdViolationCheckMojo {
@Override
- protected boolean isAggregator()
- {
+ protected boolean isAggregator() {
return true;
}
}
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 522b3503..e5c4249b 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
@@ -1,5 +1,3 @@
-package org.apache.maven.plugins.pmd;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -18,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+package org.apache.maven.plugins.pmd;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
@@ -25,6 +24,8 @@
import java.util.Properties;
import java.util.ResourceBundle;
+import net.sourceforge.pmd.cpd.JavaTokenizer;
+import net.sourceforge.pmd.cpd.renderer.CPDRenderer;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.pmd.exec.CpdExecutor;
@@ -33,9 +34,6 @@
import org.apache.maven.reporting.MavenReportException;
import org.apache.maven.toolchain.Toolchain;
-import net.sourceforge.pmd.cpd.JavaTokenizer;
-import net.sourceforge.pmd.cpd.renderer.CPDRenderer;
-
/**
* Creates a report for PMD's Copy/Paste Detector (CPD) tool.
* It can also generate a cpd results file in any of these formats: xml, csv or txt.
@@ -47,23 +45,21 @@
* @version $Id$
* @since 2.0
*/
-@Mojo( name = "cpd", threadSafe = true )
-public class CpdReport
- extends AbstractPmdReport
-{
+@Mojo(name = "cpd", threadSafe = true)
+public class CpdReport extends AbstractPmdReport {
/**
* The programming language to be analyzed by CPD. Valid values are currently java
,
* javascript
or jsp
.
*
* @since 3.5
*/
- @Parameter( defaultValue = "java" )
+ @Parameter(defaultValue = "java")
private String language;
/**
* The minimum number of tokens that need to be duplicated before it causes a violation.
*/
- @Parameter( property = "minimumTokens", defaultValue = "100" )
+ @Parameter(property = "minimumTokens", defaultValue = "100")
private int minimumTokens;
/**
@@ -71,7 +67,7 @@ public class CpdReport
*
* @since 2.1
*/
- @Parameter( property = "cpd.skip", defaultValue = "false" )
+ @Parameter(property = "cpd.skip", defaultValue = "false")
private boolean skip;
/**
@@ -81,7 +77,7 @@ public class CpdReport
*
* @since 2.5
*/
- @Parameter( property = "cpd.ignoreLiterals", defaultValue = "false" )
+ @Parameter(property = "cpd.ignoreLiterals", defaultValue = "false")
private boolean ignoreLiterals;
/**
@@ -89,7 +85,7 @@ public class CpdReport
*
* @since 2.5
*/
- @Parameter( property = "cpd.ignoreIdentifiers", defaultValue = "false" )
+ @Parameter(property = "cpd.ignoreIdentifiers", defaultValue = "false")
private boolean ignoreIdentifiers;
/**
@@ -97,7 +93,7 @@ public class CpdReport
*
* @since 3.11.0
*/
- @Parameter( property = "cpd.ignoreAnnotations", defaultValue = "false" )
+ @Parameter(property = "cpd.ignoreAnnotations", defaultValue = "false")
private boolean ignoreAnnotations;
/**
@@ -110,154 +106,123 @@ public class CpdReport
/**
* {@inheritDoc}
*/
- public String getName( Locale locale )
- {
- return getBundle( locale ).getString( "report.cpd.name" );
+ public String getName(Locale locale) {
+ return getBundle(locale).getString("report.cpd.name");
}
/**
* {@inheritDoc}
*/
- public String getDescription( Locale locale )
- {
- return getBundle( locale ).getString( "report.cpd.description" );
+ public String getDescription(Locale locale) {
+ return getBundle(locale).getString("report.cpd.description");
}
/**
* {@inheritDoc}
*/
@Override
- public void executeReport( Locale locale )
- throws MavenReportException
- {
+ public void executeReport(Locale locale) throws MavenReportException {
ClassLoader origLoader = Thread.currentThread().getContextClassLoader();
- try
- {
- Thread.currentThread().setContextClassLoader( this.getClass().getClassLoader() );
+ try {
+ Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
- generateMavenSiteReport( locale );
- }
- finally
- {
- Thread.currentThread().setContextClassLoader( origLoader );
+ generateMavenSiteReport(locale);
+ } finally {
+ Thread.currentThread().setContextClassLoader(origLoader);
}
}
@Override
- public boolean canGenerateReport()
- {
- if ( skip )
- {
- getLog().info( "Skipping CPD execution" );
+ public boolean canGenerateReport() {
+ if (skip) {
+ getLog().info("Skipping CPD execution");
return false;
}
boolean result = super.canGenerateReport();
- if ( result )
- {
- try
- {
+ if (result) {
+ try {
executeCpd();
- if ( skipEmptyReport )
- {
+ if (skipEmptyReport) {
result = cpdResult.hasDuplications();
- if ( !result )
- {
- getLog().debug( "Skipping report since skipEmptyReport is true and there are no CPD issues." );
+ if (!result) {
+ getLog().debug("Skipping report since skipEmptyReport is true and there are no CPD issues.");
}
}
- }
- catch ( MavenReportException e )
- {
- throw new RuntimeException( e );
+ } catch (MavenReportException e) {
+ throw new RuntimeException(e);
}
}
return result;
}
- private void executeCpd()
- throws MavenReportException
- {
- if ( cpdResult != null )
- {
+ private void executeCpd() throws MavenReportException {
+ if (cpdResult != null) {
// CPD has already been run
- getLog().debug( "CPD has already been run - skipping redundant execution." );
+ getLog().debug("CPD has already been run - skipping redundant execution.");
return;
}
Properties languageProperties = new Properties();
- if ( ignoreLiterals )
- {
- languageProperties.setProperty( JavaTokenizer.IGNORE_LITERALS, "true" );
+ if (ignoreLiterals) {
+ languageProperties.setProperty(JavaTokenizer.IGNORE_LITERALS, "true");
}
- if ( ignoreIdentifiers )
- {
- languageProperties.setProperty( JavaTokenizer.IGNORE_IDENTIFIERS, "true" );
+ if (ignoreIdentifiers) {
+ languageProperties.setProperty(JavaTokenizer.IGNORE_IDENTIFIERS, "true");
}
- if ( ignoreAnnotations )
- {
- languageProperties.setProperty( JavaTokenizer.IGNORE_ANNOTATIONS, "true" );
+ if (ignoreAnnotations) {
+ languageProperties.setProperty(JavaTokenizer.IGNORE_ANNOTATIONS, "true");
}
- try
- {
+ try {
filesToProcess = getFilesToProcess();
CpdRequest request = new CpdRequest();
- request.setMinimumTokens( minimumTokens );
- request.setLanguage( language );
- request.setLanguageProperties( languageProperties );
- request.setSourceEncoding( getInputEncoding() );
- request.addFiles( filesToProcess.keySet() );
-
- request.setShowPmdLog( showPmdLog );
- request.setLogLevel( determineCurrentRootLogLevel() );
-
- request.setExcludeFromFailureFile( excludeFromFailureFile );
- request.setTargetDirectory( targetDirectory.getAbsolutePath() );
- request.setOutputEncoding( getOutputEncoding() );
- request.setFormat( format );
- request.setIncludeXmlInSite( includeXmlInSite );
- request.setReportOutputDirectory( getReportOutputDirectory().getAbsolutePath() );
+ request.setMinimumTokens(minimumTokens);
+ request.setLanguage(language);
+ request.setLanguageProperties(languageProperties);
+ request.setSourceEncoding(getInputEncoding());
+ request.addFiles(filesToProcess.keySet());
+
+ request.setShowPmdLog(showPmdLog);
+ request.setLogLevel(determineCurrentRootLogLevel());
+
+ request.setExcludeFromFailureFile(excludeFromFailureFile);
+ request.setTargetDirectory(targetDirectory.getAbsolutePath());
+ request.setOutputEncoding(getOutputEncoding());
+ request.setFormat(format);
+ request.setIncludeXmlInSite(includeXmlInSite);
+ request.setReportOutputDirectory(getReportOutputDirectory().getAbsolutePath());
Toolchain tc = getToolchain();
- if ( tc != null )
- {
- getLog().info( "Toolchain in maven-pmd-plugin: " + tc );
- String javaExecutable = tc.findTool( "java" ); //NOI18N
- request.setJavaExecutable( javaExecutable );
+ if (tc != null) {
+ getLog().info("Toolchain in maven-pmd-plugin: " + tc);
+ String javaExecutable = tc.findTool("java"); // NOI18N
+ request.setJavaExecutable(javaExecutable);
}
- getLog().info( "PMD version: " + AbstractPmdReport.getPmdVersion() );
- cpdResult = CpdExecutor.execute( request );
- }
- catch ( UnsupportedEncodingException e )
- {
- throw new MavenReportException( "Encoding '" + getInputEncoding() + "' is not supported.", e );
- }
- catch ( IOException e )
- {
- throw new MavenReportException( e.getMessage(), e );
+ getLog().info("PMD version: " + AbstractPmdReport.getPmdVersion());
+ cpdResult = CpdExecutor.execute(request);
+ } catch (UnsupportedEncodingException e) {
+ throw new MavenReportException("Encoding '" + getInputEncoding() + "' is not supported.", e);
+ } catch (IOException e) {
+ throw new MavenReportException(e.getMessage(), e);
}
}
- private void generateMavenSiteReport( Locale locale )
- {
- CpdReportGenerator gen = new CpdReportGenerator( getSink(), filesToProcess, getBundle( locale ),
- isAggregator() );
- gen.generate( cpdResult.getDuplications() );
+ private void generateMavenSiteReport(Locale locale) {
+ CpdReportGenerator gen = new CpdReportGenerator(getSink(), filesToProcess, getBundle(locale), isAggregator());
+ gen.generate(cpdResult.getDuplications());
}
/**
* {@inheritDoc}
*/
- public String getOutputName()
- {
+ public String getOutputName() {
return "cpd";
}
- private static ResourceBundle getBundle( Locale locale )
- {
- return ResourceBundle.getBundle( "cpd-report", locale, CpdReport.class.getClassLoader() );
+ private static ResourceBundle getBundle(Locale locale) {
+ return ResourceBundle.getBundle("cpd-report", locale, CpdReport.class.getClassLoader());
}
/**
@@ -268,8 +233,7 @@ private static ResourceBundle getBundle( Locale locale )
* @deprecated Use {@link CpdExecutor#createRenderer(String, String)} instead.
*/
@Deprecated
- public CPDRenderer createRenderer() throws MavenReportException
- {
- return CpdExecutor.createRenderer( format, getOutputEncoding() );
+ public CPDRenderer createRenderer() throws MavenReportException {
+ return CpdExecutor.createRenderer(format, getOutputEncoding());
}
}
diff --git a/src/main/java/org/apache/maven/plugins/pmd/CpdReportGenerator.java b/src/main/java/org/apache/maven/plugins/pmd/CpdReportGenerator.java
index 18230556..dd135949 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/CpdReportGenerator.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/CpdReportGenerator.java
@@ -1,5 +1,3 @@
-package org.apache.maven.plugins.pmd;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -18,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+package org.apache.maven.plugins.pmd;
import java.io.File;
import java.util.List;
@@ -36,8 +35,7 @@
* @author mperham
* @version $Id$
*/
-public class CpdReportGenerator
-{
+public class CpdReportGenerator {
private Sink sink;
private Map" );
- sink.verbatim( null );
- sink.text( code );
+ sink.rawText(" ");
+ sink.verbatim(null);
+ sink.text(code);
sink.verbatim_();
- sink.rawText( " " );
+ sink.rawText("");
sink.tableRow_();
sink.tableRows_();
sink.table_();
diff --git a/src/main/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojo.java b/src/main/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojo.java
index 3feab0d1..7852f760 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojo.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojo.java
@@ -1,5 +1,3 @@
-package org.apache.maven.plugins.pmd;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -9,7 +7,7 @@
* "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
+ * 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
@@ -18,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+package org.apache.maven.plugins.pmd;
import java.io.File;
import java.io.FileInputStream;
@@ -27,14 +26,14 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
-import org.apache.maven.plugins.pmd.model.CpdErrorDetail;
-import org.apache.maven.plugins.pmd.model.CpdFile;
-import org.apache.maven.plugins.pmd.model.Duplication;
-import org.apache.maven.plugins.pmd.model.io.xpp3.CpdXpp3Reader;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.plugins.pmd.model.CpdErrorDetail;
+import org.apache.maven.plugins.pmd.model.CpdFile;
+import org.apache.maven.plugins.pmd.model.Duplication;
+import org.apache.maven.plugins.pmd.model.io.xpp3.CpdXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
/**
@@ -43,23 +42,20 @@
* @version $Id$
* @since 2.0
*/
-@Mojo( name = "cpd-check", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true )
-@Execute( goal = "cpd" )
-public class CpdViolationCheckMojo
- extends AbstractPmdViolationCheckMojotrue
if the given duplication should be excluded, false
otherwise.
*/
- public boolean isExcludedFromFailure( final Match errorDetail )
- {
+ public boolean isExcludedFromFailure(final Match errorDetail) {
final Settrue
if the violation should be excluded, false
otherwise.
*/
- boolean isExcludedFromFailure( D errorDetail );
-
+ boolean isExcludedFromFailure(D errorDetail);
}
diff --git a/src/main/java/org/apache/maven/plugins/pmd/ExcludeViolationsFromFile.java b/src/main/java/org/apache/maven/plugins/pmd/ExcludeViolationsFromFile.java
index a0e96814..13981b3f 100644
--- a/src/main/java/org/apache/maven/plugins/pmd/ExcludeViolationsFromFile.java
+++ b/src/main/java/org/apache/maven/plugins/pmd/ExcludeViolationsFromFile.java
@@ -1,5 +1,3 @@
-package org.apache.maven.plugins.pmd;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -18,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+package org.apache.maven.plugins.pmd;
import java.io.File;
import java.io.FileInputStream;
@@ -29,12 +28,11 @@
import java.util.Properties;
import java.util.Set;
+import net.sourceforge.pmd.RuleViolation;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.pmd.model.Violation;
-import net.sourceforge.pmd.RuleViolation;
-
/**
* This class contains utility for loading property files, which define which PMD violations
* from which classes should be ignored and not cause a failure.
@@ -42,52 +40,41 @@
*
* @author Andreas Dangel
*/
-public class ExcludeViolationsFromFile implements ExcludeFromFile
/rulesets/java/maven-pmd-plugin-default.xml
).
*/
@Parameter
- String[] rulesets = new String[] { "/rulesets/java/maven-pmd-plugin-default.xml" };
+ String[] rulesets = new String[] {"/rulesets/java/maven-pmd-plugin-default.xml"};
/**
* Controls whether the project's compile/test classpath should be passed to PMD to enable its type resolution
@@ -125,7 +121,7 @@ public class PmdReport
*
* @since 3.0
*/
- @Parameter( property = "pmd.typeResolution", defaultValue = "true" )
+ @Parameter(property = "pmd.typeResolution", defaultValue = "true")
private boolean typeResolution;
/**
@@ -133,7 +129,7 @@ public class PmdReport
*
* @since 3.1
*/
- @Parameter( property = "pmd.benchmark", defaultValue = "false" )
+ @Parameter(property = "pmd.benchmark", defaultValue = "false")
private boolean benchmark;
/**
@@ -141,8 +137,7 @@ public class PmdReport
*
* @since 3.1
*/
- @Parameter( property = "pmd.benchmarkOutputFilename",
- defaultValue = "${project.build.directory}/pmd-benchmark.txt" )
+ @Parameter(property = "pmd.benchmarkOutputFilename", defaultValue = "${project.build.directory}/pmd-benchmark.txt")
private String benchmarkOutputFilename;
/**
@@ -152,7 +147,7 @@ public class PmdReport
*
* @since 3.4
*/
- @Parameter( property = "pmd.suppressMarker" )
+ @Parameter(property = "pmd.suppressMarker")
private String suppressMarker;
/**
@@ -160,7 +155,7 @@ public class PmdReport
*
* @since 3.1
*/
- @Parameter( property = "pmd.skipPmdError", defaultValue = "true" )
+ @Parameter(property = "pmd.skipPmdError", defaultValue = "true")
private boolean skipPmdError;
/**
@@ -171,7 +166,7 @@ public class PmdReport
*
* @since 3.8
*/
- @Parameter( property = "pmd.analysisCache", defaultValue = "false" )
+ @Parameter(property = "pmd.analysisCache", defaultValue = "false")
private boolean analysisCache;
/**
@@ -183,7 +178,7 @@ public class PmdReport
*
* @since 3.8
*/
- @Parameter( property = "pmd.analysisCacheLocation", defaultValue = "${project.build.directory}/pmd/pmd.cache" )
+ @Parameter(property = "pmd.analysisCacheLocation", defaultValue = "${project.build.directory}/pmd/pmd.cache")
private String analysisCacheLocation;
/**
@@ -195,7 +190,7 @@ public class PmdReport
*
* @since 3.9.0
*/
- @Parameter( property = "pmd.renderProcessingErrors", defaultValue = "true" )
+ @Parameter(property = "pmd.renderProcessingErrors", defaultValue = "true")
private boolean renderProcessingErrors = true;
/**
@@ -203,7 +198,7 @@ public class PmdReport
*
* @since 3.10.0
*/
- @Parameter( property = "pmd.renderRuleViolationPriority", defaultValue = "true" )
+ @Parameter(property = "pmd.renderRuleViolationPriority", defaultValue = "true")
private boolean renderRuleViolationPriority = true;
/**
@@ -212,7 +207,7 @@ public class PmdReport
*
* @since 3.12.0
*/
- @Parameter( property = "pmd.renderViolationsByPriority", defaultValue = "true" )
+ @Parameter(property = "pmd.renderViolationsByPriority", defaultValue = "true")
private boolean renderViolationsByPriority = true;
/**
@@ -220,7 +215,7 @@ public class PmdReport
*
* @since 3.17.0
*/
- @Parameter( property = "pmd.renderSuppressedViolations", defaultValue = "true" )
+ @Parameter(property = "pmd.renderSuppressedViolations", defaultValue = "true")
private boolean renderSuppressedViolations = true;
/**
@@ -229,7 +224,7 @@ public class PmdReport
*
* @since 3.13.0
*/
- @Parameter( property = "pmd.rulesetsTargetDirectory", defaultValue = "${project.build.directory}/pmd/rulesets" )
+ @Parameter(property = "pmd.rulesetsTargetDirectory", defaultValue = "${project.build.directory}/pmd/rulesets")
private File rulesetsTargetDirectory;
/**
@@ -253,18 +248,16 @@ public class PmdReport
* {@inheritDoc}
*/
@Override
- public String getName( Locale locale )
- {
- return getBundle( locale ).getString( "report.pmd.name" );
+ public String getName(Locale locale) {
+ return getBundle(locale).getString("report.pmd.name");
}
/**
* {@inheritDoc}
*/
@Override
- public String getDescription( Locale locale )
- {
- return getBundle( locale ).getString( "report.pmd.description" );
+ public String getDescription(Locale locale) {
+ return getBundle(locale).getString("report.pmd.description");
}
/**
@@ -274,121 +267,98 @@ public String getDescription( Locale locale )
* @param rulesets the PMD rulesets to be used.
* @see #rulesets
*/
- public void setRulesets( String[] rulesets )
- {
- this.rulesets = Arrays.copyOf( rulesets, rulesets.length );
+ public void setRulesets(String[] rulesets) {
+ this.rulesets = Arrays.copyOf(rulesets, rulesets.length);
}
/**
* {@inheritDoc}
*/
@Override
- public void executeReport( Locale locale )
- throws MavenReportException
- {
+ public void executeReport(Locale locale) throws MavenReportException {
ClassLoader origLoader = Thread.currentThread().getContextClassLoader();
- try
- {
- Thread.currentThread().setContextClassLoader( this.getClass().getClassLoader() );
+ try {
+ Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
- generateMavenSiteReport( locale );
- }
- finally
- {
- Thread.currentThread().setContextClassLoader( origLoader );
+ generateMavenSiteReport(locale);
+ } finally {
+ Thread.currentThread().setContextClassLoader(origLoader);
}
}
@Override
- public boolean canGenerateReport()
- {
- if ( skip )
- {
- getLog().info( "Skipping PMD execution" );
+ public boolean canGenerateReport() {
+ if (skip) {
+ getLog().info("Skipping PMD execution");
return false;
}
boolean result = super.canGenerateReport();
- if ( result )
- {
- try
- {
+ if (result) {
+ try {
executePmd();
- if ( skipEmptyReport )
- {
+ if (skipEmptyReport) {
result = pmdResult.hasViolations();
- if ( !result )
- {
- getLog().debug( "Skipping report since skipEmptyReport is true and "
- + "there are no PMD violations." );
+ if (!result) {
+ getLog().debug("Skipping report since skipEmptyReport is true and "
+ + "there are no PMD violations.");
}
}
- }
- catch ( MavenReportException e )
- {
- throw new RuntimeException( e );
+ } catch (MavenReportException e) {
+ throw new RuntimeException(e);
}
}
return result;
}
- private void executePmd()
- throws MavenReportException
- {
- if ( pmdResult != null )
- {
+ private void executePmd() throws MavenReportException {
+ if (pmdResult != null) {
// PMD has already been run
- getLog().debug( "PMD has already been run - skipping redundant execution." );
+ getLog().debug("PMD has already been run - skipping redundant execution.");
return;
}
- try
- {
+ try {
filesToProcess = getFilesToProcess();
- if ( filesToProcess.isEmpty() && !"java".equals( language ) )
- {
- getLog().warn( "No files found to process. Did you add your additional source folders like javascript?"
- + " (see also build-helper-maven-plugin)" );
+ if (filesToProcess.isEmpty() && !"java".equals(language)) {
+ getLog().warn("No files found to process. Did you add your additional source folders like javascript?"
+ + " (see also build-helper-maven-plugin)");
}
+ } catch (IOException e) {
+ throw new MavenReportException("Can't get file list", e);
}
- catch ( IOException e )
- {
- throw new MavenReportException( "Can't get file list", e );
- }
-
PmdRequest request = new PmdRequest();
- request.setLanguageAndVersion( language, targetJdk );
- request.setRulesets( resolveRulesets() );
- request.setAuxClasspath( typeResolution ? determineAuxClasspath() : null );
- request.setSourceEncoding( getInputEncoding() );
- request.addFiles( filesToProcess.keySet() );
- request.setMinimumPriority( minimumPriority );
- request.setSuppressMarker( suppressMarker );
- request.setBenchmarkOutputLocation( benchmark ? benchmarkOutputFilename : null );
- request.setAnalysisCacheLocation( analysisCache ? analysisCacheLocation : null );
- request.setExcludeFromFailureFile( excludeFromFailureFile );
-
- request.setTargetDirectory( targetDirectory.getAbsolutePath() );
- request.setOutputEncoding( getOutputEncoding() );
- request.setFormat( format );
- request.setShowPmdLog( showPmdLog );
- request.setSkipPmdError( skipPmdError );
- request.setIncludeXmlInSite( includeXmlInSite );
- request.setReportOutputDirectory( getReportOutputDirectory().getAbsolutePath() );
- request.setLogLevel( determineCurrentRootLogLevel() );
+ request.setLanguageAndVersion(language, targetJdk);
+ request.setRulesets(resolveRulesets());
+ request.setAuxClasspath(typeResolution ? determineAuxClasspath() : null);
+ request.setSourceEncoding(getInputEncoding());
+ request.addFiles(filesToProcess.keySet());
+ request.setMinimumPriority(minimumPriority);
+ request.setSuppressMarker(suppressMarker);
+ request.setBenchmarkOutputLocation(benchmark ? benchmarkOutputFilename : null);
+ request.setAnalysisCacheLocation(analysisCache ? analysisCacheLocation : null);
+ request.setExcludeFromFailureFile(excludeFromFailureFile);
+
+ request.setTargetDirectory(targetDirectory.getAbsolutePath());
+ request.setOutputEncoding(getOutputEncoding());
+ request.setFormat(format);
+ request.setShowPmdLog(showPmdLog);
+ request.setSkipPmdError(skipPmdError);
+ request.setIncludeXmlInSite(includeXmlInSite);
+ request.setReportOutputDirectory(getReportOutputDirectory().getAbsolutePath());
+ request.setLogLevel(determineCurrentRootLogLevel());
Toolchain tc = getToolchain();
- if ( tc != null )
- {
- getLog().info( "Toolchain in maven-pmd-plugin: " + tc );
- String javaExecutable = tc.findTool( "java" ); //NOI18N
- request.setJavaExecutable( javaExecutable );
+ if (tc != null) {
+ getLog().info("Toolchain in maven-pmd-plugin: " + tc);
+ String javaExecutable = tc.findTool("java"); // NOI18N
+ request.setJavaExecutable(javaExecutable);
}
- getLog().info( "PMD version: " + AbstractPmdReport.getPmdVersion() );
- pmdResult = PmdExecutor.execute( request );
+ getLog().info("PMD version: " + AbstractPmdReport.getPmdVersion());
+ pmdResult = PmdExecutor.execute(request);
}
/**
@@ -397,99 +367,81 @@ private void executePmd()
* @return comma separated list of absolute file paths of ruleset files
* @throws MavenReportException if a ruleset could not be found
*/
- private ListThis class is intended to be serialized and read back. - * + * *
Some properties might be optional and can be This class is intended to be serialized and read back.
- *
+ *
* Some properties might be optional and can be null
.
*/
-public class CpdRequest implements Serializable
-{
+public class CpdRequest implements Serializable {
private static final long serialVersionUID = -7585852992660240668L;
private String javaExecutable;
@@ -56,143 +54,115 @@ public class CpdRequest implements Serializable
private boolean includeXmlInSite;
private String reportOutputDirectory;
- public void setJavaExecutable( String javaExecutable )
- {
+ public void setJavaExecutable(String javaExecutable) {
this.javaExecutable = javaExecutable;
}
- public void setMinimumTokens( int minimumTokens )
- {
+ public void setMinimumTokens(int minimumTokens) {
this.minimumTokens = minimumTokens;
}
- public void setLanguage( String language )
- {
+ public void setLanguage(String language) {
this.language = language;
}
- public void setLanguageProperties( Properties languageProperties )
- {
+ public void setLanguageProperties(Properties languageProperties) {
this.languageProperties = languageProperties;
}
- public void setSourceEncoding( String sourceEncoding )
- {
+ public void setSourceEncoding(String sourceEncoding) {
this.sourceEncoding = sourceEncoding;
}
- public void addFiles( Collectiontrue
then add the error details additionally (contains e.g. the stacktrace)
* @return the errors as string
*/
- private String getErrorsAsString( Listnull
.
*/
-public class PmdRequest implements Serializable
-{
+public class PmdRequest implements Serializable {
private static final long serialVersionUID = -6324416880563476455L;
private String javaExecutable;
@@ -67,223 +65,172 @@ public class PmdRequest implements Serializable
* @param language the language
* @param targetJdk the language version, optional, can be null
*/
- public void setLanguageAndVersion( String language, String targetJdk )
- {
- if ( "java".equals( language ) || null == language )
- {
+ public void setLanguageAndVersion(String language, String targetJdk) {
+ if ("java".equals(language) || null == language) {
this.language = "java";
this.languageVersion = targetJdk;
- }
- else if ( "javascript".equals( language ) || "ecmascript".equals( language ) )
- {
+ } else if ("javascript".equals(language) || "ecmascript".equals(language)) {
this.language = "ecmascript";
- }
- else if ( "jsp".equals( language ) )
- {
+ } else if ("jsp".equals(language)) {
this.language = "jsp";
- }
- else
- {
+ } else {
this.language = language;
}
}
- public void setJavaExecutable( String javaExecutable )
- {
+ public void setJavaExecutable(String javaExecutable) {
this.javaExecutable = javaExecutable;
}
- public void setMinimumPriority( int minimumPriority )
- {
+ public void setMinimumPriority(int minimumPriority) {
this.minimumPriority = minimumPriority;
}
- public void setAuxClasspath( String auxClasspath )
- {
+ public void setAuxClasspath(String auxClasspath) {
this.auxClasspath = auxClasspath;
}
- public void setSuppressMarker( String suppressMarker )
- {
+ public void setSuppressMarker(String suppressMarker) {
this.suppressMarker = suppressMarker;
}
- public void setAnalysisCacheLocation( String analysisCacheLocation )
- {
+ public void setAnalysisCacheLocation(String analysisCacheLocation) {
this.analysisCacheLocation = analysisCacheLocation;
}
- public void setRulesets( Listcontained
is contained in
* the given text
ignoring case.
@@ -161,8 +146,7 @@ protected String readFile( File pmdTestDir, String fileName ) throws IOException
* @param contains the string, the should be searched
* @return true
if the string is contained, otherwise false
.
*/
- public static boolean lowerCaseContains( String text, String contains )
- {
- return text.toLowerCase( Locale.ROOT ).contains( contains.toLowerCase( Locale.ROOT ) );
+ public static boolean lowerCaseContains(String text, String contains) {
+ return text.toLowerCase(Locale.ROOT).contains(contains.toLowerCase(Locale.ROOT));
}
}
diff --git a/src/test/java/org/apache/maven/plugins/pmd/CapturingPrintStream.java b/src/test/java/org/apache/maven/plugins/pmd/CapturingPrintStream.java
index 272a302b..f22d8fa5 100644
--- a/src/test/java/org/apache/maven/plugins/pmd/CapturingPrintStream.java
+++ b/src/test/java/org/apache/maven/plugins/pmd/CapturingPrintStream.java
@@ -1,5 +1,3 @@
-package org.apache.maven.plugins.pmd;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -9,7 +7,7 @@
* "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
+ * 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
@@ -18,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+package org.apache.maven.plugins.pmd;
import java.io.PrintStream;
@@ -30,54 +29,44 @@ class CapturingPrintStream extends PrintStream {
private boolean quiet;
private StringBuilder buffer = new StringBuilder();
- private CapturingPrintStream( boolean quiet ) {
- super( System.out, true );
+ private CapturingPrintStream(boolean quiet) {
+ super(System.out, true);
this.quiet = quiet;
}
@Override
- public void println( String x )
- {
- if ( !quiet )
- {
- super.println( x );
+ public void println(String x) {
+ if (!quiet) {
+ super.println(x);
}
- buffer.append( x ).append( System.lineSeparator() );
+ buffer.append(x).append(System.lineSeparator());
}
- public static void init( boolean quiet )
- {
+ public static void init(boolean quiet) {
CapturingPrintStream capture = get();
- if ( capture != null )
- {
- capture.buffer.setLength( 0 );
+ if (capture != null) {
+ capture.buffer.setLength(0);
capture.quiet = quiet;
- }
- else
- {
- capture = new CapturingPrintStream( quiet );
- System.setOut( capture );
+ } else {
+ capture = new CapturingPrintStream(quiet);
+ System.setOut(capture);
MavenSlf4jSimpleFriend.init();
}
}
- public static CapturingPrintStream get()
- {
- if ( System.out instanceof CapturingPrintStream )
- {
+ public static CapturingPrintStream get() {
+ if (System.out instanceof CapturingPrintStream) {
return (CapturingPrintStream) System.out;
}
return null;
}
- public static String getOutput()
- {
+ public static String getOutput() {
CapturingPrintStream stream = get();
- if ( stream != null )
- {
+ if (stream != null) {
stream.flush();
return stream.buffer.toString();
}
return "";
}
-}
\ No newline at end of file
+}
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 efb3d581..ac49a29b 100644
--- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
+++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java
@@ -1,5 +1,3 @@
-package org.apache.maven.plugins.pmd;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -9,7 +7,7 @@
* "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
+ * 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
@@ -18,6 +16,10 @@
* specific language governing permissions and limitations
* under the License.
*/
+package org.apache.maven.plugins.pmd;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
import java.io.BufferedReader;
import java.io.File;
@@ -25,9 +27,6 @@
import java.io.IOException;
import java.util.Locale;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-
import org.apache.commons.lang3.StringUtils;
import org.codehaus.plexus.util.FileUtils;
import org.w3c.dom.Document;
@@ -36,19 +35,15 @@
* @author Maria Odea Ching
* @version $Id$
*/
-public class CpdReportTest
- extends AbstractPmdReportTestCase
-{
+public class CpdReportTest extends AbstractPmdReportTestCase {
/**
* {@inheritDoc}
*/
@Override
- protected void setUp()
- throws Exception
- {
+ protected void setUp() throws Exception {
super.setUp();
- Locale.setDefault( Locale.ENGLISH );
- FileUtils.deleteDirectory( new File( getBasedir(), "target/test/unit" ) );
+ Locale.setDefault(Locale.ENGLISH);
+ FileUtils.deleteDirectory(new File(getBasedir(), "target/test/unit"));
}
/**
@@ -56,26 +51,25 @@ protected void setUp()
*
* @throws Exception
*/
- public void testDefaultConfiguration()
- throws Exception
- {
- File generatedReport = generateReport( "cpd", "default-configuration/cpd-default-configuration-plugin-config.xml" );
- assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
+ public void testDefaultConfiguration() throws Exception {
+ File generatedReport =
+ generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");
+ assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath()));
// check if the CPD files were generated
- File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
// check the contents of cpd.html
- String str = readFile( generatedReport );
- assertTrue( lowerCaseContains( str, "AppSample.java" ) );
- assertTrue( lowerCaseContains( str, "App.java" ) );
- assertTrue( lowerCaseContains( str, "public String dup( String str )" ) );
- assertTrue( lowerCaseContains( str, "tmp = tmp + str.substring( i, i + 1);" ) );
+ String str = readFile(generatedReport);
+ assertTrue(lowerCaseContains(str, "AppSample.java"));
+ assertTrue(lowerCaseContains(str, "App.java"));
+ assertTrue(lowerCaseContains(str, "public String dup( String str )"));
+ assertTrue(lowerCaseContains(str, "tmp = tmp + str.substring( i, i + 1);"));
// the version should be logged
String output = CapturingPrintStream.getOutput();
- assertTrue ( output.contains( "PMD version: " + AbstractPmdReport.getPmdVersion() ) );
+ assertTrue(output.contains("PMD version: " + AbstractPmdReport.getPmdVersion()));
}
/**
@@ -83,23 +77,21 @@ public void testDefaultConfiguration()
*
* @throws Exception
*/
- public void testTxtFormat()
- throws Exception
- {
- generateReport( "cpd", "custom-configuration/cpd-txt-format-configuration-plugin-config.xml" );
+ public void testTxtFormat() throws Exception {
+ generateReport("cpd", "custom-configuration/cpd-txt-format-configuration-plugin-config.xml");
// 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() ) );
+ 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 );
+ String str = readFile(generatedFile);
// Contents that should NOT be in the report
- assertFalse( lowerCaseContains( str, "public static void main( String[] args )" ) );
+ assertFalse(lowerCaseContains(str, "public static void main( String[] args )"));
// Contents that should be in the report
- assertTrue( lowerCaseContains( str, "public void duplicateMethod( int i )" ) );
+ assertTrue(lowerCaseContains(str, "public void duplicateMethod( int i )"));
}
/**
@@ -107,24 +99,22 @@ public void testTxtFormat()
*
* @throws Exception
*/
- public void testCustomConfiguration()
- throws Exception
- {
- File generatedReport = generateReport( "cpd", "custom-configuration/cpd-custom-configuration-plugin-config.xml" );
- assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
+ public void testCustomConfiguration() throws Exception {
+ File generatedReport = generateReport("cpd", "custom-configuration/cpd-custom-configuration-plugin-config.xml");
+ assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath()));
// check if the CPD files were generated
- File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.csv" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ File generatedFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/cpd.csv");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
- String str = readFile( generatedReport );
+ String str = readFile(generatedReport);
// Contents that should NOT be in the report
- assertFalse( lowerCaseContains( str, "/Sample.java" ) );
- assertFalse( lowerCaseContains( str, "public void duplicateMethod( int i )" ) );
+ assertFalse(lowerCaseContains(str, "/Sample.java"));
+ assertFalse(lowerCaseContains(str, "public void duplicateMethod( int i )"));
// Contents that should be in the report
- assertTrue( lowerCaseContains( str, "AnotherSample.java" ) );
- assertTrue( lowerCaseContains( str, "public static void main( String[] args )" ) );
- assertTrue( lowerCaseContains( str, "private String unusedMethod(" ) );
+ assertTrue(lowerCaseContains(str, "AnotherSample.java"));
+ assertTrue(lowerCaseContains(str, "public static void main( String[] args )"));
+ assertTrue(lowerCaseContains(str, "private String unusedMethod("));
}
/**
@@ -132,24 +122,19 @@ public void testCustomConfiguration()
*
* @throws Exception
*/
- public void testInvalidFormat()
- throws Exception
- {
- try
- {
- File testPom =
- new File( getBasedir(), "src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml" );
- AbstractPmdReport mojo = createReportMojo( "cpd", testPom );
- setVariableValueToObject( mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots() );
- generateReport( mojo, testPom );
-
- fail( "MavenReportException must be thrown" );
- }
- catch ( Exception e )
- {
- assertTrue( true );
+ public void testInvalidFormat() throws Exception {
+ try {
+ File testPom = new File(
+ getBasedir(), "src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml");
+ AbstractPmdReport mojo = createReportMojo("cpd", testPom);
+ setVariableValueToObject(
+ mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots());
+ generateReport(mojo, testPom);
+
+ fail("MavenReportException must be thrown");
+ } catch (Exception e) {
+ assertTrue(true);
}
-
}
/**
@@ -159,147 +144,123 @@ public void testInvalidFormat()
* @return a String object that contains the contents of the file
* @throws java.io.IOException
*/
- private String readFile( File file )
- throws IOException
- {
+ private String readFile(File file) throws IOException {
String strTmp;
- StringBuilder str = new StringBuilder( (int) file.length() );
- try ( BufferedReader in = new BufferedReader( new FileReader( file ) ) )
- {
- while ( ( strTmp = in.readLine() ) != null )
- {
- str.append( ' ' );
- str.append( strTmp );
+ StringBuilder str = new StringBuilder((int) file.length());
+ try (BufferedReader in = new BufferedReader(new FileReader(file))) {
+ while ((strTmp = in.readLine()) != null) {
+ str.append(' ');
+ str.append(strTmp);
}
}
return str.toString();
}
- public void testWriteNonHtml()
- throws Exception
- {
- generateReport( "cpd", "default-configuration/cpd-default-configuration-plugin-config.xml" );
+ public void testWriteNonHtml() throws Exception {
+ generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");
// check if the CPD files were generated
- File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
- Document pmdCpdDocument = builder.parse( generatedFile );
- assertNotNull( pmdCpdDocument );
-
- String str = readFile( generatedFile );
- assertTrue( lowerCaseContains( str, "AppSample.java" ) );
- assertTrue( lowerCaseContains( str, "App.java" ) );
- assertTrue( lowerCaseContains( str, "public String dup( String str )" ) );
- assertTrue( lowerCaseContains( str, "tmp = tmp + str.substring( i, i + 1);" ) );
+ Document pmdCpdDocument = builder.parse(generatedFile);
+ assertNotNull(pmdCpdDocument);
+
+ String str = readFile(generatedFile);
+ assertTrue(lowerCaseContains(str, "AppSample.java"));
+ assertTrue(lowerCaseContains(str, "App.java"));
+ assertTrue(lowerCaseContains(str, "public String dup( String str )"));
+ assertTrue(lowerCaseContains(str, "tmp = tmp + str.substring( i, i + 1);"));
}
/**
* verify the cpd.xml file is included in the site when requested.
* @throws Exception
*/
- public void testIncludeXmlInSite()
- throws Exception
- {
- generateReport( "cpd", "default-configuration/cpd-report-include-xml-in-site-plugin-config.xml" );
+ public void testIncludeXmlInSite() throws Exception {
+ generateReport("cpd", "default-configuration/cpd-report-include-xml-in-site-plugin-config.xml");
- File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
- Document pmdCpdDocument = builder.parse( generatedFile );
- assertNotNull( pmdCpdDocument );
+ Document pmdCpdDocument = builder.parse(generatedFile);
+ assertNotNull(pmdCpdDocument);
- String str = readFile( generatedFile );
- assertTrue( str.contains( "" ) );
+ String str = readFile(generatedFile);
+ assertTrue(str.contains(""));
- File siteReport = new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.xml" );
- assertTrue( FileUtils.fileExists( siteReport.getAbsolutePath() ) );
- String siteReportContent = readFile( siteReport );
- assertTrue( siteReportContent.contains( "" ) );
- assertEquals( str, siteReportContent );
+ File siteReport = new File(getBasedir(), "target/test/unit/default-configuration/target/site/cpd.xml");
+ assertTrue(FileUtils.fileExists(siteReport.getAbsolutePath()));
+ String siteReportContent = readFile(siteReport);
+ assertTrue(siteReportContent.contains(""));
+ assertEquals(str, siteReportContent);
}
-
- public void testSkipEmptyReportConfiguration()
- throws Exception
- {
+ public void testSkipEmptyReportConfiguration() throws Exception {
// verify the generated files do not exist because PMD was skipped
- File generatedReport = generateReport( "cpd", "empty-report/cpd-skip-empty-report-plugin-config.xml" );
- assertFalse( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
+ File generatedReport = generateReport("cpd", "empty-report/cpd-skip-empty-report-plugin-config.xml");
+ assertFalse(FileUtils.fileExists(generatedReport.getAbsolutePath()));
}
- public void testEmptyReportConfiguration()
- throws Exception
- {
+ public void testEmptyReportConfiguration() throws Exception {
// verify the generated files do exist, even if there are no violations
- File generatedReport = generateReport( "cpd", "empty-report/cpd-empty-report-plugin-config.xml" );
- assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
+ File generatedReport = generateReport("cpd", "empty-report/cpd-empty-report-plugin-config.xml");
+ assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath()));
- String str = readFile( generatedReport );
- assertFalse( lowerCaseContains( str, "Hello.java" ) );
- assertTrue( str.contains( "CPD found no problems in your source code." ) );
+ String str = readFile(generatedReport);
+ assertFalse(lowerCaseContains(str, "Hello.java"));
+ assertTrue(str.contains("CPD found no problems in your source code."));
}
- public void testCpdEncodingConfiguration()
- throws Exception
- {
- String originalEncoding = System.getProperty( "file.encoding" );
- try
- {
- System.setProperty( "file.encoding", "UTF-16" );
+ public void testCpdEncodingConfiguration() throws Exception {
+ String originalEncoding = System.getProperty("file.encoding");
+ try {
+ System.setProperty("file.encoding", "UTF-16");
- generateReport( "cpd", "default-configuration/cpd-default-configuration-plugin-config.xml" );
+ generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");
// check if the CPD files were generated
- File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
- String str = readFile( generatedFile );
- assertTrue( lowerCaseContains( str, "AppSample.java" ) );
- }
- finally
- {
- System.setProperty( "file.encoding", originalEncoding );
+ File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
+ String str = readFile(generatedFile);
+ assertTrue(lowerCaseContains(str, "AppSample.java"));
+ } finally {
+ System.setProperty("file.encoding", originalEncoding);
}
}
- public void testCpdJavascriptConfiguration()
- throws Exception
- {
- generateReport( "cpd", "default-configuration/cpd-javascript-plugin-config.xml" );
+ public void testCpdJavascriptConfiguration() throws Exception {
+ generateReport("cpd", "default-configuration/cpd-javascript-plugin-config.xml");
// verify the generated file to exist and violations are reported
- File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
- String str = readFile( generatedFile );
- assertTrue( lowerCaseContains( str, "Sample.js" ) );
- assertTrue( lowerCaseContains( str, "SampleDup.js" ) );
+ File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
+ String str = readFile(generatedFile);
+ assertTrue(lowerCaseContains(str, "Sample.js"));
+ assertTrue(lowerCaseContains(str, "SampleDup.js"));
}
- public void testCpdJspConfiguration()
- throws Exception
- {
- generateReport( "cpd", "default-configuration/cpd-jsp-plugin-config.xml" );
+ public void testCpdJspConfiguration() throws Exception {
+ generateReport("cpd", "default-configuration/cpd-jsp-plugin-config.xml");
// verify the generated file to exist and violations are reported
- File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
- String str = readFile( generatedFile );
- assertTrue( lowerCaseContains( str, "sample.jsp" ) );
- assertTrue( lowerCaseContains( str, "sampleDup.jsp" ) );
+ File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
+ String str = readFile(generatedFile);
+ assertTrue(lowerCaseContains(str, "sample.jsp"));
+ assertTrue(lowerCaseContains(str, "sampleDup.jsp"));
}
- public void testExclusionsConfiguration()
- throws Exception
- {
- generateReport( "cpd", "default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml" );
+ public void testExclusionsConfiguration() throws Exception {
+ generateReport("cpd", "default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml");
// verify the generated file to exist and no duplications are reported
- File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
- String str = readFile( generatedFile );
- assertEquals( 0, StringUtils.countMatches( str, "Priority " ) );
+ assertTrue(str.contains("Priority "));
// there should be a rule column
- assertTrue( str.contains( "Rule " ) );
+ assertTrue(str.contains("Rule "));
// along with a link to the rule
- assertTrue( str.contains( "pmd_rules_java_bestpractices.html#unusedprivatefield\">UnusedPrivateField" ) );
+ assertTrue(str.contains("pmd_rules_java_bestpractices.html#unusedprivatefield\">UnusedPrivateField"));
// there should be the section Violations By Priority
- assertTrue( str.contains( "Violations By Priority" ) );
- assertTrue( str.contains( "Priority 3" ) );
- assertTrue( str.contains( "Priority 4" ) );
+ assertTrue(str.contains("Violations By Priority"));
+ assertTrue(str.contains("Priority 3"));
+ assertTrue(str.contains("Priority 4"));
// the file App.java is mentioned 3 times: in prio 3, in prio 4 and in the files section
- assertEquals( 3, StringUtils.countMatches( str, "def/configuration/App.java" ) );
+ assertEquals(3, StringUtils.countMatches(str, "def/configuration/App.java"));
// there must be no warnings (like deprecated rules) in the log output
String output = CapturingPrintStream.getOutput();
- assertFalse( output.contains( "deprecated Rule name" ) );
- assertFalse( output.contains( "Discontinue using Rule name" ) );
- assertFalse( output.contains( "is referenced multiple times" ) );
+ assertFalse(output.contains("deprecated Rule name"));
+ assertFalse(output.contains("Discontinue using Rule name"));
+ assertFalse(output.contains("is referenced multiple times"));
// the version should be logged
- assertTrue ( output.contains( "PMD version: " + AbstractPmdReport.getPmdVersion() ) );
+ assertTrue(output.contains("PMD version: " + AbstractPmdReport.getPmdVersion()));
}
- public void testDefaultConfigurationNotRenderRuleViolationPriority()
- throws Exception
- {
- FileUtils.copyDirectoryStructure( new File( getBasedir(),
- "src/test/resources/unit/default-configuration/jxr-files" ),
- new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
+ public void testDefaultConfigurationNotRenderRuleViolationPriority() throws Exception {
+ FileUtils.copyDirectoryStructure(
+ new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"),
+ new File(getBasedir(), "target/test/unit/default-configuration/target/site"));
- File generatedReport = generateReport( "pmd", "default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml" );
- assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
+ File generatedReport =
+ generateReport("pmd", "default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml");
+ assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath()));
- String str = readFile( generatedReport );
+ String str = readFile(generatedReport);
// check that there's no priority column
- assertFalse( str.contains( "Priority " ) );
+ assertFalse(str.contains("Priority "));
}
- public void testDefaultConfigurationNoRenderViolationsByPriority()
- throws Exception
- {
- FileUtils.copyDirectoryStructure( new File( getBasedir(),
- "src/test/resources/unit/default-configuration/jxr-files" ),
- new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
+ public void testDefaultConfigurationNoRenderViolationsByPriority() throws Exception {
+ FileUtils.copyDirectoryStructure(
+ new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"),
+ new File(getBasedir(), "target/test/unit/default-configuration/target/site"));
- File generatedReport = generateReport( "pmd", "default-configuration/pmd-report-no-render-violations-by-priority.xml" );
- assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
+ File generatedReport =
+ generateReport("pmd", "default-configuration/pmd-report-no-render-violations-by-priority.xml");
+ assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath()));
- String str = readFile( generatedReport );
+ String str = readFile(generatedReport);
// there should be no section Violations By Priority
- assertFalse( str.contains( "Violations By Priority" ) );
- assertFalse( str.contains( "Priority 3" ) );
- assertFalse( str.contains( "Priority 4" ) );
+ assertFalse(str.contains("Violations By Priority"));
+ assertFalse(str.contains("Priority 3"));
+ assertFalse(str.contains("Priority 4"));
// the file App.java is mentioned once: in the files section
- assertEquals( 1, StringUtils.countMatches( str, "def/configuration/App.java" ) );
+ assertEquals(1, StringUtils.countMatches(str, "def/configuration/App.java"));
}
+ public void testDefaultConfigurationWithAnalysisCache() throws Exception {
+ FileUtils.copyDirectoryStructure(
+ new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"),
+ new File(getBasedir(), "target/test/unit/pmd-with-analysis-cache-plugin-config/target/site"));
- public void testDefaultConfigurationWithAnalysisCache()
- throws Exception
- {
- FileUtils.copyDirectoryStructure( new File( getBasedir(),
- "src/test/resources/unit/default-configuration/jxr-files" ),
- new File( getBasedir(), "target/test/unit/pmd-with-analysis-cache-plugin-config/target/site" ) );
-
- generateReport( "pmd", "default-configuration/pmd-with-analysis-cache-plugin-config.xml" );
+ generateReport("pmd", "default-configuration/pmd-with-analysis-cache-plugin-config.xml");
// check if the PMD analysis cache file has been generated
- File cacheFile = new File( getBasedir(), "target/test/unit/pmd-with-analysis-cache-plugin-config/target/pmd/pmd.cache" );
- assertTrue( FileUtils.fileExists( cacheFile.getAbsolutePath() ) );
+ File cacheFile =
+ new File(getBasedir(), "target/test/unit/pmd-with-analysis-cache-plugin-config/target/pmd/pmd.cache");
+ assertTrue(FileUtils.fileExists(cacheFile.getAbsolutePath()));
}
- public void testJavascriptConfiguration()
- throws Exception
- {
- File generatedReport = generateReport( "pmd", "default-configuration/javascript-configuration-plugin-config.xml" );
- assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
+ public void testJavascriptConfiguration() throws Exception {
+ File generatedReport =
+ generateReport("pmd", "default-configuration/javascript-configuration-plugin-config.xml");
+ assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath()));
// check if the PMD files were generated
- File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/pmd.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
// these are the rulesets, that have been applied...
- generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/bestpractices.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ generatedFile =
+ new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/bestpractices.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
- generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/codestyle.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ generatedFile =
+ new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/codestyle.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
- generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/errorprone.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ generatedFile =
+ new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/errorprone.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
- String str = readFile( generatedReport );
- assertTrue( str.contains( "Avoid using global variables" ) );
+ String str = readFile(generatedReport);
+ assertTrue(str.contains("Avoid using global variables"));
}
- public void testFileURL()
- throws Exception
- {
- FileUtils.copyDirectoryStructure( new File( getBasedir(),
- "src/test/resources/unit/default-configuration/jxr-files" ),
- new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
+ public void testFileURL() throws Exception {
+ FileUtils.copyDirectoryStructure(
+ new File(getBasedir(), "src/test/resources/unit/default-configuration/jxr-files"),
+ new File(getBasedir(), "target/test/unit/default-configuration/target/site"));
- File testPom =
- new File( getBasedir(),
- "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
- PmdReport mojo = (PmdReport) createReportMojo( "pmd", testPom );
+ File testPom = new File(
+ getBasedir(), "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml");
+ PmdReport mojo = (PmdReport) createReportMojo("pmd", testPom);
// Additional test case for MPMD-174 (https://issues.apache.org/jira/browse/MPMD-174).
int port = determineFreePort();
- WireMockServer mockServer = new WireMockServer( port );
+ WireMockServer mockServer = new WireMockServer(port);
mockServer.start();
- String sonarRuleset =
- IOUtils.toString( getClass().getClassLoader().getResourceAsStream( "unit/default-configuration/rulesets/sonar-way-ruleset.xml" ),
- StandardCharsets.UTF_8 );
+ String sonarRuleset = IOUtils.toString(
+ getClass()
+ .getClassLoader()
+ .getResourceAsStream("unit/default-configuration/rulesets/sonar-way-ruleset.xml"),
+ StandardCharsets.UTF_8);
- String sonarMainPageHtml =
- IOUtils.toString( getClass().getClassLoader().getResourceAsStream( "unit/default-configuration/rulesets/sonar-main-page.html" ),
- StandardCharsets.UTF_8 );
+ String sonarMainPageHtml = IOUtils.toString(
+ getClass()
+ .getClassLoader()
+ .getResourceAsStream("unit/default-configuration/rulesets/sonar-main-page.html"),
+ StandardCharsets.UTF_8);
final String sonarBaseUrl = "/profiles";
final String sonarProfileUrl = sonarBaseUrl + "/export?format=pmd&language=java&name=Sonar%2520way";
final String sonarExportRulesetUrl = "http://localhost:" + mockServer.port() + sonarProfileUrl;
- WireMock.configureFor( "localhost", port );
- WireMock.stubFor( WireMock.get( WireMock.urlEqualTo( sonarBaseUrl ) ).willReturn( WireMock.aResponse().withStatus( 200 ).withHeader( "Content-Type",
- "text/html" ).withBody( sonarMainPageHtml ) ) );
+ WireMock.configureFor("localhost", port);
+ WireMock.stubFor(WireMock.get(WireMock.urlEqualTo(sonarBaseUrl))
+ .willReturn(WireMock.aResponse()
+ .withStatus(200)
+ .withHeader("Content-Type", "text/html")
+ .withBody(sonarMainPageHtml)));
- WireMock.stubFor( WireMock.get( WireMock.urlEqualTo( sonarProfileUrl ) ).willReturn( WireMock.aResponse().withStatus( 200 ).withHeader( "Content-Type",
- "text/xml" ).withBody( sonarRuleset ) ) );
+ WireMock.stubFor(WireMock.get(WireMock.urlEqualTo(sonarProfileUrl))
+ .willReturn(WireMock.aResponse()
+ .withStatus(200)
+ .withHeader("Content-Type", "text/xml")
+ .withBody(sonarRuleset)));
- URL url = getClass().getClassLoader().getResource( "rulesets/java/basic.xml" );
- URL url2 = getClass().getClassLoader().getResource( "rulesets/java/unusedcode.xml" );
- URL url3 = getClass().getClassLoader().getResource( "rulesets/java/imports.xml" );
- mojo.setRulesets( new String[] { url.toString(), url2.toString(), url3.toString(), sonarExportRulesetUrl } );
+ URL url = getClass().getClassLoader().getResource("rulesets/java/basic.xml");
+ URL url2 = getClass().getClassLoader().getResource("rulesets/java/unusedcode.xml");
+ URL url3 = getClass().getClassLoader().getResource("rulesets/java/imports.xml");
+ mojo.setRulesets(new String[] {url.toString(), url2.toString(), url3.toString(), sonarExportRulesetUrl});
- File generatedReport = generateReport( mojo, testPom );
- assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
+ File generatedReport = generateReport(mojo, testPom);
+ assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath()));
// check if the PMD files were generated
- File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/pmd.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
// the resolved and extracted rulesets
- generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/basic.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
-
- generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/imports.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/basic.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
- generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/unusedcode.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ generatedFile =
+ new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/imports.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
generatedFile =
- new File( getBasedir(),
- "target/test/unit/default-configuration/target/pmd/rulesets/export_format_pmd_language_java_name_Sonar_2520way.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ new File(getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/unusedcode.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
+
+ generatedFile = new File(
+ getBasedir(),
+ "target/test/unit/default-configuration/target/pmd/rulesets/export_format_pmd_language_java_name_Sonar_2520way.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
// check if there's a link to the JXR files
- String str = readFile( generatedReport );
+ String str = readFile(generatedReport);
- assertTrue( str.contains( "/xref/def/configuration/App.html#L31" ) );
+ assertTrue(str.contains("/xref/def/configuration/App.html#L31"));
- assertTrue( str.contains( "/xref/def/configuration/AppSample.html#L45" ) );
+ assertTrue(str.contains("/xref/def/configuration/AppSample.html#L45"));
mockServer.stop();
}
- private int determineFreePort()
- {
+ private int determineFreePort() {
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
} catch (IOException e) {
- throw new RuntimeException( "Couldn't find a free port.", e );
+ throw new RuntimeException("Couldn't find a free port.", e);
}
}
@@ -271,35 +270,34 @@ private int determineFreePort()
*
* @throws Exception
*/
- public void testCustomConfiguration()
- throws Exception
- {
- File generatedReport = generateReport( "pmd", "custom-configuration/custom-configuration-plugin-config.xml" );
- assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
+ public void testCustomConfiguration() throws Exception {
+ File generatedReport = generateReport("pmd", "custom-configuration/custom-configuration-plugin-config.xml");
+ assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath()));
// check the generated files
- File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/pmd.csv" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ File generatedFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/pmd.csv");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
- generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/pmd/rulesets/custom.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ generatedFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/pmd/rulesets/custom.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
// check if custom ruleset was applied
- String str = readFile( generatedReport );
+ String str = readFile(generatedReport);
// codestyle.xml/ControlStatementBraces:
- assertTrue( lowerCaseContains( str, "This statement should have braces" ) );
+ assertTrue(lowerCaseContains(str, "This statement should have braces"));
// Must be false as codestyle.xml/ControlStatementBraces with checkIfElseStmt=false is used
- assertFalse( lowerCaseContains( str, "Avoid using if...else statements without curly braces" ) );
+ assertFalse(lowerCaseContains(str, "Avoid using if...else statements without curly braces"));
- assertFalse( "unnecessary constructor should not be triggered because of low priority",
- lowerCaseContains( str, "Avoid unnecessary constructors - the compiler will generate these for you" ) );
+ assertFalse(
+ "unnecessary constructor should not be triggered because of low priority",
+ lowerCaseContains(str, "Avoid unnecessary constructors - the compiler will generate these for you"));
// veryLongVariableNameWithViolation is really too long
- assertTrue( lowerCaseContains( str, "veryLongVariableNameWithViolation" ) );
+ assertTrue(lowerCaseContains(str, "veryLongVariableNameWithViolation"));
// notSoLongVariableName should not be reported
- assertFalse( lowerCaseContains( str, "notSoLongVariableName" ) );
+ assertFalse(lowerCaseContains(str, "notSoLongVariableName"));
}
/**
@@ -307,78 +305,63 @@ public void testCustomConfiguration()
*
* @throws Exception
*/
- public void testSkipConfiguration()
- throws Exception
- {
- File generatedReport = generateReport( "pmd", "custom-configuration/skip-plugin-config.xml" );
- assertFalse( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
+ public void testSkipConfiguration() throws Exception {
+ File generatedReport = generateReport("pmd", "custom-configuration/skip-plugin-config.xml");
+ assertFalse(FileUtils.fileExists(generatedReport.getAbsolutePath()));
// verify the generated files do not exist because PMD was skipped
- File generatedFile = new File( getBasedir(), "target/test/unit/skip-configuration/target/pmd.csv" );
- assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ File generatedFile = new File(getBasedir(), "target/test/unit/skip-configuration/target/pmd.csv");
+ assertFalse(FileUtils.fileExists(generatedFile.getAbsolutePath()));
- generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/custom.xml" );
- assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ generatedFile = new File(getBasedir(), "target/test/unit/custom-configuration/target/custom.xml");
+ assertFalse(FileUtils.fileExists(generatedFile.getAbsolutePath()));
// the fact, the PMD execution has been skipped, should be logged
String output = CapturingPrintStream.getOutput();
- assertTrue ( output.contains( "Skipping PMD execution" ) );
+ assertTrue(output.contains("Skipping PMD execution"));
}
- public void testSkipEmptyReportConfiguration()
- throws Exception
- {
+ public void testSkipEmptyReportConfiguration() throws Exception {
// verify the generated files do not exist because PMD was skipped
- File generatedReport = generateReport( "pmd", "empty-report/skip-empty-report-plugin-config.xml" );
- assertFalse( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
+ File generatedReport = generateReport("pmd", "empty-report/skip-empty-report-plugin-config.xml");
+ assertFalse(FileUtils.fileExists(generatedReport.getAbsolutePath()));
}
- public void testEmptyReportConfiguration()
- throws Exception
- {
- File generatedReport = generateReport( "pmd", "empty-report/empty-report-plugin-config.xml" );
- assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
+ public void testEmptyReportConfiguration() throws Exception {
+ File generatedReport = generateReport("pmd", "empty-report/empty-report-plugin-config.xml");
+ assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath()));
// verify the generated files do exist, even if there are no violations
- String str = readFile( generatedReport );
- assertFalse( lowerCaseContains( str, "Hello.java" ) );
- assertEquals( 1, StringUtils.countMatches( str, "PMD found no problems in your source code." ) );
+ String str = readFile(generatedReport);
+ assertFalse(lowerCaseContains(str, "Hello.java"));
+ assertEquals(1, StringUtils.countMatches(str, "PMD found no problems in your source code."));
// no sections files or violations by priority
- assertFalse( str.contains( "Files" ) );
- assertFalse( str.contains( "Violations By Priority" ) );
+ assertFalse(str.contains("Files"));
+ assertFalse(str.contains("Violations By Priority"));
}
- public void testInvalidFormat()
- throws Exception
- {
- try
- {
+ public void testInvalidFormat() throws Exception {
+ try {
File testPom =
- new File( getBasedir(), "src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml" );
- AbstractPmdReport mojo = createReportMojo( "pmd", testPom );
- setVariableValueToObject( mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots() );
- generateReport( mojo, testPom );
-
- fail( "Must throw MavenReportException." );
- }
- catch ( Exception e )
- {
- assertTrue( true );
+ new File(getBasedir(), "src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml");
+ AbstractPmdReport mojo = createReportMojo("pmd", testPom);
+ setVariableValueToObject(
+ mojo, "compileSourceRoots", mojo.getProject().getCompileSourceRoots());
+ generateReport(mojo, testPom);
+
+ fail("Must throw MavenReportException.");
+ } catch (Exception e) {
+ assertTrue(true);
}
}
- public void testInvalidTargetJdk()
- throws Exception
- {
- try
- {
- generateReport( "pmd", "empty-report/invalid-format/invalid-target-jdk-plugin-config.xml" );
+ public void testInvalidTargetJdk() throws Exception {
+ try {
+ generateReport("pmd", "empty-report/invalid-format/invalid-target-jdk-plugin-config.xml");
- fail( "Must throw MavenReportException." );
- }
- catch ( Exception e )
- {
- assertTrue( true );
+ fail("Must throw MavenReportException.");
+ } catch (Exception e) {
+ assertTrue(true);
}
}
@@ -386,22 +369,21 @@ public void testInvalidTargetJdk()
* verify the pmd.xml file is included in the site when requested.
* @throws Exception
*/
- public void testIncludeXmlInSite()
- throws Exception
- {
- File generatedReport = generateReport( "pmd", "default-configuration/pmd-report-include-xml-in-site-plugin-config.xml" );
- assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
+ public void testIncludeXmlInSite() throws Exception {
+ File generatedReport =
+ generateReport("pmd", "default-configuration/pmd-report-include-xml-in-site-plugin-config.xml");
+ assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath()));
// verify the pmd file is included in site
- File generatedXmlFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.xml" );
- assertTrue( FileUtils.fileExists( generatedXmlFile.getAbsolutePath() ) );
+ File generatedXmlFile = new File(getBasedir(), "target/test/unit/default-configuration/target/site/pmd.xml");
+ assertTrue(FileUtils.fileExists(generatedXmlFile.getAbsolutePath()));
- String pmdXmlTarget = readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" ) );
- assertTrue( pmdXmlTarget.contains( "" ) );
+ String pmdXmlTarget = readFile(new File(getBasedir(), "target/test/unit/default-configuration/target/pmd.xml"));
+ assertTrue(pmdXmlTarget.contains(""));
// check that pmd.xml file has the closing element
- String pmdXml = readFile( generatedXmlFile );
- assertTrue( pmdXml.contains( "" ) );
+ String pmdXml = readFile(generatedXmlFile);
+ assertTrue(pmdXml.contains(""));
}
/**
@@ -411,18 +393,14 @@ public void testIncludeXmlInSite()
* @return a String object that contains the contents of the file
* @throws java.io.IOException
*/
- private String readFile( File file )
- throws IOException
- {
- try ( BufferedReader reader = new BufferedReader( new FileReader( file ) ) )
- {
- final StringBuilder str = new StringBuilder( (int) file.length() );
-
- for ( String line = reader.readLine(); line != null; line = reader.readLine() )
- {
- str.append( ' ' );
- str.append( line );
- str.append( '\n' );
+ private String readFile(File file) throws IOException {
+ try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
+ final StringBuilder str = new StringBuilder((int) file.length());
+
+ for (String line = reader.readLine(); line != null; line = reader.readLine()) {
+ str.append(' ');
+ str.append(line);
+ str.append('\n');
}
return str.toString();
}
@@ -433,19 +411,17 @@ private String readFile( File file )
*
* @throws Exception
*/
- public void testLocationTemp()
- throws Exception
- {
-
- File testPom =
- new File( getBasedir(),
- "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
- PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
+ public void testLocationTemp() throws Exception {
- assertEquals( "locationTemp is not correctly encoding filename",
- "export_format_pmd_language_java_name_some_2520name.xml",
- mojo.getLocationTemp( "http://nemo.sonarsource.org/sonar/profiles/export?format=pmd&language=java&name=some%2520name" ) );
+ File testPom = new File(
+ getBasedir(), "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml");
+ PmdReport mojo = (PmdReport) lookupMojo("pmd", testPom);
+ assertEquals(
+ "locationTemp is not correctly encoding filename",
+ "export_format_pmd_language_java_name_some_2520name.xml",
+ mojo.getLocationTemp(
+ "http://nemo.sonarsource.org/sonar/profiles/export?format=pmd&language=java&name=some%2520name"));
}
/**
@@ -453,259 +429,262 @@ public void testLocationTemp()
*
* @throws Exception
*/
- public void testSuppressMarkerConfiguration()
- throws Exception
- {
- File generatedReport = generateReport( "pmd", "default-configuration/pmd-with-suppressMarker-plugin-config.xml" );
- assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
+ public void testSuppressMarkerConfiguration() throws Exception {
+ File generatedReport = generateReport("pmd", "default-configuration/pmd-with-suppressMarker-plugin-config.xml");
+ assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath()));
// check if the PMD files were generated
- File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
- assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
+ File generatedFile = new File(getBasedir(), "target/test/unit/default-configuration/target/pmd.xml");
+ assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
- String str = readFile( generatedFile );
+ String str = readFile(generatedFile);
// check that there is no violation reported for "unusedVar2" - as it is suppressed
- assertFalse( str.contains( "Avoid unused private fields such as 'unusedVar2'.\n