Author: dennisl Date: Fri May 29 11:58:03 2015 New Revision: 1682434 URL: http://svn.apache.org/r1682434 Log: [MPMD-207] Support Javascript and JSP for CPD, closes apache/maven-plugins#48 Submitted by: Andreas Dangel Reviewed by: Dennis Lundberg
Added: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/js/SampleDup.js maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/jsp/sampleDup.jsp Modified: maven/plugins/trunk/maven-pmd-plugin/pom.xml maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/CpdReportTest.java Modified: maven/plugins/trunk/maven-pmd-plugin/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/pom.xml?rev=1682434&r1=1682433&r2=1682434&view=diff ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/pom.xml (original) +++ maven/plugins/trunk/maven-pmd-plugin/pom.xml Fri May 29 11:58:03 2015 @@ -198,13 +198,11 @@ under the License. <groupId>net.sourceforge.pmd</groupId> <artifactId>pmd-javascript</artifactId> <version>${pmdVersion}</version> - <scope>runtime</scope> </dependency> <dependency> <groupId>net.sourceforge.pmd</groupId> <artifactId>pmd-jsp</artifactId> <version>${pmdVersion}</version> - <scope>runtime</scope> </dependency> <!-- test --> Modified: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java?rev=1682434&r1=1682433&r2=1682434&view=diff ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java (original) +++ maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java Fri May 29 11:58:03 2015 @@ -33,8 +33,12 @@ import java.util.ResourceBundle; import net.sourceforge.pmd.cpd.CPD; import net.sourceforge.pmd.cpd.CPDConfiguration; import net.sourceforge.pmd.cpd.CSVRenderer; +import net.sourceforge.pmd.cpd.EcmascriptLanguage; +import net.sourceforge.pmd.cpd.JSPLanguage; import net.sourceforge.pmd.cpd.JavaLanguage; import net.sourceforge.pmd.cpd.JavaTokenizer; +import net.sourceforge.pmd.cpd.Language; +import net.sourceforge.pmd.cpd.LanguageFactory; import net.sourceforge.pmd.cpd.Renderer; import net.sourceforge.pmd.cpd.XMLRenderer; @@ -59,6 +63,15 @@ public class CpdReport extends AbstractPmdReport { /** + * The programming language to be analyzed by CPD. Valid values are currently <code>java</code>, + * <code>javascript</code> or <code>jsp</code>. + * + * @since 3.5 + */ + @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" ) @@ -228,10 +241,27 @@ public class CpdReport } String encoding = determineEncoding( !filesToProcess.isEmpty() ); + Language cpdLanguage; + if ( "java".equals ( language ) || null == language ) + { + cpdLanguage = new JavaLanguage( p ); + } + else if ( "javascript".equals( language ) ) + { + cpdLanguage = new EcmascriptLanguage(); + } + else if ( "jsp".equals( language ) ) + { + cpdLanguage = new JSPLanguage(); + } + else + { + cpdLanguage = LanguageFactory.createLanguage( language, p ); + } CPDConfiguration cpdConfiguration = new CPDConfiguration(); cpdConfiguration.setMinimumTileSize( minimumTokens ); - cpdConfiguration.setLanguage( new JavaLanguage( p ) ); + cpdConfiguration.setLanguage( cpdLanguage ); cpdConfiguration.setSourceEncoding( encoding ); cpd = new CPD( cpdConfiguration ); Modified: maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/CpdReportTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/CpdReportTest.java?rev=1682434&r1=1682433&r2=1682434&view=diff ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/CpdReportTest.java (original) +++ maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/CpdReportTest.java Fri May 29 11:58:03 2015 @@ -26,6 +26,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Locale; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -274,6 +275,38 @@ public class CpdReportTest } } + 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(); + + // 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( str.toLowerCase(Locale.ROOT).contains( "Sample.js".toLowerCase(Locale.ROOT) ) ); + assertTrue( str.toLowerCase(Locale.ROOT).contains( "SampleDup.js".toLowerCase(Locale.ROOT) ) ); + } + + 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(); + + // 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( str.toLowerCase(Locale.ROOT).contains( "sample.jsp".toLowerCase(Locale.ROOT) ) ); + assertTrue( str.toLowerCase(Locale.ROOT).contains( "sampleDup.jsp".toLowerCase(Locale.ROOT) ) ); + } + public static class MockCpd extends CPD { Added: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml?rev=1682434&view=auto ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml (added) +++ maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/cpd-javascript-plugin-config.xml Fri May 29 11:58:03 2015 @@ -0,0 +1,63 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +<project> + <modelVersion>4.0.0</modelVersion> + <groupId>def.configuration</groupId> + <artifactId>cpd-javascript</artifactId> + <packaging>jar</packaging> + <version>1.0-SNAPSHOT</version> + <inceptionYear>2006</inceptionYear> + <name>Maven CPD Plugin Javascript Configuration Test</name> + <url>http://maven.apache.org</url> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-pmd-plugin</artifactId> + <configuration> + <project implementation="org.apache.maven.plugin.pmd.stubs.DefaultConfigurationMavenProjectStub"/> + <outputDirectory>${basedir}/target/test/unit/default-configuration/target/site</outputDirectory> + <targetDirectory>${basedir}/target/test/unit/default-configuration/target</targetDirectory> + <format>xml</format> + <linkXRef>false</linkXRef> + <xrefLocation>${basedir}/target/test/unit/default-configuration/target/site/xref</xrefLocation> + <minimumTokens>100</minimumTokens> + <language>javascript</language> + <includes> + <include>**/*.js</include> + </includes> + + <compileSourceRoots> + <compileSourceRoot>${basedir}/src/test/resources/unit/default-configuration/</compileSourceRoot> + </compileSourceRoots> + <sourceEncoding>UTF-8</sourceEncoding> + </configuration> + </plugin> + </plugins> + </build> + <reporting> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jxr-plugin</artifactId> + </plugin> + </plugins> + </reporting> +</project> Added: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml?rev=1682434&view=auto ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml (added) +++ maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/cpd-jsp-plugin-config.xml Fri May 29 11:58:03 2015 @@ -0,0 +1,63 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +<project> + <modelVersion>4.0.0</modelVersion> + <groupId>def.configuration</groupId> + <artifactId>cpd-jsp</artifactId> + <packaging>jar</packaging> + <version>1.0-SNAPSHOT</version> + <inceptionYear>2006</inceptionYear> + <name>Maven CPD Plugin JSP Configuration Test</name> + <url>http://maven.apache.org</url> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-pmd-plugin</artifactId> + <configuration> + <project implementation="org.apache.maven.plugin.pmd.stubs.DefaultConfigurationMavenProjectStub"/> + <outputDirectory>${basedir}/target/test/unit/default-configuration/target/site</outputDirectory> + <targetDirectory>${basedir}/target/test/unit/default-configuration/target</targetDirectory> + <format>xml</format> + <linkXRef>false</linkXRef> + <xrefLocation>${basedir}/target/test/unit/default-configuration/target/site/xref</xrefLocation> + <minimumTokens>5</minimumTokens> + <language>jsp</language> + <includes> + <include>**/*.jsp</include> + </includes> + + <compileSourceRoots> + <compileSourceRoot>${basedir}/src/test/resources/unit/default-configuration/</compileSourceRoot> + </compileSourceRoots> + <sourceEncoding>UTF-8</sourceEncoding> + </configuration> + </plugin> + </plugins> + </build> + <reporting> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jxr-plugin</artifactId> + </plugin> + </plugins> + </reporting> +</project> Added: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/js/SampleDup.js URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/js/SampleDup.js?rev=1682434&view=auto ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/js/SampleDup.js (added) +++ maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/js/SampleDup.js Fri May 29 11:58:03 2015 @@ -0,0 +1,24 @@ +/* + * 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. + */ + +(function() { + + globalVariable = 1; + +})(); \ No newline at end of file Added: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/jsp/sampleDup.jsp URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/jsp/sampleDup.jsp?rev=1682434&view=auto ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/jsp/sampleDup.jsp (added) +++ maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/jsp/sampleDup.jsp Fri May 29 11:58:03 2015 @@ -0,0 +1,26 @@ +<%-- This file will fail the Apache Rat tests if it does not contain the license, +therefore, the license is included. --%> + +<%-- +* 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. +--%> + +<%-- Introduce unsanitized JSP expression --%> +${my.variable.is.bad} +<%-- Introduce inline style --%> +<p align="center">Inline Styles are bad.</p> \ No newline at end of file