View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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 org.junit.jupiter.api.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
27  
28  public class IllegalTokenCheckTest
29      extends AbstractModuleTestSupport {
30  
31      @Override
32      protected String getPackageLocation() {
33          return "com/puppycrawl/tools/checkstyle/checks/coding/illegaltoken";
34      }
35  
36      @Test
37      public void testIllegalTokensCheckDefaultTokenLabel() throws Exception {
38          final String[] expected = {
39              "36:14: " + getCheckMessage(MSG_KEY, "label:"),
40              "38:25: " + getCheckMessage(MSG_KEY, "anotherLabel:"),
41          };
42          verifyWithInlineConfigParser(
43                  getPath("InputIllegalTokensCheckDefaultTokenLabel.java"), expected);
44      }
45  
46      @Test
47      public void testIllegalTokensCheckSwitchAndPostIncDec() throws Exception {
48          final String[] expected = {
49              "18:9: " + getCheckMessage(MSG_KEY, "switch"),
50              "21:18: " + getCheckMessage(MSG_KEY, "--"),
51              "22:18: " + getCheckMessage(MSG_KEY, "++"),
52          };
53          verifyWithInlineConfigParser(
54                  getPath("InputIllegalTokensCheckSwitchAndPostIncDec.java"), expected);
55      }
56  
57      @Test
58      public void testIllegalTokensCheckTokenNative() throws Exception {
59          final String[] expected = {
60              "27:12: " + getCheckMessage(MSG_KEY, "native"),
61          };
62          verifyWithInlineConfigParser(
63                  getPath("InputIllegalTokensCheckTokenNative.java"), expected);
64      }
65  
66      @Test
67      public void testIllegalTokensCheckCommentsContent() throws Exception {
68  
69          final String path = getPath("InputIllegalTokensCheckCommentsContent.java");
70          final String[] expected = {
71              "1:3: " + getCheckMessage(MSG_KEY,
72                          "\\nIllegalToken\\ntokens = COMMENT_CONTENT\\n\\n\\n"),
73              "10:3: " + getCheckMessage(MSG_KEY,
74                          "*\\n * // violation 10"
75                              + " lines above 'is not allowed'\\n"
76                              + " * // violation 2 lines above 'is not allowed'\\n"
77                              + " * Test for illegal tokens\\n "),
78              "40:29: " + getCheckMessage(MSG_KEY,
79                          " some comment href // violation, 'is not allowed'\\n"),
80              "44:28: " + getCheckMessage(MSG_KEY,
81                          " some a href // violation, 'is not allowed'\\n"),
82          };
83          verifyWithInlineConfigParser(path, expected);
84      }
85  
86      @Test
87      public void testIllegalTokensCheckBlockCommentBegin() throws Exception {
88  
89          final String[] expected = {
90              "1:1: " + getCheckMessage(MSG_KEY, "/*"),
91              "10:1: " + getCheckMessage(MSG_KEY, "/*"),
92          };
93          verifyWithInlineConfigParser(
94                  getPath("InputIllegalTokensCheckBlockCommentBegin.java"), expected);
95      }
96  
97      @Test
98      public void testIllegalTokensCheckBlockCommentEnd() throws Exception {
99  
100         final String[] expected = {
101             "6:1: " + getCheckMessage(MSG_KEY, "*/"),
102             "12:2: " + getCheckMessage(MSG_KEY, "*/"),
103         };
104         verifyWithInlineConfigParser(
105                 getPath("InputIllegalTokensCheckBlockCommentEnd.java"), expected);
106     }
107 
108     @Test
109     public void testIllegalTokensCheckSingleLineComment() throws Exception {
110 
111         final String[] expected = {
112             "38:27: " + getCheckMessage(MSG_KEY, "//"),
113             "42:26: " + getCheckMessage(MSG_KEY, "//"),
114         };
115         verifyWithInlineConfigParser(
116                 getPath("InputIllegalTokensCheckSingleLineComment.java"), expected);
117     }
118 
119 }