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.sizes;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.sizes.OuterTypeNumberCheck.MSG_KEY;
24  
25  import java.io.File;
26  import java.util.Optional;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31  import com.puppycrawl.tools.checkstyle.JavaParser;
32  import com.puppycrawl.tools.checkstyle.api.DetailAST;
33  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
34  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
35  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
36  
37  public class OuterTypeNumberCheckTest extends AbstractModuleTestSupport {
38  
39      @Override
40      protected String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/checks/sizes/outertypenumber";
42      }
43  
44      @Test
45      public void testGetRequiredTokens() {
46          final OuterTypeNumberCheck checkObj = new OuterTypeNumberCheck();
47          final int[] expected = {
48              TokenTypes.CLASS_DEF,
49              TokenTypes.INTERFACE_DEF,
50              TokenTypes.ENUM_DEF,
51              TokenTypes.ANNOTATION_DEF,
52              TokenTypes.RECORD_DEF,
53          };
54          assertWithMessage("Default required tokens are invalid")
55              .that(checkObj.getRequiredTokens())
56              .isEqualTo(expected);
57      }
58  
59      @Test
60      public void testGetAcceptableTokens() {
61          final OuterTypeNumberCheck outerTypeNumberObj =
62              new OuterTypeNumberCheck();
63          final int[] actual = outerTypeNumberObj.getAcceptableTokens();
64          final int[] expected = {
65              TokenTypes.CLASS_DEF,
66              TokenTypes.INTERFACE_DEF,
67              TokenTypes.ENUM_DEF,
68              TokenTypes.ANNOTATION_DEF,
69              TokenTypes.RECORD_DEF,
70          };
71  
72          assertWithMessage("Default acceptable tokens are invalid")
73              .that(actual)
74              .isEqualTo(expected);
75      }
76  
77      @Test
78      public void testDefault() throws Exception {
79          final String[] expected = {
80              "8:1: " + getCheckMessage(MSG_KEY, 3, 1),
81          };
82          verifyWithInlineConfigParser(
83                  getPath("InputOuterTypeNumberSimple.java"), expected);
84      }
85  
86      @Test
87      public void testMax30() throws Exception {
88          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
89          verifyWithInlineConfigParser(
90                  getPath("InputOuterTypeNumberSimple1.java"), expected);
91      }
92  
93      @Test
94      public void testWithInnerClass() throws Exception {
95          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
96          verifyWithInlineConfigParser(
97                  getPath("InputOuterTypeNumberEmptyInner.java"), expected);
98      }
99  
100     @Test
101     public void testWithRecords() throws Exception {
102 
103         final int max = 1;
104 
105         final String[] expected = {
106             "9:1: " + getCheckMessage(MSG_KEY, 2, max),
107         };
108 
109         verifyWithInlineConfigParser(
110                 getNonCompilablePath("InputOuterTypeNumberRecords.java"), expected);
111     }
112 
113     /**
114      * Checks if the private field {@code currentDepth} and {@code outerNum} is
115      * properly cleared during the start of processing the next file in the
116      * check as they are file specific values.
117      *
118      * @throws Exception if there is an error.
119      */
120     @Test
121     public void testClearState() throws Exception {
122         final OuterTypeNumberCheck check = new OuterTypeNumberCheck();
123         final DetailAST root = JavaParser.parseFile(
124                 new File(getPath("InputOuterTypeNumberSimple.java")),
125                 JavaParser.Options.WITHOUT_COMMENTS);
126         final Optional<DetailAST> classDef = TestUtil.findTokenInAstByPredicate(root,
127             ast -> ast.getType() == TokenTypes.CLASS_DEF);
128 
129         assertWithMessage("Ast should contain CLASS_DEF")
130                 .that(classDef.isPresent())
131                 .isTrue();
132         assertWithMessage("State is not cleared on beginTree")
133                 .that(
134                     TestUtil.isStatefulFieldClearedDuringBeginTree(check, classDef.get(),
135                             "currentDepth",
136                             currentDepth -> ((Number) currentDepth).intValue() == 0))
137                 .isTrue();
138         assertWithMessage("State is not cleared on beginTree")
139                 .that(
140                     TestUtil.isStatefulFieldClearedDuringBeginTree(check, classDef.get(),
141                             "outerNum",
142                             outerNum -> ((Number) outerNum).intValue() == 0))
143                 .isTrue();
144     }
145 
146 }