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.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      protected 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              "27:8: " + getCheckMessage(MSG_KEY, "incrementCOUNTER", DEFAULT_EXPECTED_CAPITAL_COUNT),
42              "31: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              "27:15: " + getCheckMessage(MSG_KEY, "printCOUNTER", DEFAULT_EXPECTED_CAPITAL_COUNT),
54              "31:8: " + getCheckMessage(MSG_KEY, "incrementCOUNTER", DEFAULT_EXPECTED_CAPITAL_COUNT),
55              "35: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              "21:7: " + getCheckMessage(MSG_KEY, "secondNUM", expectedCapitalCount),
67              "23:14: " + getCheckMessage(MSG_KEY, "fourthNUm", expectedCapitalCount),
68          };
69  
70          verifyWithInlineXmlConfig(getPath("Example3.java"), expected);
71      }
72  
73      @Test
74      public void testExample4() throws Exception {
75          final int expectedCapitalCount = 2;
76  
77          final String[] expected = {
78              "22:7: " + getCheckMessage(MSG_KEY, "secondMYNum", expectedCapitalCount),
79              "23:7: " + getCheckMessage(MSG_KEY, "thirdNUM", expectedCapitalCount),
80              "26:10: " + getCheckMessage(MSG_KEY, "firstXML", expectedCapitalCount),
81          };
82  
83          verifyWithInlineXmlConfig(getPath("Example4.java"), expected);
84      }
85  
86      @Test
87      public void testExample5() throws Exception {
88          final int expectedCapitalCount = 1;
89  
90          final String[] expected = {
91              "21:7: " + getCheckMessage(MSG_KEY, "counterXYZ", expectedCapitalCount),
92              "23:13: " + getCheckMessage(MSG_KEY, "customerID", expectedCapitalCount),
93              "24:14: " + getCheckMessage(MSG_KEY, "nextID", expectedCapitalCount),
94          };
95  
96          verifyWithInlineXmlConfig(getPath("Example5.java"), expected);
97      }
98  
99      @Test
100     public void testExample6() throws Exception {
101         final int expectedCapitalCount = 1;
102 
103         final String[] expected = {
104             "21:7: " + getCheckMessage(MSG_KEY, "counterXYZ", expectedCapitalCount),
105             "23:13: " + getCheckMessage(MSG_KEY, "customerID", expectedCapitalCount),
106             "26:20: " + getCheckMessage(MSG_KEY, "MAX_ALLOWED", expectedCapitalCount),
107         };
108 
109         verifyWithInlineXmlConfig(getPath("Example6.java"), expected);
110     }
111 
112     @Test
113     public void testExample7() throws Exception {
114         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
115 
116         verifyWithInlineXmlConfig(getPath("Example7.java"), expected);
117     }
118 }