View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.lang.java.rule.migrating;
    5
    6   import java.util.ArrayList;
    7   import java.util.List;
    8
    9   import net.sourceforge.pmd.lang.ast.Node;
    10  import net.sourceforge.pmd.lang.java.ast.ASTAnnotation;
    11  import net.sourceforge.pmd.lang.java.ast.ASTBlock;
    12  import net.sourceforge.pmd.lang.java.ast.ASTBlockStatement;
    13  import net.sourceforge.pmd.lang.java.ast.ASTCatchStatement;
    14  import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration;
    15  import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
    16  import net.sourceforge.pmd.lang.java.ast.ASTName;
    17  import net.sourceforge.pmd.lang.java.ast.ASTThrowStatement;
    18  import net.sourceforge.pmd.lang.java.ast.ASTTryStatement;
    19  import net.sourceforge.pmd.lang.java.rule.junit.AbstractJUnitRule;
    20
    21  /**
    22   * This rule finds code like this:
    23   *
    24   * <pre>
    25   * public void testFoo() {
    26   *     try {
    27   *         doSomething();
    28   *         fail(&quot;should have thrown an exception&quot;);
    29   *     } catch (Exception e) {
    30   *     }
    31   * }
    32   * </pre>
    33   *
    34   * In JUnit 4, use
    35   *
    36   * <pre>
    37   *  &#064;Test(expected = Exception.class)
    38   * </pre>
    39   *
    40   * @author acaplan
    41   *
    42   */
    43  public class JUnitUseExpectedRule extends AbstractJUnitRule {
    44
    45      @Override
    46      public Object visit(ASTClassOrInterfaceBodyDeclaration node, Object data) {
    47          boolean inAnnotation = false;
    48          for (int i = 0; i < node.jjtGetNumChildren(); i++) {
    49              Node child = node.jjtGetChild(i);
    50              if (child instanceof ASTAnnotation) {
    51                  ASTName annotationName = child.getFirstDescendantOfType(ASTName.class);
    52                  if ("Test".equals(annotationName.getImage())) {
    53                      inAnnotation = true;
    54                      continue;
    55                  }
    56              }
    57              if (child instanceof ASTMethodDeclaration) {
    58                  boolean isJUnitMethod = isJUnitMethod((ASTMethodDeclaration) child, data);
    59                  if (inAnnotation || isJUnitMethod) {
    60                      List<Node> found = new ArrayList<Node>();
    61                      found.addAll((List<Node>) visit((ASTMethodDeclaration) child, data));
    62                      for (Node name : found) {
    63                          addViolation(data, name);
    64                      }
    65                  }
    66              }
    67              inAnnotation = false;
    68          }
    69
    70          return super.visit(node, data);
    71      }
    72
    73      @Override
    74      public Object visit(ASTMethodDeclaration node, Object data) {
    75          List<ASTTryStatement> catches = node.findDescendantsOfType(ASTTryStatement.class);
    76          List<Node> found = new ArrayList<Node>();
    77          if (catches.isEmpty()) {
    78              return found;
    79          }
    80          for (ASTTryStatement trySt : catches) {
    81              ASTCatchStatement cStatement = getCatch(trySt);
    82              if (cStatement != null) {
    83                  ASTBlock block = (ASTBlock) cStatement.jjtGetChild(1);
    84                  if (block.jjtGetNumChildren() != 0) {
    85                      continue;
    86                  }
    87                  List<ASTBlockStatement> blocks = trySt.jjtGetChild(0).findDescendantsOfType(ASTBlockStatement.class);
    88                  if (blocks.isEmpty()) {
    89                      continue;
    90                  }
    91                  ASTBlockStatement st = blocks.get(blocks.size() - 1);
    92                  ASTName name = st.getFirstDescendantOfType(ASTName.class);
    93                  if (name != null && st.equals(name.getNthParent(5)) && "fail".equals(name.getImage())) {
    94                      found.add(name);
    95                      continue;
    96                  }
    97                  ASTThrowStatement th = st.getFirstDescendantOfType(ASTThrowStatement.class);
    98                  if (th != null && st.equals(th.getNthParent(2))) {
    99                      found.add(th);
    100                     continue;
    101                 }
    102             }
    103         }
    104         return found;
    105     }
    106
    107     private ASTCatchStatement getCatch(Node n) {
    108         for (int i = 0; i < n.jjtGetNumChildren(); i++) {
    109             if (n.jjtGetChild(i) instanceof ASTCatchStatement) {
    110                 return (ASTCatchStatement) n.jjtGetChild(i);
    111             }
    112         }
    113         return null;
    114     }
    115 }