Corrected a defect, plus "pmdOnPmd" (added some suppresswarning when defect did not appear to be fixable)
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5797 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -24,8 +24,8 @@ public class ScopedLogHandlersManager {
|
||||
|
||||
public ScopedLogHandlersManager(Level level, Handler... handlers) {
|
||||
newHandlers = handlers;
|
||||
oldHandlers = logger.getHandlers();
|
||||
logger = Logger.getLogger(PACKAGE_NAME);
|
||||
oldHandlers = logger.getHandlers();
|
||||
oldLogLevel = logger.getLevel();
|
||||
logger.setLevel(level);
|
||||
//The Ant logger filters itself
|
||||
|
@ -3,7 +3,6 @@
|
||||
*/
|
||||
package net.sourceforge.pmd.rules;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import net.sourceforge.pmd.AbstractJavaRule;
|
||||
|
@ -69,6 +69,7 @@ public class GenericClassCounterRule extends AbstractJavaRule {
|
||||
private List<String> simpleClassname = new ArrayList<String>(0);
|
||||
|
||||
|
||||
@SuppressWarnings("PMD") // When the rule is finished, this field will be used.
|
||||
private String operand;
|
||||
private int threshold;
|
||||
|
||||
|
@ -1,6 +1,8 @@
|
||||
package net.sourceforge.pmd.rules.junit;
|
||||
|
||||
import net.sourceforge.pmd.AbstractRule;
|
||||
import java.util.List;
|
||||
|
||||
import net.sourceforge.pmd.AbstractJavaRule;
|
||||
import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
|
||||
import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
|
||||
import net.sourceforge.pmd.ast.ASTCompilationUnit;
|
||||
@ -14,17 +16,17 @@ import net.sourceforge.pmd.ast.Node;
|
||||
import net.sourceforge.pmd.ast.SimpleNode;
|
||||
import net.sourceforge.pmd.typeresolution.TypeHelper;
|
||||
|
||||
import java.util.List;
|
||||
@SuppressWarnings("PMD.AvoidCatchingThrowable") // Don't think we can otherwise here...
|
||||
public abstract class AbstractJUnitRule extends AbstractJavaRule {
|
||||
|
||||
public abstract class AbstractJUnitRule extends AbstractRule {
|
||||
|
||||
public static Class junit4Class = null;
|
||||
|
||||
|
||||
public static Class junit3Class = null;
|
||||
|
||||
|
||||
private boolean isJUnit3Class;
|
||||
private boolean isJUnit4Class;
|
||||
|
||||
|
||||
|
||||
static {
|
||||
try {
|
||||
junit4Class = Class.forName("org.junit.Test");
|
||||
@ -38,40 +40,40 @@ public abstract class AbstractJUnitRule extends AbstractRule {
|
||||
junit3Class = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Object visit(ASTCompilationUnit node, Object data){
|
||||
|
||||
|
||||
isJUnit3Class = isJUnit4Class = false;
|
||||
|
||||
|
||||
isJUnit3Class = isJUnit3Class(node);
|
||||
if (!isJUnit3Class) {
|
||||
isJUnit4Class = isJUnit4Class(node);
|
||||
}
|
||||
|
||||
|
||||
if(isJUnit3Class || isJUnit4Class){
|
||||
return super.visit(node, data);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
public boolean isJUnitMethod(ASTMethodDeclaration method, Object data) {
|
||||
|
||||
|
||||
if (!method.isPublic() || method.isAbstract() || method.isNative() || method.isStatic()) {
|
||||
return false; // skip various inapplicable method variations
|
||||
}
|
||||
|
||||
|
||||
if (isJUnit3Class) {
|
||||
return isJUnit3Method(method);
|
||||
return isJUnit3Method(method);
|
||||
}
|
||||
else {
|
||||
return isJUnit4Method(method);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private boolean isJUnit4Method(ASTMethodDeclaration method){
|
||||
return doesNodeContainJUnitAnnotation((SimpleNode)method.jjtGetParent());
|
||||
}
|
||||
|
||||
|
||||
private boolean isJUnit3Method(ASTMethodDeclaration method) {
|
||||
Node node = method.jjtGetChild(0);
|
||||
if (node instanceof ASTTypeParameters) {
|
||||
@ -79,11 +81,11 @@ public abstract class AbstractJUnitRule extends AbstractRule {
|
||||
}
|
||||
return ((ASTResultType) node).isVoid() && method.getMethodName().startsWith("test");
|
||||
}
|
||||
|
||||
|
||||
private boolean isJUnit3Class(ASTCompilationUnit node) {
|
||||
if (node.getType() != null && TypeHelper.isA(node, junit3Class)) {
|
||||
return true;
|
||||
|
||||
|
||||
} else if (node.getType() == null) {
|
||||
ASTClassOrInterfaceDeclaration cid = node.getFirstChildOfType(ASTClassOrInterfaceDeclaration.class);
|
||||
if (cid == null) {
|
||||
@ -92,7 +94,7 @@ public abstract class AbstractJUnitRule extends AbstractRule {
|
||||
ASTExtendsList extendsList = cid.getFirstChildOfType(ASTExtendsList.class);
|
||||
if(extendsList == null){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if(((ASTClassOrInterfaceType)extendsList.jjtGetChild(0)).getImage().endsWith("TestCase")){
|
||||
return true;
|
||||
}
|
||||
@ -101,11 +103,11 @@ public abstract class AbstractJUnitRule extends AbstractRule {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private boolean isJUnit4Class(ASTCompilationUnit node){
|
||||
return doesNodeContainJUnitAnnotation(node);
|
||||
}
|
||||
|
||||
|
||||
private boolean doesNodeContainJUnitAnnotation(SimpleNode node) {
|
||||
List<ASTMarkerAnnotation> lstAnnotations = node.findChildrenOfType(ASTMarkerAnnotation.class);
|
||||
for(ASTMarkerAnnotation annotation : lstAnnotations){
|
||||
@ -114,12 +116,12 @@ public abstract class AbstractJUnitRule extends AbstractRule {
|
||||
if("Test".equals(name.getImage())){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(annotation.getType().equals(junit4Class)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -3,32 +3,31 @@
|
||||
*/
|
||||
package net.sourceforge.pmd.rules.junit;
|
||||
|
||||
import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
|
||||
import net.sourceforge.pmd.ast.ASTMethodDeclaration;
|
||||
import net.sourceforge.pmd.ast.ASTMethodDeclarator;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
|
||||
import net.sourceforge.pmd.ast.ASTMethodDeclaration;
|
||||
|
||||
public class TestClassWithoutTestCases extends AbstractJUnitRule {
|
||||
|
||||
|
||||
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
|
||||
if (node.isAbstract() || node.isInterface() || node.isNested()) {
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
List<ASTMethodDeclaration> m = node.findChildrenOfType(ASTMethodDeclaration.class);
|
||||
boolean testsFound = false;
|
||||
|
||||
|
||||
if (m != null) {
|
||||
for (Iterator<ASTMethodDeclaration> it = m.iterator(); it.hasNext() && !testsFound;) {
|
||||
ASTMethodDeclaration md = it.next();
|
||||
if (!isInInnerClassOrInterface(md)
|
||||
&& isJUnitMethod(md, data))
|
||||
&& isJUnitMethod(md, data))
|
||||
testsFound = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!testsFound) {
|
||||
addViolation(data, node);
|
||||
}
|
||||
|
Reference in New Issue
Block a user