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