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.NoWhitespaceBeforeCheck.MSG_KEY;
23  
24  import org.junit.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
27  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
28  
29  public class NoWhitespaceBeforeCheckTest
30      extends AbstractModuleTestSupport {
31      @Override
32      protected String getPackageLocation() {
33          return "com/puppycrawl/tools/checkstyle/checks/whitespace/nowhitespacebefore";
34      }
35  
36      @Test
37      public void testDefault() throws Exception {
38          final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceBeforeCheck.class);
39          final String[] expected = {
40              "30:14: " + getCheckMessage(MSG_KEY, "++"),
41              "30:21: " + getCheckMessage(MSG_KEY, "--"),
42              "176:18: " + getCheckMessage(MSG_KEY, ";"),
43              "178:23: " + getCheckMessage(MSG_KEY, ";"),
44              "185:18: " + getCheckMessage(MSG_KEY, ";"),
45              "187:27: " + getCheckMessage(MSG_KEY, ";"),
46              "195:26: " + getCheckMessage(MSG_KEY, ";"),
47              "211:15: " + getCheckMessage(MSG_KEY, ";"),
48              "266:1: " + getCheckMessage(MSG_KEY, ";"),
49              "270:15: " + getCheckMessage(MSG_KEY, ";"),
50              "284:1: " + getCheckMessage(MSG_KEY, ";"),
51              "287:61: " + getCheckMessage(MSG_KEY, "..."),
52          };
53          verify(checkConfig, getPath("InputNoWhitespaceBeforeDefault.java"), expected);
54      }
55  
56      @Test
57      public void testDot() throws Exception {
58          final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceBeforeCheck.class);
59          checkConfig.addAttribute("tokens", "DOT");
60          final String[] expected = {
61              "5:12: " + getCheckMessage(MSG_KEY, "."),
62              "6:4: " + getCheckMessage(MSG_KEY, "."),
63              "129:17: " + getCheckMessage(MSG_KEY, "."),
64              "135:12: " + getCheckMessage(MSG_KEY, "."),
65              "136:10: " + getCheckMessage(MSG_KEY, "."),
66              "264:1: " + getCheckMessage(MSG_KEY, "."),
67          };
68          verify(checkConfig, getPath("InputNoWhitespaceBeforeDot.java"), expected);
69      }
70  
71      @Test
72      public void testDotAllowLineBreaks() throws Exception {
73          final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceBeforeCheck.class);
74          checkConfig.addAttribute("tokens", "DOT");
75          checkConfig.addAttribute("allowLineBreaks", "yes");
76          final String[] expected = {
77              "5:12: " + getCheckMessage(MSG_KEY, "."),
78              "129:17: " + getCheckMessage(MSG_KEY, "."),
79              "136:10: " + getCheckMessage(MSG_KEY, "."),
80          };
81          verify(checkConfig, getPath("InputNoWhitespaceBeforeDotAllowLineBreaks.java"), expected);
82      }
83  
84      @Test
85      public void testMethodReference() throws Exception {
86          final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceBeforeCheck.class);
87          checkConfig.addAttribute("tokens", "METHOD_REF");
88          final String[] expected = {
89              "17:31: " + getCheckMessage(MSG_KEY, "::"),
90              "18:60: " + getCheckMessage(MSG_KEY, "::"),
91          };
92          verify(checkConfig, getPath("InputNoWhitespaceBeforeMethodRef.java"), expected);
93      }
94  
95      @Test
96      public void testDotAtTheStartOfTheLine() throws Exception {
97          final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceBeforeCheck.class);
98          checkConfig.addAttribute("tokens", "DOT");
99          final String[] expected = {
100             "2:1: " + getCheckMessage(MSG_KEY, "."),
101         };
102         verify(checkConfig, getPath("InputNoWhitespaceBeforeAtStartOfTheLine.java"), expected);
103     }
104 
105     @Test
106     public void testMethodRefAtTheStartOfTheLine() throws Exception {
107         final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceBeforeCheck.class);
108         checkConfig.addAttribute("tokens", "METHOD_REF");
109         checkConfig.addAttribute("allowLineBreaks", "yes");
110         final String[] expected = {
111             "14:2: " + getCheckMessage(MSG_KEY, "::"),
112         };
113         verify(checkConfig, getPath("InputNoWhitespaceBeforeAtStartOfTheLine.java"), expected);
114     }
115 
116     @Test
117     public void testEmptyForLoop() throws Exception {
118         final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceBeforeCheck.class);
119         checkConfig.addAttribute("tokens", "SEMI");
120         checkConfig.addAttribute("allowLineBreaks", "yes");
121         final String[] expected = {
122             "12:23: " + getCheckMessage(MSG_KEY, ";"),
123             "18:31: " + getCheckMessage(MSG_KEY, ";"),
124         };
125         verify(checkConfig, getPath("InputNoWhitespaceBeforeEmptyForLoop.java"), expected);
126     }
127 }