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.SeparatorWrapCheck.MSG_LINE_NEW;
24  import static com.puppycrawl.tools.checkstyle.checks.whitespace.SeparatorWrapCheck.MSG_LINE_PREVIOUS;
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 SeparatorWrapCheckTest
34          extends AbstractModuleTestSupport {
35  
36      @Override
37      protected String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/whitespace/separatorwrap";
39      }
40  
41      @Test
42      public void testDot()
43              throws Exception {
44          final String[] expected = {
45              "39:10: " + getCheckMessage(MSG_LINE_NEW, "."),
46          };
47          verifyWithInlineConfigParser(
48                  getPath("InputSeparatorWrapForTestDot.java"), expected);
49      }
50  
51      @Test
52      public void testComma() throws Exception {
53          final String[] expected = {
54              "47:17: " + getCheckMessage(MSG_LINE_PREVIOUS, ","),
55          };
56          verifyWithInlineConfigParser(
57                  getPath("InputSeparatorWrapForTestComma.java"), expected);
58      }
59  
60      @Test
61      public void testMethodRef() throws Exception {
62          final String[] expected = {
63              "25:56: " + getCheckMessage(MSG_LINE_NEW, "::"),
64          };
65          verifyWithInlineConfigParser(
66                  getPath("InputSeparatorWrapForTestMethodRef.java"), expected);
67      }
68  
69      @Test
70      public void testGetDefaultTokens() {
71          final SeparatorWrapCheck separatorWrapCheckObj = new SeparatorWrapCheck();
72          final int[] actual = separatorWrapCheckObj.getDefaultTokens();
73          final int[] expected = {
74              TokenTypes.DOT,
75              TokenTypes.COMMA,
76          };
77          assertWithMessage("Invalid default tokens")
78              .that(actual)
79              .isEqualTo(expected);
80      }
81  
82      @Test
83      public void testInvalidOption() throws Exception {
84  
85          try {
86              final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
87  
88              verifyWithInlineConfigParser(
89                      getPath("InputSeparatorWrapForInvalidOption.java"), expected);
90              assertWithMessage("exception expected").fail();
91          }
92          catch (CheckstyleException ex) {
93              assertWithMessage("Invalid exception message")
94                  .that(ex.getMessage())
95                  .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
96                      + "cannot initialize module com.puppycrawl.tools.checkstyle.checks."
97                      + "whitespace.SeparatorWrapCheck - "
98                      + "Cannot set property 'option' to 'invalid_option'");
99          }
100     }
101 
102     @Test
103     public void testEllipsis() throws Exception {
104         final String[] expected = {
105             "19:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "..."),
106         };
107         verifyWithInlineConfigParser(
108                 getPath("InputSeparatorWrapForEllipsis.java"), expected);
109     }
110 
111     @Test
112     public void testArrayDeclarator() throws Exception {
113         final String[] expected = {
114             "17:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "["),
115         };
116         verifyWithInlineConfigParser(
117                 getPath("InputSeparatorWrapForArrayDeclarator.java"), expected);
118     }
119 
120     @Test
121     public void testWithEmoji() throws Exception {
122         final String[] expected = {
123             "13:39: " + getCheckMessage(MSG_LINE_NEW, '['),
124             "16:57: " + getCheckMessage(MSG_LINE_NEW, '['),
125             "19:39: " + getCheckMessage(MSG_LINE_NEW, "..."),
126             "26:19: " + getCheckMessage(MSG_LINE_NEW, '.'),
127             "39:50: " + getCheckMessage(MSG_LINE_NEW, ','),
128             "41:50: " + getCheckMessage(MSG_LINE_NEW, "::"),
129         };
130         verifyWithInlineConfigParser(
131             getPath("InputSeparatorWrapWithEmoji.java"), expected);
132     }
133 
134     @Test
135     public void testTrimOptionProperty() throws Exception {
136         final String[] expected = {
137             "18:44: " + getCheckMessage(MSG_LINE_NEW, "::"),
138         };
139         verifyWithInlineConfigParser(
140                 getPath("InputSeparatorWrapSetOptionTrim.java"), expected);
141     }
142 
143     @Test
144     public void testCommaOnNewLine() throws Exception {
145         final String[] expected = {
146             "16:10: " + getCheckMessage(MSG_LINE_NEW, ","),
147             "21:26: " + getCheckMessage(MSG_LINE_NEW, ","),
148         };
149         verifyWithInlineConfigParser(
150                 getPath("InputSeparatorWrapForTestTrailingWhitespace.java"), expected);
151     }
152 }