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