View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.whitespace;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyForIteratorPadCheck.MSG_WS_FOLLOWED;
24  import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyForIteratorPadCheck.MSG_WS_NOT_FOLLOWED;
25  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
26  
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
31  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
33  
34  public class EmptyForIteratorPadCheckTest
35      extends AbstractModuleTestSupport {
36  
37      @Override
38      public String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/checks/whitespace/emptyforiteratorpad";
40      }
41  
42      @Test
43      public void testGetRequiredTokens() {
44          final EmptyForIteratorPadCheck checkObj = new EmptyForIteratorPadCheck();
45          final int[] expected = {TokenTypes.FOR_ITERATOR};
46          assertWithMessage("Default required tokens are invalid")
47              .that(checkObj.getRequiredTokens())
48              .isEqualTo(expected);
49      }
50  
51      @Test
52      public void testDefault() throws Exception {
53          final String[] expected = {
54              "30:32: " + getCheckMessage(MSG_WS_FOLLOWED, ";"),
55              "46:33: " + getCheckMessage(MSG_WS_FOLLOWED, ";"),
56              "58:12: " + getCheckMessage(MSG_WS_FOLLOWED, ";"),
57          };
58          verifyWithInlineConfigParser(
59                  getPath("InputEmptyForIteratorPad.java"), expected);
60      }
61  
62      @Test
63      public void testSpaceOption() throws Exception {
64          final String[] expected = {
65              "26:31: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
66          };
67          verifyWithInlineConfigParser(
68                  getPath("InputEmptyForIteratorPad1.java"), expected);
69      }
70  
71      @Test
72      public void testWithEmoji() throws Exception {
73          final String[] expected = {
74              "24:40: " + getCheckMessage(MSG_WS_FOLLOWED, ";"),
75          };
76          verifyWithInlineConfigParser(
77                  getPath("InputEmptyForIteratorPadWithEmoji.java"), expected);
78  
79      }
80  
81      @Test
82      public void testGetAcceptableTokens() {
83          final EmptyForIteratorPadCheck emptyForIteratorPadCheckObj = new EmptyForIteratorPadCheck();
84          final int[] actual = emptyForIteratorPadCheckObj.getAcceptableTokens();
85          final int[] expected = {
86              TokenTypes.FOR_ITERATOR,
87          };
88          assertWithMessage("Default acceptable tokens are invalid")
89              .that(actual)
90              .isEqualTo(expected);
91      }
92  
93      @Test
94      public void testInvalidOption() {
95          final CheckstyleException exc = getExpectedThrowable(CheckstyleException.class, () -> {
96              final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
97  
98              verifyWithInlineConfigParser(getPath("InputEmptyForIteratorPad2.java"), expected);
99          });
100         assertWithMessage("Invalid exception message")
101             .that(exc.getMessage())
102             .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
103                 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks."
104                 + "whitespace.EmptyForIteratorPadCheck");
105     }
106 
107     @Test
108     public void testTrimOptionProperty() throws Exception {
109         final String[] expected = {
110             "20:31: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
111         };
112         verifyWithInlineConfigParser(
113                 getPath("InputEmptyForIteratorPadToCheckTrimFunctionInOptionProperty.java"),
114                 expected);
115 
116     }
117 
118     @Test
119     public void testUppercaseOptionProperty() throws Exception {
120         final String[] expected = {
121             "20:31: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
122         };
123         verifyWithInlineConfigParser(
124                 getPath("InputEmptyForIteratorPadToCheckUppercaseFunctionInOptionProperty.java"),
125                 expected);
126 
127     }
128 }