From 746f7c0682a3b2aeb0f67bce10a66d2b26d7d4ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Thu, 23 Mar 2017 21:03:12 +0100 Subject: [PATCH] Get the @Rule annotated ExpectedExceptions fields --- .../JUnitTestsShouldIncludeAssertRule.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/JUnitTestsShouldIncludeAssertRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/JUnitTestsShouldIncludeAssertRule.java index f0e7d35442..53e8d2fe38 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/JUnitTestsShouldIncludeAssertRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/junit/JUnitTestsShouldIncludeAssertRule.java @@ -4,16 +4,25 @@ package net.sourceforge.pmd.lang.java.rule.junit; +import java.util.ArrayList; import java.util.List; +import java.util.Map; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.java.ast.ASTAnnotation; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; +import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; +import net.sourceforge.pmd.lang.java.ast.ASTMarkerAnnotation; import net.sourceforge.pmd.lang.java.ast.ASTMemberValuePair; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTName; import net.sourceforge.pmd.lang.java.ast.ASTNormalAnnotation; import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression; +import net.sourceforge.pmd.lang.java.ast.ASTReferenceType; import net.sourceforge.pmd.lang.java.ast.ASTStatementExpression; +import net.sourceforge.pmd.lang.symboltable.NameDeclaration; +import net.sourceforge.pmd.lang.symboltable.NameOccurrence; +import net.sourceforge.pmd.lang.symboltable.Scope; public class JUnitTestsShouldIncludeAssertRule extends AbstractJUnitRule { @@ -35,6 +44,34 @@ public class JUnitTestsShouldIncludeAssertRule extends AbstractJUnitRule { return data; } + private List getRuleAnnotatedExpectedExceptions(Scope classScope) { + List result = new ArrayList<>(); + Map> decls = classScope.getDeclarations(); + + for (NameDeclaration decl : decls.keySet()) { + Node parent = decl.getNode().jjtGetParent().jjtGetParent().jjtGetParent(); + if (parent.hasDescendantOfType(ASTAnnotation.class) + && parent.jjtGetChild(1) instanceof ASTFieldDeclaration) { + if (!"Rule" + .equals(parent.getFirstDescendantOfType(ASTMarkerAnnotation.class).jjtGetChild(0).getImage())) { + System.out.println( + parent.getFirstDescendantOfType(ASTMarkerAnnotation.class).jjtGetChild(0).getImage()); + continue; + } + + Node type = parent.getFirstDescendantOfType(ASTReferenceType.class); + if (!"ExpectedException".equals(type.jjtGetChild(0).getImage())) { + System.out.println(type.jjtGetChild(0).getImage()); + continue; + } + + result.add(decl); + } + } + + return result; + } + private boolean containsAssert(Node n, boolean assertFound) { if (!assertFound) { if (n instanceof ASTStatementExpression) {