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.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.IllegalTokenTextCheck.MSG_KEY;
24  
25  import java.util.List;
26  import java.util.regex.Pattern;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
34  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
35  
36  public class IllegalTokenTextCheckTest
37      extends AbstractModuleTestSupport {
38  
39      @Override
40      protected String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/checks/coding/illegaltokentext";
42      }
43  
44      @Test
45      public void testIllegalTokenTextCheckDefaultCaseSensitive()
46              throws Exception {
47          final String[] expected = {
48              "34:28: " + getCheckMessage(MSG_KEY, "a href"),
49          };
50          verifyWithInlineConfigParser(
51                  getPath("InputIllegalTokenTextCheckDefaultCaseSensitive.java"), expected);
52      }
53  
54      @Test
55      public void testIllegalTokenTextCheckCaseInSensitive()
56              throws Exception {
57          final String[] expected = {
58              "34:28: " + getCheckMessage(MSG_KEY, "a href"),
59              "35:32: " + getCheckMessage(MSG_KEY, "a href"),
60          };
61          verifyWithInlineConfigParser(
62                  getPath("InputIllegalTokenTextCheckCaseInSensitive.java"), expected);
63      }
64  
65      @Test
66      public void testIllegalTokenTextCheckCustomMessageInStringLiteral()
67              throws Exception {
68  
69          final String[] expected = {
70              "34:28: " + "My custom message",
71          };
72          verifyWithInlineConfigParser(
73                  getPath("InputIllegalTokenTextCheckCustomMessageInStringLiteral.java"), expected);
74      }
75  
76      @Test
77      public void testIllegalTokenTextCheckNullMessageInStringLiteral()
78              throws Exception {
79  
80          final String[] expected = {
81              "34:28: " + getCheckMessage(MSG_KEY, "a href"),
82          };
83          verifyWithInlineConfigParser(
84                  getPath("InputIllegalTokenTextCheckNullMessageInStringLiteral.java"), expected);
85      }
86  
87      @Test
88      public void testIllegalTokenTextTextBlocks() throws Exception {
89  
90          final String[] expected = {
91              "16:28: " + getCheckMessage(MSG_KEY, "a href"),
92              "19:32: " + getCheckMessage(MSG_KEY, "a href"),
93              "31:37: " + getCheckMessage(MSG_KEY, "a href"),
94              "36:37: " + getCheckMessage(MSG_KEY, "a href"),
95              "42:54: " + getCheckMessage(MSG_KEY, "a href"),
96          };
97          verifyWithInlineConfigParser(
98                  getNonCompilablePath("InputIllegalTokenTextTextBlocks.java"), expected);
99      }
100 
101     @Test
102     public void testIllegalTokenTextTextBlocksQuotes() throws Exception {
103 
104         final String[] expected = {
105             "16:28: " + getCheckMessage(MSG_KEY, "\""),
106             "17:33: " + getCheckMessage(MSG_KEY, "\""),
107             "19:32: " + getCheckMessage(MSG_KEY, "\""),
108             "21:36: " + getCheckMessage(MSG_KEY, "\""),
109             "31:37: " + getCheckMessage(MSG_KEY, "\""),
110             "36:37: " + getCheckMessage(MSG_KEY, "\""),
111             "42:42: " + getCheckMessage(MSG_KEY, "\""),
112         };
113         verifyWithInlineConfigParser(
114                 getNonCompilablePath("InputIllegalTokenTextTextBlocksQuotes.java"), expected);
115     }
116 
117     @Test
118     public void testTokensNotNull() {
119         final IllegalTokenTextCheck check = new IllegalTokenTextCheck();
120         assertWithMessage("Acceptable tokens should not be null")
121             .that(check.getAcceptableTokens())
122             .isNotNull();
123         assertWithMessage("Default tokens should not be null")
124             .that(check.getDefaultTokens())
125             .isNotNull();
126         assertWithMessage("Required tokens should not be null")
127             .that(check.getRequiredTokens())
128             .isNotNull();
129         assertWithMessage("Comments are also TokenType token")
130                 .that(check.isCommentNodesRequired())
131                 .isTrue();
132     }
133 
134     @Test
135     public void testIllegalTokenTextCheckCommentToken()
136             throws Exception {
137 
138         final String[] expected = {
139             "1:3: " + getCheckMessage(MSG_KEY, "a href"),
140             "45:28: " + getCheckMessage(MSG_KEY, "a href"),
141         };
142         verifyWithInlineConfigParser(
143                 getPath("InputIllegalTokenTextCheckCommentToken.java"), expected);
144     }
145 
146     @Test
147     public void testOrderOfProperties() {
148         // pure class must be used as configuration doesn't guarantee order of
149         // attributes
150         final IllegalTokenTextCheck check = new IllegalTokenTextCheck();
151         check.setFormat("test");
152         check.setIgnoreCase(true);
153         final Pattern actual = TestUtil.getInternalState(check, "format");
154         assertWithMessage("should match")
155             .that(actual.flags())
156             .isEqualTo(Pattern.CASE_INSENSITIVE);
157         assertWithMessage("should match")
158             .that(actual.pattern())
159             .isEqualTo("test");
160     }
161 
162     @Test
163     public void testAcceptableTokensMakeSense() {
164         final int expectedTokenTypesTotalNumber = 189;
165         assertWithMessage("Total number of TokenTypes has changed, acceptable tokens in"
166                 + " IllegalTokenTextCheck need to be reconsidered.")
167             .that(TokenUtil.getTokenTypesTotalNumber())
168             .isEqualTo(expectedTokenTypesTotalNumber);
169 
170         final IllegalTokenTextCheck check = new IllegalTokenTextCheck();
171         final int[] allowedTokens = check.getAcceptableTokens();
172         final List<Integer> tokenTypesWithMutableText = List.of(
173             TokenTypes.NUM_DOUBLE,
174             TokenTypes.NUM_FLOAT,
175             TokenTypes.NUM_INT,
176             TokenTypes.NUM_LONG,
177             TokenTypes.IDENT,
178             TokenTypes.COMMENT_CONTENT,
179             TokenTypes.STRING_LITERAL,
180             TokenTypes.CHAR_LITERAL,
181             TokenTypes.TEXT_BLOCK_CONTENT
182         );
183         for (int tokenType : allowedTokens) {
184             assertWithMessage(TokenUtil.getTokenName(tokenType) + " should not be allowed"
185                     + " in this check as its text is a constant"
186                     + " (IllegalTokenCheck should be used for such cases).")
187                             .that(tokenTypesWithMutableText)
188                             .contains(tokenType);
189         }
190     }
191 
192     @Test
193     public void testDefaultFormat() throws Exception {
194 
195         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
196         verifyWithInlineConfigParser(
197                 getPath("InputIllegalTokenTextDefaultFormat.java"), expected);
198     }
199 }