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.coding;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.WhenShouldBeUsedCheck.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  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  public class WhenShouldBeUsedCheckTest
32          extends AbstractModuleTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/coding/whenshouldbeused";
37      }
38  
39      @Test
40      public void testTokensNotNull() {
41          final WhenShouldBeUsedCheck check = new WhenShouldBeUsedCheck();
42          final int[] expected = {TokenTypes.LITERAL_CASE};
43  
44          assertWithMessage("Acceptable tokens is not valid")
45              .that(check.getAcceptableTokens())
46              .isEqualTo(expected);
47          assertWithMessage("Default tokens is not valid")
48              .that(check.getDefaultTokens())
49              .isEqualTo(expected);
50          assertWithMessage("Required tokens is not valid")
51              .that(check.getRequiredTokens())
52              .isEqualTo(expected);
53      }
54  
55      @Test
56      public void testSwitchStatements() throws Exception {
57          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
58          verifyWithInlineConfigParser(
59                  getNonCompilablePath("InputWhenShouldBeUsedSwitchStatements.java"),
60              expected);
61      }
62  
63      @Test
64      public void testSwitchRule() throws Exception {
65          final String[] expected = {
66              "13:13: " + getCheckMessage(MSG_KEY),
67              "18:13: " + getCheckMessage(MSG_KEY),
68              "82:13: " + getCheckMessage(MSG_KEY),
69              "96:13: " + getCheckMessage(MSG_KEY),
70              "105:13: " + getCheckMessage(MSG_KEY),
71          };
72          verifyWithInlineConfigParser(
73                  getNonCompilablePath("InputWhenShouldBeUsedSwitchRule.java"),
74              expected);
75  
76      }
77  
78      @Test
79      public void testSwitchExpression() throws Exception {
80          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
81          verifyWithInlineConfigParser(
82                  getNonCompilablePath("InputWhenShouldBeUsedSwitchExpression.java"),
83                  expected);
84      }
85  
86      @Test
87      public void testNonPatternsSwitch() throws Exception {
88          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
89          verifyWithInlineConfigParser(
90                  getNonCompilablePath("InputWhenShouldBeUsedNonPatternsSwitch.java"),
91                  expected);
92      }
93  }