Skip to content

Commit 597d4cb

Browse files
committed
[MPMD-290] - Add integration test for CPD for C#
Also add example in doc Closes #32
1 parent d766fdb commit 597d4cb

File tree

8 files changed

+270
-0
lines changed

8 files changed

+270
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.goals = clean verify
19+
invoker.buildResult = failure
20+
invoker.debug = true
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<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">
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<groupId>org.apache.maven.plugins.pmd.its</groupId>
26+
<artifactId>MPMD-290-cpd-for-csharp</artifactId>
27+
<version>1.0-SNAPSHOT</version>
28+
29+
<description>
30+
Use CPD via m-pmd-p to analyze duplications in c# files.
31+
</description>
32+
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-pmd-plugin</artifactId>
38+
<version>@project.version@</version>
39+
<configuration>
40+
<language>cs</language>
41+
<minimumTokens>10</minimumTokens>
42+
<includes>
43+
<include>**/*.cs</include>
44+
</includes>
45+
<compileSourceRoots>
46+
<compileSourceRoot>${basedir}/src/main/cs</compileSourceRoot>
47+
</compileSourceRoots>
48+
<printFailingErrors>true</printFailingErrors>
49+
</configuration>
50+
<executions>
51+
<execution>
52+
<goals>
53+
<goal>cpd-check</goal>
54+
</goals>
55+
</execution>
56+
</executions>
57+
<dependencies>
58+
<dependency>
59+
<groupId>net.sourceforge.pmd</groupId>
60+
<artifactId>pmd-cs</artifactId>
61+
<version>@pmdVersion@</version>
62+
</dependency>
63+
</dependencies>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
68+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
class Sample1 {
21+
public void bar() {
22+
int x = 1;
23+
int y = 2;
24+
int z = x + y;
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
class Sample2 {
21+
public void bar() {
22+
int x = 1;
23+
int y = 2;
24+
int z = x + y;
25+
}
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
21+
File buildLog = new File( basedir, 'build.log' )
22+
assert buildLog.exists()
23+
24+
assert buildLog.text.contains( "[INFO] CPD Failure: Found 7 lines of duplicated code at locations" )
25+
assert buildLog.text.contains( "[DEBUG] PMD failureCount: 1, warningCount: 0" )
26+
27+
File cpdXml = new File( basedir, 'target/cpd.xml' )
28+
assert cpdXml.exists()
29+
30+
// no duplication for the license header - if this is reported, then CPD uses the wrong language/tokenizer
31+
assert !cpdXml.text.contains( '<duplication lines="20" tokens="148">' )
32+
assert !cpdXml.text.contains( 'line="1"' )
33+
34+
// the only valid duplication
35+
assert cpdXml.text.contains( '<duplication lines="7" tokens="26">' )
36+
assert cpdXml.text.contains( 'line="20"' )
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
------
2+
Finding duplicated code in C#
3+
------
4+
Andreas Dangel
5+
------
6+
2020-10-02
7+
------
8+
9+
~~ Licensed to the Apache Software Foundation (ASF) under one
10+
~~ or more contributor license agreements. See the NOTICE file
11+
~~ distributed with this work for additional information
12+
~~ regarding copyright ownership. The ASF licenses this file
13+
~~ to you under the Apache License, Version 2.0 (the
14+
~~ "License"); you may not use this file except in compliance
15+
~~ with the License. You may obtain a copy of the License at
16+
~~
17+
~~ http://www.apache.org/licenses/LICENSE-2.0
18+
~~
19+
~~ Unless required by applicable law or agreed to in writing,
20+
~~ software distributed under the License is distributed on an
21+
~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22+
~~ KIND, either express or implied. See the License for the
23+
~~ specific language governing permissions and limitations
24+
~~ under the License.
25+
26+
~~ NOTE: For help with the syntax of this file, see:
27+
~~ http://maven.apache.org/doxia/references/apt-format.html
28+
29+
Finding duplicated code in C#
30+
31+
By default, the maven-pmd-plugin only supports the languages Java, JavaScript and JSP.
32+
But {{{https://pmd.github.io/latest/pmd_userdocs_cpd.html#supported-languages}CPD supports many more languages}},
33+
e.g. C#. In order to enable C# in your build, you need to
34+
configure several parts:
35+
36+
* Add an additional plugin dependency for c# (pmd-cs module)
37+
38+
* Select the language <<<cs>>>.
39+
40+
* Configure the includes filter to consider <<<*.cs>>> (otherwise only java files will be analyzed)
41+
42+
* Configure the source directory (by default, only <<<src/main/java>>> is analyzed)
43+
44+
45+
+-----+
46+
<project>
47+
...
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-pmd-plugin</artifactId>
53+
<version>${project.version}</version>
54+
<configuration>
55+
<language>cs</language>
56+
<minimumTokens>10</minimumTokens>
57+
<includes>
58+
<include>**/*.cs</include>
59+
</includes>
60+
<compileSourceRoots>
61+
<compileSourceRoot>${basedir}/src/main/cs</compileSourceRoot>
62+
</compileSourceRoots>
63+
<printFailingErrors>true</printFailingErrors>
64+
</configuration>
65+
<executions>
66+
<execution>
67+
<goals>
68+
<goal>cpd-check</goal>
69+
</goals>
70+
</execution>
71+
</executions>
72+
<dependencies>
73+
<dependency>
74+
<groupId>net.sourceforge.pmd</groupId>
75+
<artifactId>pmd-cs</artifactId>
76+
<version>${pmdVersion}</version>
77+
</dependency>
78+
</dependencies>
79+
</plugin>
80+
...
81+
</plugins>
82+
</build>
83+
</project>
84+
+-----+
85+
86+
In this example, the C# source files are located in <<<src/main/cs>>>.
87+
88+
<<Note:>> The version for <<<net.sourceforge.pmd:pmd-cs>>> needs to match the PMD version, that is
89+
being used. If you {{{./upgrading-PMD-at-runtime.html}upgrade PMD at runtime}} you'll need to make
90+
sure, to change the version here as well.
91+

src/site/apt/index.apt.vm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,6 @@ ${project.name}
9898

9999
* {{{./examples/jspReport.html}Analyzing Java Server Pages Code}}
100100

101+
* {{{./examples/cpdCsharp.html}Finding duplicated code in C#}}
102+
101103
[]

src/site/site.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ under the License.
4242
<item name="Analyzing JavaScript" href="examples/javascriptReport.html"/>
4343
<item name="Analyzing Java Server Pages" href="examples/jspReport.html"/>
4444
<item name="Violations Exclusions" href="examples/violation-exclusions.html"/>
45+
<item name="Duplicated code in C#" href="examples/cpdCsharp.html"/>
4546
</menu>
4647
</body>
4748
</project>

0 commit comments

Comments
 (0)