View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCaseDefaultColonCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  
30  public class NoWhitespaceBeforeCaseDefaultColonCheckTest
31      extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/whitespace"
36                  + "/nowhitespacebeforecasedefaultcolon";
37      }
38  
39      @Test
40      public void testDefault() throws Exception {
41          createModuleConfig(
42                  NoWhitespaceBeforeCaseDefaultColonCheck.class);
43          final String[] expected = {
44              "15:20: " + getCheckMessage(MSG_KEY, ":"),
45              "19:21: " + getCheckMessage(MSG_KEY, ":"),
46              "32:37: " + getCheckMessage(MSG_KEY, ":"),
47              "40:21: " + getCheckMessage(MSG_KEY, ":"),
48              "44:28: " + getCheckMessage(MSG_KEY, ":"),
49              "47:1: " + getCheckMessage(MSG_KEY, ":"),
50              "50:27: " + getCheckMessage(MSG_KEY, ":"),
51              "61:38: " + getCheckMessage(MSG_KEY, ":"),
52              "66:38: " + getCheckMessage(MSG_KEY, ":"),
53              "79:24: " + getCheckMessage(MSG_KEY, ":"),
54              "82:21: " + getCheckMessage(MSG_KEY, ":"),
55              "89:38: " + getCheckMessage(MSG_KEY, ":"),
56              "92:35: " + getCheckMessage(MSG_KEY, ":"),
57          };
58          verifyWithInlineConfigParser(
59                  getPath("InputNoWhitespaceBeforeCaseDefaultColon.java"),
60                  expected);
61      }
62  
63      @Test
64      public void testDefaultNonCompilable() throws Exception {
65          createModuleConfig(
66                  NoWhitespaceBeforeCaseDefaultColonCheck.class);
67          final String[] expected = {
68              "36:22: " + getCheckMessage(MSG_KEY, ":"),
69              "39:21: " + getCheckMessage(MSG_KEY, ":"),
70              "42:25: " + getCheckMessage(MSG_KEY, ":"),
71              "61:20: " + getCheckMessage(MSG_KEY, ":"),
72              "74:32: " + getCheckMessage(MSG_KEY, ":"),
73              "91:17: " + getCheckMessage(MSG_KEY, ":"),
74              "94:20: " + getCheckMessage(MSG_KEY, ":"),
75              "97:21: " + getCheckMessage(MSG_KEY, ":"),
76          };
77          verifyWithInlineConfigParser(
78                  getNonCompilablePath("InputNoWhitespaceBeforeCaseDefaultColonEnumAndStrings.java"),
79                  expected);
80      }
81  
82      @Test
83      public void testAcceptableTokenIsColon() {
84          final NoWhitespaceBeforeCaseDefaultColonCheck check =
85                  new NoWhitespaceBeforeCaseDefaultColonCheck();
86          assertWithMessage("Acceptable token should be colon")
87                  .that(new int[] {TokenTypes.COLON})
88                  .isEqualTo(check.getAcceptableTokens());
89      }
90  
91      @Test
92      public void testPatternMatchingForSwitch() throws Exception {
93          final String[] expected = {
94              "14:62: " + getCheckMessage(MSG_KEY, ":"),
95              "16:21: " + getCheckMessage(MSG_KEY, ":"),
96              "18:21: " + getCheckMessage(MSG_KEY, ":"),
97              "20:67: " + getCheckMessage(MSG_KEY, ":"),
98              "23:36: " + getCheckMessage(MSG_KEY, ":"),
99              "25:21: " + getCheckMessage(MSG_KEY, ":"),
100         };
101         verifyWithInlineConfigParser(
102                 getNonCompilablePath(
103                         "InputNoWhitespaceBeforeCaseDefaultColonPatternMatchingForSwitch.java"),
104                 expected);
105     }
106 
107 }