View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.puppycrawl.tools.checkstyle.checks.coding.HiddenFieldCheck.MSG_KEY;
23  
24  import org.junit.jupiter.api.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
27  
28  public class HiddenFieldCheckExamplesTest extends AbstractExamplesModuleTestSupport {
29      @Override
30      protected String getPackageLocation() {
31          return "com/puppycrawl/tools/checkstyle/checks/coding/hiddenfield";
32      }
33  
34      @Test
35      public void testExample1() throws Exception {
36          final String[] expected = {
37              "16:19: " + getCheckMessage(MSG_KEY, "testField"),
38              "19:12: " + getCheckMessage(MSG_KEY, "field"),
39              "21:28: " + getCheckMessage(MSG_KEY, "testField"),
40              "24:28: " + getCheckMessage(MSG_KEY, "field"),
41              "29:32: " + getCheckMessage(MSG_KEY, "field"),
42          };
43  
44          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
45      }
46  
47      @Test
48      public void testExample2() throws Exception {
49          final String[] expected = {
50              "21:12: " + getCheckMessage(MSG_KEY, "field"),
51          };
52  
53          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
54      }
55  
56      @Test
57      public void testExample3() throws Exception {
58          final String[] expected = {
59              "21:12: " + getCheckMessage(MSG_KEY, "field"),
60              "26:28: " + getCheckMessage(MSG_KEY, "field"),
61              "31:32: " + getCheckMessage(MSG_KEY, "field"),
62          };
63  
64          verifyWithInlineConfigParser(getPath("Example3.java"), expected);
65      }
66  
67      @Test
68      public void testExample4() throws Exception {
69          final String[] expected = {
70              "21:12: " + getCheckMessage(MSG_KEY, "field"),
71              "23:28: " + getCheckMessage(MSG_KEY, "testField"),
72              "26:28: " + getCheckMessage(MSG_KEY, "field"),
73              "31:32: " + getCheckMessage(MSG_KEY, "field"),
74          };
75  
76          verifyWithInlineConfigParser(getPath("Example4.java"), expected);
77      }
78  
79      @Test
80      public void testExample5() throws Exception {
81          final String[] expected = {
82              "18:19: " + getCheckMessage(MSG_KEY, "testField"),
83              "21:12: " + getCheckMessage(MSG_KEY, "field"),
84              "26:28: " + getCheckMessage(MSG_KEY, "field"),
85              "31:32: " + getCheckMessage(MSG_KEY, "field"),
86          };
87  
88          verifyWithInlineConfigParser(getPath("Example5.java"), expected);
89      }
90  
91      @Test
92      public void testExample6() throws Exception {
93          final String[] expected = {
94              "19:19: " + getCheckMessage(MSG_KEY, "testField"),
95              "22:12: " + getCheckMessage(MSG_KEY, "field"),
96              "32:32: " + getCheckMessage(MSG_KEY, "field"),
97          };
98  
99          verifyWithInlineConfigParser(getPath("Example6.java"), expected);
100     }
101 
102     @Test
103     public void testExample7() throws Exception {
104         final String[] expected = {
105             "18:16: " + getCheckMessage(MSG_KEY, "field"),
106             "22:12: " + getCheckMessage(MSG_KEY, "field"),
107             "24:28: " + getCheckMessage(MSG_KEY, "testField"),
108             "27:28: " + getCheckMessage(MSG_KEY, "field"),
109         };
110 
111         verifyWithInlineConfigParser(getPath("Example7.java"), expected);
112     }
113 }