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.coding;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.NestedForDepthCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
29  
30  public class NestedForDepthCheckTest extends AbstractModuleTestSupport {
31  
32      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/coding/nestedfordepth";
35      }
36  
37      /**
38       * Call the check allowing 2 layers of nested for-statements. This
39       * means the top-level for can contain up to 2 levels of nested for
40       * statements. As the test input has 4 layers of for-statements below
41       * the top-level for statement, this must cause 2 error-messages.
42       *
43       * @throws Exception necessary to fulfill JUnit's
44       *     interface-requirements for test-methods.
45       */
46      @Test
47      public void testNestedForDepthCheckCustomMaxLevelTwo() throws Exception {
48  
49          final String[] expected = {
50              "32:11: " + getCheckMessage(MSG_KEY, 3, 2),
51              "33:13: " + getCheckMessage(MSG_KEY, 4, 2),
52              "36:13: " + getCheckMessage(MSG_KEY, 4, 2),
53          };
54  
55          verifyWithInlineConfigParser(
56                  getPath("InputNestedForDepthCheckCustomMaxLevelTwo.java"),
57                 expected);
58      }
59  
60      /**
61       * Call the check allowing 4 layers of nested for-statements. This
62       * means the top-level for can contain up to 4 levels of nested for
63       * statements. As the test input has 4 layers of for-statements below
64       * the top-level for statement, this must not cause an
65       * error-message.
66       *
67       * @throws Exception necessary to fulfill JUnit's
68       *     interface-requirements for test-methods.
69       */
70      @Test
71      public void testNestedForDepthCheckCustomMaxLevelFour() throws Exception {
72  
73          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
74  
75          verifyWithInlineConfigParser(
76                  getPath("InputNestedForDepthCheckCustomMaxLevelFour.java"),
77                 expected);
78      }
79  
80      @Test
81      public void testTokensNotNull() {
82          final NestedForDepthCheck check = new NestedForDepthCheck();
83          assertWithMessage("Acceptable tokens should not be null")
84              .that(check.getAcceptableTokens())
85              .isNotNull();
86          assertWithMessage("Default tokens should not be null")
87              .that(check.getDefaultTokens())
88              .isNotNull();
89          assertWithMessage("Required tokens should not be null")
90              .that(check.getRequiredTokens())
91              .isNotNull();
92      }
93  
94      @Test
95      public void testNestedForDepthCheckDefaultMaxLevel() throws Exception {
96          final String[] expected = {
97              "27:9: " + getCheckMessage(MSG_KEY, 2, 1),
98          };
99  
100         verifyWithInlineConfigParser(
101                 getPath("InputNestedForDepthCheckDefaultMaxLevel.java"),
102                expected);
103     }
104 }