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