1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.design;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck.MSG_KEY;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30
31 public class DesignForExtensionCheckTest
32 extends AbstractModuleTestSupport {
33
34 @Override
35 public String getPackageLocation() {
36 return "com/puppycrawl/tools/checkstyle/checks/design/designforextension";
37 }
38
39 @Test
40 public void testGetRequiredTokens() {
41 final DesignForExtensionCheck checkObj = new DesignForExtensionCheck();
42 final int[] expected = {TokenTypes.METHOD_DEF};
43 assertWithMessage("Default required tokens are invalid")
44 .that(checkObj.getRequiredTokens())
45 .isEqualTo(expected);
46 }
47
48 @Test
49 public void testIt() throws Exception {
50 final String[] expected = {
51 "51:5: " + getCheckMessage(MSG_KEY, "InputDesignForExtension", "doh"),
52 "106:9: " + getCheckMessage(MSG_KEY, "anotherNonFinalClass", "someMethod"),
53 };
54 verifyWithInlineConfigParser(
55 getPath("InputDesignForExtension.java"), expected);
56 }
57
58 @Test
59 public void testGetAcceptableTokens() {
60 final DesignForExtensionCheck obj = new DesignForExtensionCheck();
61 final int[] expected = {TokenTypes.METHOD_DEF};
62 assertWithMessage("Default acceptable tokens are invalid")
63 .that(obj.getAcceptableTokens())
64 .isEqualTo(expected);
65 }
66
67 @Test
68 public void testOverridableMethods() throws Exception {
69 final String[] expected = {
70 "15:9: " + getCheckMessage(MSG_KEY, "A", "foo1"),
71 "40:9: " + getCheckMessage(MSG_KEY, "A", "foo8"),
72 "46:9: " + getCheckMessage(MSG_KEY, "A", "foo9"),
73 "54:9: " + getCheckMessage(MSG_KEY, "A", "foo10"),
74 "62:9: " + getCheckMessage(MSG_KEY, "A", "foo11"),
75 "68:9: " + getCheckMessage(MSG_KEY, "A", "foo12"),
76 "76:9: " + getCheckMessage(MSG_KEY, "A", "foo13"),
77 "84:9: " + getCheckMessage(MSG_KEY, "A", "foo14"),
78 "107:9: " + getCheckMessage(MSG_KEY, "A", "foo22"),
79 "114:9: " + getCheckMessage(MSG_KEY, "A", "foo23"),
80 "124:9: " + getCheckMessage(MSG_KEY, "A", "foo25"),
81 "130:9: " + getCheckMessage(MSG_KEY, "A", "foo26"),
82 "138:9: " + getCheckMessage(MSG_KEY, "A", "foo27"),
83 "151:9: " + getCheckMessage(MSG_KEY, "A", "foo29"),
84 "174:9: " + getCheckMessage(MSG_KEY, "A", "foo31"),
85 "186:9: " + getCheckMessage(MSG_KEY, "A", "foo33"),
86 "193:9: " + getCheckMessage(MSG_KEY, "A", "foo34"),
87 "216:9: " + getCheckMessage(MSG_KEY, "A", "foo39"),
88 "224:9: " + getCheckMessage(MSG_KEY, "A", "foo41"),
89 };
90 verifyWithInlineConfigParser(
91 getPath("InputDesignForExtensionOverridableMethods.java"), expected);
92 }
93
94 @Test
95 public void testIgnoredAnnotationsOption() throws Exception {
96 final String[] expected = {
97 "40:5: "
98 + getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "foo1"),
99 "151:5: "
100 + getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "foo21"),
101 "157:5: "
102 + getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "setAge"),
103 "173:5: "
104 + getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "foo24"),
105 "181:5: "
106 + getCheckMessage(MSG_KEY, "InputDesignForExtensionIgnoredAnnotations", "dontUse4"),
107 };
108 verifyWithInlineConfigParser(
109 getPath("InputDesignForExtensionIgnoredAnnotations.java"), expected);
110 }
111
112 @Test
113 public void testIgnoreAnnotationsOptionWithMultipleAnnotations() throws Exception {
114 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
115 verifyWithInlineConfigParser(
116 getPath("InputDesignForExtensionMultipleAnnotations.java"), expected);
117 }
118
119 @Test
120 public void testNativeMethods() throws Exception {
121 final String[] expected = {
122 "14:5: " + getCheckMessage(MSG_KEY, "InputDesignForExtensionNativeMethods", "foo1"),
123 "31:5: " + getCheckMessage(MSG_KEY, "InputDesignForExtensionNativeMethods", "foo6"),
124 };
125 verifyWithInlineConfigParser(
126 getPath("InputDesignForExtensionNativeMethods.java"), expected);
127 }
128
129 @Test
130 public void testDesignForExtensionRecords() throws Exception {
131
132 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
133
134 verifyWithInlineConfigParser(
135 getPath("InputDesignForExtensionRecords.java"), expected);
136 }
137
138 @Test
139 public void testRequiredJavadocPhrase() throws Exception {
140 final String className = "InputDesignForExtensionRequiredJavadocPhrase";
141 final String[] expected = {
142 "42:5: " + getCheckMessage(MSG_KEY, className, "foo5"),
143 "50:5: " + getCheckMessage(MSG_KEY, className, "foo8"),
144 "54:5: " + getCheckMessage(MSG_KEY, className, "foo9"),
145 "71:5: " + getCheckMessage(MSG_KEY, className, "foo12"),
146 };
147 verifyWithInlineConfigParser(
148 getPath("InputDesignForExtensionRequiredJavadocPhrase.java"), expected);
149 }
150
151 @Test
152 public void testRequiredJavadocPhraseMultiLine() throws Exception {
153 final String className = "InputDesignForExtensionRequiredJavadocPhraseMultiLine";
154 final String[] expected = {
155 "24:5: " + getCheckMessage(MSG_KEY, className, "foo2"),
156 };
157 verifyWithInlineConfigParser(
158 getPath("InputDesignForExtensionRequiredJavadocPhraseMultiLine.java"),
159 expected);
160 }
161
162 @Test
163 public void testInterfaceMemberScopeIsPublic() throws Exception {
164 final String[] expected = {
165 "16:9: " + getCheckMessage(MSG_KEY, "Inner", "getProperty"),
166 };
167 verifyWithInlineConfigParser(
168 getPath("InputDesignForExtensionInterfaceMemberScopeIsPublic.java"),
169 expected);
170 }
171
172 }