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.whitespace;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.whitespace.SingleSpaceSeparatorCheck.MSG_KEY;
23  import static org.junit.Assert.assertArrayEquals;
24  
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
30  
31  public class SingleSpaceSeparatorCheckTest extends AbstractModuleTestSupport {
32      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/whitespace/singlespaceseparator";
35      }
36  
37      @Test
38      public void testNoSpaceErrors() throws Exception {
39          final DefaultConfiguration checkConfig =
40                  createModuleConfig(SingleSpaceSeparatorCheck.class);
41          verify(checkConfig, getPath("InputSingleSpaceSeparatorNoErrors.java"),
42                  CommonUtils.EMPTY_STRING_ARRAY);
43      }
44  
45      @Test
46      public void testGetAcceptableTokens() {
47          final SingleSpaceSeparatorCheck check = new SingleSpaceSeparatorCheck();
48  
49          assertArrayEquals("Invalid acceptable tockens",
50              CommonUtils.EMPTY_INT_ARRAY, check.getAcceptableTokens());
51      }
52  
53      @Test
54      public void testSpaceErrors() throws Exception {
55          final DefaultConfiguration checkConfig =
56                  createModuleConfig(SingleSpaceSeparatorCheck.class);
57          checkConfig.addAttribute("validateComments", "true");
58          final String[] expected = {
59              "1:9: " + getCheckMessage(MSG_KEY),
60              "1:27: " + getCheckMessage(MSG_KEY),
61              "4:8: " + getCheckMessage(MSG_KEY),
62              "6:18: " + getCheckMessage(MSG_KEY),
63              "6:51: " + getCheckMessage(MSG_KEY),
64              "7:20: " + getCheckMessage(MSG_KEY),
65              "8:11: " + getCheckMessage(MSG_KEY),
66              "8:15: " + getCheckMessage(MSG_KEY),
67              "11:3: " + getCheckMessage(MSG_KEY),
68              "12:5: " + getCheckMessage(MSG_KEY),
69              "13:7: " + getCheckMessage(MSG_KEY),
70              "14:8: " + getCheckMessage(MSG_KEY),
71              "17:13: " + getCheckMessage(MSG_KEY),
72              "17:23: " + getCheckMessage(MSG_KEY),
73              "17:32: " + getCheckMessage(MSG_KEY),
74              "18:15: " + getCheckMessage(MSG_KEY),
75              "18:22: " + getCheckMessage(MSG_KEY),
76              "19:16: " + getCheckMessage(MSG_KEY),
77              "19:23: " + getCheckMessage(MSG_KEY),
78              "20:19: " + getCheckMessage(MSG_KEY),
79              "21:21: " + getCheckMessage(MSG_KEY),
80              "26:21: " + getCheckMessage(MSG_KEY),
81              "26:27: " + getCheckMessage(MSG_KEY),
82              "27:14: " + getCheckMessage(MSG_KEY),
83              "27:23: " + getCheckMessage(MSG_KEY),
84              "27:31: " + getCheckMessage(MSG_KEY),
85              "27:46: " + getCheckMessage(MSG_KEY),
86              "28:14: " + getCheckMessage(MSG_KEY),
87              "28:22: " + getCheckMessage(MSG_KEY),
88              "30:16: " + getCheckMessage(MSG_KEY),
89              "30:33: " + getCheckMessage(MSG_KEY),
90              "31:7: " + getCheckMessage(MSG_KEY),
91          };
92  
93          verify(checkConfig, getPath("InputSingleSpaceSeparatorErrors.java"), expected);
94      }
95  
96      @Test
97      public void testSpaceErrorsAroundComments() throws Exception {
98          final DefaultConfiguration checkConfig =
99                  createModuleConfig(SingleSpaceSeparatorCheck.class);
100         checkConfig.addAttribute("validateComments", "true");
101         final String[] expected = {
102             "5:10: " + getCheckMessage(MSG_KEY),
103             "5:42: " + getCheckMessage(MSG_KEY),
104             "6:13: " + getCheckMessage(MSG_KEY),
105             "13:13: " + getCheckMessage(MSG_KEY),
106             "13:20: " + getCheckMessage(MSG_KEY),
107             "14:7: " + getCheckMessage(MSG_KEY),
108         };
109 
110         verify(checkConfig, getPath("InputSingleSpaceSeparatorComments.java"), expected);
111     }
112 
113     @Test
114     public void testSpaceErrorsInChildNodes() throws Exception {
115         final DefaultConfiguration checkConfig =
116             createModuleConfig(SingleSpaceSeparatorCheck.class);
117         final String[] expected = {
118             "5:15: " + getCheckMessage(MSG_KEY),
119         };
120 
121         verify(checkConfig, getPath("InputSingleSpaceSeparatorChildNodes.java"), expected);
122     }
123 
124     @Test
125     public void testMinColumnNo() throws Exception {
126         final DefaultConfiguration checkConfig =
127             createModuleConfig(SingleSpaceSeparatorCheck.class);
128         checkConfig.addAttribute("validateComments", "true");
129         final String[] expected = {
130             "5:3: " + getCheckMessage(MSG_KEY),
131         };
132 
133         verify(checkConfig, getPath("InputSingleSpaceSeparatorMinColumnNo.java"), expected);
134     }
135 
136     @Test
137     public void testWhitespaceInStartOfTheLine() throws Exception {
138         final DefaultConfiguration checkConfig =
139             createModuleConfig(SingleSpaceSeparatorCheck.class);
140         final String[] expected = {
141             "5:6: " + getCheckMessage(MSG_KEY),
142         };
143 
144         verify(checkConfig, getPath("InputSingleSpaceSeparatorStartOfTheLine.java"), expected);
145     }
146 
147     @Test
148     public void testSpaceErrorsIfCommentsIgnored() throws Exception {
149         final DefaultConfiguration checkConfig =
150                 createModuleConfig(SingleSpaceSeparatorCheck.class);
151         final String[] expected = {
152             "13:13: " + getCheckMessage(MSG_KEY),
153         };
154 
155         verify(checkConfig, getPath("InputSingleSpaceSeparatorComments.java"), expected);
156     }
157 }