1
2
3
4
5 package com.qulice.checkstyle;
6
7 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
8 import com.puppycrawl.tools.checkstyle.api.DetailAST;
9 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public final class ProhibitTestExpectedCheck extends AbstractCheck {
26
27
28
29
30 public ProhibitTestExpectedCheck() {
31
32 }
33
34 @Override
35 public int[] getDefaultTokens() {
36 return this.getRequiredTokens();
37 }
38
39 @Override
40 public int[] getAcceptableTokens() {
41 return this.getRequiredTokens();
42 }
43
44 @Override
45 public int[] getRequiredTokens() {
46 return new int[] {TokenTypes.ANNOTATION};
47 }
48
49 @Override
50 public void visitToken(final DetailAST ast) {
51 if (ProhibitTestExpectedCheck.isTest(ast)
52 && ProhibitTestExpectedCheck.hasExpected(ast)) {
53 this.log(
54 ast.getLineNo(),
55 "@Test(expected = ...) is not allowed, use Assertions.assertThrows() instead"
56 );
57 }
58 }
59
60 private static boolean isTest(final DetailAST ast) {
61 final DetailAST ident = ast.findFirstToken(TokenTypes.IDENT);
62 final boolean match;
63 if (ident == null) {
64 final DetailAST dot = ast.findFirstToken(TokenTypes.DOT);
65 match = dot != null
66 && dot.getLastChild() != null
67 && "Test".equals(dot.getLastChild().getText());
68 } else {
69 match = "Test".equals(ident.getText());
70 }
71 return match;
72 }
73
74 private static boolean hasExpected(final DetailAST ast) {
75 boolean found = false;
76 DetailAST child = ast.getFirstChild();
77 while (child != null) {
78 if (child.getType() == TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR) {
79 final DetailAST name = child.findFirstToken(TokenTypes.IDENT);
80 if (name != null && "expected".equals(name.getText())) {
81 found = true;
82 break;
83 }
84 }
85 child = child.getNextSibling();
86 }
87 return found;
88 }
89 }