Completed tests for signatures
This commit is contained in:
1 parent
a052550130
commit
8e9313791f
13 files changed
+449
-62
No files matched your search
@@ -40,7 +40,6 @@ class ClassStats {
|
||||
private Map<ClassMetricKey, Double> memo = new HashMap<>();
|
||||
|
||||
// References to the hierarchy
|
||||
// We store strings so that classes not analysed are ignored
|
||||
// TODO:cf useful?
|
||||
// private String superclass;
|
||||
// private List<String> subclasses;
|
||||
@@ -102,7 +101,7 @@ class ClassStats {
|
||||
// Indexing on signatures optimises this type of request
|
||||
for (OperationSignature sig : operations.keySet()) {
|
||||
if (mask.covers(sig)) {
|
||||
if (operations.get(sig).contains(new OperationStats(name))) { // TODO:cf eliminate "new" here
|
||||
if (operations.get(sig).contains(new OperationStats(name))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,11 +12,9 @@ import net.sourceforge.pmd.lang.java.oom.metrics.WmcMetric;
|
||||
|
||||
|
||||
/**
|
||||
* User bound façade of the Metrics Framework. Provides a uniform interface for the calculation of
|
||||
* metrics.
|
||||
* User bound façade of the Metrics Framework. Provides a uniform interface for the calculation of metrics.
|
||||
*
|
||||
*
|
||||
* TODO:cf split up get(ASTMethodOrConstructorDeclaration) and use default methods in
|
||||
* OperationMetric
|
||||
*
|
||||
* @author Clément Fournier
|
||||
*/
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.util.Map;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.QualifiedName;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.FieldSigMask;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.OperationSigMask;
|
||||
|
||||
|
||||
@@ -28,7 +29,7 @@ public final class PackageStats {
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public PackageStats() {
|
||||
PackageStats() {
|
||||
|
||||
}
|
||||
|
||||
@@ -99,6 +100,7 @@ public final class PackageStats {
|
||||
return next;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if the signature of the operation designated by the qualified name is covered by
|
||||
* the mask.
|
||||
@@ -114,8 +116,21 @@ public final class PackageStats {
|
||||
return clazz != null && clazz.hasMatchingSig(qname.getOperation(), sigMask);
|
||||
}
|
||||
|
||||
// TODO:cf make memo routines use a computeIfNotFound parameter
|
||||
// TODO that would save the overhead of going down, returning NaN, computing, going down again and setting it
|
||||
/**
|
||||
* Returns true if the signature of the field designated by its name and the qualified name of its class is
|
||||
* covered by the mask.
|
||||
*
|
||||
* @param qname The class of the field
|
||||
* @param fieldName The name of the field
|
||||
* @param sigMask The signature mask to use
|
||||
*
|
||||
* @return True if the signature of the field is covered by the mask.
|
||||
*/
|
||||
public boolean hasMatchingSig(QualifiedName qname, String fieldName, FieldSigMask sigMask) {
|
||||
ClassStats clazz = getClassStats(qname, false);
|
||||
|
||||
return clazz != null && clazz.hasMatchingSig(fieldName, sigMask);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the value of a metric on a class.
|
||||
|
||||
+6
-1
@@ -5,7 +5,7 @@
|
||||
package net.sourceforge.pmd.lang.java.oom.signature;
|
||||
|
||||
/**
|
||||
* Signature mask for a field.
|
||||
* Signature mask for a field. Newly created masks cover any field.
|
||||
*
|
||||
* @author Clément Fournier
|
||||
*/
|
||||
@@ -27,4 +27,9 @@ public class FieldSigMask extends SigMask<FieldSignature> {
|
||||
public void coverStatic(boolean coverStatic) {
|
||||
this.coverStatic = coverStatic;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean covers(FieldSignature sig) {
|
||||
return super.covers(sig) && (coverFinal || !sig.isFinal) && (coverStatic || !sig.isStatic);
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -35,16 +35,16 @@ public class FieldSignature extends Signature {
|
||||
* @return The signature of the field.
|
||||
*/
|
||||
public static FieldSignature buildFor(ASTFieldDeclaration node) {
|
||||
int code = code(Visibility.get(node), node.isStatic(), node.isAbstract());
|
||||
int code = code(Visibility.get(node), node.isStatic(), node.isFinal());
|
||||
if (!POOL.containsKey(code)) {
|
||||
POOL.put(code, new FieldSignature(Visibility.get(node), node.isStatic(), node.isAbstract()));
|
||||
POOL.put(code, new FieldSignature(Visibility.get(node), node.isStatic(), node.isFinal()));
|
||||
}
|
||||
return POOL.get(code);
|
||||
}
|
||||
|
||||
/** Used internally by the pooler. */
|
||||
private static int code(Visibility visibility, boolean isStatic, boolean isAbstract) {
|
||||
return visibility.hashCode() * 31 + (isStatic ? 1 : 0) * 2 + (isAbstract ? 1 : 0);
|
||||
private static int code(Visibility visibility, boolean isStatic, boolean isFinal) {
|
||||
return visibility.hashCode() * 31 + (isStatic ? 1 : 0) * 2 + (isFinal ? 1 : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Signature mask for an operation.
|
||||
* Signature mask for an operation. Newly created masks cover any operation that is not abstract.
|
||||
*
|
||||
* @author Clément Fournier
|
||||
*/
|
||||
|
||||
+2
-1
@@ -74,7 +74,8 @@ public class OperationSignature extends Signature {
|
||||
private static Role get(ASTMethodDeclaration node) {
|
||||
if (node.isStatic()) {
|
||||
return STATIC;
|
||||
} else if (node.getName().startsWith("get") || node.getName().startsWith("set")) {
|
||||
} else if (node.getName() != null && (node.getName().startsWith("get")
|
||||
|| node.getName().startsWith("set"))) {
|
||||
return GETTER_OR_SETTER; // TODO:cf better getter or setter detection
|
||||
} else {
|
||||
return METHOD;
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Generic signature mask. Newly created masks cover everything.
|
||||
* Generic signature mask.
|
||||
*
|
||||
* @param <T> The type of Signature to handle.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.java.oom;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.ParserTst;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.QualifiedName;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.FieldSigMask;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.FieldSignature;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.OperationSigMask;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.OperationSignature;
|
||||
|
||||
/**
|
||||
* Tests functionality of the whole data structure (PackageStats, ClassStats, OperationStats). The behaviour of the
|
||||
* structure is very encapsulated, so the API to test is restricted per class.
|
||||
*
|
||||
* @author Clément Fournier
|
||||
*/
|
||||
public class DataStructureTest extends ParserTst {
|
||||
|
||||
private PackageStats pack;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
pack = new PackageStats();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddClass() {
|
||||
QualifiedName qname = QualifiedName.parseName("org.foo.Boo");
|
||||
|
||||
assertNull(pack.getClassStats(qname, false));
|
||||
assertNotNull(pack.getClassStats(qname, true));
|
||||
|
||||
// now it's added, this shouldn't return null
|
||||
assertNotNull(pack.getClassStats(qname, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddOperation() {
|
||||
final String TEST = "package org.foo; class Boo{ "
|
||||
+ "public void foo(){}}";
|
||||
|
||||
ASTMethodOrConstructorDeclaration node = getOrderedNodes(ASTMethodDeclaration.class, TEST).get(0);
|
||||
|
||||
QualifiedName qname = node.getQualifiedName();
|
||||
OperationSignature signature = OperationSignature.buildFor(node);
|
||||
|
||||
assertFalse(pack.hasMatchingSig(qname, new OperationSigMask()));
|
||||
|
||||
ClassStats clazz = pack.getClassStats(qname, true);
|
||||
clazz.addOperation("foo()", signature);
|
||||
assertTrue(pack.hasMatchingSig(qname, new OperationSigMask()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddField() {
|
||||
final String TEST = "package org.foo; class Boo{ "
|
||||
+ "public String bar;}";
|
||||
|
||||
ASTFieldDeclaration node = getOrderedNodes(ASTFieldDeclaration.class, TEST).get(0);
|
||||
|
||||
QualifiedName qname = QualifiedName.parseName("org.foo.Boo");
|
||||
String fieldName = "bar";
|
||||
FieldSignature signature = FieldSignature.buildFor(node);
|
||||
|
||||
assertFalse(pack.hasMatchingSig(qname, fieldName, new FieldSigMask()));
|
||||
|
||||
ClassStats clazz = pack.getClassStats(qname, true);
|
||||
clazz.addField(fieldName, signature);
|
||||
assertTrue(pack.hasMatchingSig(qname, fieldName, new FieldSigMask()));
|
||||
}
|
||||
}
|
||||
@@ -22,9 +22,11 @@ import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter;
|
||||
import net.sourceforge.pmd.lang.java.ast.QualifiedName;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.FieldSigMask;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.OperationSigMask;
|
||||
import net.sourceforge.pmd.lang.java.oom.testdata.MetricsVisitorTestClass;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.OperationSignature.Role;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.Signature.Visibility;
|
||||
import net.sourceforge.pmd.lang.java.oom.testdata.MetricsVisitorTestData;
|
||||
import net.sourceforge.pmd.typeresolution.ClassTypeResolverTest;
|
||||
|
||||
/**
|
||||
@@ -38,9 +40,10 @@ public class MetricsVisitorTest {
|
||||
assertNotNull(Metrics.getTopLevelPackageStats());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testAllOperations() {
|
||||
ASTCompilationUnit acu = parseAndVisitForClass15(MetricsVisitorTestClass.class);
|
||||
public void testOperationsAreThere() {
|
||||
ASTCompilationUnit acu = parseAndVisitForClass15(MetricsVisitorTestData.class);
|
||||
|
||||
final PackageStats toplevel = Metrics.getTopLevelPackageStats();
|
||||
|
||||
@@ -54,29 +57,52 @@ public class MetricsVisitorTest {
|
||||
return data;
|
||||
}
|
||||
}, null);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testStaticOperationsSig() {
|
||||
parseAndVisitForClass15(MetricsVisitorTestClass.class);
|
||||
public void testFieldsAreThere() {
|
||||
parseAndVisitForClass15(MetricsVisitorTestData.class);
|
||||
|
||||
|
||||
final PackageStats toplevel = Metrics.getTopLevelPackageStats();
|
||||
|
||||
final OperationSigMask opMask = new OperationSigMask();
|
||||
opMask.restrictRolesTo(Role.STATIC);
|
||||
final FieldSigMask fieldSigMask = new FieldSigMask();
|
||||
|
||||
QualifiedName clazz = QualifiedName.parseName("net.sourceforge.pmd.lang.java"
|
||||
+ ".oom.testdata"
|
||||
+ ".MetricsVisitorTestData");
|
||||
String[] fieldNames = {"x", "y", "z", "t"};
|
||||
Visibility[] visibilities = {Visibility.PUBLIC, Visibility.PRIVATE, Visibility.PROTECTED, Visibility.PACKAGE};
|
||||
|
||||
for (int i = 0; i < fieldNames.length; i++) {
|
||||
fieldSigMask.restrictVisibilitiesTo(visibilities[i]);
|
||||
assertTrue(toplevel.hasMatchingSig(clazz, fieldNames[i], fieldSigMask));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// this test is probably useless, SignatureTest and SigMaskTest already ensure signatures and sigmask have no
|
||||
// problem
|
||||
@Test
|
||||
public void testStaticOperationsSig() {
|
||||
parseAndVisitForClass15(MetricsVisitorTestData.class);
|
||||
|
||||
final PackageStats toplevel = Metrics.getTopLevelPackageStats();
|
||||
|
||||
final OperationSigMask operationSigMask = new OperationSigMask();
|
||||
operationSigMask.restrictRolesTo(Role.STATIC);
|
||||
|
||||
QualifiedName q1 = QualifiedName.parseName("net.sourceforge.pmd.lang.java"
|
||||
+ ".oom.testdata"
|
||||
+ ".MetricsVisitorTestClass#mystatic1()");
|
||||
+ ".oom.testdata"
|
||||
+ ".MetricsVisitorTestData#mystatic1()");
|
||||
|
||||
assertTrue(toplevel.hasMatchingSig(q1, opMask));
|
||||
assertTrue(toplevel.hasMatchingSig(q1, operationSigMask));
|
||||
|
||||
opMask.coverAllRoles();
|
||||
opMask.forbid(Role.STATIC);
|
||||
operationSigMask.coverAllRoles();
|
||||
operationSigMask.forbid(Role.STATIC);
|
||||
|
||||
assertFalse(toplevel.hasMatchingSig(q1, opMask));
|
||||
assertFalse(toplevel.hasMatchingSig(q1, operationSigMask));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
package net.sourceforge.pmd.lang.java.oom;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
@@ -11,56 +12,88 @@ import java.util.List;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.ParserTst;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.FieldSigMask;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.FieldSignature;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.OperationSigMask;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.OperationSignature;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.OperationSignature.Role;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.SigMask;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.Signature.Visibility;
|
||||
|
||||
/**
|
||||
* @author Clément Fournier
|
||||
*/
|
||||
public class SigMaskTest extends ParserTst {
|
||||
|
||||
private static final String TEST_FIELDS = "class Bzaz{"
|
||||
+ "public String x;"
|
||||
+ "private int y;"
|
||||
+ "protected String z;"
|
||||
+ "int s;"
|
||||
+ "public final int t;"
|
||||
+ "private final int a;"
|
||||
+ "protected final double u;"
|
||||
+ "final long v;"
|
||||
+ "static int aa;"
|
||||
+ "static final int ab;"
|
||||
+ "private static int ac;"
|
||||
+ "protected static final int ad;"
|
||||
+ "public static int ag;"
|
||||
+ "}";
|
||||
|
||||
private static final String TEST_OPERATIONS = "abstract class Bzaz{ "
|
||||
// constructors
|
||||
+ "public Bzaz() {}"
|
||||
+ "private Bzaz(int x){}"
|
||||
+ "protected Bzaz(int x, String y){}"
|
||||
// static
|
||||
+ "public static void main(String[] args){}"
|
||||
+ "protected static void makeFoo(){}"
|
||||
+ "private static void makeBar(){}"
|
||||
// getters and setters
|
||||
+ "public int getX(){return 2;}"
|
||||
+ "int getY(){return 0;}"
|
||||
+ "protected void setY(int y){}"
|
||||
+ "private void setX(int x){}"
|
||||
// methods
|
||||
+ "public void foo(){} "
|
||||
+ "void bar(){} "
|
||||
+ "protected void foo(int x){} "
|
||||
+ "private void rand(){}"
|
||||
// abstract
|
||||
+ "protected abstract int getXAbs();"
|
||||
+ "abstract int abs2();"
|
||||
+ "public static abstract String abstr();"
|
||||
+ "abstract int setXAbs();"
|
||||
+ "}";
|
||||
|
||||
/**
|
||||
* Ensure any method is covered by an empty mask.
|
||||
* Ensure any non-abstract method is covered by a newly created mask.
|
||||
*/
|
||||
@Test
|
||||
public void testEmptyOperationMask() {
|
||||
final String TEST = "class Bzaz{ "
|
||||
+ "public void foo(){} "
|
||||
+ "void bar(){} "
|
||||
+ "protected void foo(int x){} "
|
||||
+ "private void rand(){}}";
|
||||
|
||||
List<ASTMethodDeclaration> nodes = getOrderedNodes(ASTMethodDeclaration.class, TEST);
|
||||
List<ASTMethodOrConstructorDeclaration> nodes = getOrderedNodes(ASTMethodOrConstructorDeclaration.class, TEST_OPERATIONS);
|
||||
SigMask<OperationSignature> mask = new OperationSigMask();
|
||||
|
||||
for (ASTMethodDeclaration node : nodes) {
|
||||
assertTrue(mask.covers(OperationSignature.buildFor(node)));
|
||||
for (ASTMethodOrConstructorDeclaration node : nodes) {
|
||||
if (node.isAbstract()) {
|
||||
assertFalse(mask.covers(OperationSignature.buildFor(node)));
|
||||
} else {
|
||||
assertTrue(mask.covers(OperationSignature.buildFor(node)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure any field is covered by an empty mask.
|
||||
* Ensure any field is covered by a newly created mask.
|
||||
*/
|
||||
@Test
|
||||
public void testEmptyFieldMask() {
|
||||
final String TEST = "class Bzaz{"
|
||||
+ "public String x;"
|
||||
+ "private int y;"
|
||||
+ "protected String z;"
|
||||
+ "int s;"
|
||||
+ "public final int t;"
|
||||
+ "private final int a;"
|
||||
+ "protected final double u;"
|
||||
+ "final long v;"
|
||||
+ "}";
|
||||
|
||||
|
||||
List<ASTFieldDeclaration> nodes = getOrderedNodes(ASTFieldDeclaration.class, TEST);
|
||||
List<ASTFieldDeclaration> nodes = getOrderedNodes(ASTFieldDeclaration.class, TEST_FIELDS);
|
||||
SigMask<FieldSignature> mask = new FieldSigMask();
|
||||
|
||||
for (ASTFieldDeclaration node : nodes) {
|
||||
@@ -68,5 +101,181 @@ public class SigMaskTest extends ParserTst {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFinalFields() {
|
||||
List<ASTFieldDeclaration> nodes = getOrderedNodes(ASTFieldDeclaration.class, TEST_FIELDS);
|
||||
FieldSigMask mask = new FieldSigMask();
|
||||
mask.coverFinal(false);
|
||||
|
||||
for (ASTFieldDeclaration node : nodes) {
|
||||
if (node.isFinal()) {
|
||||
assertFalse(mask.covers(FieldSignature.buildFor(node)));
|
||||
} else {
|
||||
assertTrue(mask.covers(FieldSignature.buildFor(node)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStaticFields() {
|
||||
List<ASTFieldDeclaration> nodes = getOrderedNodes(ASTFieldDeclaration.class, TEST_FIELDS);
|
||||
FieldSigMask mask = new FieldSigMask();
|
||||
mask.coverStatic(false);
|
||||
|
||||
for (ASTFieldDeclaration node : nodes) {
|
||||
if (node.isStatic()) {
|
||||
assertFalse(mask.covers(FieldSignature.buildFor(node)));
|
||||
} else {
|
||||
assertTrue(mask.covers(FieldSignature.buildFor(node)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldvisibility() {
|
||||
List<ASTFieldDeclaration> nodes = getOrderedNodes(ASTFieldDeclaration.class, TEST_FIELDS);
|
||||
FieldSigMask mask = new FieldSigMask();
|
||||
|
||||
mask.restrictVisibilitiesTo(Visibility.PUBLIC);
|
||||
|
||||
for (ASTFieldDeclaration node : nodes) {
|
||||
if (node.isPublic()) {
|
||||
assertTrue(mask.covers(FieldSignature.buildFor(node)));
|
||||
} else {
|
||||
assertFalse(mask.covers(FieldSignature.buildFor(node)));
|
||||
}
|
||||
}
|
||||
|
||||
mask.restrictVisibilitiesTo(Visibility.PRIVATE);
|
||||
|
||||
for (ASTFieldDeclaration node : nodes) {
|
||||
if (node.isPrivate()) {
|
||||
assertTrue(mask.covers(FieldSignature.buildFor(node)));
|
||||
} else {
|
||||
assertFalse(mask.covers(FieldSignature.buildFor(node)));
|
||||
}
|
||||
}
|
||||
|
||||
mask.restrictVisibilitiesTo(Visibility.PACKAGE);
|
||||
|
||||
for (ASTFieldDeclaration node : nodes) {
|
||||
if (node.isPackagePrivate()) {
|
||||
assertTrue(mask.covers(FieldSignature.buildFor(node)));
|
||||
} else {
|
||||
assertFalse(mask.covers(FieldSignature.buildFor(node)));
|
||||
}
|
||||
}
|
||||
|
||||
mask.restrictVisibilitiesTo(Visibility.PROTECTED);
|
||||
|
||||
for (ASTFieldDeclaration node : nodes) {
|
||||
if (node.isProtected()) {
|
||||
assertTrue(mask.covers(FieldSignature.buildFor(node)));
|
||||
} else {
|
||||
assertFalse(mask.covers(FieldSignature.buildFor(node)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testOperationVisibility() {
|
||||
List<ASTMethodOrConstructorDeclaration> nodes = getOrderedNodes(ASTMethodOrConstructorDeclaration.class,
|
||||
TEST_OPERATIONS);
|
||||
|
||||
OperationSigMask mask = new OperationSigMask();
|
||||
mask.coverAbstract(true);
|
||||
|
||||
mask.restrictVisibilitiesTo(Visibility.PUBLIC);
|
||||
|
||||
for (ASTMethodOrConstructorDeclaration node : nodes) {
|
||||
if (node.isPublic()) {
|
||||
assertTrue(mask.covers(OperationSignature.buildFor(node)));
|
||||
} else {
|
||||
assertFalse(mask.covers(OperationSignature.buildFor(node)));
|
||||
}
|
||||
}
|
||||
|
||||
mask.restrictVisibilitiesTo(Visibility.PRIVATE);
|
||||
|
||||
for (ASTMethodOrConstructorDeclaration node : nodes) {
|
||||
if (node.isPrivate()) {
|
||||
assertTrue(mask.covers(OperationSignature.buildFor(node)));
|
||||
} else {
|
||||
assertFalse(mask.covers(OperationSignature.buildFor(node)));
|
||||
}
|
||||
}
|
||||
|
||||
mask.restrictVisibilitiesTo(Visibility.PACKAGE);
|
||||
|
||||
for (ASTMethodOrConstructorDeclaration node : nodes) {
|
||||
if (node.isPackagePrivate()) {
|
||||
assertTrue(mask.covers(OperationSignature.buildFor(node)));
|
||||
} else {
|
||||
assertFalse(mask.covers(OperationSignature.buildFor(node)));
|
||||
}
|
||||
}
|
||||
|
||||
mask.restrictVisibilitiesTo(Visibility.PROTECTED);
|
||||
|
||||
for (ASTMethodOrConstructorDeclaration node : nodes) {
|
||||
if (node.isProtected()) {
|
||||
assertTrue(mask.covers(OperationSignature.buildFor(node)));
|
||||
} else {
|
||||
assertFalse(mask.covers(OperationSignature.buildFor(node)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOperationRoles() {
|
||||
List<ASTMethodOrConstructorDeclaration> nodes = getOrderedNodes(ASTMethodOrConstructorDeclaration.class,
|
||||
TEST_OPERATIONS);
|
||||
OperationSigMask mask = new OperationSigMask();
|
||||
mask.restrictRolesTo(Role.STATIC);
|
||||
mask.coverAbstract(true);
|
||||
|
||||
for (ASTMethodOrConstructorDeclaration node : nodes) {
|
||||
if (node.isStatic()) {
|
||||
assertTrue(mask.covers(OperationSignature.buildFor(node)));
|
||||
} else {
|
||||
assertFalse(mask.covers(OperationSignature.buildFor(node)));
|
||||
}
|
||||
}
|
||||
|
||||
mask.restrictRolesTo(Role.CONSTRUCTOR);
|
||||
|
||||
for (ASTMethodOrConstructorDeclaration node : nodes) {
|
||||
if (node instanceof ASTConstructorDeclaration) {
|
||||
assertTrue(mask.covers(OperationSignature.buildFor(node)));
|
||||
} else {
|
||||
assertFalse(mask.covers(OperationSignature.buildFor(node)));
|
||||
}
|
||||
}
|
||||
|
||||
mask.restrictRolesTo(Role.GETTER_OR_SETTER);
|
||||
|
||||
for (ASTMethodOrConstructorDeclaration node : nodes) {
|
||||
if (node instanceof ASTMethodDeclaration
|
||||
&& ((ASTMethodDeclaration) node).getMethodName().matches("(get|set).*")) {
|
||||
assertTrue(mask.covers(OperationSignature.buildFor(node)));
|
||||
} else {
|
||||
assertFalse(mask.covers(OperationSignature.buildFor(node)));
|
||||
}
|
||||
}
|
||||
|
||||
mask.restrictRolesTo(Role.METHOD);
|
||||
|
||||
for (ASTMethodOrConstructorDeclaration node : nodes) {
|
||||
if (node instanceof ASTMethodDeclaration
|
||||
&& !node.isStatic()
|
||||
&& !((ASTMethodDeclaration) node).getMethodName().matches("(get|set).*")) {
|
||||
assertTrue(mask.covers(OperationSignature.buildFor(node)));
|
||||
} else {
|
||||
assertFalse(mask.covers(OperationSignature.buildFor(node)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -16,11 +16,11 @@ import org.junit.Test;
|
||||
import net.sourceforge.pmd.lang.java.ParserTst;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.FieldSignature;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.OperationSignature;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.OperationSignature.Role;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.Signature;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.Signature.Visibility;
|
||||
import net.sourceforge.pmd.lang.java.oom.signature.FieldSignature;
|
||||
|
||||
/**
|
||||
* Test class for {@link Signature} and its subclasses.
|
||||
@@ -72,7 +72,7 @@ public class SignatureTest extends ParserTst {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void roleTest() {
|
||||
public void operationRoleTest() {
|
||||
final String TEST = "class Bzaz{ int x; " +
|
||||
"public static void foo(){} " +
|
||||
"Bzaz(){} " +
|
||||
@@ -96,7 +96,7 @@ public class SignatureTest extends ParserTst {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAbstractTest() {
|
||||
public void isAbstractOperationTest() {
|
||||
final String TEST = "abstract class Bzaz{ int x; " +
|
||||
"public static abstract void foo();" +
|
||||
"protected abstract int bar(int x);" +
|
||||
@@ -120,7 +120,55 @@ public class SignatureTest extends ParserTst {
|
||||
assertFalse(sigs.get(4).isAbstract);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isFinalFieldTest() {
|
||||
final String TEST = "class Bzaz{"
|
||||
+ "public String x;"
|
||||
+ "private int y;"
|
||||
+ "private final int a;"
|
||||
+ "protected final double u;"
|
||||
+ "final long v;"
|
||||
+ "}";
|
||||
|
||||
List<ASTFieldDeclaration> nodes = getOrderedNodes(ASTFieldDeclaration.class, TEST);
|
||||
List<FieldSignature> sigs = new ArrayList<>();
|
||||
|
||||
for (ASTFieldDeclaration node : nodes) {
|
||||
sigs.add(FieldSignature.buildFor(node));
|
||||
}
|
||||
|
||||
assertFalse(sigs.get(0).isFinal);
|
||||
assertFalse(sigs.get(1).isFinal);
|
||||
assertTrue(sigs.get(2).isFinal);
|
||||
assertTrue(sigs.get(3).isFinal);
|
||||
assertTrue(sigs.get(4).isFinal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isStaticFieldTest() {
|
||||
final String TEST = "class Bzaz{"
|
||||
+ "public final String x;"
|
||||
+ "private int y;"
|
||||
+ "private static int a;"
|
||||
+ "protected static final double u;"
|
||||
+ "static long v;"
|
||||
+ "}";
|
||||
|
||||
List<ASTFieldDeclaration> nodes = getOrderedNodes(ASTFieldDeclaration.class, TEST);
|
||||
List<FieldSignature> sigs = new ArrayList<>();
|
||||
|
||||
for (ASTFieldDeclaration node : nodes) {
|
||||
sigs.add(FieldSignature.buildFor(node));
|
||||
}
|
||||
|
||||
assertFalse(sigs.get(0).isStatic);
|
||||
assertFalse(sigs.get(1).isStatic);
|
||||
assertTrue(sigs.get(2).isStatic);
|
||||
assertTrue(sigs.get(3).isStatic);
|
||||
assertTrue(sigs.get(4).isStatic);
|
||||
}
|
||||
|
||||
// Ensure only one instance of a signature is created.
|
||||
@Test
|
||||
public void operationPoolTest() {
|
||||
final String TEST = "class Bzaz{ " +
|
||||
@@ -152,6 +200,7 @@ public class SignatureTest extends ParserTst {
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure only one instance of a signature is created.
|
||||
@Test
|
||||
public void fieldPoolTest() {
|
||||
final String TEST = "class Bzaz {" +
|
||||
|
||||
+3
-3
@@ -9,18 +9,18 @@ package net.sourceforge.pmd.lang.java.oom.testdata;
|
||||
*
|
||||
* @author Clément Fournier
|
||||
*/
|
||||
public class MetricsVisitorTestClass {
|
||||
public class MetricsVisitorTestData {
|
||||
|
||||
public String x;
|
||||
private String y;
|
||||
protected String z;
|
||||
String t;
|
||||
|
||||
public MetricsVisitorTestClass() {
|
||||
public MetricsVisitorTestData() {
|
||||
|
||||
}
|
||||
|
||||
private MetricsVisitorTestClass(String x) {
|
||||
private MetricsVisitorTestData(String x) {
|
||||
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user