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.naming;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.naming.AbbreviationAsWordInNameCheck.MSG_KEY;
23  
24  import org.junit.jupiter.api.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
27  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
28  
29  public class AbbreviationAsWordInNameExamplesTest extends AbstractExamplesModuleTestSupport {
30  
31      private static final int DEFAULT_EXPECTED_CAPITAL_COUNT = 4;
32  
33      @Override
34      public String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/naming/abbreviationaswordinname";
36      }
37  
38      @Test
39      public void testExample1() throws Exception {
40          final String[] expected = {
41              "18:7: " + getCheckMessage(MSG_KEY, "CURRENT_COUNTER", DEFAULT_EXPECTED_CAPITAL_COUNT),
42              "44:8: " + getCheckMessage(MSG_KEY, "incrementCOUNTER", DEFAULT_EXPECTED_CAPITAL_COUNT),
43              "46:15: " + getCheckMessage(MSG_KEY, "incrementGLOBAL", DEFAULT_EXPECTED_CAPITAL_COUNT),
44          };
45  
46          verifyWithInlineXmlConfig(getPath("Example1.java"), expected);
47      }
48  
49      @Test
50      public void testExample2() throws Exception {
51          final String[] expected = {
52              "21:7: " + getCheckMessage(MSG_KEY, "CURRENT_COUNTER", DEFAULT_EXPECTED_CAPITAL_COUNT),
53              "23:14: " + getCheckMessage(MSG_KEY, "GLOBAL_COUNTER", DEFAULT_EXPECTED_CAPITAL_COUNT),
54              "45:15: " + getCheckMessage(MSG_KEY, "printCOUNTER", DEFAULT_EXPECTED_CAPITAL_COUNT),
55              "47:8: " + getCheckMessage(MSG_KEY, "incrementCOUNTER", DEFAULT_EXPECTED_CAPITAL_COUNT),
56              "49:15: " + getCheckMessage(MSG_KEY, "incrementGLOBAL", DEFAULT_EXPECTED_CAPITAL_COUNT),
57          };
58  
59          verifyWithInlineXmlConfig(getPath("Example2.java"), expected);
60      }
61  
62      @Test
63      public void testExample3() throws Exception {
64          final int expectedCapitalCount = 1;
65  
66          final String[] expected = {
67              "23:7: " + getCheckMessage(MSG_KEY, "CURRENT_COUNTER", expectedCapitalCount),
68              "25:14: " + getCheckMessage(MSG_KEY, "GLOBAL_COUNTER", expectedCapitalCount),
69              "29:7: " + getCheckMessage(MSG_KEY, "secondNUM", expectedCapitalCount),
70              "31:14: " + getCheckMessage(MSG_KEY, "fourthNUm", expectedCapitalCount),
71              "36:7: " + getCheckMessage(MSG_KEY, "nextXYZ", expectedCapitalCount),
72              "39:14: " + getCheckMessage(MSG_KEY, "nextID", expectedCapitalCount),
73          };
74  
75          verifyWithInlineXmlConfig(getPath("Example3.java"), expected);
76      }
77  
78      @Test
79      public void testExample4() throws Exception {
80          final int expectedCapitalCount = 4;
81  
82          final String[] expected = {
83              "20:7: " + getCheckMessage(MSG_KEY, "CURRENT_COUNTER", expectedCapitalCount),
84              "24:21: " + getCheckMessage(MSG_KEY, "stringsFOUND", expectedCapitalCount),
85              "31:13: " + getCheckMessage(MSG_KEY, "TOTAL", expectedCapitalCount),
86              "46:8: " + getCheckMessage(MSG_KEY, "incrementCOUNTER", expectedCapitalCount),
87              "48:15: " + getCheckMessage(MSG_KEY, "incrementGLOBAL", expectedCapitalCount),
88          };
89  
90          verifyWithInlineXmlConfig(getPath("Example4.java"), expected);
91      }
92  
93      @Test
94      public void testExample6() throws Exception {
95          final int expectedCapitalCount = 3;
96  
97          final String[] expected = {
98              "20:7: " + getCheckMessage(MSG_KEY, "CURRENT_COUNTER", expectedCapitalCount),
99              "46:8: " + getCheckMessage(MSG_KEY, "incrementCOUNTER", expectedCapitalCount),
100             "48:15: " + getCheckMessage(MSG_KEY, "incrementGLOBAL", expectedCapitalCount),
101         };
102 
103         verifyWithInlineXmlConfig(getPath("Example6.java"), expected);
104     }
105 
106     @Test
107     public void testExample7() throws Exception {
108         final int expectedCapitalCount = 4;
109 
110         final String[] expected = {
111             "46:8: " + getCheckMessage(MSG_KEY, "incrementCOUNTER", expectedCapitalCount),
112             "48:15: " + getCheckMessage(MSG_KEY, "incrementGLOBAL", expectedCapitalCount),
113         };
114 
115         verifyWithInlineXmlConfig(getPath("Example7.java"), expected);
116     }
117 
118     @Test
119     public void testExample8() throws Exception {
120         final int expectedCapitalCount = 2;
121 
122         final String[] expected = {
123             "22:7: " + getCheckMessage(MSG_KEY, "CURRENT_COUNTER", expectedCapitalCount),
124             "28:7: " + getCheckMessage(MSG_KEY, "secondNUM", expectedCapitalCount),
125             "31:10: " + getCheckMessage(MSG_KEY, "firstXML", expectedCapitalCount),
126             "32:10: " + getCheckMessage(MSG_KEY, "firstURL", expectedCapitalCount),
127             "35:7: " + getCheckMessage(MSG_KEY, "nextXYZ", expectedCapitalCount),
128             "43:8: " + getCheckMessage(MSG_KEY, "OAUth2", expectedCapitalCount),
129         };
130 
131         verifyWithInlineXmlConfig(getPath("Example8.java"), expected);
132     }
133 
134     @Test
135     public void testUseCase1() throws Exception {
136         final int expectedCapitalCount = 2;
137 
138         final String[] expected = {
139             "22:7: " + getCheckMessage(MSG_KEY, "secondMYNum", expectedCapitalCount),
140             "23:7: " + getCheckMessage(MSG_KEY, "thirdNUM", expectedCapitalCount),
141             "26:10: " + getCheckMessage(MSG_KEY, "firstXML", expectedCapitalCount),
142         };
143 
144         verifyWithInlineXmlConfig(getPath("UseCase1.java"), expected);
145     }
146 
147     @Test
148     public void testExample5() throws Exception {
149         final int expectedCapitalCount = 1;
150 
151         final String[] expected = {
152             "24:7: " + getCheckMessage(MSG_KEY, "CURRENT_COUNTER", expectedCapitalCount),
153             "26:14: " + getCheckMessage(MSG_KEY, "GLOBAL_COUNTER", expectedCapitalCount),
154             "28:21: " + getCheckMessage(MSG_KEY, "stringsFOUND", expectedCapitalCount),
155             "30:7: " + getCheckMessage(MSG_KEY, "secondNUM", expectedCapitalCount),
156             "32:14: " + getCheckMessage(MSG_KEY, "fourthNUm", expectedCapitalCount),
157             "33:10: " + getCheckMessage(MSG_KEY, "firstXML", expectedCapitalCount),
158             "34:10: " + getCheckMessage(MSG_KEY, "firstURL", expectedCapitalCount),
159             "35:13: " + getCheckMessage(MSG_KEY, "TOTAL", expectedCapitalCount),
160             "37:7: " + getCheckMessage(MSG_KEY, "nextXYZ", expectedCapitalCount),
161             "38:13: " + getCheckMessage(MSG_KEY, "countID", expectedCapitalCount),
162             "40:14: " + getCheckMessage(MSG_KEY, "nextID", expectedCapitalCount),
163         };
164 
165         verifyWithInlineXmlConfig(getPath("Example5.java"), expected);
166     }
167 
168     @Test
169     public void testUseCase2() throws Exception {
170         final int expectedCapitalCount = 1;
171 
172         final String[] expected = {
173             "21:7: " + getCheckMessage(MSG_KEY, "counterXYZ", expectedCapitalCount),
174             "23:13: " + getCheckMessage(MSG_KEY, "customerID", expectedCapitalCount),
175             "26:20: " + getCheckMessage(MSG_KEY, "MAX_ALLOWED", expectedCapitalCount),
176         };
177 
178         verifyWithInlineXmlConfig(getPath("UseCase2.java"), expected);
179     }
180 
181     @Test
182     public void testUseCase3() throws Exception {
183         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
184 
185         verifyWithInlineXmlConfig(getPath("UseCase3.java"), expected);
186     }
187 
188 }