-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
DesignForExtensionCheckTest.java
166 lines (148 loc) · 7.9 KB
/
DesignForExtensionCheckTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2021 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.design;
import static com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck.MSG_KEY;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
public class DesignForExtensionCheckTest
extends AbstractModuleTestSupport {
@Override
protected String getPackageLocation() {
return "com/puppycrawl/tools/checkstyle/checks/design/designforextension";
}
@Test
public void testGetRequiredTokens() {
final DesignForExtensionCheck checkObj = new DesignForExtensionCheck();
final int[] expected = {TokenTypes.METHOD_DEF};
assertArrayEquals(expected, checkObj.getRequiredTokens(),
"Default required tokens are invalid");
}
@Test
public void testIt() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(DesignForExtensionCheck.class);
final String[] expected = {
"46:5: " + getCheckMessage(MSG_KEY, "InputDesignForExtension", "doh"),
"100:9: " + getCheckMessage(MSG_KEY, "anotherNonFinalClass", "someMethod"),
};
verify(checkConfig, getPath("InputDesignForExtension.java"), expected);
}
@Test
public void testGetAcceptableTokens() {
final DesignForExtensionCheck obj = new DesignForExtensionCheck();
final int[] expected = {TokenTypes.METHOD_DEF};
assertArrayEquals(expected, obj.getAcceptableTokens(),
"Default acceptable tokens are invalid");
}
@Test
public void testOverridableMethods() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(DesignForExtensionCheck.class);
final String[] expected = {
"6:9: " + getCheckMessage(MSG_KEY, "A", "foo1"),
"30:9: " + getCheckMessage(MSG_KEY, "A", "foo8"),
"35:9: " + getCheckMessage(MSG_KEY, "A", "foo9"),
"42:9: " + getCheckMessage(MSG_KEY, "A", "foo10"),
"49:9: " + getCheckMessage(MSG_KEY, "A", "foo11"),
"54:9: " + getCheckMessage(MSG_KEY, "A", "foo12"),
"61:9: " + getCheckMessage(MSG_KEY, "A", "foo13"),
"68:9: " + getCheckMessage(MSG_KEY, "A", "foo14"),
"90:9: " + getCheckMessage(MSG_KEY, "A", "foo22"),
"96:9: " + getCheckMessage(MSG_KEY, "A", "foo23"),
"105:9: " + getCheckMessage(MSG_KEY, "A", "foo25"),
"110:9: " + getCheckMessage(MSG_KEY, "A", "foo26"),
"117:9: " + getCheckMessage(MSG_KEY, "A", "foo27"),
"129:9: " + getCheckMessage(MSG_KEY, "A", "foo29"),
"151:9: " + getCheckMessage(MSG_KEY, "A", "foo31"),
"162:9: " + getCheckMessage(MSG_KEY, "A", "foo33"),
"168:9: " + getCheckMessage(MSG_KEY, "A", "foo34"),
"190:9: " + getCheckMessage(MSG_KEY, "A", "foo39"),
"197:9: " + getCheckMessage(MSG_KEY, "A", "foo41"),
};
verify(checkConfig, getPath("InputDesignForExtensionOverridableMethods.java"), expected);
}
@Test
public void testIgnoredAnnotationsOption() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(DesignForExtensionCheck.class);
checkConfig.addAttribute("ignoredAnnotations", "Override, Deprecated, MyAnnotation");
final String[] expected = {
"31:5: "
+ getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "foo1"),
"141:5: "
+ getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "foo21"),
"146:5: "
+ getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "setAge"),
"161:5: "
+ getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "foo24"),
};
verify(checkConfig, getPath("InputDesignForExtensionIgnoredAnnotations.java"), expected);
}
@Test
public void testIgnoreAnnotationsOptionWithMultipleAnnotations() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(DesignForExtensionCheck.class);
checkConfig.addAttribute("ignoredAnnotations",
"Override, Deprecated, Before, After, BeforeClass, AfterClass");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputDesignForExtensionMultipleAnnotations.java"), expected);
}
@Test
public void testNativeMethods() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(DesignForExtensionCheck.class);
checkConfig.addAttribute("ignoredAnnotations", "Deprecated");
final String[] expected = {
"8:5: " + getCheckMessage(MSG_KEY, "InputDesignForExtensionNativeMethods", "foo1"),
"24:5: " + getCheckMessage(MSG_KEY, "InputDesignForExtensionNativeMethods", "foo6"),
};
verify(checkConfig, getPath("InputDesignForExtensionNativeMethods.java"), expected);
}
@Test
public void testDesignForExtensionRecords() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(DesignForExtensionCheck.class);
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(checkConfig,
getNonCompilablePath("InputDesignForExtensionRecords.java"), expected);
}
@Test
public void testRequiredJavadocPhrase() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(DesignForExtensionCheck.class);
checkConfig.addAttribute("requiredJavadocPhrase", "This implementation");
final String className = "InputDesignForExtensionRequiredJavadocPhrase";
final String[] expected = {
"37:5: " + getCheckMessage(MSG_KEY, className, "foo5"),
"44:5: " + getCheckMessage(MSG_KEY, className, "foo8"),
"47:5: " + getCheckMessage(MSG_KEY, className, "foo9"),
"63:5: " + getCheckMessage(MSG_KEY, className, "foo12"),
};
verify(checkConfig, getPath("InputDesignForExtensionRequiredJavadocPhrase.java"), expected);
}
@Test
public void testRequiredJavadocPhraseMultiLine() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(DesignForExtensionCheck.class);
checkConfig.addAttribute("requiredJavadocPhrase", "This[\\s\\S]*implementation");
final String className = "InputDesignForExtensionRequiredJavadocPhraseMultiLine";
final String[] expected = {
"19:5: " + getCheckMessage(MSG_KEY, className, "foo2"),
};
verify(checkConfig, getPath("InputDesignForExtensionRequiredJavadocPhraseMultiLine.java"),
expected);
}
}