View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.OverloadMethodsDeclarationOrderCheck.MSG_KEY;
24  import static com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheck.MSG_ORDER;
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 OverloadMethodsDeclarationOrderCheckTest
32      extends AbstractModuleTestSupport {
33  
34      @Override
35      public String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/coding/overloadmethodsdeclarationorder";
37      }
38  
39      @Test
40      public void testDefaultPart1() throws Exception {
41          final String[] expected = {
42              "33:5: " + getCheckMessage(MSG_KEY, 21),
43              "62:9: " + getCheckMessage(MSG_KEY, 50),
44              "67:9: " + getCheckMessage(MSG_KEY, 62),
45              "87:5: " + getCheckMessage(MSG_KEY, 83),
46          };
47          verifyWithInlineConfigParser(
48              getPath("InputOverloadMethodsDeclarationOrder1.java"), expected);
49      }
50  
51      @Test
52      public void testDefaultPart2() throws Exception {
53          final String[] expected = {
54              "53:9: " + getCheckMessage(MSG_KEY, 41),
55              "65:9: " + getCheckMessage(MSG_KEY, 58),
56              "77:13: " + getCheckMessage(MSG_KEY, 70),
57              "80:9: " + getCheckMessage(MSG_KEY, 65),
58          };
59          verifyWithInlineConfigParser(
60              getPath("InputOverloadMethodsDeclarationOrder2.java"), expected);
61      }
62  
63      @Test
64      public void testOverloadMethodsDeclarationOrderRecords() throws Exception {
65  
66          final String[] expected = {
67              "22:9: " + getCheckMessage(MSG_KEY, 15),
68              "43:9: " + getCheckMessage(MSG_KEY, 36),
69              "59:9: " + getCheckMessage(MSG_KEY, 52),
70              "66:9: " + getCheckMessage(MSG_KEY, 59),
71          };
72          verifyWithInlineConfigParser(
73                  getPath("InputOverloadMethodsDeclarationOrderRecords.java"),
74              expected);
75      }
76  
77      @Test
78      public void testOverloadMethodsDeclarationOrderPrivateAndStaticMethods() throws Exception {
79  
80          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
81  
82          verifyWithInlineConfigParser(
83                  getPath(
84                          "InputOverloadMethodsDeclarationOrderPrivateAndStaticMethods.java"
85                  ), expected);
86      }
87  
88      @Test
89      public void testOrderByIncreasingArity() throws Exception {
90          final String[] expected = {
91              "21:5: " + getCheckMessage(MSG_ORDER),
92              "24:5: " + getCheckMessage(MSG_ORDER),
93              "32:9: " + getCheckMessage(MSG_ORDER),
94              "35:9: " + getCheckMessage(MSG_ORDER),
95              "48:9: " + getCheckMessage(MSG_ORDER),
96          };
97          verifyWithInlineConfigParser(
98              getPath("InputOverloadMethodsDeclarationOrderArity.java"), expected);
99      }
100 
101     @Test
102     public void testMisc() throws Exception {
103 
104         final String[] expected = {
105             "17:5: " + getCheckMessage(MSG_KEY, 11),
106             "19:5: " + getCheckMessage(MSG_ORDER),
107             "33:9: " + getCheckMessage(MSG_KEY, 26),
108             "33:9: " + getCheckMessage(MSG_ORDER),
109             "36:9: " + getCheckMessage(MSG_ORDER),
110         };
111 
112         verifyWithInlineConfigParser(
113                 getPath(
114                         "InputOverloadMethodsDeclarationOrderMisc.java"
115                 ), expected);
116     }
117 
118     @Test
119     public void testCompactSourceFileOverloads() throws Exception {
120         final String[] expected = {
121             "13:1: " + getCheckMessage(MSG_KEY, 8),
122         };
123         verifyWithInlineConfigParser(
124             getNonCompilablePath(
125                 "InputOverloadMethodsDeclarationOrderCompactSourceFile.java"),
126             expected);
127     }
128 
129     @Test
130     public void testCompactSourceFileValid() throws Exception {
131         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
132         verifyWithInlineConfigParser(
133             getNonCompilablePath(
134                 "InputOverloadMethodsDeclarationOrderCompactSourceFileValid.java"),
135             expected);
136     }
137 
138     @Test
139     public void testCompactSourceFileFirstMethodOverload() throws Exception {
140         final String[] expected = {
141             "11:1: " + getCheckMessage(MSG_KEY, 8),
142         };
143         verifyWithInlineConfigParser(
144             getNonCompilablePath(
145                 "InputOverloadMethodsDeclarationOrderCompactSourceFileFirstMethodOverload.java"),
146             expected);
147     }
148 
149     @Test
150     public void testTokensNotNull() {
151         final OverloadMethodsDeclarationOrderCheck check =
152             new OverloadMethodsDeclarationOrderCheck();
153         assertWithMessage("Acceptable tokens should not be null")
154             .that(check.getAcceptableTokens())
155             .isNotNull();
156         assertWithMessage("Default tokens should not be null")
157             .that(check.getDefaultTokens())
158             .isNotNull();
159         assertWithMessage("Required tokens should not be null")
160             .that(check.getRequiredTokens())
161             .isNotNull();
162     }
163 
164 }