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      private static final int DEFAULT_EXPECTED_CAPITAL_COUNT = 4;
31  
32      @Override
33      public String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/naming/abbreviationaswordinname";
35      }
36  
37      @Test
38      public void testExample1() throws Exception {
39          final String[] expected = {
40              "18:7: " + getCheckMessage(MSG_KEY, "CURRENT_COUNTER", DEFAULT_EXPECTED_CAPITAL_COUNT),
41              "44:8: " + getCheckMessage(MSG_KEY, "incrementCOUNTER", DEFAULT_EXPECTED_CAPITAL_COUNT),
42              "46:15: " + getCheckMessage(MSG_KEY, "incrementGLOBAL", DEFAULT_EXPECTED_CAPITAL_COUNT),
43          };
44  
45          verifyWithInlineXmlConfig(getPath("Example1.java"), expected);
46      }
47  
48      @Test
49      public void testExample2() throws Exception {
50          final String[] expected = {
51              "21:7: " + getCheckMessage(MSG_KEY, "CURRENT_COUNTER", DEFAULT_EXPECTED_CAPITAL_COUNT),
52              "23:14: " + getCheckMessage(MSG_KEY, "GLOBAL_COUNTER", DEFAULT_EXPECTED_CAPITAL_COUNT),
53              "45:15: " + getCheckMessage(MSG_KEY, "printCOUNTER", DEFAULT_EXPECTED_CAPITAL_COUNT),
54              "47:8: " + getCheckMessage(MSG_KEY, "incrementCOUNTER", DEFAULT_EXPECTED_CAPITAL_COUNT),
55              "49:15: " + getCheckMessage(MSG_KEY, "incrementGLOBAL", DEFAULT_EXPECTED_CAPITAL_COUNT),
56          };
57  
58          verifyWithInlineXmlConfig(getPath("Example2.java"), expected);
59      }
60  
61      @Test
62      public void testExample3() throws Exception {
63          final int expectedCapitalCount = 1;
64  
65          final String[] expected = {
66              "23:7: " + getCheckMessage(MSG_KEY, "CURRENT_COUNTER", expectedCapitalCount),
67              "25:14: " + getCheckMessage(MSG_KEY, "GLOBAL_COUNTER", expectedCapitalCount),
68              "29:7: " + getCheckMessage(MSG_KEY, "secondNUM", expectedCapitalCount),
69              "31:14: " + getCheckMessage(MSG_KEY, "fourthNUm", expectedCapitalCount),
70              "36:7: " + getCheckMessage(MSG_KEY, "nextXYZ", expectedCapitalCount),
71              "39:14: " + getCheckMessage(MSG_KEY, "nextID", expectedCapitalCount),
72          };
73  
74          verifyWithInlineXmlConfig(getPath("Example3.java"), expected);
75      }
76  
77      @Test
78      public void testExample4() throws Exception {
79          final int expectedCapitalCount = 2;
80  
81          final String[] expected = {
82              "22:7: " + getCheckMessage(MSG_KEY, "secondMYNum", expectedCapitalCount),
83              "23:7: " + getCheckMessage(MSG_KEY, "thirdNUM", expectedCapitalCount),
84              "26:10: " + getCheckMessage(MSG_KEY, "firstXML", expectedCapitalCount),
85          };
86  
87          verifyWithInlineXmlConfig(getPath("Example4.java"), expected);
88      }
89  
90      @Test
91      public void testExample5() throws Exception {
92          final int expectedCapitalCount = 1;
93  
94          final String[] expected = {
95              "24:7: " + getCheckMessage(MSG_KEY, "CURRENT_COUNTER", expectedCapitalCount),
96              "26:14: " + getCheckMessage(MSG_KEY, "GLOBAL_COUNTER", expectedCapitalCount),
97              "28:21: " + getCheckMessage(MSG_KEY, "stringsFOUND", expectedCapitalCount),
98              "30:7: " + getCheckMessage(MSG_KEY, "secondNUM", expectedCapitalCount),
99              "32:14: " + getCheckMessage(MSG_KEY, "fourthNUm", expectedCapitalCount),
100             "33:10: " + getCheckMessage(MSG_KEY, "firstXML", expectedCapitalCount),
101             "34:10: " + getCheckMessage(MSG_KEY, "firstURL", expectedCapitalCount),
102             "35:13: " + getCheckMessage(MSG_KEY, "TOTAL", expectedCapitalCount),
103             "37:7: " + getCheckMessage(MSG_KEY, "nextXYZ", expectedCapitalCount),
104             "38:13: " + getCheckMessage(MSG_KEY, "countID", expectedCapitalCount),
105             "40:14: " + getCheckMessage(MSG_KEY, "nextID", expectedCapitalCount),
106         };
107 
108         verifyWithInlineXmlConfig(getPath("Example5.java"), expected);
109     }
110 
111     @Test
112     public void testExample6() throws Exception {
113         final int expectedCapitalCount = 1;
114 
115         final String[] expected = {
116             "21:7: " + getCheckMessage(MSG_KEY, "counterXYZ", expectedCapitalCount),
117             "23:13: " + getCheckMessage(MSG_KEY, "customerID", expectedCapitalCount),
118             "26:20: " + getCheckMessage(MSG_KEY, "MAX_ALLOWED", expectedCapitalCount),
119         };
120 
121         verifyWithInlineXmlConfig(getPath("Example6.java"), expected);
122     }
123 
124     @Test
125     public void testExample7() throws Exception {
126         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
127 
128         verifyWithInlineXmlConfig(getPath("Example7.java"), expected);
129     }
130 }