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.EmptyLineSeparatorCheck.MSG_MULTIPLE_LINES;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck.MSG_MULTIPLE_LINES_AFTER;
24  import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck.MSG_MULTIPLE_LINES_INSIDE;
25  import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED;
26  import static org.junit.Assert.assertArrayEquals;
27  
28  import org.junit.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
34  
35  public class EmptyLineSeparatorCheckTest
36      extends AbstractModuleTestSupport {
37      @Override
38      protected String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/checks/whitespace/emptylineseparator";
40      }
41  
42      @Test
43      public void testGetRequiredTokens() {
44          final EmptyLineSeparatorCheck checkObj = new EmptyLineSeparatorCheck();
45          assertArrayEquals(
46              "EmptyLineSeparatorCheck#getRequiredTockens should return empty array by default",
47              CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
48      }
49  
50      @Test
51      public void testDefault() throws Exception {
52          final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
53  
54          final String[] expected = {
55              "21: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
56              "38: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
57              "41: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "VARIABLE_DEF"),
58              "42: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "STATIC_INIT"),
59              "46: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INSTANCE_INIT"),
60              "60: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
61              "65: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
62              "82: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
63              "113: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INTERFACE_DEF"),
64          };
65          verify(checkConfig, getPath("InputEmptyLineSeparator.java"), expected);
66      }
67  
68      @Test
69      public void testAllowNoEmptyLineBetweenFields() throws Exception {
70  
71          final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
72          checkConfig.addAttribute("allowNoEmptyLineBetweenFields", "true");
73  
74          final String[] expected = {
75              "21: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
76              "38: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CLASS_DEF"),
77              "42: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "STATIC_INIT"),
78              "46: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INSTANCE_INIT"),
79              "60: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "CTOR_DEF"),
80              "65: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
81              "82: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
82              "113: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "INTERFACE_DEF"),
83          };
84          verify(checkConfig, getPath("InputEmptyLineSeparator.java"), expected);
85      }
86  
87      @Test
88      public void testHeader() throws Exception {
89          final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
90          final String[] expected = {
91              "19: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
92          };
93          verify(checkConfig, getPath("InputEmptyLineSeparatorHeader.java"), expected);
94      }
95  
96      @Test
97      public void testMultipleEmptyLinesBetweenClassMembers() throws Exception {
98          final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
99          checkConfig.addAttribute("allowMultipleEmptyLines", "false");
100         final String[] expected = {
101             "21: " + getCheckMessage(MSG_MULTIPLE_LINES, "package"),
102             "24: " + getCheckMessage(MSG_MULTIPLE_LINES, "import"),
103             "29: " + getCheckMessage(MSG_MULTIPLE_LINES, "CLASS_DEF"),
104             "33: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
105             "38: " + getCheckMessage(MSG_MULTIPLE_LINES, "VARIABLE_DEF"),
106             "43: " + getCheckMessage(MSG_MULTIPLE_LINES, "METHOD_DEF"),
107             "45: " + getCheckMessage(MSG_MULTIPLE_LINES_AFTER, "METHOD_DEF"),
108         };
109         verify(checkConfig, getPath("InputEmptyLineSeparatorMultipleEmptyLines.java"), expected);
110     }
111 
112     @Test
113     public void testFormerArrayIndexOutOfBounds() throws Exception {
114         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
115         checkConfig.addAttribute("allowMultipleEmptyLines", "false");
116         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
117         verify(checkConfig, getPath("InputEmptyLineSeparatorFormerException.java"), expected);
118     }
119 
120     @Test
121     public void testAllowMultipleFieldInClass() throws Exception {
122         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
123         checkConfig.addAttribute("allowMultipleEmptyLines", "false");
124         checkConfig.addAttribute("allowNoEmptyLineBetweenFields", "true");
125         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
126         verify(checkConfig, getPath("InputEmptyLineSeparatorMultipleFieldsInClass.java"), expected);
127     }
128 
129     @Test
130     public void testAllowMultipleImportSeparatedFromPackage() throws Exception {
131         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
132         checkConfig.addAttribute("allowMultipleEmptyLines", "false");
133         final String[] expected = {
134             "1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
135         };
136         verify(checkConfig, getPath("InputEmptyLineSeparatorMultipleImportEmptyClass.java"),
137             expected);
138     }
139 
140     @Test
141     public void testGetAcceptableTokens() {
142         final EmptyLineSeparatorCheck emptyLineSeparatorCheckObj = new EmptyLineSeparatorCheck();
143         final int[] actual = emptyLineSeparatorCheckObj.getAcceptableTokens();
144         final int[] expected = {
145             TokenTypes.PACKAGE_DEF,
146             TokenTypes.IMPORT,
147             TokenTypes.CLASS_DEF,
148             TokenTypes.INTERFACE_DEF,
149             TokenTypes.ENUM_DEF,
150             TokenTypes.STATIC_INIT,
151             TokenTypes.INSTANCE_INIT,
152             TokenTypes.METHOD_DEF,
153             TokenTypes.CTOR_DEF,
154             TokenTypes.VARIABLE_DEF,
155         };
156         assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
157     }
158 
159     @Test
160     public void testPrePreviousLineEmptiness() throws Exception {
161         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
162         checkConfig.addAttribute("allowMultipleEmptyLines", "false");
163         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
164         verify(checkConfig,
165             getPath("InputEmptyLineSeparatorPrePreviousLineEmptiness.java"), expected);
166     }
167 
168     @Test
169     public void testPrePreviousLineIsEmpty() throws Exception {
170         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
171         checkConfig.addAttribute("allowMultipleEmptyLines", "false");
172         final String[] expected = {
173             "3: " + getCheckMessage(MSG_MULTIPLE_LINES, "package"),
174         };
175         verify(checkConfig,
176                 getPath("InputEmptyLineSeparatorPrePreviousLineIsEmpty.java"), expected);
177     }
178 
179     @Test
180     public void testPreviousLineEmptiness() throws Exception {
181         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
182         checkConfig.addAttribute("allowMultipleEmptyLinesInsideClassMembers", "false");
183         final String[] expected = {
184             "11: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
185             "16: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
186             "22: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
187             "31: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
188         };
189         verify(checkConfig,
190                 getPath("InputEmptyLineSeparatorPreviousLineEmptiness.java"), expected);
191     }
192 
193     @Test
194     public void testDisAllowMultipleEmptyLinesInsideClassMembers() throws Exception {
195         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
196         checkConfig.addAttribute("allowMultipleEmptyLinesInsideClassMembers", "false");
197         final String[] expected = {
198             "27: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
199             "39: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
200             "45: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
201             "50: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
202             "55: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
203             "56: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
204         };
205         verify(checkConfig,
206                 getPath("InputEmptyLineSeparatorMultipleEmptyLinesInside.java"),
207                 expected);
208     }
209 
210     @Test
211     public void testAllowMultipleEmptyLinesInsideClassMembers() throws Exception {
212         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
213         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
214         verify(checkConfig,
215                 getPath("InputEmptyLineSeparatorMultipleEmptyLinesInside.java"),
216                 expected);
217     }
218 
219     @Test
220     public void testAllowPackageAnnotation() throws Exception {
221         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
222         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
223         verify(checkConfig,
224                 getPath("package-info/test1/package-info.java"),
225                 expected);
226     }
227 
228     @Test
229     public void testAllowJavadocBeforePackage() throws Exception {
230         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
231         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
232         verify(checkConfig,
233                 getPath("package-info/test2/package-info.java"),
234                 expected);
235     }
236 
237     @Test
238     public void testDisAllowBlockCommentBeforePackage() throws Exception {
239         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
240         final String[] expected = {
241             "3: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
242         };
243         verify(checkConfig,
244                 getPath("package-info/test3/package-info.java"),
245                 expected);
246     }
247 
248     @Test
249     public void testAllowSingleLineCommentPackage() throws Exception {
250         final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
251         final String[] expected = {
252             "3: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "package"),
253         };
254         verify(checkConfig,
255                 getPath("package-info/test4/package-info.java"),
256                 expected);
257     }
258 }