From 2346176fe578d4021e5cafdd270564abb7bb4675 Mon Sep 17 00:00:00 2001 From: oowekyala Date: Sat, 24 Jun 2017 22:20:52 +0200 Subject: [PATCH] Added tests for LOC --- .../ast/ASTClassOrInterfaceDeclaration.java | 2 +- .../pmd/lang/java/ast/ASTEnumDeclaration.java | 2 +- .../pmd/lang/java/oom/ClassStats.java | 4 +- .../pmd/lang/java/oom/OperationStats.java | 1 - .../pmd/lang/java/oom/metrics/AtfdMetric.java | 2 - .../pmd/lang/java/oom/metrics/LocMetric.java | 7 +- .../pmd/lang/java/oom/metrics/NcssMetric.java | 3 +- .../pmd/lang/java/oom/MetricsForceHook.java | 4 + .../lang/java/oom/metrics/AllMetricsTest.java | 1 + .../lang/java/oom/metrics/LocTestRule.java | 24 ++++ .../pmd/lang/java/oom/metrics/xml/LocTest.xml | 134 ++++++++++++++++++ .../resources/rulesets/java/metrics_test.xml | 6 + 12 files changed, 177 insertions(+), 13 deletions(-) create mode 100644 pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/metrics/LocTestRule.java create mode 100644 pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/oom/metrics/xml/LocTest.xml diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassOrInterfaceDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassOrInterfaceDeclaration.java index b939df5c09..e9ac0a2262 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassOrInterfaceDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTClassOrInterfaceDeclaration.java @@ -61,7 +61,7 @@ public class ASTClassOrInterfaceDeclaration extends AbstractJavaAccessTypeNode i } @Override - public TypeKind getTypeKind(){ + public TypeKind getTypeKind() { return isInterface() ? TypeKind.INTERFACE : TypeKind.CLASS; } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumDeclaration.java index fa88fdfbe5..1b8543289b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTEnumDeclaration.java @@ -43,7 +43,7 @@ public class ASTEnumDeclaration extends AbstractJavaAccessTypeNode implements AS } @Override - public TypeKind getTypeKind(){ + public TypeKind getTypeKind() { return TypeKind.ENUM; } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/ClassStats.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/ClassStats.java index e1076cf257..0a884d5270 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/ClassStats.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/ClassStats.java @@ -29,8 +29,8 @@ import net.sourceforge.pmd.lang.java.oom.signature.OperationSignature; /** * Statistics about a class, enum, interface, or annotation. Gathers information about the contained members and their * signatures. This class does not provide methods to operate directly on its nested classes, but only on itself. To - * operate on a nested class, retrieve the correct ClassStats with {@link PackageStats#getClassStats(QualifiedName, - * boolean)} then use the methods of ClassStats. + * operate on a nested class, retrieve the correct ClassStats with + * {@link PackageStats#getClassStats(QualifiedName, boolean)} then use the methods of ClassStats. * *

Note that at this level, entities of the DS do not manipulate QualifiedNames anymore, only Strings. * diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/OperationStats.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/OperationStats.java index c1d9acf422..742b270133 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/OperationStats.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/OperationStats.java @@ -9,7 +9,6 @@ import java.util.Map; import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration; import net.sourceforge.pmd.lang.java.oom.api.MemoKey; -import net.sourceforge.pmd.lang.java.oom.api.Metric; import net.sourceforge.pmd.lang.java.oom.api.Metric.Version; import net.sourceforge.pmd.lang.java.oom.api.MetricVersion; import net.sourceforge.pmd.lang.java.oom.api.OperationMetric; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/AtfdMetric.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/AtfdMetric.java index b2656980e0..e7f768f03a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/AtfdMetric.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/AtfdMetric.java @@ -7,9 +7,7 @@ package net.sourceforge.pmd.lang.java.oom.metrics; import java.util.List; import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration; -import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration; -import net.sourceforge.pmd.lang.java.ast.AccessNode; import net.sourceforge.pmd.lang.java.ast.QualifiedName; import net.sourceforge.pmd.lang.java.oom.AbstractMetric; import net.sourceforge.pmd.lang.java.oom.api.ClassMetric; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/LocMetric.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/LocMetric.java index 658df4ee62..41740dafd6 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/LocMetric.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/LocMetric.java @@ -35,16 +35,13 @@ public final class LocMetric extends AbstractMetric implements ClassMetric, Oper @Override public double computeFor(ASTAnyTypeDeclaration node, MetricVersion version) { - return node.getEndLine() - node.getBeginLine(); + return 1 + node.getEndLine() - node.getBeginLine(); } @Override public double computeFor(ASTMethodOrConstructorDeclaration node, MetricVersion version) { - if (node.isAbstract()) { - return 1; - } - return node.getEndLine() - node.getBeginLine(); + return 1 + node.getEndLine() - node.getBeginLine(); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/NcssMetric.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/NcssMetric.java index 60dc7a6571..6f85a47acd 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/NcssMetric.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/NcssMetric.java @@ -44,7 +44,8 @@ import net.sourceforge.pmd.lang.java.oom.api.OperationMetric; /** * Non Commenting Source Statements. Similar to LOC but only counts statements, which is roughly equivalent * to counting the number of semicolons and opening braces in the program. The precise rules for counting - * statements comply with JavaNCSS rules. + * statements comply with JavaNCSS rules. The only + * difference is that import and package statements are not counted. * * @author Clément Fournier * @see LocMetric diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/MetricsForceHook.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/MetricsForceHook.java index e9b1b6432e..0688c2a4f7 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/MetricsForceHook.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/MetricsForceHook.java @@ -15,4 +15,8 @@ public class MetricsForceHook { Metrics.setForce(b); } + private MetricsForceHook() { + + } + } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/metrics/AllMetricsTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/metrics/AllMetricsTest.java index 5312260473..1d341568c5 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/metrics/AllMetricsTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/metrics/AllMetricsTest.java @@ -18,6 +18,7 @@ public class AllMetricsTest extends SimpleAggregatorTst { addRule(RULESET, "CycloTest"); addRule(RULESET, "NcssTest"); addRule(RULESET, "WmcTest"); + addRule(RULESET, "LocTest"); } } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/metrics/LocTestRule.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/metrics/LocTestRule.java new file mode 100644 index 0000000000..cd7469f1e0 --- /dev/null +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/metrics/LocTestRule.java @@ -0,0 +1,24 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.java.oom.metrics; + +import net.sourceforge.pmd.lang.java.oom.api.ClassMetricKey; +import net.sourceforge.pmd.lang.java.oom.api.OperationMetricKey; + +/** + * @author Clément Fournier + */ +public class LocTestRule extends AbstractMetricTestRule { + + @Override + protected ClassMetricKey getClassKey() { + return ClassMetricKey.LOC; + } + + @Override + protected OperationMetricKey getOpKey() { + return OperationMetricKey.LOC; + } +} diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/oom/metrics/xml/LocTest.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/oom/metrics/xml/LocTest.xml new file mode 100644 index 0000000000..c5cb71a41e --- /dev/null +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/oom/metrics/xml/LocTest.xml @@ -0,0 +1,134 @@ + + + + + + + + + Full example + 3 + + '.Foo' has value 58 highest 40. + '.Foo#foo()' has value 1. + '.Foo#main(String)' has value 40. + + + + + + Empty method + 2 + + '.Foo' has value 5 highest 3. + '.Foo#foo()' has value 3. + + + + + + + + Empty class + 1 + + '.Foo' has value 2 highest 0. + + + + + + + + Switch + 2 + + '.Foo' has value 12 highest 10. + '.Foo#foo()' has value 10. + + + + + + \ No newline at end of file diff --git a/pmd-java/src/test/resources/rulesets/java/metrics_test.xml b/pmd-java/src/test/resources/rulesets/java/metrics_test.xml index b20d689df4..ca7f84ddac 100644 --- a/pmd-java/src/test/resources/rulesets/java/metrics_test.xml +++ b/pmd-java/src/test/resources/rulesets/java/metrics_test.xml @@ -27,4 +27,10 @@ metrics="true"> + + +