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;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheck.MSG_KEY;
24  
25  import java.io.File;
26  import java.util.List;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.google.common.collect.ImmutableMap;
31  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
33  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
34  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
35  
36  public class OuterTypeFilenameCheckTest extends AbstractModuleTestSupport {
37  
38      @Override
39      protected String getPackageLocation() {
40          return "com/puppycrawl/tools/checkstyle/checks/outertypefilename";
41      }
42  
43      @Test
44      public void testGetRequiredTokens() {
45          final OuterTypeFilenameCheck checkObj = new OuterTypeFilenameCheck();
46          final int[] expected = {
47              TokenTypes.CLASS_DEF,
48              TokenTypes.INTERFACE_DEF,
49              TokenTypes.ENUM_DEF,
50              TokenTypes.ANNOTATION_DEF,
51              TokenTypes.RECORD_DEF,
52          };
53          assertWithMessage("Required tokens array differs from expected")
54                  .that(checkObj.getRequiredTokens())
55                  .isEqualTo(expected);
56      }
57  
58      @Test
59      public void testGood1() throws Exception {
60          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
61          verifyWithInlineConfigParser(
62                  getPath("InputOuterTypeFilenameIllegalTokens.java"), expected);
63      }
64  
65      @Test
66      public void testGood2() throws Exception {
67          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
68          verifyWithInlineConfigParser(
69                  getPath("InputOuterTypeFilename15Extensions.java"), expected);
70      }
71  
72      @Test
73      public void testGetAcceptableTokens() {
74          final OuterTypeFilenameCheck check = new OuterTypeFilenameCheck();
75          final int[] actual = check.getAcceptableTokens();
76          final int[] expected = {
77              TokenTypes.CLASS_DEF,
78              TokenTypes.INTERFACE_DEF,
79              TokenTypes.ENUM_DEF,
80              TokenTypes.ANNOTATION_DEF,
81              TokenTypes.RECORD_DEF,
82          };
83          assertWithMessage("Acceptable tokens array differs from expected")
84                  .that(actual)
85                  .isEqualTo(expected);
86      }
87  
88      @Test
89      public void testNestedClass() throws Exception {
90          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
91          verifyWithInlineConfigParser(
92                  getPath("InputOuterTypeFilename1.java"), expected);
93      }
94  
95      @Test
96      public void testNestedClass2() throws Exception {
97          final String[] expected = {
98              "9:1: " + getCheckMessage(MSG_KEY),
99          };
100         verifyWithInlineConfigParser(
101                 getPath("InputOuterTypeFilename1a.java"), expected);
102     }
103 
104     @Test
105     public void testFinePublic() throws Exception {
106         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
107         verifyWithInlineConfigParser(
108                 getPath("InputOuterTypeFilename2.java"), expected);
109     }
110 
111     @Test
112     public void testPublicClassIsNotFirst() throws Exception {
113         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
114         verifyWithInlineConfigParser(
115                 getPath("InputOuterTypeFilenameCheckPublic.java"), expected);
116     }
117 
118     @Test
119     public void testNoPublicClasses() throws Exception {
120         final String[] expected = {
121             "9:1: " + getCheckMessage(MSG_KEY),
122         };
123         verifyWithInlineConfigParser(
124                 getPath("InputOuterTypeFilenameNoPublic.java"), expected);
125     }
126 
127     @Test
128     public void testFineDefault() throws Exception {
129         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
130         verifyWithInlineConfigParser(
131                 getPath("InputOuterTypeFilename3.java"), expected);
132     }
133 
134     @Test
135     public void testWrongDefault() throws Exception {
136         final String[] expected = {
137             "10:2: " + getCheckMessage(MSG_KEY),
138         };
139         verifyWithInlineConfigParser(
140                 getPath("InputOuterTypeFilename5.java"), expected);
141     }
142 
143     @Test
144     public void testPackageAnnotation() throws Exception {
145 
146         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
147 
148         verifyWithInlineConfigParser(
149                 getNonCompilablePath("package-info.java"), expected);
150     }
151 
152     @Test
153     public void testOuterTypeFilenameRecords() throws Exception {
154 
155         final String[] expected = {
156             "10:1: " + getCheckMessage(MSG_KEY),
157         };
158         verifyWithInlineConfigParser(
159                 getNonCompilablePath("InputOuterTypeFilenameRecordMethodRecordDef.java"),
160                 expected);
161     }
162 
163     @Test
164     public void testOuterTypeFilenameRecordsMethodRecordDef() throws Exception {
165 
166         final String[] expected = {
167             "10:1: " + getCheckMessage(MSG_KEY),
168         };
169         verifyWithInlineConfigParser(
170                 getNonCompilablePath("InputOuterTypeFilenameRecord.java"), expected);
171     }
172 
173     @Test
174     public void testStateIsClearedOnBeginTree2() throws Exception {
175         final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class);
176         final String file1 = getPath(
177                 "InputOuterTypeFilenameBeginTree1.java");
178         final String file2 = getPath(
179                 "InputOuterTypeFilenameBeginTree2.java");
180         final List<String> expectedFirstInput = List.of(CommonUtil.EMPTY_STRING_ARRAY);
181         final List<String> expectedSecondInput = List.of(CommonUtil.EMPTY_STRING_ARRAY);
182         final File[] inputs = {new File(file1), new File(file2)};
183 
184         verify(createChecker(checkConfig), inputs, ImmutableMap.of(
185             file1, expectedFirstInput,
186             file2, expectedSecondInput));
187     }
188 
189     @Test
190     public void testStateIsClearedOnBeginTree3() throws Exception {
191         final DefaultConfiguration checkConfig = createModuleConfig(OuterTypeFilenameCheck.class);
192         final String file1 = getPath(
193                 "InputOuterTypeFilenameBeginTree1.java");
194         final String file2 = getPath(
195                 "InputOuterTypeFilename1a.java");
196         final List<String> expectedFirstInput = List.of(CommonUtil.EMPTY_STRING_ARRAY);
197         final List<String> expectedSecondInput = List.of(
198                 "9:1: " + getCheckMessage(MSG_KEY)
199         );
200         final File[] inputs = {new File(file1), new File(file2)};
201 
202         verify(createChecker(checkConfig), inputs, ImmutableMap.of(
203             file1, expectedFirstInput,
204             file2, expectedSecondInput));
205     }
206 }