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