View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
27  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
28  import com.puppycrawl.tools.checkstyle.utils.JavadocUtils;
29  
30  public class IllegalTokenCheckTest
31      extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/coding/illegaltoken";
36      }
37  
38      @Test
39      public void testCheckWithDefaultSettings()
40              throws Exception {
41          final DefaultConfiguration checkConfig =
42              createModuleConfig(IllegalTokenCheck.class);
43          final String[] expected = {
44              "29:14: " + getCheckMessage(MSG_KEY, "label:"),
45              "31:25: " + getCheckMessage(MSG_KEY, "anotherLabel:"),
46          };
47          verify(checkConfig, getPath("InputIllegalTokens.java"), expected);
48      }
49  
50      @Test
51      public void testPreviouslyIllegalTokens()
52              throws Exception {
53          final DefaultConfiguration checkConfig =
54              createModuleConfig(IllegalTokenCheck.class);
55          checkConfig.addAttribute("tokens", "LITERAL_SWITCH,POST_INC,POST_DEC");
56          final String[] expected = {
57              "11:9: " + getCheckMessage(MSG_KEY, "switch"),
58              "14:18: " + getCheckMessage(MSG_KEY, "--"),
59              "15:18: " + getCheckMessage(MSG_KEY, "++"),
60          };
61          verify(checkConfig, getPath("InputIllegalTokens.java"), expected);
62      }
63  
64      @Test
65      public void testNative() throws Exception {
66          final DefaultConfiguration checkConfig =
67              createModuleConfig(IllegalTokenCheck.class);
68          checkConfig.addAttribute("tokens", "LITERAL_NATIVE");
69          final String[] expected = {
70              "20:12: " + getCheckMessage(MSG_KEY, "native"),
71          };
72          verify(checkConfig, getPath("InputIllegalTokens.java"), expected);
73      }
74  
75      @Test
76      public void testCommentContentToken()
77              throws Exception {
78          final DefaultConfiguration checkConfig =
79                  createModuleConfig(IllegalTokenCheck.class);
80          checkConfig.addAttribute("tokens", "COMMENT_CONTENT");
81  
82          final String[] expected = {
83              "3:3: " + getCheckMessage(MSG_KEY,
84                          JavadocUtils.escapeAllControlChars(
85                              "*" + System.lineSeparator()
86                              + " * Test for illegal tokens"
87                              + System.lineSeparator() + " ")),
88              "31:29: " + getCheckMessage(MSG_KEY,
89                          JavadocUtils.escapeAllControlChars(
90                              " some comment href" + System.lineSeparator())),
91              "35:28: " + getCheckMessage(MSG_KEY,
92                          JavadocUtils.escapeAllControlChars(
93                              " some a href" + System.lineSeparator())),
94          };
95          verify(checkConfig, getPath("InputIllegalTokens.java"), expected);
96      }
97  
98      @Test
99      public void testBlockCommentBeginToken()
100             throws Exception {
101         final DefaultConfiguration checkConfig =
102                 createModuleConfig(IllegalTokenCheck.class);
103         checkConfig.addAttribute("tokens", "BLOCK_COMMENT_BEGIN");
104 
105         final String[] expected = {
106             "3:1: " + getCheckMessage(MSG_KEY, "/*"),
107         };
108         verify(checkConfig, getPath("InputIllegalTokens.java"), expected);
109     }
110 
111     @Test
112     public void testBlockCommentEndToken()
113             throws Exception {
114         final DefaultConfiguration checkConfig =
115                 createModuleConfig(IllegalTokenCheck.class);
116         checkConfig.addAttribute("tokens", "BLOCK_COMMENT_END");
117 
118         final String[] expected = {
119             "5:2: " + getCheckMessage(MSG_KEY, "*/"),
120         };
121         verify(checkConfig, getPath("InputIllegalTokens.java"), expected);
122     }
123 
124     @Test
125     public void testSingleLineCommentToken()
126             throws Exception {
127         final DefaultConfiguration checkConfig =
128                 createModuleConfig(IllegalTokenCheck.class);
129         checkConfig.addAttribute("tokens", "SINGLE_LINE_COMMENT");
130 
131         final String[] expected = {
132             "31:27: " + getCheckMessage(MSG_KEY, "//"),
133             "35:26: " + getCheckMessage(MSG_KEY, "//"),
134         };
135         verify(checkConfig, getPath("InputIllegalTokens.java"), expected);
136     }
137 
138 }