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.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      public 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 first line 'is not allowed'\\n"
75                              + "// violation 2 lines above 'is not allowed'\\n"
76                              + "// violation 2 lines below 'is not allowed'\\n"),
77              "15:3: " + getCheckMessage(MSG_KEY,
78                          "*\\n * Test for illegal tokens\\n "),
79              "43:29: " + getCheckMessage(MSG_KEY,
80                          " some comment href // violation 'is not allowed'\\n"),
81              "47:28: " + getCheckMessage(MSG_KEY,
82                          " some a href // violation 'is not allowed'\\n"),
83          };
84          verifyWithInlineConfigParser(path, expected);
85      }
86  
87      @Test
88      public void testIllegalTokensCheckBlockCommentBegin() throws Exception {
89  
90          final String[] expected = {
91              "1:1: " + getCheckMessage(MSG_KEY, "/*"),
92              "11:1: " + getCheckMessage(MSG_KEY, "/*"),
93          };
94          verifyWithInlineConfigParser(
95                  getPath("InputIllegalTokensCheckBlockCommentBegin.java"), expected);
96      }
97  
98      @Test
99      public void testIllegalTokensCheckBlockCommentEnd() throws Exception {
100 
101         final String[] expected = {
102             "6:1: " + getCheckMessage(MSG_KEY, "*/"),
103             "12:2: " + getCheckMessage(MSG_KEY, "*/"),
104         };
105         verifyWithInlineConfigParser(
106                 getPath("InputIllegalTokensCheckBlockCommentEnd.java"), expected);
107     }
108 
109     @Test
110     public void testIllegalTokensCheckSingleLineComment() throws Exception {
111 
112         final String[] expected = {
113             "38:27: " + getCheckMessage(MSG_KEY, "//"),
114             "42:26: " + getCheckMessage(MSG_KEY, "//"),
115         };
116         verifyWithInlineConfigParser(
117                 getPath("InputIllegalTokensCheckSingleLineComment.java"), expected);
118     }
119 
120 }