View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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      /** Non-static methods - always OK. */
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 }