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.MissingNullCaseInSwitchCheck.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 MissingNullCaseInSwitchCheckTest extends
32          AbstractModuleTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/coding/missingnullcaseinswitch";
37      }
38  
39      @Test
40      public void testTokensNotNull() {
41          final MissingNullCaseInSwitchCheck check = new MissingNullCaseInSwitchCheck();
42          final int[] expected = {TokenTypes.LITERAL_SWITCH};
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 testMissingNullCaseInSwitchWithPattern() throws Exception {
57          final String[] expected = {
58              "12:9: " + getCheckMessage(MSG_KEY),
59              "31:9: " + getCheckMessage(MSG_KEY),
60              "51:20: " + getCheckMessage(MSG_KEY),
61              "68:20: " + getCheckMessage(MSG_KEY),
62              "88:17: " + getCheckMessage(MSG_KEY),
63          };
64          verifyWithInlineConfigParser(
65                  getNonCompilablePath("InputMissingNullCaseInSwitchWithPattern.java"),
66              expected);
67      }
68  
69      @Test
70      public void testMissingNullCaseInSwitchWithPattern2() throws Exception {
71          final String[] expected = {
72              "11:9: " + getCheckMessage(MSG_KEY),
73              "40:9: " + getCheckMessage(MSG_KEY),
74          };
75          verifyWithInlineConfigParser(
76                  getNonCompilablePath("InputMissingNullCaseInSwitchWithPattern2.java"),
77              expected);
78      }
79  
80      @Test
81      public void testMissingNullCaseInSwitchWithRecordPattern() throws Exception {
82          final String[] expected = {
83              "12:9: " + getCheckMessage(MSG_KEY),
84              "32:9: " + getCheckMessage(MSG_KEY),
85              "52:20: " + getCheckMessage(MSG_KEY),
86              "69:20: " + getCheckMessage(MSG_KEY),
87              "90:17: " + getCheckMessage(MSG_KEY),
88          };
89          verifyWithInlineConfigParser(
90                  getNonCompilablePath("InputMissingNullCaseInSwitchWithRecordPattern.java"),
91              expected);
92      }
93  
94      @Test
95      public void testMissingNullCaseInSwitchWithStringLiterals() throws Exception {
96          final String[] expected = {
97              "12:9: " + getCheckMessage(MSG_KEY),
98              "23:9: " + getCheckMessage(MSG_KEY),
99              "31:9: " + getCheckMessage(MSG_KEY),
100             "51:20: " + getCheckMessage(MSG_KEY),
101             "68:20: " + getCheckMessage(MSG_KEY),
102             "89:17: " + getCheckMessage(MSG_KEY),
103             "98:17: " + getCheckMessage(MSG_KEY),
104             "103:17: " + getCheckMessage(MSG_KEY),
105             "108:17: " + getCheckMessage(MSG_KEY),
106         };
107         verifyWithInlineConfigParser(
108                 getNonCompilablePath("InputMissingNullCaseInSwitchWithStringLiterals.java"),
109             expected);
110     }
111 
112     @Test
113     public void testMissingNullCaseInSwitchWithPrimitives() throws Exception {
114         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
115         verifyWithInlineConfigParser(
116                 getNonCompilablePath("InputMissingNullCaseInSwitchWithPrimitives.java"),
117             expected);
118     }
119 }
120