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