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.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  
30      @Override
31      public String getPackageLocation() {
32          return "com/puppycrawl/tools/checkstyle/checks/coding/hiddenfield";
33      }
34  
35      @Test
36      public void testExample1() throws Exception {
37          final String[] expected = {
38              "16:19: " + getCheckMessage(MSG_KEY, "testField"),
39              "19:12: " + getCheckMessage(MSG_KEY, "field"),
40              "21:28: " + getCheckMessage(MSG_KEY, "testField"),
41              "24:24: " + getCheckMessage(MSG_KEY, "field"),
42              "28:32: " + getCheckMessage(MSG_KEY, "field"),
43          };
44  
45          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
46      }
47  
48      @Test
49      public void testExample2() throws Exception {
50          final String[] expected = {
51              "21:12: " + getCheckMessage(MSG_KEY, "field"),
52          };
53  
54          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
55      }
56  
57      @Test
58      public void testExample3() throws Exception {
59          final String[] expected = {
60              "21:12: " + getCheckMessage(MSG_KEY, "field"),
61              "26:24: " + getCheckMessage(MSG_KEY, "field"),
62              "30:32: " + getCheckMessage(MSG_KEY, "field"),
63          };
64  
65          verifyWithInlineConfigParser(getPath("Example3.java"), expected);
66      }
67  
68      @Test
69      public void testExample4() throws Exception {
70          final String[] expected = {
71              "21:12: " + getCheckMessage(MSG_KEY, "field"),
72              "23:28: " + getCheckMessage(MSG_KEY, "testField"),
73              "26:24: " + getCheckMessage(MSG_KEY, "field"),
74              "30:32: " + getCheckMessage(MSG_KEY, "field"),
75          };
76  
77          verifyWithInlineConfigParser(getPath("Example4.java"), expected);
78      }
79  
80      @Test
81      public void testExample5() throws Exception {
82          final String[] expected = {
83              "18:19: " + getCheckMessage(MSG_KEY, "testField"),
84              "21:12: " + getCheckMessage(MSG_KEY, "field"),
85              "30: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              "31: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:19: " + getCheckMessage(MSG_KEY, "testField"),
106             "21:12: " + getCheckMessage(MSG_KEY, "field"),
107             "23:28: " + getCheckMessage(MSG_KEY, "testField"),
108             "26:24: " + getCheckMessage(MSG_KEY, "field"),
109         };
110 
111         verifyWithInlineConfigParser(getPath("Example7.java"), expected);
112     }
113 
114 }