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.HideUtilityClassConstructorCheck.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 HideUtilityClassConstructorCheckTest
32 extends AbstractModuleTestSupport {
33
34 @Override
35 protected String getPackageLocation() {
36 return "com/puppycrawl/tools/checkstyle/checks/design/hideutilityclassconstructor";
37 }
38
39 @Test
40 public void testGetRequiredTokens() {
41 final HideUtilityClassConstructorCheck checkObj =
42 new HideUtilityClassConstructorCheck();
43 final int[] expected = {TokenTypes.CLASS_DEF};
44 assertWithMessage("Default required tokens are invalid")
45 .that(checkObj.getRequiredTokens())
46 .isEqualTo(expected);
47 }
48
49 @Test
50 public void testUtilClass() throws Exception {
51 final String[] expected = {
52 "9:1: " + getCheckMessage(MSG_KEY),
53 };
54 verifyWithInlineConfigParser(
55 getPath("InputHideUtilityClassConstructorInnerStaticClasses.java"),
56 expected);
57 }
58
59 @Test
60 public void testUtilClassPublicCtor() throws Exception {
61 final String[] expected = {
62 "9:1: " + getCheckMessage(MSG_KEY),
63 };
64 verifyWithInlineConfigParser(
65 getPath("InputHideUtilityClassConstructorPublic.java"), expected);
66 }
67
68 @Test
69 public void testUtilClassPrivateCtor() throws Exception {
70 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
71 verifyWithInlineConfigParser(
72 getPath("InputHideUtilityClassConstructorPrivate.java"), expected);
73 }
74
75
76 @Test
77 public void testNonUtilClass() throws Exception {
78 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
79 verifyWithInlineConfigParser(
80 getPath("InputHideUtilityClassConstructorDesignForExtension.java"),
81 expected);
82 }
83
84 @Test
85 public void testDerivedNonUtilClass() throws Exception {
86 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
87 verifyWithInlineConfigParser(
88 getPath("InputHideUtilityClassConstructorNonUtilityClass.java"),
89 expected);
90 }
91
92 @Test
93 public void testOnlyNonStaticFieldNonUtilClass() throws Exception {
94 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
95 verifyWithInlineConfigParser(
96 getPath("InputHideUtilityClassConstructorRegression.java"),
97 expected);
98 }
99
100 @Test
101 public void testEmptyAbstractClass() throws Exception {
102 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
103 verifyWithInlineConfigParser(
104 getPath("InputHideUtilityClassConstructorAbstractSerializable.java"),
105 expected);
106 }
107
108 @Test
109 public void testEmptyAbstractClass2() throws Exception {
110 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
111 verifyWithInlineConfigParser(
112 getPath("InputHideUtilityClassConstructorAbstract.java"),
113 expected);
114 }
115
116 @Test
117 public void testEmptyClassWithOnlyPrivateFields() throws Exception {
118 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
119 verifyWithInlineConfigParser(
120 getPath("InputHideUtilityClassConstructorSerializable.java"),
121 expected);
122 }
123
124 @Test
125 public void testClassWithStaticInnerClass() throws Exception {
126 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
127 verifyWithInlineConfigParser(
128 getPath(
129 "InputHideUtilityClassConstructorSerializableInnerStatic.java"),
130 expected);
131 }
132
133 @Test
134 public void testProtectedCtor() throws Exception {
135 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
136 verifyWithInlineConfigParser(
137 getPath("InputHideUtilityClassConstructor.java"), expected);
138 }
139
140 @Test
141 public void testGetAcceptableTokens() {
142 final HideUtilityClassConstructorCheck obj = new HideUtilityClassConstructorCheck();
143 final int[] expected = {TokenTypes.CLASS_DEF};
144 assertWithMessage("Default acceptable tokens are invalid")
145 .that(obj.getAcceptableTokens())
146 .isEqualTo(expected);
147 }
148
149 @Test
150 public void testIgnoreAnnotatedBy() throws Exception {
151 final String[] expected = {
152 "30:1: " + getCheckMessage(MSG_KEY),
153 };
154 verifyWithInlineConfigParser(
155 getPath("InputHideUtilityClassConstructorIgnoreAnnotationBy.java"),
156 expected
157 );
158 }
159
160 @Test
161 public void testIgnoreAnnotatedByFullQualifier() throws Exception {
162 final String[] expected = {
163 "9:1: " + getCheckMessage(MSG_KEY),
164 };
165 verifyWithInlineConfigParser(
166 getPath("InputHideUtilityClassConstructor"
167 + "IgnoreAnnotationByFullyQualifiedName.java"),
168 expected
169 );
170 }
171 }