View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.IllegalTokenTextCheck.MSG_KEY;
23  
24  import java.util.Arrays;
25  import java.util.List;
26  import java.util.regex.Pattern;
27  
28  import org.junit.Assert;
29  import org.junit.Test;
30  
31  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
33  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
34  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
35  import com.puppycrawl.tools.checkstyle.utils.TokenUtils;
36  
37  public class IllegalTokenTextCheckTest
38      extends AbstractModuleTestSupport {
39      @Override
40      protected String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/checks/coding/illegaltokentext";
42      }
43  
44      @Test
45      public void testCaseSensitive()
46              throws Exception {
47          final DefaultConfiguration checkConfig =
48              createModuleConfig(IllegalTokenTextCheck.class);
49          checkConfig.addAttribute("tokens", "STRING_LITERAL");
50          checkConfig.addAttribute("format", "a href");
51          checkConfig.addAttribute("ignoreCase", "false");
52          final String[] expected = {
53              "24:28: " + getCheckMessage(MSG_KEY, "a href"),
54          };
55          verify(checkConfig, getPath("InputIllegalTokenTextTokens.java"), expected);
56      }
57  
58      @Test
59      public void testCaseInSensitive()
60              throws Exception {
61          final DefaultConfiguration checkConfig =
62              createModuleConfig(IllegalTokenTextCheck.class);
63          checkConfig.addAttribute("tokens", "STRING_LITERAL");
64          checkConfig.addAttribute("format", "a href");
65          checkConfig.addAttribute("ignoreCase", "true");
66          final String[] expected = {
67              "24:28: " + getCheckMessage(MSG_KEY, "a href"),
68              "25:32: " + getCheckMessage(MSG_KEY, "a href"),
69          };
70          verify(checkConfig, getPath("InputIllegalTokenTextTokens.java"), expected);
71      }
72  
73      @Test
74      public void testCustomMessage()
75              throws Exception {
76          final DefaultConfiguration checkConfig =
77              createModuleConfig(IllegalTokenTextCheck.class);
78          checkConfig.addAttribute("tokens", "STRING_LITERAL");
79          checkConfig.addAttribute("format", "a href");
80  
81          checkConfig.addAttribute("message", "My custom message");
82          final String[] expected = {
83              "24:28: " + "My custom message",
84          };
85          verify(checkConfig, getPath("InputIllegalTokenTextTokens.java"), expected);
86      }
87  
88      @Test
89      public void testNullCustomMessage()
90              throws Exception {
91          final DefaultConfiguration checkConfig =
92              createModuleConfig(IllegalTokenTextCheck.class);
93          checkConfig.addAttribute("tokens", "STRING_LITERAL");
94          checkConfig.addAttribute("format", "a href");
95  
96          checkConfig.addAttribute("message", null);
97          final String[] expected = {
98              "24:28: " + getCheckMessage(MSG_KEY, "a href"),
99          };
100         verify(checkConfig, getPath("InputIllegalTokenTextTokens.java"), expected);
101     }
102 
103     @Test
104     public void testTokensNotNull() {
105         final IllegalTokenTextCheck check = new IllegalTokenTextCheck();
106         Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
107         Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
108         Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
109         Assert.assertTrue("Comments are also TokenType token", check.isCommentNodesRequired());
110     }
111 
112     @Test
113     public void testCommentToken()
114             throws Exception {
115         final DefaultConfiguration checkConfig =
116                 createModuleConfig(IllegalTokenTextCheck.class);
117         checkConfig.addAttribute("tokens", "COMMENT_CONTENT");
118         checkConfig.addAttribute("format", "a href");
119 
120         checkConfig.addAttribute("message", null);
121         final String[] expected = {
122             "35:28: " + getCheckMessage(MSG_KEY, "a href"),
123         };
124         verify(checkConfig, getPath("InputIllegalTokenTextTokens.java"), expected);
125     }
126 
127     @Test
128     public void testOrderOfProperties() throws Exception {
129         // pure class must be used as configuration doesn't guarantee order of
130         // attributes
131         final IllegalTokenTextCheck check = new IllegalTokenTextCheck();
132         check.setFormat("test");
133         check.setIgnoreCase(true);
134         final Pattern actual = (Pattern) TestUtil.getClassDeclaredField(
135                 IllegalTokenTextCheck.class, "regexp").get(check);
136         Assert.assertEquals("should match", Pattern.CASE_INSENSITIVE, actual.flags());
137         Assert.assertEquals("should match", "test", actual.pattern());
138     }
139 
140     @Test
141     public void testAcceptableTokensMakeSense() {
142         final int expectedTokenTypesTotalNumber = 169;
143         Assert.assertEquals("Total number of TokenTypes has changed, acceptable tokens in"
144                 + " IllegalTokenTextCheck need to be reconsidered.",
145             expectedTokenTypesTotalNumber, TokenUtils.getTokenTypesTotalNumber());
146 
147         final IllegalTokenTextCheck check = new IllegalTokenTextCheck();
148         final int[] allowedTokens = check.getAcceptableTokens();
149         final List<Integer> tokenTypesWithMutableText = Arrays.asList(
150             TokenTypes.NUM_DOUBLE,
151             TokenTypes.NUM_FLOAT,
152             TokenTypes.NUM_INT,
153             TokenTypes.NUM_LONG,
154             TokenTypes.IDENT,
155             TokenTypes.COMMENT_CONTENT,
156             TokenTypes.STRING_LITERAL,
157             TokenTypes.CHAR_LITERAL
158         );
159         for (int tokenType : allowedTokens) {
160             Assert.assertTrue(TokenUtils.getTokenName(tokenType) + " should not be allowed"
161                 + " in this check as its text is a constant (IllegalTokenCheck should be used for"
162                 + " such cases).", tokenTypesWithMutableText.contains(tokenType));
163         }
164     }
165 }