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.design;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.design.VisibilityModifierCheck.MSG_KEY;
23  
24  import org.junit.jupiter.api.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
27  
28  public class VisibilityModifierCheckExamplesTest extends AbstractExamplesModuleTestSupport {
29      @Override
30      public String getPackageLocation() {
31          return "com/puppycrawl/tools/checkstyle/checks/design/visibilitymodifier";
32      }
33  
34      @Test
35      public void testExample1() throws Exception {
36          final String[] expected = {
37              "25:7: " + getCheckMessage(MSG_KEY, "field1"),
38              "27:20: " + getCheckMessage(MSG_KEY, "field2"),
39              "29:14: " + getCheckMessage(MSG_KEY, "field3"),
40              "36:20: " + getCheckMessage(MSG_KEY, "field5"),
41              "39:33: " + getCheckMessage(MSG_KEY, "notes"),
42              "42:28: " + getCheckMessage(MSG_KEY, "mySet1"),
43              "45:37: " + getCheckMessage(MSG_KEY, "mySet2"),
44              "48:45: " + getCheckMessage(MSG_KEY, "objects1"),
45              "51:10: " + getCheckMessage(MSG_KEY, "annotatedString"),
46              "55:10: " + getCheckMessage(MSG_KEY, "shortCustomAnnotated"),
47              "61:20: " + getCheckMessage(MSG_KEY, "someIntValue"),
48              "64:37: " + getCheckMessage(MSG_KEY, "includes"),
49              "67:27: " + getCheckMessage(MSG_KEY, "value"),
50              "70:21: " + getCheckMessage(MSG_KEY, "list"),
51          };
52  
53          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
54      }
55  
56      @Test
57      public void testExample2() throws Exception {
58          final String[] expected = {
59              "29:20: " + getCheckMessage(MSG_KEY, "field2"),
60              "32:14: " + getCheckMessage(MSG_KEY, "field3"),
61              "39:20: " + getCheckMessage(MSG_KEY, "field5"),
62              "42:33: " + getCheckMessage(MSG_KEY, "notes"),
63              "45:28: " + getCheckMessage(MSG_KEY, "mySet1"),
64              "48:37: " + getCheckMessage(MSG_KEY, "mySet2"),
65              "51:45: " + getCheckMessage(MSG_KEY, "objects1"),
66              "63:20: " + getCheckMessage(MSG_KEY, "someIntValue"),
67              "66:37: " + getCheckMessage(MSG_KEY, "includes"),
68              "69:27: " + getCheckMessage(MSG_KEY, "value"),
69              "72:21: " + getCheckMessage(MSG_KEY, "list"),
70          };
71  
72          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
73      }
74  
75      @Test
76      public void testExample3() throws Exception {
77          final String[] expected = {
78              "27:7: " + getCheckMessage(MSG_KEY, "field1"),
79              "32:14: " + getCheckMessage(MSG_KEY, "field3"),
80              "39:20: " + getCheckMessage(MSG_KEY, "field5"),
81              "42:33: " + getCheckMessage(MSG_KEY, "notes"),
82              "45:28: " + getCheckMessage(MSG_KEY, "mySet1"),
83              "48:37: " + getCheckMessage(MSG_KEY, "mySet2"),
84              "51:45: " + getCheckMessage(MSG_KEY, "objects1"),
85              "54:10: " + getCheckMessage(MSG_KEY, "annotatedString"),
86              "58:10: " + getCheckMessage(MSG_KEY, "shortCustomAnnotated"),
87              "64:20: " + getCheckMessage(MSG_KEY, "someIntValue"),
88              "67:37: " + getCheckMessage(MSG_KEY, "includes"),
89              "70:27: " + getCheckMessage(MSG_KEY, "value"),
90              "73:21: " + getCheckMessage(MSG_KEY, "list"),
91          };
92  
93          verifyWithInlineConfigParser(getPath("Example3.java"), expected);
94      }
95  
96      @Test
97      public void testExample4() throws Exception {
98          final String[] expected = {
99              "27:7: " + getCheckMessage(MSG_KEY, "field1"),
100             "29:20: " + getCheckMessage(MSG_KEY, "field2"),
101             "32:14: " + getCheckMessage(MSG_KEY, "field3"),
102             "35:15: " + getCheckMessage(MSG_KEY, "serialVersionUID"),
103             "40:20: " + getCheckMessage(MSG_KEY, "field5"),
104             "43:33: " + getCheckMessage(MSG_KEY, "notes"),
105             "46:28: " + getCheckMessage(MSG_KEY, "mySet1"),
106             "49:37: " + getCheckMessage(MSG_KEY, "mySet2"),
107             "52:45: " + getCheckMessage(MSG_KEY, "objects1"),
108             "55:10: " + getCheckMessage(MSG_KEY, "annotatedString"),
109             "59:10: " + getCheckMessage(MSG_KEY, "shortCustomAnnotated"),
110             "65:20: " + getCheckMessage(MSG_KEY, "someIntValue"),
111             "68:37: " + getCheckMessage(MSG_KEY, "includes"),
112             "71:27: " + getCheckMessage(MSG_KEY, "value"),
113             "74:21: " + getCheckMessage(MSG_KEY, "list"),
114         };
115 
116         verifyWithInlineConfigParser(getPath("Example4.java"), expected);
117     }
118 
119     @Test
120     public void testExample5() throws Exception {
121         final String[] expected = {
122             "27:7: " + getCheckMessage(MSG_KEY, "field1"),
123             "29:20: " + getCheckMessage(MSG_KEY, "field2"),
124             "32:14: " + getCheckMessage(MSG_KEY, "field3"),
125             "38:20: " + getCheckMessage(MSG_KEY, "field5"),
126             "40:33: " + getCheckMessage(MSG_KEY, "notes"),
127             "43:28: " + getCheckMessage(MSG_KEY, "mySet1"),
128             "46:37: " + getCheckMessage(MSG_KEY, "mySet2"),
129             "49:45: " + getCheckMessage(MSG_KEY, "objects1"),
130             "52:10: " + getCheckMessage(MSG_KEY, "annotatedString"),
131             "56:10: " + getCheckMessage(MSG_KEY, "shortCustomAnnotated"),
132             "61:20: " + getCheckMessage(MSG_KEY, "someIntValue"),
133             "64:37: " + getCheckMessage(MSG_KEY, "includes"),
134             "67:27: " + getCheckMessage(MSG_KEY, "value"),
135             "70:21: " + getCheckMessage(MSG_KEY, "list"),
136         };
137 
138         verifyWithInlineConfigParser(getPath("Example5.java"), expected);
139     }
140 
141     @Test
142     public void testExample6() throws Exception {
143         final String[] expected = {
144             "30:7: " + getCheckMessage(MSG_KEY, "field1"),
145             "32:20: " + getCheckMessage(MSG_KEY, "field2"),
146             "35:14: " + getCheckMessage(MSG_KEY, "field3"),
147             "41:20: " + getCheckMessage(MSG_KEY, "field5"),
148             "43:33: " + getCheckMessage(MSG_KEY, "notes"),
149             "46:28: " + getCheckMessage(MSG_KEY, "mySet1"),
150             "49:37: " + getCheckMessage(MSG_KEY, "mySet2"),
151             "52:45: " + getCheckMessage(MSG_KEY, "objects1"),
152             "55:10: " + getCheckMessage(MSG_KEY, "annotatedString"),
153             "59:10: " + getCheckMessage(MSG_KEY, "shortCustomAnnotated"),
154             "64:20: " + getCheckMessage(MSG_KEY, "someIntValue"),
155             "66:37: " + getCheckMessage(MSG_KEY, "includes"),
156             "69:27: " + getCheckMessage(MSG_KEY, "value"),
157             "72:21: " + getCheckMessage(MSG_KEY, "list"),
158         };
159 
160         verifyWithInlineConfigParser(getPath("Example6.java"), expected);
161     }
162 
163     @Test
164     public void testExample7() throws Exception {
165         final String[] expected = {
166             "28:7: " + getCheckMessage(MSG_KEY, "field1"),
167             "30:20: " + getCheckMessage(MSG_KEY, "field2"),
168             "33:14: " + getCheckMessage(MSG_KEY, "field3"),
169             "39:20: " + getCheckMessage(MSG_KEY, "field5"),
170             "41:33: " + getCheckMessage(MSG_KEY, "notes"),
171             "44:28: " + getCheckMessage(MSG_KEY, "mySet1"),
172             "47:37: " + getCheckMessage(MSG_KEY, "mySet2"),
173             "50:45: " + getCheckMessage(MSG_KEY, "objects1"),
174             "53:10: " + getCheckMessage(MSG_KEY, "annotatedString"),
175             "57:10: " + getCheckMessage(MSG_KEY, "shortCustomAnnotated"),
176         };
177 
178         verifyWithInlineConfigParser(getPath("Example7.java"), expected);
179     }
180 
181     @Test
182     public void testExample8() throws Exception {
183         final String[] expected = {
184             "28:7: " + getCheckMessage(MSG_KEY, "field1"),
185             "30:20: " + getCheckMessage(MSG_KEY, "field2"),
186             "33:14: " + getCheckMessage(MSG_KEY, "field3"),
187             "39:20: " + getCheckMessage(MSG_KEY, "field5"),
188             "41:33: " + getCheckMessage(MSG_KEY, "notes"),
189             "44:28: " + getCheckMessage(MSG_KEY, "mySet1"),
190             "47:37: " + getCheckMessage(MSG_KEY, "mySet2"),
191             "50:45: " + getCheckMessage(MSG_KEY, "objects1"),
192             "60:17: " + getCheckMessage(MSG_KEY, "testString"),
193             "63:20: " + getCheckMessage(MSG_KEY, "someIntValue"),
194             "66:37: " + getCheckMessage(MSG_KEY, "includes"),
195             "69:27: " + getCheckMessage(MSG_KEY, "value"),
196             "72:21: " + getCheckMessage(MSG_KEY, "list"),
197         };
198 
199         verifyWithInlineConfigParser(getPath("Example8.java"), expected);
200     }
201 
202     @Test
203     public void testExample9() throws Exception {
204         final String[] expected = {
205             "23:7: " + getCheckMessage(MSG_KEY, "field1"),
206             "25:20: " + getCheckMessage(MSG_KEY, "field2"),
207             "28:14: " + getCheckMessage(MSG_KEY, "field3"),
208             "34:20: " + getCheckMessage(MSG_KEY, "field5"),
209             "36:33: " + getCheckMessage(MSG_KEY, "notes"),
210             "39:28: " + getCheckMessage(MSG_KEY, "mySet1"),
211             "42:37: " + getCheckMessage(MSG_KEY, "mySet2"),
212             "45:45: " + getCheckMessage(MSG_KEY, "objects1"),
213             "48:10: " + getCheckMessage(MSG_KEY, "annotatedString"),
214             "52:10: " + getCheckMessage(MSG_KEY, "shortCustomAnnotated"),
215         };
216 
217         verifyWithInlineConfigParser(getPath("Example9.java"), expected);
218     }
219 
220     @Test
221     public void testExample10() throws Exception {
222         final String[] expected = {
223             "25:7: " + getCheckMessage(MSG_KEY, "field1"),
224             "27:20: " + getCheckMessage(MSG_KEY, "field2"),
225             "30:14: " + getCheckMessage(MSG_KEY, "field3"),
226             "36:20: " + getCheckMessage(MSG_KEY, "field5"),
227             "38:33: " + getCheckMessage(MSG_KEY, "notes"),
228             "41:28: " + getCheckMessage(MSG_KEY, "mySet1"),
229             "44:37: " + getCheckMessage(MSG_KEY, "mySet2"),
230             "47:45: " + getCheckMessage(MSG_KEY, "objects1"),
231             "50:10: " + getCheckMessage(MSG_KEY, "annotatedString"),
232             "57:17: " + getCheckMessage(MSG_KEY, "testString"),
233         };
234 
235         verifyWithInlineConfigParser(getPath("Example10.java"), expected);
236     }
237 
238     @Test
239     public void testExample11() throws Exception {
240         final String[] expected = {
241             "22:20: " + getCheckMessage(MSG_KEY, "someIntValue"),
242             "24:37: " + getCheckMessage(MSG_KEY, "includes"),
243             "26:33: " + getCheckMessage(MSG_KEY, "notes"),
244             "28:27: " + getCheckMessage(MSG_KEY, "value"),
245             "30:21: " + getCheckMessage(MSG_KEY, "list"),
246         };
247 
248         verifyWithInlineConfigParser(getPath("Example11.java"), expected);
249     }
250 
251     @Test
252     public void testExample12() throws Exception {
253         final String[] expected = {
254             "27:7: " + getCheckMessage(MSG_KEY, "field1"),
255             "29:20: " + getCheckMessage(MSG_KEY, "field2"),
256             "32:14: " + getCheckMessage(MSG_KEY, "field3"),
257             "49:10: " + getCheckMessage(MSG_KEY, "annotatedString"),
258             "53:10: " + getCheckMessage(MSG_KEY, "shortCustomAnnotated"),
259         };
260 
261         verifyWithInlineConfigParser(getPath("Example12.java"), expected);
262     }
263 }