This is an automated email from the ASF dual-hosted git repository. adangel pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-pmd-plugin.git
commit 597d4cb712cf63a17daf67e6f0c0516fe1643e69 Author: Andreas Dangel <adan...@apache.org> AuthorDate: Fri Oct 16 12:49:27 2020 +0200 [MPMD-290] - Add integration test for CPD for C# Also add example in doc Closes #32 --- src/it/MPMD-290-cpd-for-csharp/invoker.properties | 20 +++++ src/it/MPMD-290-cpd-for-csharp/pom.xml | 68 ++++++++++++++++ .../MPMD-290-cpd-for-csharp/src/main/cs/Sample1.cs | 26 +++++++ .../MPMD-290-cpd-for-csharp/src/main/cs/Sample2.cs | 26 +++++++ src/it/MPMD-290-cpd-for-csharp/verify.groovy | 36 +++++++++ src/site/apt/examples/cpdCsharp.apt.vm | 91 ++++++++++++++++++++++ src/site/apt/index.apt.vm | 2 + src/site/site.xml | 1 + 8 files changed, 270 insertions(+) diff --git a/src/it/MPMD-290-cpd-for-csharp/invoker.properties b/src/it/MPMD-290-cpd-for-csharp/invoker.properties new file mode 100644 index 0000000..7154df4 --- /dev/null +++ b/src/it/MPMD-290-cpd-for-csharp/invoker.properties @@ -0,0 +1,20 @@ +# 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. + +invoker.goals = clean verify +invoker.buildResult = failure +invoker.debug = true diff --git a/src/it/MPMD-290-cpd-for-csharp/pom.xml b/src/it/MPMD-290-cpd-for-csharp/pom.xml new file mode 100644 index 0000000..38907b4 --- /dev/null +++ b/src/it/MPMD-290-cpd-for-csharp/pom.xml @@ -0,0 +1,68 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- +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 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.apache.maven.plugins.pmd.its</groupId> + <artifactId>MPMD-290-cpd-for-csharp</artifactId> + <version>1.0-SNAPSHOT</version> + + <description> + Use CPD via m-pmd-p to analyze duplications in c# files. + </description> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-pmd-plugin</artifactId> + <version>@project.version@</version> + <configuration> + <language>cs</language> + <minimumTokens>10</minimumTokens> + <includes> + <include>**/*.cs</include> + </includes> + <compileSourceRoots> + <compileSourceRoot>${basedir}/src/main/cs</compileSourceRoot> + </compileSourceRoots> + <printFailingErrors>true</printFailingErrors> + </configuration> + <executions> + <execution> + <goals> + <goal>cpd-check</goal> + </goals> + </execution> + </executions> + <dependencies> + <dependency> + <groupId>net.sourceforge.pmd</groupId> + <artifactId>pmd-cs</artifactId> + <version>@pmdVersion@</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + +</project> diff --git a/src/it/MPMD-290-cpd-for-csharp/src/main/cs/Sample1.cs b/src/it/MPMD-290-cpd-for-csharp/src/main/cs/Sample1.cs new file mode 100644 index 0000000..de5f199 --- /dev/null +++ b/src/it/MPMD-290-cpd-for-csharp/src/main/cs/Sample1.cs @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +class Sample1 { + public void bar() { + int x = 1; + int y = 2; + int z = x + y; + } +} \ No newline at end of file diff --git a/src/it/MPMD-290-cpd-for-csharp/src/main/cs/Sample2.cs b/src/it/MPMD-290-cpd-for-csharp/src/main/cs/Sample2.cs new file mode 100644 index 0000000..8730046 --- /dev/null +++ b/src/it/MPMD-290-cpd-for-csharp/src/main/cs/Sample2.cs @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +class Sample2 { + public void bar() { + int x = 1; + int y = 2; + int z = x + y; + } +} \ No newline at end of file diff --git a/src/it/MPMD-290-cpd-for-csharp/verify.groovy b/src/it/MPMD-290-cpd-for-csharp/verify.groovy new file mode 100644 index 0000000..18b724a --- /dev/null +++ b/src/it/MPMD-290-cpd-for-csharp/verify.groovy @@ -0,0 +1,36 @@ + +/* + * 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. + */ + +File buildLog = new File( basedir, 'build.log' ) +assert buildLog.exists() + +assert buildLog.text.contains( "[INFO] CPD Failure: Found 7 lines of duplicated code at locations" ) +assert buildLog.text.contains( "[DEBUG] PMD failureCount: 1, warningCount: 0" ) + +File cpdXml = new File( basedir, 'target/cpd.xml' ) +assert cpdXml.exists() + +// no duplication for the license header - if this is reported, then CPD uses the wrong language/tokenizer +assert !cpdXml.text.contains( '<duplication lines="20" tokens="148">' ) +assert !cpdXml.text.contains( 'line="1"' ) + +// the only valid duplication +assert cpdXml.text.contains( '<duplication lines="7" tokens="26">' ) +assert cpdXml.text.contains( 'line="20"' ) diff --git a/src/site/apt/examples/cpdCsharp.apt.vm b/src/site/apt/examples/cpdCsharp.apt.vm new file mode 100644 index 0000000..4fbf230 --- /dev/null +++ b/src/site/apt/examples/cpdCsharp.apt.vm @@ -0,0 +1,91 @@ + ------ + Finding duplicated code in C# + ------ + Andreas Dangel + ------ + 2020-10-02 + ------ + + ~~ 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. + +~~ NOTE: For help with the syntax of this file, see: +~~ http://maven.apache.org/doxia/references/apt-format.html + +Finding duplicated code in C# + + By default, the maven-pmd-plugin only supports the languages Java, JavaScript and JSP. + But {{{https://pmd.github.io/latest/pmd_userdocs_cpd.html#supported-languages}CPD supports many more languages}}, + e.g. C#. In order to enable C# in your build, you need to + configure several parts: + + * Add an additional plugin dependency for c# (pmd-cs module) + + * Select the language <<<cs>>>. + + * Configure the includes filter to consider <<<*.cs>>> (otherwise only java files will be analyzed) + + * Configure the source directory (by default, only <<<src/main/java>>> is analyzed) + + ++-----+ +<project> + ... + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-pmd-plugin</artifactId> + <version>${project.version}</version> + <configuration> + <language>cs</language> + <minimumTokens>10</minimumTokens> + <includes> + <include>**/*.cs</include> + </includes> + <compileSourceRoots> + <compileSourceRoot>${basedir}/src/main/cs</compileSourceRoot> + </compileSourceRoots> + <printFailingErrors>true</printFailingErrors> + </configuration> + <executions> + <execution> + <goals> + <goal>cpd-check</goal> + </goals> + </execution> + </executions> + <dependencies> + <dependency> + <groupId>net.sourceforge.pmd</groupId> + <artifactId>pmd-cs</artifactId> + <version>${pmdVersion}</version> + </dependency> + </dependencies> + </plugin> + ... + </plugins> + </build> +</project> ++-----+ + + In this example, the C# source files are located in <<<src/main/cs>>>. + + <<Note:>> The version for <<<net.sourceforge.pmd:pmd-cs>>> needs to match the PMD version, that is + being used. If you {{{./upgrading-PMD-at-runtime.html}upgrade PMD at runtime}} you'll need to make + sure, to change the version here as well. + diff --git a/src/site/apt/index.apt.vm b/src/site/apt/index.apt.vm index 38161a7..ff3bceb 100644 --- a/src/site/apt/index.apt.vm +++ b/src/site/apt/index.apt.vm @@ -98,4 +98,6 @@ ${project.name} * {{{./examples/jspReport.html}Analyzing Java Server Pages Code}} + * {{{./examples/cpdCsharp.html}Finding duplicated code in C#}} + [] diff --git a/src/site/site.xml b/src/site/site.xml index ca64ccb..455f516 100644 --- a/src/site/site.xml +++ b/src/site/site.xml @@ -42,6 +42,7 @@ under the License. <item name="Analyzing JavaScript" href="examples/javascriptReport.html"/> <item name="Analyzing Java Server Pages" href="examples/jspReport.html"/> <item name="Violations Exclusions" href="examples/violation-exclusions.html"/> + <item name="Duplicated code in C#" href="examples/cpdCsharp.html"/> </menu> </body> </project>