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.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 testSkipIfLastAndSharedWithCase() 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              "104:13: " + getCheckMessage(MSG_KEY),
49          };
50  
51          verifyWithInlineConfigParser(
52                  getPath("InputDefaultComesLastSkipIfLastAndSharedWithCase.java"),
53                  expected);
54      }
55  
56      @Test
57      public void testDefault() throws Exception {
58          final String[] expected = {
59              "31:9: " + getCheckMessage(MSG_KEY),
60              "38:24: " + getCheckMessage(MSG_KEY),
61              "43:13: " + getCheckMessage(MSG_KEY),
62              "51:13: " + getCheckMessage(MSG_KEY),
63              "59:13: " + getCheckMessage(MSG_KEY),
64              "67:21: " + getCheckMessage(MSG_KEY),
65              "71:13: " + getCheckMessage(MSG_KEY),
66              "75:21: " + getCheckMessage(MSG_KEY),
67              "80:13: " + getCheckMessage(MSG_KEY),
68              "91:13: " + getCheckMessage(MSG_KEY),
69              "102:13: " + getCheckMessage(MSG_KEY),
70              "112:13: " + getCheckMessage(MSG_KEY),
71              "120:13: " + getCheckMessage(MSG_KEY),
72              "131:13: " + getCheckMessage(MSG_KEY),
73          };
74          verifyWithInlineConfigParser(
75                  getPath("InputDefaultComesLast.java"),
76                 expected);
77      }
78  
79      @Test
80      public void testDefaultMethodsInJava8()
81              throws Exception {
82          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
83          verifyWithInlineConfigParser(
84                  getPath("InputDefaultComesLastDefaultMethodsInInterface.java"),
85                  expected);
86      }
87  
88      @Test
89      public void testDefaultComesLastSwitchExpressions() throws Exception {
90          final String[] expected = {
91              "16:13: " + getCheckMessage(MSG_KEY),
92              "32:13: " + getCheckMessage(MSG_KEY),
93              "46:13: " + getCheckMessage(MSG_KEY),
94          };
95          verifyWithInlineConfigParser(
96                  getNonCompilablePath("InputDefaultComesLastSwitchExpressions.java"),
97              expected);
98      }
99  
100     @Test
101     public void testDefaultComesLastSwitchExpressionsSkipIfLast() throws Exception {
102 
103         final String[] expected = {
104             "33:13: " + getCheckMessage(MSG_KEY),
105             "48:13: " + getCheckMessage(MSG_KEY),
106         };
107         verifyWithInlineConfigParser(
108                 getNonCompilablePath("InputDefaultComesLastSwitchExpressionsSkipIfLast.java"),
109             expected);
110     }
111 
112     @Test
113     public void testTokensNotNull() {
114         final DefaultComesLastCheck check = new DefaultComesLastCheck();
115         assertWithMessage("Acceptable tokens should not be null")
116                 .that(check.getAcceptableTokens())
117                 .isNotNull();
118         assertWithMessage("Default tokens should not be null")
119                 .that(check.getDefaultTokens())
120                 .isNotNull();
121         assertWithMessage("Required tokens should not be null")
122                 .that(check.getRequiredTokens())
123                 .isNotNull();
124     }
125 
126     @Test
127     public void testDefaultMethodsInJava8Interface2()
128             throws Exception {
129         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
130         verifyWithInlineConfigParser(
131                 getPath("InputDefaultComesLastDefaultMethodsInInterface2.java"),
132                 expected);
133     }
134 
135 }