1 package com.puppycrawl.tools.checkstyle.checks.coding; 20 21 import com.puppycrawl.tools.checkstyle.api.Check; 22 import com.puppycrawl.tools.checkstyle.api.DetailAST; 23 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 24 25 import com.puppycrawl.tools.checkstyle.checks.CheckUtils; 26 27 38 public final class JUnitTestCaseCheck extends Check 39 { 40 41 private static final String SET_UP_METHOD_NAME = "setUp"; 42 43 private static final String TEAR_DOWN_METHOD_NAME = "tearDown"; 44 45 private static final String SUITE_METHOD_NAME = "suite"; 46 47 48 public int[] getDefaultTokens() 49 { 50 return new int[] {TokenTypes.METHOD_DEF}; 51 } 52 53 54 public int[] getRequiredTokens() 55 { 56 return getDefaultTokens(); 57 } 58 59 60 public void visitToken(DetailAST aAST) 61 { 62 switch (aAST.getType()) { 63 case TokenTypes.METHOD_DEF: 64 visitMethodDef(aAST); 65 break; 66 default: 67 throw new IllegalStateException (aAST.toString()); 68 } 69 } 70 71 75 private void visitMethodDef(DetailAST aAST) 76 { 77 final String name = aAST.findFirstToken(TokenTypes.IDENT).getText(); 78 79 if (name.equalsIgnoreCase(SET_UP_METHOD_NAME)) { 80 checkSetUpTearDownMethod(aAST, name, SET_UP_METHOD_NAME); 81 } 82 else if (name.equalsIgnoreCase(TEAR_DOWN_METHOD_NAME)) { 83 checkSetUpTearDownMethod(aAST, name, TEAR_DOWN_METHOD_NAME); 84 } 85 else if (name.equalsIgnoreCase(SUITE_METHOD_NAME)) { 86 checkSuiteMethod(aAST, name); 87 } 88 } 89 90 95 private void checkSuiteMethod(DetailAST aAST, String aActualName) 96 { 97 if (!aActualName.equals(SUITE_METHOD_NAME)) { 98 log(aAST, "junit.method.name", SUITE_METHOD_NAME); 99 } 100 101 if (!isPublicAndStatic(aAST)) { 102 log(aAST, "junit.method.public.and.static", SUITE_METHOD_NAME); 103 } 104 105 final DetailAST typeAST = aAST.findFirstToken(TokenTypes.TYPE); 107 final boolean isArray = 108 (typeAST.findFirstToken(TokenTypes.ARRAY_DECLARATOR) != null); 109 final String type = CheckUtils.createFullType(typeAST).getText(); 110 if (isArray 111 || (!"Test".equals(type) 112 && !"junit.framework.Test".equals(type))) 113 { 114 log(aAST, "junit.method.return.type", 115 SUITE_METHOD_NAME, "junit.framework.Test"); 116 } 117 checkParameters(aAST, SUITE_METHOD_NAME); 118 } 119 120 126 private void checkSetUpTearDownMethod(DetailAST aAST, String aActualName, 127 String aExpectedName) 128 { 129 if (!aActualName.equals(aExpectedName)) { 130 log(aAST, "junit.method.name", aActualName, aExpectedName); 131 } 132 133 if (!isPublicOrProtected(aAST)) { 134 log(aAST, "junit.method.protected.or.public", aExpectedName); 135 } 136 137 if (isStatic(aAST)) { 138 log(aAST, "junit.method.static", aExpectedName); 139 } 140 141 checkReturnValue(aAST, aActualName); 142 checkParameters(aAST, aActualName); 143 } 144 145 150 private void checkReturnValue(DetailAST aAST, String aName) 151 { 152 final DetailAST returnValueAST = aAST.findFirstToken(TokenTypes.TYPE); 153 154 if (returnValueAST.findFirstToken(TokenTypes.LITERAL_VOID) == null) { 155 log(aAST, "junit.method.return.type", aName, "void"); 156 } 157 } 158 159 164 private void checkParameters(DetailAST aAST, String aName) 165 { 166 final DetailAST parametersAST = 167 aAST.findFirstToken(TokenTypes.PARAMETERS); 168 169 if (parametersAST.getChildCount() != 0) { 170 log(aAST, "junit.method.parameters", aName); 171 } 172 } 173 174 180 private boolean isPublicOrProtected(DetailAST aAST) 181 { 182 final DetailAST modifiersAST = 183 aAST.findFirstToken(TokenTypes.MODIFIERS); 184 final DetailAST publicAST = 185 modifiersAST.findFirstToken(TokenTypes.LITERAL_PUBLIC); 186 final DetailAST protectedAST = 187 modifiersAST.findFirstToken(TokenTypes.LITERAL_PROTECTED); 188 189 return (publicAST != null) || (protectedAST != null); 190 } 191 192 198 private boolean isPublicAndStatic(DetailAST aAST) 199 { 200 final DetailAST modifiersAST = 201 aAST.findFirstToken(TokenTypes.MODIFIERS); 202 final DetailAST publicAST = 203 modifiersAST.findFirstToken(TokenTypes.LITERAL_PUBLIC); 204 final DetailAST staticAST = 205 modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC); 206 207 return (publicAST != null) && (staticAST != null); 208 } 209 210 215 private boolean isStatic(DetailAST aAST) 216 { 217 final DetailAST modifiersAST = 218 aAST.findFirstToken(TokenTypes.MODIFIERS); 219 final DetailAST staticAST = 220 modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC); 221 222 return (staticAST != null); 223 } 224 } 225 | Popular Tags |