View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.DefaultComesLastCheck.MSG_KEY;
24  import static com.puppycrawl.tools.checkstyle.checks.coding.DefaultComesLastCheck.MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  public class DefaultComesLastCheckTest extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/coding/defaultcomeslast";
36      }
37  
38      @Test
39      public void testSkipIfLastAndSharedWithCaseOne() throws Exception {
40          final String[] expected = {
41              "23:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
42              "31:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
43              "39:21: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
44              "43:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
45              "63:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
46              "83:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
47              "95:13: " + getCheckMessage(MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
48          };
49  
50          verifyWithInlineConfigParser(
51                  getPath("InputDefaultComesLastSkipIfLastAndSharedWithCaseOne.java"),
52                  expected);
53      }
54  
55      @Test
56      public void testSkipIfLastAndSharedWithCaseTwo() throws Exception {
57          final String[] expected = {
58              "16:13: " + getCheckMessage(MSG_KEY),
59          };
60  
61          verifyWithInlineConfigParser(
62                  getPath("InputDefaultComesLastSkipIfLastAndSharedWithCaseTwo.java"),
63                  expected);
64      }
65  
66      @Test
67      public void testDefaultOne() throws Exception {
68          final String[] expected = {
69              "31:9: " + getCheckMessage(MSG_KEY),
70              "38:24: " + getCheckMessage(MSG_KEY),
71              "43:13: " + getCheckMessage(MSG_KEY),
72              "51:13: " + getCheckMessage(MSG_KEY),
73              "59:13: " + getCheckMessage(MSG_KEY),
74              "67:21: " + getCheckMessage(MSG_KEY),
75              "71:13: " + getCheckMessage(MSG_KEY),
76              "75:21: " + getCheckMessage(MSG_KEY),
77              "80:13: " + getCheckMessage(MSG_KEY),
78              "91:13: " + getCheckMessage(MSG_KEY),
79              "102:13: " + getCheckMessage(MSG_KEY),
80              "112:13: " + getCheckMessage(MSG_KEY),
81          };
82          verifyWithInlineConfigParser(
83                  getPath("InputDefaultComesLastOne.java"),
84                 expected);
85      }
86  
87      @Test
88      public void testDefaultTwo() throws Exception {
89          final String[] expected = {
90              "15:13: " + getCheckMessage(MSG_KEY),
91              "26:13: " + getCheckMessage(MSG_KEY),
92          };
93          verifyWithInlineConfigParser(
94                  getPath("InputDefaultComesLastTwo.java"),
95                 expected);
96      }
97  
98      @Test
99      public void testDefaultMethodsInJava8()
100             throws Exception {
101         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
102         verifyWithInlineConfigParser(
103                 getPath("InputDefaultComesLastDefaultMethodsInInterface.java"),
104                 expected);
105     }
106 
107     @Test
108     public void testDefaultComesLastSwitchExpressions() throws Exception {
109         final String[] expected = {
110             "16:13: " + getCheckMessage(MSG_KEY),
111             "32:13: " + getCheckMessage(MSG_KEY),
112             "46:13: " + getCheckMessage(MSG_KEY),
113         };
114         verifyWithInlineConfigParser(
115                 getNonCompilablePath("InputDefaultComesLastSwitchExpressions.java"),
116             expected);
117     }
118 
119     @Test
120     public void testDefaultComesLastSwitchExpressionsSkipIfLast() throws Exception {
121 
122         final String[] expected = {
123             "33:13: " + getCheckMessage(MSG_KEY),
124             "48:13: " + getCheckMessage(MSG_KEY),
125         };
126         verifyWithInlineConfigParser(
127                 getNonCompilablePath("InputDefaultComesLastSwitchExpressionsSkipIfLast.java"),
128             expected);
129     }
130 
131     @Test
132     public void testTokensNotNull() {
133         final DefaultComesLastCheck check = new DefaultComesLastCheck();
134         assertWithMessage("Acceptable tokens should not be null")
135                 .that(check.getAcceptableTokens())
136                 .isNotNull();
137         assertWithMessage("Default tokens should not be null")
138                 .that(check.getDefaultTokens())
139                 .isNotNull();
140         assertWithMessage("Required tokens should not be null")
141                 .that(check.getRequiredTokens())
142                 .isNotNull();
143     }
144 
145     @Test
146     public void testDefaultMethodsInJava8Interface2()
147             throws Exception {
148         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
149         verifyWithInlineConfigParser(
150                 getPath("InputDefaultComesLastDefaultMethodsInInterface2.java"),
151                 expected);
152     }
153 
154 }