diff --git a/pom.xml b/pom.xml index 1926d5ef..f6186e9b 100644 --- a/pom.xml +++ b/pom.xml @@ -91,6 +91,7 @@ under the License. 8 6.48.0 1.7.36 + 1.0.0.v20140518 3.12.1 3.4.1 2022-05-31T17:37:31Z @@ -203,6 +204,12 @@ under the License. doxia-site-renderer requires transitively dom4j 1.1 which provides a very old version of jaxen (same jar). PMD requires a newer version of jaxen, which is not compatible. --> + + + org.apache.httpcomponents + httpclient + + @@ -243,11 +250,43 @@ under the License. com.github.tomakehurst wiremock - - 1.49 + 1.58 + test + + + org.apache.httpcomponents + httpclient + + + + + org.eclipse.aether + aether-api + ${aetherVersion} + test + + + org.eclipse.aether + aether-impl + ${aetherVersion} + test + + + org.eclipse.aether + aether-connector-basic + ${aetherVersion} + test + + + org.eclipse.aether + aether-transport-wagon + ${aetherVersion} + test + + + org.apache.maven.wagon + wagon-http-lightweight + 3.5.2 test diff --git a/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTest.java deleted file mode 100644 index 3656d0d1..00000000 --- a/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package org.apache.maven.plugins.pmd; - -/* - * 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. - */ - -import java.io.File; -import java.io.IOException; -import java.io.Writer; -import java.util.Locale; - -import org.apache.maven.doxia.site.decoration.DecorationModel; -import org.apache.maven.doxia.siterenderer.DocumentContent; -import org.apache.maven.doxia.siterenderer.RendererException; -import org.apache.maven.doxia.siterenderer.SiteRenderingContext; -import org.apache.maven.execution.DefaultMavenExecutionRequest; -import org.apache.maven.execution.MavenExecutionRequest; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.plugin.Mojo; -import org.apache.maven.plugin.testing.AbstractMojoTestCase; -import org.codehaus.plexus.util.ReflectionUtils; -import org.codehaus.plexus.util.WriterFactory; - -/** - * @author Vincent Siveton - * @version $Id$ - * @since 2.5 - */ -public abstract class AbstractPmdReportTest - extends AbstractMojoTestCase -{ - @Override - protected void setUp() - throws Exception - { - super.setUp(); - CapturingPrintStream.init( true ); - } - - /** - * Renderer the sink from the report mojo. - * - * @param mojo not null - * @param outputHtml not null - * @throws RendererException if any - * @throws IOException if any - */ - protected void renderer( AbstractPmdReport mojo, File outputHtml ) - throws RendererException, IOException - { - SiteRenderingContext context = new SiteRenderingContext(); - context.setDecoration( new DecorationModel() ); - context.setTemplateName( "org/apache/maven/doxia/siterenderer/resources/default-site.vm" ); - context.setLocale( Locale.ENGLISH ); - - outputHtml.getParentFile().mkdirs(); - - try ( Writer writer = WriterFactory.newXmlWriter( outputHtml ) ) - { - mojo.getSiteRenderer().mergeDocumentIntoSite( writer, (DocumentContent) mojo.getSink(), context ); - } - } - - /** - * Checks, whether the string contained is contained in - * the given text ignoring case. - * - * @param text the string in which the search is executed - * @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 ) ); - } - - @Override - protected Mojo lookupMojo( String goal, File pom ) throws Exception - { - Mojo mojo = super.lookupMojo( goal, pom ); - return mockMavenSession( mojo ); - } - - private Mojo mockMavenSession(Mojo mojo) throws IllegalAccessException { - String basedir = getBasedir(); - if ( ReflectionUtils.getFieldByNameIncludingSuperclasses( "session", mojo.getClass() ) != null ) - { - MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest() { - public String getBaseDirectory() { - return basedir; - }; - }; - ReflectionUtils.setVariableValueInObject( mojo, "session", - new MavenSession( null, null, executionRequest, null ) ); - } - return mojo; - } -} diff --git a/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java b/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java new file mode 100644 index 00000000..f0d5524c --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java @@ -0,0 +1,168 @@ +package org.apache.maven.plugins.pmd; + +/* + * 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. + */ + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Locale; + +import org.apache.maven.plugin.LegacySupport; +import org.apache.maven.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.plugin.testing.ArtifactStubFactory; +import org.apache.maven.plugin.testing.stubs.MavenProjectStub; +import org.apache.maven.project.DefaultProjectBuildingRequest; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.ProjectBuilder; +import org.apache.maven.project.ProjectBuildingRequest; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; +import org.eclipse.aether.repository.LocalRepository; + +/** + * @author Vincent Siveton + * @version $Id$ + * @since 2.5 + */ +public abstract class AbstractPmdReportTestCase + extends AbstractMojoTestCase +{ + private ArtifactStubFactory artifactStubFactory; + + /** + * The current project to be test. + */ + private MavenProject testMavenProject; + + @Override + protected void setUp() + throws Exception + { + super.setUp(); + CapturingPrintStream.init( true ); + + artifactStubFactory = new DependencyArtifactStubFactory( getTestFile( "target" ), true, false ); + artifactStubFactory.getWorkingDir().mkdirs(); + } + + /** + * Get the current Maven project + * + * @return the maven project + */ + protected MavenProject getTestMavenProject() + { + return testMavenProject; + } + + /** + * Get the generated report as file in the test maven project. + * + * @param name the name of the report. + * @return the generated report as file + * @throws IOException if the return file doesnt exist + */ + protected File getGeneratedReport( String name ) + throws IOException + { + String outputDirectory = getBasedir() + "/target/test/unit/" + getTestMavenProject().getArtifactId(); + + File report = new File( outputDirectory, name ); + if ( !report.exists() ) + { + throw new IOException( "File not found. Attempted: " + report ); + } + + return report; + } + + /** + * Generate the report and return the generated file + * + * @param goal the mojo goal. + * @param pluginXml the name of the xml file in "src/test/resources/plugin-configs/". + * @return the generated HTML file + * @throws Exception if any + */ + protected File generateReport( String goal, String pluginXml ) + throws Exception + { + File pluginXmlFile = new File( getBasedir(), "src/test/resources/unit/" + pluginXml ); + AbstractPmdReport mojo = createReportMojo( goal, pluginXmlFile ); + return generateReport( mojo, pluginXmlFile ); + } + + protected AbstractPmdReport createReportMojo( String goal, File pluginXmlFile ) + throws Exception + { + AbstractPmdReport mojo = (AbstractPmdReport) lookupMojo( goal, pluginXmlFile ); + assertNotNull( "Mojo not found.", mojo ); + + LegacySupport legacySupport = lookup( LegacySupport.class ); + legacySupport.setSession( newMavenSession( new MavenProjectStub() ) ); + DefaultRepositorySystemSession repoSession = + (DefaultRepositorySystemSession) legacySupport.getRepositorySession(); + repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repoSession, new LocalRepository( artifactStubFactory.getWorkingDir() ) ) ); + + setVariableValueToObject( mojo, "session", legacySupport.getSession() ); + setVariableValueToObject( mojo, "remoteRepositories", mojo.getProject().getRemoteArtifactRepositories() ); + return mojo; + } + + protected File generateReport( AbstractPmdReport mojo, File pluginXmlFile ) + throws Exception + { + mojo.execute(); + + ProjectBuilder builder = lookup( ProjectBuilder.class ); + + ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); + buildingRequest.setRepositorySession( lookup( LegacySupport.class ).getRepositorySession() ); + + testMavenProject = builder.build( pluginXmlFile, buildingRequest ).getProject(); + + File outputDir = mojo.getReportOutputDirectory(); + String filename = mojo.getOutputName() + ".html"; + + return new File( outputDir, filename ); + } + + /** + * Read the contents of the specified file object into a string + */ + protected String readFile( File pmdTestDir, String fileName ) throws IOException + { + return new String( Files.readAllBytes( pmdTestDir.toPath().resolve( fileName ) ) ); + } + + + /** + * Checks, whether the string contained is contained in + * the given text ignoring case. + * + * @param text the string in which the search is executed + * @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 ) ); + } +} 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 75447481..efb3d581 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java @@ -37,7 +37,7 @@ * @version $Id$ */ public class CpdReportTest - extends AbstractPmdReportTest + extends AbstractPmdReportTestCase { /** * {@inheritDoc} @@ -59,24 +59,15 @@ protected void setUp() public void testDefaultConfiguration() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" ); - CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom ); - setVariableValueToObject( mojo, "remoteRepositories", mojo.getProject().getRemoteArtifactRepositories() ); - setVariableValueToObject( mojo, "pluginRepositories", mojo.getProject().getPluginArtifactRepositories() ); - mojo.execute(); + 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() ) ); - generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.html" ); - renderer( mojo, generatedFile ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - // check the contents of cpd.html - String str = readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.html" ) ); + String str = readFile( generatedReport ); assertTrue( lowerCaseContains( str, "AppSample.java" ) ); assertTrue( lowerCaseContains( str, "App.java" ) ); assertTrue( lowerCaseContains( str, "public String dup( String str )" ) ); @@ -95,11 +86,7 @@ public void testDefaultConfiguration() public void testTxtFormat() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml" ); - CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom ); - mojo.execute(); + 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" ); @@ -123,35 +110,20 @@ public void testTxtFormat() public void testCustomConfiguration() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml" ); - CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom ); - mojo.execute(); + 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() ) ); - generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ); - renderer( mojo, generatedFile ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - + String str = readFile( generatedReport ); // Contents that should NOT be in the report - String str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) ); assertFalse( lowerCaseContains( str, "/Sample.java" ) ); - - str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) ); assertFalse( lowerCaseContains( str, "public void duplicateMethod( int i )" ) ); - // Contents that should be in the report - str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) ); assertTrue( lowerCaseContains( str, "AnotherSample.java" ) ); - - str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) ); assertTrue( lowerCaseContains( str, "public static void main( String[] args )" ) ); - - str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) ); assertTrue( lowerCaseContains( str, "private String unusedMethod(" ) ); } @@ -166,10 +138,10 @@ public void testInvalidFormat() try { File testPom = - new File( getBasedir(), "src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml" ); - CpdReport mojo = (CpdReport) lookupMojo( "cpd", 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() ); - mojo.execute(); + generateReport( mojo, testPom ); fail( "MavenReportException must be thrown" ); } @@ -207,20 +179,17 @@ private String readFile( File file ) public void testWriteNonHtml() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" ); - CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom ); - assertNotNull( mojo ); - mojo.execute(); + generateReport( "cpd", "default-configuration/cpd-default-configuration-plugin-config.xml" ); - File tReport = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.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() ) ); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - Document pmdCpdDocument = builder.parse( tReport ); + Document pmdCpdDocument = builder.parse( generatedFile ); assertNotNull( pmdCpdDocument ); - String str = readFile( tReport ); + String str = readFile( generatedFile ); assertTrue( lowerCaseContains( str, "AppSample.java" ) ); assertTrue( lowerCaseContains( str, "App.java" ) ); assertTrue( lowerCaseContains( str, "public String dup( String str )" ) ); @@ -234,21 +203,16 @@ public void testWriteNonHtml() public void testIncludeXmlInSite() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/cpd-report-include-xml-in-site-plugin-config.xml" ); - CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom ); - assertNotNull( mojo ); - mojo.execute(); + generateReport( "cpd", "default-configuration/cpd-report-include-xml-in-site-plugin-config.xml" ); - File tReport = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" ); - assertTrue( FileUtils.fileExists( tReport.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( tReport ); + Document pmdCpdDocument = builder.parse( generatedFile ); assertNotNull( pmdCpdDocument ); - String str = readFile( tReport ); + String str = readFile( generatedFile ); assertTrue( str.contains( "" ) ); File siteReport = new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.xml" ); @@ -262,28 +226,19 @@ public void testIncludeXmlInSite() public void testSkipEmptyReportConfiguration() throws Exception { - File testPom = - new File( getBasedir(), "src/test/resources/unit/empty-report/cpd-skip-empty-report-plugin-config.xml" ); - CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom ); - mojo.execute(); - // verify the generated files do not exist because PMD was skipped - File generatedFile = new File( getBasedir(), "target/test/unit/empty-report/target/site/cpd.html" ); - assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + File generatedReport = generateReport( "cpd", "empty-report/cpd-skip-empty-report-plugin-config.xml" ); + assertFalse( FileUtils.fileExists( generatedReport.getAbsolutePath() ) ); } public void testEmptyReportConfiguration() throws Exception { - File testPom = - new File( getBasedir(), "src/test/resources/unit/empty-report/cpd-empty-report-plugin-config.xml" ); - CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom ); - mojo.execute(); - // verify the generated files do exist, even if there are no violations - File generatedFile = new File( getBasedir(), "target/test/unit/empty-report/target/site/cpd.html" ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - String str = readFile( new File( getBasedir(), "target/test/unit/empty-report/target/site/cpd.html" ) ); + 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." ) ); } @@ -296,11 +251,7 @@ public void testCpdEncodingConfiguration() { System.setProperty( "file.encoding", "UTF-16" ); - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" ); - CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom ); - mojo.execute(); + 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" ); @@ -317,43 +268,33 @@ public void testCpdEncodingConfiguration() public void testCpdJavascriptConfiguration() throws Exception { - File testPom = - new File( getBasedir(), "src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml" ); - CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom ); - mojo.execute(); + 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" ) ); + // 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" ) ); } public void testCpdJspConfiguration() throws Exception { - File testPom = - new File( getBasedir(), "src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml" ); - CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom ); - mojo.execute(); + 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" ) ); + // 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" ) ); } public void testExclusionsConfiguration() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/cpd-report-cpd-exclusions-configuration-plugin-config.xml" ); - final CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom ); - mojo.execute(); + 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" ); diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java index b70ae8eb..31adb8c2 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/CpdViolationCheckMojoTest.java @@ -26,24 +26,20 @@ * @version $Id$ */ public class CpdViolationCheckMojoTest - extends AbstractPmdReportTest + extends AbstractPmdReportTestCase { public void testDefaultConfiguration() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" ); - final CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom ); - mojo.execute(); + generateReport( "cpd", "default-configuration/cpd-default-configuration-plugin-config.xml" ); // clear the output from previous pmd:cpd execution CapturingPrintStream.init( true ); try { - testPom = + File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-check-default-configuration-plugin-config.xml" ); final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo( "cpd-check", testPom ); @@ -65,13 +61,9 @@ public void testNotFailOnViolation() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" ); - final CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom ); - mojo.execute(); + generateReport( "cpd", "default-configuration/cpd-default-configuration-plugin-config.xml" ); - testPom = + File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/cpd-check-notfailonviolation-plugin-config.xml" ); final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo( "cpd-check", testPom ); @@ -102,13 +94,9 @@ public void testException() public void testExclusionsConfiguration() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" ); - final CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom ); - mojo.execute(); + generateReport( "cpd", "default-configuration/cpd-default-configuration-plugin-config.xml" ); - testPom = + File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/cpd-check-cpd-exclusions-configuration-plugin-config.xml" ); final CpdViolationCheckMojo cpdViolationMojo = (CpdViolationCheckMojo) lookupMojo( "cpd-check", testPom ); diff --git a/src/test/java/org/apache/maven/plugins/pmd/DependencyArtifactStubFactory.java b/src/test/java/org/apache/maven/plugins/pmd/DependencyArtifactStubFactory.java new file mode 100644 index 00000000..7cd70cdc --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/pmd/DependencyArtifactStubFactory.java @@ -0,0 +1,69 @@ +package org.apache.maven.plugins.pmd; + +/* + * 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. + */ + +import java.io.File; +import java.io.IOException; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.ArtifactUtils; +import org.apache.maven.artifact.versioning.VersionRange; +import org.apache.maven.plugin.testing.ArtifactStubFactory; + +public class DependencyArtifactStubFactory + extends ArtifactStubFactory +{ + private boolean flattenedPath = true; + + public DependencyArtifactStubFactory( File theWorkingDir, boolean theCreateFiles, boolean flattenedPath ) + { + this( theWorkingDir, theCreateFiles ); + this.flattenedPath = flattenedPath; + } + + public DependencyArtifactStubFactory( File theWorkingDir, boolean theCreateFiles ) + { + super( theWorkingDir, theCreateFiles ); + } + + @Override + public Artifact createArtifact( String groupId, String artifactId, VersionRange versionRange, String scope, + String type, String classifier, boolean optional ) + throws IOException + { + File workingDir = getWorkingDir(); + + if ( !flattenedPath ) + { + // don't use flatten directories, won't happen at runtime + String path = groupId.replace( '.', '/' ) + '/' + + artifactId + '/' + + ArtifactUtils.toSnapshotVersion( versionRange.getRecommendedVersion().toString() ); + setWorkingDir( new File( workingDir, path ) ); + } + + Artifact artifact = + super.createArtifact( groupId, artifactId, versionRange, scope, type, classifier, optional ); + + setWorkingDir( workingDir ); + + return artifact; + } +} diff --git a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java index 4aeca37e..d53ba481 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java @@ -44,7 +44,7 @@ * @version $Id$ */ public class PmdReportTest - extends AbstractPmdReportTest + extends AbstractPmdReportTestCase { /** @@ -66,11 +66,8 @@ public void testDefaultConfiguration() "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) lookupMojo( "pmd", testPom ); - mojo.execute(); + File generatedReport = generateReport( "pmd", "default-configuration/default-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" ); @@ -80,12 +77,8 @@ public void testDefaultConfiguration() generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/maven-pmd-plugin-default.xml" ); assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" ); - renderer( mojo, generatedFile ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - // check if there's a link to the JXR files - String str = readFile( generatedFile ); + String str = readFile( generatedReport ); assertTrue( str.contains( "/xref/def/configuration/App.html#L31" ) ); @@ -123,17 +116,10 @@ public void testDefaultConfigurationNotRenderRuleViolationPriority() "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/pmd-report-not-render-rule-priority-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + File generatedReport = generateReport( "pmd", "default-configuration/pmd-report-not-render-rule-priority-plugin-config.xml" ); + assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) ); - File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" ); - renderer( mojo, generatedFile ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - - String str = readFile( generatedFile ); + String str = readFile( generatedReport ); // check that there's no priority column assertFalse( str.contains( "Priority" ) ); @@ -141,30 +127,23 @@ public void testDefaultConfigurationNotRenderRuleViolationPriority() 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" ) ); + { + 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/pmd-report-no-render-violations-by-priority.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); - - File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" ); - renderer( mojo, generatedFile ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - - String str = readFile( generatedFile ); - - // there should be no section Violations By Priority - 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" ) ); - } + File generatedReport = generateReport( "pmd", "default-configuration/pmd-report-no-render-violations-by-priority.xml" ); + assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) ); + + 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" ) ); + // the file App.java is mentioned once: in the files section + assertEquals( 1, StringUtils.countMatches( str, "def/configuration/App.java" ) ); + } public void testDefaultConfigurationWithAnalysisCache() @@ -174,11 +153,7 @@ public void testDefaultConfigurationWithAnalysisCache() "src/test/resources/unit/default-configuration/jxr-files" ), new File( getBasedir(), "target/test/unit/pmd-with-analysis-cache-plugin-config/target/site" ) ); - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/pmd-with-analysis-cache-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + 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" ); @@ -188,11 +163,8 @@ public void testDefaultConfigurationWithAnalysisCache() public void testJavascriptConfiguration() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + 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" ); @@ -208,11 +180,7 @@ public void testJavascriptConfiguration() 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/site/pmd.html" ); - renderer( mojo, generatedFile ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - - String str = readFile( generatedFile ); + String str = readFile( generatedReport ); assertTrue( str.contains( "Avoid using global variables" ) ); } @@ -226,7 +194,7 @@ public void testFileURL() File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); + PmdReport mojo = (PmdReport) createReportMojo( "pmd", testPom ); // Additional test case for MPMD-174 (https://issues.apache.org/jira/browse/MPMD-174). int port = determineFreePort(); @@ -257,7 +225,8 @@ public void testFileURL() URL url3 = getClass().getClassLoader().getResource( "rulesets/java/imports.xml" ); mojo.setRulesets( new String[] { url.toString(), url2.toString(), url3.toString(), sonarExportRulesetUrl } ); - mojo.execute(); + 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" ); @@ -278,12 +247,8 @@ public void testFileURL() "target/test/unit/default-configuration/target/pmd/rulesets/export_format_pmd_language_java_name_Sonar_2520way.xml" ); assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" ); - renderer( mojo, generatedFile ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - // check if there's a link to the JXR files - String str = readFile( generatedFile ); + String str = readFile( generatedReport ); assertTrue( str.contains( "/xref/def/configuration/App.html#L31" ) ); @@ -309,12 +274,8 @@ private int determineFreePort() public void testCustomConfiguration() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml" ); - - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + 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" ); @@ -323,12 +284,8 @@ public void testCustomConfiguration() 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/site/pmd.html" ); - renderer( mojo, generatedFile ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - // check if custom ruleset was applied - String str = readFile( generatedFile ); + String str = readFile( generatedReport ); // codestyle.xml/ControlStatementBraces: assertTrue( lowerCaseContains( str, "This statement should have braces" ) ); @@ -353,9 +310,8 @@ public void testCustomConfiguration() public void testSkipConfiguration() throws Exception { - File testPom = new File( getBasedir(), "src/test/resources/unit/custom-configuration/skip-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + 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" ); @@ -364,9 +320,6 @@ public void testSkipConfiguration() 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/site/pmd.html" ); - 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" ) ); @@ -375,27 +328,19 @@ public void testSkipConfiguration() public void testSkipEmptyReportConfiguration() throws Exception { - File testPom = - new File( getBasedir(), "src/test/resources/unit/empty-report/skip-empty-report-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); - // verify the generated files do not exist because PMD was skipped - File generatedFile = new File( getBasedir(), "target/test/unit/empty-report/target/site/pmd.html" ); - assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + File generatedReport = generateReport( "pmd", "empty-report/skip-empty-report-plugin-config.xml" ); + assertFalse( FileUtils.fileExists( generatedReport.getAbsolutePath() ) ); } public void testEmptyReportConfiguration() throws Exception { - File testPom = new File( getBasedir(), "src/test/resources/unit/empty-report/empty-report-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + 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 - File generatedFile = new File( getBasedir(), "target/test/unit/empty-report/target/site/pmd.html" ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - String str = readFile( generatedFile ); + 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 @@ -409,10 +354,10 @@ public void testInvalidFormat() try { File testPom = - new File( getBasedir(), "src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", 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() ); - mojo.executeReport( Locale.ENGLISH ); + generateReport( mojo, testPom ); fail( "Must throw MavenReportException." ); } @@ -427,10 +372,7 @@ public void testInvalidTargetJdk() { try { - File testPom = - new File( getBasedir(), "src/test/resources/unit/invalid-format/invalid-target-jdk-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + generateReport( "pmd", "empty-report/invalid-format/invalid-target-jdk-plugin-config.xml" ); fail( "Must throw MavenReportException." ); } @@ -447,12 +389,9 @@ public void testInvalidTargetJdk() public void testIncludeXmlInSite() throws Exception { - File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-report-include-xml-in-site-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + File generatedReport = generateReport( "pmd", "default-configuration/pmd-report-include-xml-in-site-plugin-config.xml" ); + assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) ); - File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" ); - assertTrue( FileUtils.fileExists( generatedFile.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() ) ); @@ -517,11 +456,8 @@ public void testLocationTemp() public void testSuppressMarkerConfiguration() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/pmd-with-suppressMarker-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + 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" ); @@ -534,12 +470,8 @@ public void testSuppressMarkerConfiguration() // but it appears as suppressed assertTrue( str.contains( "suppressiontype=\"nopmd\" msg=\"Avoid unused private fields such as 'unusedVar2'.\"" )); - generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" ); - renderer( mojo, generatedFile ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - // check if there's a link to the JXR files - str = readFile( generatedFile ); + str = readFile( generatedReport ); assertTrue( str.contains( "/xref/def/configuration/AppSample.html#L27" ) ); // suppressed violation @@ -549,11 +481,8 @@ public void testSuppressMarkerConfiguration() public void testSuppressMarkerConfigurationWithoutRendering() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/pmd-with-suppressMarker-no-render-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + File generatedReport = generateReport( "pmd", "default-configuration/pmd-with-suppressMarker-no-render-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" ); @@ -566,12 +495,8 @@ public void testSuppressMarkerConfigurationWithoutRendering() // but it appears as suppressed assertTrue( str.contains( "suppressiontype=\"nopmd\" msg=\"Avoid unused private fields such as 'unusedVar2'.\"" )); - generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" ); - renderer( mojo, generatedFile ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - // check if there's a link to the JXR files - str = readFile( generatedFile ); + str = readFile( generatedReport ); assertTrue( str.contains( "/xref/def/configuration/AppSample.html#L27" ) ); // suppressed violations are not rendered @@ -579,12 +504,10 @@ public void testSuppressMarkerConfigurationWithoutRendering() } public void testJspConfiguration() - throws Exception + throws Exception { - File testPom = new File( getBasedir(), - "src/test/resources/unit/default-configuration/jsp-configuration-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + File generatedReport = generateReport( "pmd", "default-configuration/jsp-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" ); @@ -606,24 +529,17 @@ public void testJspConfiguration() generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/security.xml" ); assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" ); - renderer( mojo, generatedFile ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - - String str = readFile( generatedFile ); + String str = readFile( generatedReport ); assertTrue(str.contains("JSP file should use UTF-8 encoding")); assertTrue(str.contains("Using unsanitized JSP expression can lead to Cross Site Scripting (XSS) attacks")); assertTrue(str.contains("Avoid having style information in JSP files.")); } public void testPMDProcessingError() - throws Exception + throws Exception { - File testPom = new File( getBasedir(), - "src/test/resources/unit/processing-error/pmd-processing-error-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); try { - mojo.execute(); + generateReport( "pmd", "processing-error/pmd-processing-error-plugin-config.xml" ); fail("Expected exception"); } catch (RuntimeException e) { assertTrue( e.getMessage().endsWith( "Found 1 PMD processing errors" ) ); @@ -631,13 +547,11 @@ public void testPMDProcessingError() } public void testPMDProcessingErrorWithDetailsSkipped() - throws Exception + throws Exception { - File testPom = new File( getBasedir(), - "src/test/resources/unit/processing-error/pmd-processing-error-skip-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); + File generatedReport = generateReport( "pmd", "processing-error/pmd-processing-error-skip-plugin-config.xml" ); + assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) ); - mojo.execute(); String output = CapturingPrintStream.getOutput(); assertTrue ( output.contains( "There are 1 PMD processing errors:" ) ); @@ -648,22 +562,17 @@ public void testPMDProcessingErrorWithDetailsSkipped() // The parse exception must be in the XML report assertTrue( str.contains( "ParseException: Encountered \"\" at line 23, column 5." ) ); - generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" ); - renderer( mojo, generatedFile ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - str = readFile( generatedFile ); + str = readFile( generatedReport ); // The parse exception must also be in the HTML report assertTrue( str.contains( "ParseException: Encountered \"\" at line 23, column 5." ) ); } public void testPMDProcessingErrorWithDetailsNoReport() - throws Exception + throws Exception { - File testPom = new File( getBasedir(), - "src/test/resources/unit/processing-error/pmd-processing-error-no-report-plugin-config.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); + File generatedReport = generateReport( "pmd", "processing-error/pmd-processing-error-no-report-plugin-config.xml" ); + assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) ); - mojo.execute(); String output = CapturingPrintStream.getOutput(); assertTrue ( output.contains( "There are 1 PMD processing errors:" ) ); @@ -674,43 +583,36 @@ public void testPMDProcessingErrorWithDetailsNoReport() // The parse exception must be in the XML report assertTrue( str.contains( "ParseException: Encountered \"\" at line 23, column 5." ) ); - generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" ); - renderer( mojo, generatedFile ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - str = readFile( generatedFile ); + str = readFile( generatedReport ); // The parse exception must NOT be in the HTML report, since reportProcessingErrors is false assertFalse( str.contains( "ParseException: Encountered \"\" at line 23, column 5." ) ); } - public void testPMDExcludeRootsShouldExcludeSubdirectories() throws Exception { - File testPom = new File(getBasedir(), "src/test/resources/unit/exclude-roots/pmd-exclude-roots-plugin-config.xml"); - PmdReport mojo = (PmdReport) lookupMojo ("pmd", testPom); - mojo.execute(); + public void testPMDExcludeRootsShouldExcludeSubdirectories() + throws Exception + { + generateReport( "pmd", "exclude-roots/pmd-exclude-roots-plugin-config.xml" ); File generatedFile = new File( getBasedir(), "target/test/unit/exclude-roots/target/pmd.xml" ); assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); String str = readFile( generatedFile ); - assertTrue( "Seems like all directories are excluded now", str.contains("ForLoopShouldBeWhileLoop") ); + assertTrue( "Seems like all directories are excluded now", str.contains( "ForLoopShouldBeWhileLoop" ) ); assertFalse( "Exclusion of an exact source directory not working", str.contains( "OverrideBothEqualsAndHashcode" ) ); assertFalse( "Exclusion of basedirectory with subdirectories not working (MPMD-178)", str.contains( "JumbledIncrementer") ); } public void testViolationExclusion() - throws Exception - { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml" ); - final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + throws Exception + { + generateReport( "pmd", "default-configuration/pmd-report-pmd-exclusions-configuration-plugin-config.xml" ); - File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" ); - assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); - String str = readFile( generatedFile ); + File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" ); + assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + String str = readFile( generatedFile ); - assertEquals(0, StringUtils.countMatches(str, "CustomRule" ) ); @@ -785,11 +680,11 @@ public void testPmdReportResolveRulesets() File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-report-resolve-rulesets.xml" ); - PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); + PmdReport mojo = (PmdReport) createReportMojo( "pmd", testPom ); mojo.rulesets[3] = sonarExportRulesetUrl; mojo.rulesets[4] = myRulesetUrl; mojo.rulesets[5] = notAInternalRulesetUrl; - mojo.execute(); + generateReport( mojo, testPom ); // these are the rulesets, that have been copied to target/pmd/rulesets File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd/rulesets/custom-rules.xml" ); diff --git a/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java b/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java index d5815813..e97fc8ad 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/PmdViolationCheckMojoTest.java @@ -28,17 +28,13 @@ * @version $Id$ */ public class PmdViolationCheckMojoTest - extends AbstractPmdReportTest + extends AbstractPmdReportTestCase { public void testDefaultConfiguration() throws Exception { - File testPomPmd = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" ); - final PmdReport pmdMojo = (PmdReport) lookupMojo( "pmd", testPomPmd ); - pmdMojo.execute(); + generateReport( "pmd", "default-configuration/default-configuration-plugin-config.xml" ); // clear the output from previous pmd:pmd execution CapturingPrintStream.init( true ); @@ -66,13 +62,9 @@ public void testDefaultConfiguration() public void testNotFailOnViolation() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" ); - final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + generateReport( "pmd", "default-configuration/default-configuration-plugin-config.xml" ); - testPom = + File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-check-notfailonviolation-plugin-config.xml" ); final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom ); @@ -84,13 +76,9 @@ public void testNotFailOnViolation() public void testMaxAllowedViolations() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" ); - final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + generateReport( "pmd", "default-configuration/default-configuration-plugin-config.xml" ); - testPom = + File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-check-notfailmaxviolation-plugin-config.xml" ); final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom ); @@ -125,13 +113,9 @@ public void testMaxAllowedViolations() public void testFailurePriority() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" ); - final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + generateReport( "pmd", "default-configuration/default-configuration-plugin-config.xml" ); - testPom = + File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-check-failonpriority-plugin-config.xml" ); PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom ); @@ -185,13 +169,9 @@ public void testException() public void testViolationExclusion() throws Exception { - File testPom = - new File( getBasedir(), - "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" ); - final PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); - mojo.execute(); + generateReport( "pmd", "default-configuration/default-configuration-plugin-config.xml" ); - testPom = + File testPom = new File( getBasedir(), "src/test/resources/unit/default-configuration/pmd-check-pmd-exclusions-configuration-plugin-config.xml" ); final PmdViolationCheckMojo pmdViolationMojo = (PmdViolationCheckMojo) lookupMojo( "check", testPom ); diff --git a/src/test/java/org/apache/maven/plugins/pmd/stubs/CustomConfigurationMavenProjectStub.java b/src/test/java/org/apache/maven/plugins/pmd/stubs/CustomConfigurationMavenProjectStub.java index 90033500..4fe1d674 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/stubs/CustomConfigurationMavenProjectStub.java +++ b/src/test/java/org/apache/maven/plugins/pmd/stubs/CustomConfigurationMavenProjectStub.java @@ -30,14 +30,13 @@ import org.apache.maven.model.ReportPlugin; import org.apache.maven.model.Scm; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.plugin.testing.stubs.MavenProjectStub; /** * @author Maria Odea Ching * @version $Id$ */ public class CustomConfigurationMavenProjectStub - extends MavenProjectStub + extends PmdProjectStub { private Build build; diff --git a/src/test/java/org/apache/maven/plugins/pmd/stubs/DefaultConfigurationMavenProjectStub.java b/src/test/java/org/apache/maven/plugins/pmd/stubs/DefaultConfigurationMavenProjectStub.java index 3093e14d..daac7959 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/stubs/DefaultConfigurationMavenProjectStub.java +++ b/src/test/java/org/apache/maven/plugins/pmd/stubs/DefaultConfigurationMavenProjectStub.java @@ -30,14 +30,13 @@ import org.apache.maven.model.ReportPlugin; import org.apache.maven.model.Scm; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.plugin.testing.stubs.MavenProjectStub; /** * @author Maria Odea Ching * @version $Id$ */ public class DefaultConfigurationMavenProjectStub - extends MavenProjectStub + extends PmdProjectStub { private List reportPlugins = new ArrayList<>(); diff --git a/src/test/java/org/apache/maven/plugins/pmd/stubs/InvalidFormatMavenProjectStub.java b/src/test/java/org/apache/maven/plugins/pmd/stubs/InvalidFormatMavenProjectStub.java index 293ecf93..f7003139 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/stubs/InvalidFormatMavenProjectStub.java +++ b/src/test/java/org/apache/maven/plugins/pmd/stubs/InvalidFormatMavenProjectStub.java @@ -29,14 +29,13 @@ import org.apache.maven.model.Model; import org.apache.maven.model.Scm; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.plugin.testing.stubs.MavenProjectStub; /** * @author Maria Odea Ching * @version $Id$ */ public class InvalidFormatMavenProjectStub - extends MavenProjectStub + extends PmdProjectStub { private Build build; diff --git a/src/test/java/org/apache/maven/plugins/pmd/stubs/PmdProjectStub.java b/src/test/java/org/apache/maven/plugins/pmd/stubs/PmdProjectStub.java new file mode 100644 index 00000000..0b8f6f93 --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/pmd/stubs/PmdProjectStub.java @@ -0,0 +1,42 @@ +package org.apache.maven.plugins.pmd.stubs; + +/* + * 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. + */ + +import java.util.Collections; +import java.util.List; + +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; +import org.apache.maven.artifact.repository.MavenArtifactRepository; +import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; +import org.apache.maven.plugin.testing.stubs.MavenProjectStub; + +public abstract class PmdProjectStub + extends MavenProjectStub +{ + @Override + public List getRemoteArtifactRepositories() + { + ArtifactRepository repository = new MavenArtifactRepository( "central", "https://repo1.maven.org/maven2", + new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy() ); + + return Collections.singletonList( repository ); + } +}