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.coding;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.coding.IllegalTokenCheck.MSG_KEY;
23  
24  import java.nio.charset.StandardCharsets;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.internal.utils.CheckUtil;
30  import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
31  
32  public class IllegalTokenCheckTest
33      extends AbstractModuleTestSupport {
34  
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/coding/illegaltoken";
38      }
39  
40      @Test
41      public void testIllegalTokensCheckDefaultTokenLabel()
42              throws Exception {
43          final String[] expected = {
44              "36:14: " + getCheckMessage(MSG_KEY, "label:"),
45              "38:25: " + getCheckMessage(MSG_KEY, "anotherLabel:"),
46          };
47          verifyWithInlineConfigParser(
48                  getPath("InputIllegalTokensCheckDefaultTokenLabel.java"), expected);
49      }
50  
51      @Test
52      public void testIllegalTokensCheckSwitchAndPostIncDec()
53              throws Exception {
54          final String[] expected = {
55              "18:9: " + getCheckMessage(MSG_KEY, "switch"),
56              "21:18: " + getCheckMessage(MSG_KEY, "--"),
57              "22:18: " + getCheckMessage(MSG_KEY, "++"),
58          };
59          verifyWithInlineConfigParser(
60                  getPath("InputIllegalTokensCheckSwitchAndPostIncDec.java"), expected);
61      }
62  
63      @Test
64      public void testIllegalTokensCheckTokenNative() throws Exception {
65          final String[] expected = {
66              "27:12: " + getCheckMessage(MSG_KEY, "native"),
67          };
68          verifyWithInlineConfigParser(
69                  getPath("InputIllegalTokensCheckTokenNative.java"), expected);
70      }
71  
72      @Test
73      public void testIllegalTokensCheckCommentsContent()
74              throws Exception {
75  
76          final String path = getPath("InputIllegalTokensCheckCommentsContent.java");
77          final String lineSeparator =
78                  CheckUtil.getLineSeparatorForFile(path, StandardCharsets.UTF_8);
79          final String[] expected = {
80              "1:3: " + getCheckMessage(MSG_KEY,
81                          JavadocUtil.escapeAllControlChars(
82                              " // violation\\n"
83                              + "IllegalToken\\n" + "tokens = COMMENT_CONTENT"
84                              + lineSeparator
85                              + lineSeparator
86                              + lineSeparator)),
87              "10:3: " + getCheckMessage(MSG_KEY,
88                          JavadocUtil.escapeAllControlChars(
89                              "* // violation" + lineSeparator
90                              + " * Test for illegal tokens"
91                              + lineSeparator + " ")),
92              "38:29: " + getCheckMessage(MSG_KEY,
93                          JavadocUtil.escapeAllControlChars(
94                              " some comment href // violation" + lineSeparator)),
95              "42:28: " + getCheckMessage(MSG_KEY,
96                          JavadocUtil.escapeAllControlChars(
97                              " some a href // violation" + lineSeparator)),
98          };
99          verifyWithInlineConfigParser(path, expected);
100     }
101 
102     @Test
103     public void testIllegalTokensCheckBlockCommentBegin()
104             throws Exception {
105 
106         final String[] expected = {
107             "1:1: " + getCheckMessage(MSG_KEY, "/*"),
108             "10:1: " + getCheckMessage(MSG_KEY, "/*"),
109         };
110         verifyWithInlineConfigParser(
111                 getPath("InputIllegalTokensCheckBlockCommentBegin.java"), expected);
112     }
113 
114     @Test
115     public void testIllegalTokensCheckBlockCommentEnd()
116             throws Exception {
117 
118         final String[] expected = {
119             "6:1: " + getCheckMessage(MSG_KEY, "*/"),
120             "12:2: " + getCheckMessage(MSG_KEY, "*/"),
121         };
122         verifyWithInlineConfigParser(
123                 getPath("InputIllegalTokensCheckBlockCommentEnd.java"), expected);
124     }
125 
126     @Test
127     public void testIllegalTokensCheckSingleLineComment()
128             throws Exception {
129 
130         final String[] expected = {
131             "38:27: " + getCheckMessage(MSG_KEY, "//"),
132             "42:26: " + getCheckMessage(MSG_KEY, "//"),
133         };
134         verifyWithInlineConfigParser(
135                 getPath("InputIllegalTokensCheckSingleLineComment.java"), expected);
136     }
137 
138 }