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