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.coding;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  
24  import org.junit.jupiter.api.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
27  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
28  
29  public class UseEnhancedSwitchCheckTest
30          extends AbstractModuleTestSupport {
31  
32      @Override
33      public String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/coding/useenhancedswitch";
35      }
36  
37      @Test
38      public void testTokensNotNull() {
39          final UseEnhancedSwitchCheck check = new UseEnhancedSwitchCheck();
40          final int[] expected = {TokenTypes.LITERAL_SWITCH};
41  
42          assertWithMessage("Acceptable tokens is not valid")
43              .that(check.getAcceptableTokens())
44              .isEqualTo(expected);
45          assertWithMessage("Default tokens is not valid")
46              .that(check.getDefaultTokens())
47              .isEqualTo(expected);
48          assertWithMessage("Required tokens is not valid")
49              .that(check.getRequiredTokens())
50              .isEqualTo(expected);
51      }
52  
53      @Test
54      public void testSwitchStatements() throws Exception {
55          final String[] expected = {
56              "11:9: " + getCheckMessage(UseEnhancedSwitchCheck.MSG_KEY),
57              "29:9: " + getCheckMessage(UseEnhancedSwitchCheck.MSG_KEY),
58              "52:9: " + getCheckMessage(UseEnhancedSwitchCheck.MSG_KEY),
59          };
60          verifyWithInlineConfigParser(
61              getPath("InputUseEnhancedSwitchSwitchStatements.java"),
62              expected);
63      }
64  
65      @Test
66      public void testSwitchStatementsValidFallThrough() throws Exception {
67          final String[] expected = {
68              "11:9: " + getCheckMessage(UseEnhancedSwitchCheck.MSG_KEY),
69              "27:9: " + getCheckMessage(UseEnhancedSwitchCheck.MSG_KEY),
70          };
71          verifyWithInlineConfigParser(
72              getPath("InputUseEnhancedSwitchSwitchStatementsValidFallThrough.java"),
73              expected);
74      }
75  
76      @Test
77      public void testSwitchExpressions() throws Exception {
78          final String[] expected = {
79              "15:17: " + getCheckMessage(UseEnhancedSwitchCheck.MSG_KEY),
80              "33:17: " + getCheckMessage(UseEnhancedSwitchCheck.MSG_KEY),
81          };
82          verifyWithInlineConfigParser(
83              getPath("InputUseEnhancedSwitchSwitchExpressions.java"),
84              expected);
85      }
86  
87      @Test
88      public void testSwitchExpressionsValidFallThrough() throws Exception {
89          final String[] expected = {
90              "11:20: " + getCheckMessage(UseEnhancedSwitchCheck.MSG_KEY),
91              "22:21: " + getCheckMessage(UseEnhancedSwitchCheck.MSG_KEY),
92          };
93          verifyWithInlineConfigParser(
94              getPath("InputUseEnhancedSwitchSwitchExpressionsValidFallThrough.java"),
95              expected);
96      }
97  }