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.junit;
5   
6   import java.util.List;
7   
8   import net.sourceforge.pmd.lang.ast.Node;
9   import net.sourceforge.pmd.lang.java.ast.ASTAnnotation;
10  import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
11  import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
12  import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
13  import net.sourceforge.pmd.lang.java.ast.ASTExtendsList;
14  import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
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.ASTResultType;
18  import net.sourceforge.pmd.lang.java.ast.ASTTypeParameters;
19  import net.sourceforge.pmd.lang.java.ast.TypeNode;
20  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
21  import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
22  
23  public abstract class AbstractJUnitRule extends AbstractJavaRule {
24  
25      public static final Class<?> JUNIT4_CLASS;
26  
27      public static final Class<?> JUNIT3_CLASS;
28  
29      private boolean isJUnit3Class;
30      private boolean isJUnit4Class;
31  
32      static {
33  	Class<?> c;
34  	try {
35  	    c = Class.forName("org.junit.Test");
36  	} catch (ClassNotFoundException t) {
37  	    c = null;
38  	} catch (NoClassDefFoundError t) {
39  	    c = null;
40  	}
41  	JUNIT4_CLASS = c;
42  
43  	try {
44  	    c = Class.forName("junit.framework.TestCase");
45  	} catch (ClassNotFoundException t) {
46  	    c = null;
47      } catch (NoClassDefFoundError t) {
48          c = null;
49  	}
50  	JUNIT3_CLASS = c;
51      }
52  
53      @Override
54      public Object visit(ASTCompilationUnit node, Object data) {
55  
56  	isJUnit3Class = isJUnit4Class = false;
57  
58  	isJUnit3Class = isJUnit3Class(node);
59  	if (!isJUnit3Class) {
60  	    isJUnit4Class = isJUnit4Class(node);
61  	}
62  
63  	if (!isTestNgClass(node) && (isJUnit3Class || isJUnit4Class)) {
64  	    return super.visit(node, data);
65  	}
66  	return data;
67      }
68  
69      private boolean isTestNgClass(ASTCompilationUnit node) {
70          List<ASTImportDeclaration> imports = node.findDescendantsOfType(ASTImportDeclaration.class);
71          for (ASTImportDeclaration i : imports) {
72              if (i.getImportedName() != null && i.getImportedName().startsWith("org.testng")) {
73                  return true;
74              }
75          }
76          return false;
77      }
78  
79      public boolean isJUnitMethod(ASTMethodDeclaration method, Object data) {
80  
81  	if (!method.isPublic() || method.isAbstract() || method.isNative() || method.isStatic()) {
82  	    return false; // skip various inapplicable method variations
83  	}
84  
85  	if (isJUnit3Class) {
86  	    return isJUnit3Method(method);
87  	} else {
88  	    return isJUnit4Method(method);
89  	}
90      }
91  
92      private boolean isJUnit4Method(ASTMethodDeclaration method) {
93  	return doesNodeContainJUnitAnnotation(method.jjtGetParent());
94      }
95  
96      private boolean isJUnit3Method(ASTMethodDeclaration method) {
97  	Node node = method.jjtGetChild(0);
98  	if (node instanceof ASTTypeParameters) {
99  	    node = method.jjtGetChild(1);
100 	}
101 	return ((ASTResultType) node).isVoid() && method.getMethodName().startsWith("test");
102     }
103 
104     private boolean isJUnit3Class(ASTCompilationUnit node) {
105 	if (node.getType() != null && TypeHelper.isA(node, JUNIT3_CLASS)) {
106 	    return true;
107 
108 	} else if (node.getType() == null) {
109 	    ASTClassOrInterfaceDeclaration cid = node.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class);
110 	    if (cid == null) {
111 		return false;
112 	    }
113 	    ASTExtendsList extendsList = cid.getFirstChildOfType(ASTExtendsList.class);
114 	    if (extendsList == null) {
115 		return false;
116 	    }
117 	    if (((ASTClassOrInterfaceType) extendsList.jjtGetChild(0)).getImage().endsWith("TestCase")) {
118 		return true;
119 	    }
120 	    String className = cid.getImage();
121 	    return className.endsWith("Test");
122 	}
123 	return false;
124     }
125 
126     private boolean isJUnit4Class(ASTCompilationUnit node) {
127 	return doesNodeContainJUnitAnnotation(node);
128     }
129 
130     private boolean doesNodeContainJUnitAnnotation(Node node) {
131         List<ASTAnnotation> annotations = node.findDescendantsOfType(ASTAnnotation.class);
132         for (ASTAnnotation annotation : annotations) {
133             Node annotationTypeNode = annotation.jjtGetChild(0);
134             TypeNode annotationType = (TypeNode) annotationTypeNode;
135             if (annotationType.getType() == null) {
136                 ASTName name = annotationTypeNode.getFirstChildOfType(ASTName.class);
137                 if (name != null && "Test".equals(name.getImage())) {
138                     return true;
139                 }
140             } else if (annotationType.getType().equals(JUNIT4_CLASS)) {
141                 return true;
142             }
143         }
144         return false;
145     }
146 }