View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.puppycrawl.tools.checkstyle.checks.design.HideUtilityClassConstructorCheck.MSG_KEY;
23  import static org.junit.Assert.assertArrayEquals;
24  
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
31  
32  public class HideUtilityClassConstructorCheckTest
33      extends AbstractModuleTestSupport {
34  
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/design/hideutilityclassconstructor";
38      }
39  
40      @Test
41      public void testGetRequiredTokens() {
42          final HideUtilityClassConstructorCheck checkObj =
43              new HideUtilityClassConstructorCheck();
44          final int[] expected = {TokenTypes.CLASS_DEF};
45          assertArrayEquals("Default required tokens are invalid",
46              expected, checkObj.getRequiredTokens());
47      }
48  
49      @Test
50      public void testUtilClass() throws Exception {
51          final DefaultConfiguration checkConfig =
52              createModuleConfig(HideUtilityClassConstructorCheck.class);
53          final String[] expected = {
54              "3:1: " + getCheckMessage(MSG_KEY),
55          };
56          verify(checkConfig, getPath("InputHideUtilityClassConstructorInnerStaticClasses.java"),
57                  expected);
58      }
59  
60      @Test
61      public void testUtilClassPublicCtor() throws Exception {
62          final DefaultConfiguration checkConfig =
63                  createModuleConfig(HideUtilityClassConstructorCheck.class);
64          final String[] expected = {
65              "3:1: " + getCheckMessage(MSG_KEY),
66          };
67          verify(checkConfig, getPath("InputHideUtilityClassConstructorPublic.java"), expected);
68      }
69  
70      @Test
71      public void testUtilClassPrivateCtor() throws Exception {
72          final DefaultConfiguration checkConfig =
73                  createModuleConfig(HideUtilityClassConstructorCheck.class);
74          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
75          verify(checkConfig, getPath("InputHideUtilityClassConstructorPrivate.java"), expected);
76      }
77  
78      /** Non-static methods - always OK. */
79      @Test
80      public void testNonUtilClass() throws Exception {
81          final DefaultConfiguration checkConfig =
82              createModuleConfig(HideUtilityClassConstructorCheck.class);
83          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
84          verify(checkConfig, getPath("InputHideUtilityClassConstructorDesignForExtension.java"),
85                  expected);
86      }
87  
88      @Test
89      public void testDerivedNonUtilClass() throws Exception {
90          final DefaultConfiguration checkConfig =
91              createModuleConfig(HideUtilityClassConstructorCheck.class);
92          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
93          verify(checkConfig, getPath("InputHideUtilityClassConstructorNonUtilityClass.java"),
94                  expected);
95      }
96  
97      @Test
98      public void testOnlyNonStaticFieldNonUtilClass() throws Exception {
99          final DefaultConfiguration checkConfig =
100             createModuleConfig(HideUtilityClassConstructorCheck.class);
101         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
102         verify(checkConfig, getPath("InputHideUtilityClassConstructorRegression.java"),
103                 expected);
104     }
105 
106     @Test
107     public void testEmptyAbstractClass() throws Exception {
108         final DefaultConfiguration checkConfig =
109             createModuleConfig(HideUtilityClassConstructorCheck.class);
110         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
111         verify(checkConfig, getPath("InputHideUtilityClassConstructorAbstractSerializable.java"),
112                 expected);
113     }
114 
115     @Test
116     public void testEmptyClassWithOnlyPrivateFields() throws Exception {
117         final DefaultConfiguration checkConfig =
118             createModuleConfig(HideUtilityClassConstructorCheck.class);
119         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
120         verify(checkConfig, getPath("InputHideUtilityClassConstructorSerializable.java"),
121                 expected);
122     }
123 
124     @Test
125     public void testClassWithStaticInnerClass() throws Exception {
126         final DefaultConfiguration checkConfig =
127             createModuleConfig(HideUtilityClassConstructorCheck.class);
128         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
129         verify(checkConfig, getPath(
130                 "InputHideUtilityClassConstructorSerializableInnerStatic.java"),
131                 expected);
132     }
133 
134     @Test
135     public void testProtectedCtor() throws Exception {
136         final DefaultConfiguration checkConfig =
137             createModuleConfig(HideUtilityClassConstructorCheck.class);
138         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
139         verify(checkConfig, getPath("InputHideUtilityClassConstructor.java"), expected);
140     }
141 
142     @Test
143     public void testGetAcceptableTokens() {
144         final HideUtilityClassConstructorCheck obj = new HideUtilityClassConstructorCheck();
145         final int[] expected = {TokenTypes.CLASS_DEF};
146         assertArrayEquals("Default acceptable tokens are invalid",
147             expected, obj.getAcceptableTokens());
148     }
149 
150 }