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.MissingSwitchDefaultCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
29  
30  public class MissingSwitchDefaultCheckTest
31      extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/coding/missingswitchdefault";
36      }
37  
38      @Test
39      public void testMissingSwitchDefault() throws Exception {
40          final String[] expected = {
41              "23:9: " + getCheckMessage(MSG_KEY, "default"),
42              "35:17: " + getCheckMessage(MSG_KEY, "default"),
43              "46:17: " + getCheckMessage(MSG_KEY, "default"),
44              "53:9: " + getCheckMessage(MSG_KEY, "default"),
45          };
46          verifyWithInlineConfigParser(
47                  getPath("InputMissingSwitchDefault.java"),
48              expected);
49      }
50  
51      @Test
52      public void testTokensNotNull() {
53          final MissingSwitchDefaultCheck check = new MissingSwitchDefaultCheck();
54          assertWithMessage("Acceptable tokens should not be null")
55              .that(check.getAcceptableTokens())
56              .isNotNull();
57          assertWithMessage("Default tokens should not be null")
58              .that(check.getDefaultTokens())
59              .isNotNull();
60          assertWithMessage("Required tokens should not be null")
61              .that(check.getRequiredTokens())
62              .isNotNull();
63      }
64  
65      @Test
66      public void testMissingSwitchDefaultSwitchExpressions() throws Exception {
67          final String[] expected = {
68              "14:9: " + getCheckMessage(MSG_KEY, "default"),
69          };
70          verifyWithInlineConfigParser(
71                  getNonCompilablePath("InputMissingSwitchDefaultCheckSwitchExpressions.java"),
72              expected);
73      }
74  
75      @Test
76      public void testNullCaseLabel() throws Exception {
77          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
78          verifyWithInlineConfigParser(
79                  getNonCompilablePath("InputMissingSwitchDefaultCheckNullCaseLabel.java"),
80              expected);
81      }
82  
83      @Test
84      public void testMissingSwitchDefaultSwitchExpressionsTwo() throws Exception {
85          final String[] expected = {
86              "14:9: " + getCheckMessage(MSG_KEY, "default"),
87              "26:9: " + getCheckMessage(MSG_KEY, "default"),
88          };
89          verifyWithInlineConfigParser(
90                  getNonCompilablePath("InputMissingSwitchDefaultCheckSwitchExpressionsTwo.java"),
91                  expected);
92      }
93  
94      @Test
95      public void testMissingSwitchDefaultSwitchExpressionsThree() throws Exception {
96          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
97          verifyWithInlineConfigParser(
98                  getNonCompilablePath("InputMissingSwitchDefaultCheckSwitchExpressionsThree.java"),
99                  expected);
100     }
101 
102     @Test
103     public void testMissingSwitchDefaultCaseLabelElements() throws Exception {
104         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
105         verifyWithInlineConfigParser(
106                 getNonCompilablePath("InputMissingSwitchDefaultCaseLabelElements.java"),
107                 expected);
108     }
109 
110     @Test
111     public void testMissingSwitchDefaultRecordPattern() throws Exception {
112         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
113         verifyWithInlineConfigParser(
114                 getNonCompilablePath("InputMissingSwitchDefaultRecordPattern.java"),
115                 expected);
116     }
117 
118     @Test
119     public void testMissingSwitchDefaultWithSwitchExpressionUnderMethodCall() throws Exception {
120         final String[] expected = {
121             "19:9: " + getCheckMessage(MSG_KEY),
122         };
123         verifyWithInlineConfigParser(
124                 getNonCompilablePath(
125                         "InputMissingSwitchDefaultCheckSwitchExpressionUnderMethodCall.java"),
126                 expected);
127     }
128 
129 }