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.EmptyForInitializerPadCheck.MSG_NOT_PRECEDED;
24  import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyForInitializerPadCheck.MSG_PRECEDED;
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.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
34  
35  public class EmptyForInitializerPadCheckTest
36      extends AbstractModuleTestSupport {
37  
38      @Override
39      public String getPackageLocation() {
40          return "com/puppycrawl/tools/checkstyle/checks/whitespace/emptyforinitializerpad";
41      }
42  
43      @Test
44      public void testGetRequiredTokens() {
45          final EmptyForInitializerPadCheck checkObj = new EmptyForInitializerPadCheck();
46          final int[] expected = {TokenTypes.FOR_INIT};
47          assertWithMessage("Default required tokens are invalid")
48              .that(checkObj.getRequiredTokens())
49              .isEqualTo(expected);
50      }
51  
52      @Test
53      public void testDefault() throws Exception {
54          final String[] expected = {
55              "51:15: " + getCheckMessage(MSG_PRECEDED, ";"),
56          };
57          verifyWithInlineConfigParser(
58                  getPath("InputEmptyForInitializerPadDefaultConfig.java"), expected);
59      }
60  
61      @Test
62      public void testSpaceOption() throws Exception {
63          final String[] expected = {
64              "54:14: " + getCheckMessage(MSG_NOT_PRECEDED, ";"),
65          };
66          verifyWithInlineConfigParser(
67                  getPath("InputEmptyForInitializerPad.java"), expected);
68      }
69  
70      @Test
71      public void testGetAcceptableTokens() {
72          final EmptyForInitializerPadCheck emptyForInitializerPadCheckObj =
73              new EmptyForInitializerPadCheck();
74          final int[] actual = emptyForInitializerPadCheckObj.getAcceptableTokens();
75          final int[] expected = {
76              TokenTypes.FOR_INIT,
77          };
78          assertWithMessage("Default acceptable tokens are invalid")
79              .that(actual)
80              .isEqualTo(expected);
81      }
82  
83      /* Additional test for jacoco, since valueOf()
84       * is generated by javac and jacoco reports that
85       * valueOf() is uncovered.
86       */
87      @Test
88      public void testPadOptionValueOf() {
89          final PadOption option = PadOption.valueOf("NOSPACE");
90          assertWithMessage("Result of valueOf is invalid")
91              .that(option)
92              .isEqualTo(PadOption.NOSPACE);
93      }
94  
95      /* Additional test for jacoco, since valueOf()
96       * is generated by javac and jacoco reports that
97       * valueOf() is uncovered.
98       */
99      @Test
100     public void testWrapOptionValueOf() {
101         final WrapOption option = WrapOption.valueOf("EOL");
102         assertWithMessage("Result of valueOf is invalid")
103             .that(option)
104             .isEqualTo(WrapOption.EOL);
105     }
106 
107     @Test
108     public void testWithEmoji() throws Exception {
109         final String[] expected = {
110             "23:13: " + getCheckMessage(MSG_NOT_PRECEDED, ";"),
111             "28:25: " + getCheckMessage(MSG_NOT_PRECEDED, ";"),
112         };
113         verifyWithInlineConfigParser(
114                 getPath("InputEmptyForInitializerPadWithEmoji.java"), expected);
115     }
116 
117     @Test
118     public void testInvalidOption() {
119         final DefaultConfiguration checkConfig =
120                 createModuleConfig(EmptyForInitializerPadCheck.class);
121         checkConfig.addProperty("option", "invalid_option");
122 
123         final CheckstyleException exc = getExpectedThrowable(CheckstyleException.class, () -> {
124             final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
125 
126             verifyWithInlineConfigParser(
127                     getPath("InputEmptyForInitializerPad2.java"), expected);
128         });
129         assertWithMessage("Invalid exception message")
130             .that(exc.getMessage())
131             .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
132                 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks."
133                 + "whitespace.EmptyForInitializerPadCheck");
134     }
135 
136     @Test
137     public void testTrimOptionProperty() throws Exception {
138         final String[] expected = {
139             "15:14: " + getCheckMessage(MSG_NOT_PRECEDED, ";"),
140         };
141         verifyWithInlineConfigParser(
142                 getPath("InputEmptyForInitializerPadSetOptionTrim.java"), expected);
143     }
144 }