• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.support.checkstyle;
18 
19 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
20 import com.puppycrawl.tools.checkstyle.api.DetailAST;
21 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
22 import com.puppycrawl.tools.checkstyle.utils.AnnotationUtility;
23 
24 /**
25  * A check that verifies that all the methods marked with @Test have a matching test size
26  * annotation such as @SmallTest, @MediumTest or @LargeTest. This is needed to make sure
27  * that newly added tests get run in the automatted test runner.
28  */
29 public class TestSizeAnnotationCheck extends AbstractCheck {
30     private static final String SMALL_TEST = "SmallTest";
31     private static final String MEDIUM_TEST = "MediumTest";
32     private static final String LARGE_TEST = "LargeTest";
33     private static final String TEST = "Test";
34 
35     private static final String MESSAGE = "Method with @Test annotation must have a @SmallTest, "
36             + "@MediumTest, or @LargeTest annotation. See https://goo.gl/c2I0WP for recommended "
37             + "timeouts";
38 
39     @Override
getDefaultTokens()40     public int[] getDefaultTokens() {
41         return new int[]{TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF};
42     }
43 
44     @Override
visitToken(DetailAST ast)45     public void visitToken(DetailAST ast) {
46         final boolean classHasTestSizeAnnotation =
47                 AnnotationUtility.containsAnnotation(ast, SMALL_TEST)
48                         || AnnotationUtility.containsAnnotation(ast, MEDIUM_TEST)
49                         || AnnotationUtility.containsAnnotation(ast, LARGE_TEST);
50 
51         DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK);
52         for (DetailAST child = objBlock.getFirstChild();
53                 child != null; child = child.getNextSibling()) {
54             if (child.getType() == TokenTypes.METHOD_DEF
55                     && AnnotationUtility.containsAnnotation(child, TEST)) {
56                 final boolean methodHasTestSizeAnnotation =
57                         AnnotationUtility.containsAnnotation(child, SMALL_TEST)
58                                 || AnnotationUtility.containsAnnotation(child, MEDIUM_TEST)
59                                 || AnnotationUtility.containsAnnotation(child, LARGE_TEST);
60                 if (!classHasTestSizeAnnotation && !methodHasTestSizeAnnotation) {
61                     log(child.getLineNo(), MESSAGE);
62                 }
63             }
64         }
65     }
66 }
67