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