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 @Override
28 public int[] getDefaultTokens() {
29 return this.getRequiredTokens();
30 }
31
32 @Override
33 public int[] getAcceptableTokens() {
34 return this.getRequiredTokens();
35 }
36
37 @Override
38 public int[] getRequiredTokens() {
39 return new int[] {TokenTypes.ANNOTATION};
40 }
41
42 @Override
43 public void visitToken(final DetailAST ast) {
44 if (ProhibitTestExpectedCheck.isTest(ast)
45 && ProhibitTestExpectedCheck.hasExpected(ast)) {
46 this.log(
47 ast.getLineNo(),
48 "@Test(expected = ...) is not allowed, use Assertions.assertThrows() instead"
49 );
50 }
51 }
52
53
54
55
56
57
58 private static boolean isTest(final DetailAST ast) {
59 final DetailAST ident = ast.findFirstToken(TokenTypes.IDENT);
60 final boolean match;
61 if (ident == null) {
62 final DetailAST dot = ast.findFirstToken(TokenTypes.DOT);
63 match = dot != null
64 && dot.getLastChild() != null
65 && "Test".equals(dot.getLastChild().getText());
66 } else {
67 match = "Test".equals(ident.getText());
68 }
69 return match;
70 }
71
72
73
74
75
76
77 private static boolean hasExpected(final DetailAST ast) {
78 boolean found = false;
79 DetailAST child = ast.getFirstChild();
80 while (child != null) {
81 if (child.getType() == TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR) {
82 final DetailAST name = child.findFirstToken(TokenTypes.IDENT);
83 if (name != null && "expected".equals(name.getText())) {
84 found = true;
85 break;
86 }
87 }
88 child = child.getNextSibling();
89 }
90 return found;
91 }
92 }