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