View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.regexp;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck.MSG_MATCH;
24  import static com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck.MSG_MISMATCH;
25  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
26  
27  import java.io.File;
28  import java.io.Serial;
29  import java.util.Collections;
30  import java.util.regex.Pattern;
31  
32  import org.junit.jupiter.api.Test;
33  
34  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
36  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
37  import com.puppycrawl.tools.checkstyle.api.FileText;
38  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
39  
40  public class RegexpOnFilenameCheckTest extends AbstractModuleTestSupport {
41  
42      @Override
43      protected String getPackageLocation() {
44          return "com/puppycrawl/tools/checkstyle/checks/regexp/regexponfilename";
45      }
46  
47      @Test
48      public void testDefaultConfigurationOnValidInput() throws Exception {
49          final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
50          verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
51                  CommonUtil.EMPTY_STRING_ARRAY);
52      }
53  
54      @Test
55      public void testDefaultProperties() throws Exception {
56          final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
57          final String path = getPath("InputRegexpOnFilename Space.properties");
58          final String[] expected = {
59              "1: " + getCheckMessage(MSG_MATCH, "", "\\s"),
60          };
61          verify(checkConfig, path, expected);
62      }
63  
64      @Test
65      public void testMatchFileMatches() throws Exception {
66          final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
67          checkConfig.addProperty("match", "true");
68          checkConfig.addProperty("fileNamePattern", ".*\\.java");
69          final String path = getPath("InputRegexpOnFilenameSemantic.java");
70          final String[] expected = {
71              "1: " + getCheckMessage(MSG_MATCH, "", ".*\\.java"),
72          };
73          verify(checkConfig, path, expected);
74      }
75  
76      @Test
77      public void testMatchFileNotMatches() throws Exception {
78          final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
79          checkConfig.addProperty("match", "true");
80          checkConfig.addProperty("fileNamePattern", "BAD.*");
81          verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
82                  CommonUtil.EMPTY_STRING_ARRAY);
83      }
84  
85      @Test
86      public void testNotMatchFileMatches() throws Exception {
87          final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
88          checkConfig.addProperty("match", "false");
89          checkConfig.addProperty("fileNamePattern", ".*\\.properties");
90          final String path = getPath("InputRegexpOnFilenameSemantic.java");
91          final String[] expected = {
92              "1: " + getCheckMessage(MSG_MISMATCH, "", ".*\\.properties"),
93          };
94          verify(checkConfig, path, expected);
95      }
96  
97      @Test
98      public void testNotMatchFileNotMatches() throws Exception {
99          final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
100         checkConfig.addProperty("match", "false");
101         checkConfig.addProperty("fileNamePattern", ".*\\.java");
102         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
103                 CommonUtil.EMPTY_STRING_ARRAY);
104     }
105 
106     @Test
107     public void testMatchFolderMatches() throws Exception {
108         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
109         checkConfig.addProperty("match", "true");
110         checkConfig.addProperty("folderPattern", ".*[\\\\/]resources[\\\\/].*");
111         final String path = getPath("InputRegexpOnFilenameSemantic.java");
112         final String[] expected = {
113             "1: " + getCheckMessage(MSG_MATCH, ".*[\\\\/]resources[\\\\/].*", ""),
114         };
115         verify(checkConfig, path, expected);
116     }
117 
118     @Test
119     public void testMatchFolderNotMatches() throws Exception {
120         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
121         checkConfig.addProperty("match", "true");
122         checkConfig.addProperty("folderPattern", "BAD.*");
123         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
124                 CommonUtil.EMPTY_STRING_ARRAY);
125     }
126 
127     @Test
128     public void testNotMatchFolderMatches() throws Exception {
129         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
130         checkConfig.addProperty("match", "false");
131         checkConfig.addProperty("folderPattern", ".*[\\\\/]gov[\\\\/].*");
132         final String path = getPath("InputRegexpOnFilenameSemantic.java");
133         final String[] expected = {
134             "1: " + getCheckMessage(MSG_MISMATCH, ".*[\\\\/]gov[\\\\/].*", ""),
135         };
136         verify(checkConfig, path, expected);
137     }
138 
139     @Test
140     public void testNotMatchFolderNotMatches() throws Exception {
141         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
142         checkConfig.addProperty("match", "false");
143         checkConfig.addProperty("folderPattern", ".*[\\\\/]resources[\\\\/].*");
144         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
145                 CommonUtil.EMPTY_STRING_ARRAY);
146     }
147 
148     @Test
149     public void testMatchFolderAndFileMatches() throws Exception {
150         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
151         checkConfig.addProperty("match", "true");
152         checkConfig.addProperty("folderPattern", ".*[\\\\/]resources[\\\\/].*");
153         checkConfig.addProperty("fileNamePattern", ".*\\.java");
154         final String path = getPath("InputRegexpOnFilenameSemantic.java");
155         final String[] expected = {
156             "1: " + getCheckMessage(MSG_MATCH, ".*[\\\\/]resources[\\\\/].*", ".*\\.java"),
157         };
158         verify(checkConfig, path, expected);
159     }
160 
161     @Test
162     public void testMatchFolderAndFileNotMatchesBoth() throws Exception {
163         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
164         checkConfig.addProperty("match", "true");
165         checkConfig.addProperty("folderPattern", "BAD.*");
166         checkConfig.addProperty("fileNamePattern", ".*\\.properties");
167         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
168                 CommonUtil.EMPTY_STRING_ARRAY);
169     }
170 
171     @Test
172     public void testMatchFolderAndFileNotMatchesFile() throws Exception {
173         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
174         checkConfig.addProperty("match", "true");
175         checkConfig.addProperty("folderPattern", ".*[\\\\/]resources[\\\\/].*");
176         checkConfig.addProperty("fileNamePattern", ".*\\.properties");
177         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
178                 CommonUtil.EMPTY_STRING_ARRAY);
179     }
180 
181     @Test
182     public void testMatchFolderAndFileNotMatchesFolder() throws Exception {
183         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
184         checkConfig.addProperty("match", "true");
185         checkConfig.addProperty("folderPattern", "BAD.*");
186         checkConfig.addProperty("fileNamePattern", ".*\\.java");
187         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
188                 CommonUtil.EMPTY_STRING_ARRAY);
189     }
190 
191     @Test
192     public void testNotMatchFolderAndFileMatches() throws Exception {
193         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
194         checkConfig.addProperty("match", "false");
195         checkConfig.addProperty("folderPattern", ".*[\\\\/]com[\\\\/].*");
196         checkConfig.addProperty("fileNamePattern", ".*\\.dat");
197         final String path = getPath("InputRegexpOnFilenameSemantic.java");
198         final String[] expected = {
199             "1: " + getCheckMessage(MSG_MISMATCH, ".*[\\\\/]com[\\\\/].*", ".*\\.dat"),
200         };
201         verify(checkConfig, path, expected);
202     }
203 
204     @Test
205     public void testNotMatchFolderAndFileNotMatchesFolder() throws Exception {
206         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
207         checkConfig.addProperty("match", "false");
208         checkConfig.addProperty("folderPattern", ".*[\\\\/]javastrangefolder[\\\\/].*");
209         checkConfig.addProperty("fileNamePattern", ".*\\.dat");
210         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
211                 CommonUtil.EMPTY_STRING_ARRAY);
212     }
213 
214     @Test
215     public void testNotMatchFolderAndFileNotMatchesFile() throws Exception {
216         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
217         checkConfig.addProperty("match", "false");
218         checkConfig.addProperty("folderPattern", ".*[\\\\/]govstrangefolder[\\\\/].*");
219         checkConfig.addProperty("fileNamePattern", ".*\\.java");
220         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
221                 CommonUtil.EMPTY_STRING_ARRAY);
222     }
223 
224     @Test
225     public void testIgnoreExtension() throws Exception {
226         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
227         checkConfig.addProperty("fileNamePattern", ".*\\.java");
228         checkConfig.addProperty("ignoreFileNameExtensions", "true");
229         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
230                 CommonUtil.EMPTY_STRING_ARRAY);
231     }
232 
233     @Test
234     public void testIgnoreExtensionNoExtension() throws Exception {
235         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
236         checkConfig.addProperty("fileNamePattern", "\\.");
237         checkConfig.addProperty("ignoreFileNameExtensions", "true");
238         verify(checkConfig, getPath("InputRegexpOnFilenameNoExtension"),
239                 CommonUtil.EMPTY_STRING_ARRAY);
240     }
241 
242     @Test
243     public void testException() throws Exception {
244         // escape character needed for testing IOException from File.getCanonicalPath on all OSes
245         final File file = new File(getPath("") + "\u0000" + File.separatorChar + "Test");
246         final RegexpOnFilenameCheck check = new RegexpOnFilenameCheck();
247         check.setFileNamePattern(Pattern.compile("BAD"));
248         final CheckstyleException ex = getExpectedThrowable(CheckstyleException.class,
249                 () -> check.process(file, new FileText(file, Collections.emptyList())),
250                 "CheckstyleException expected");
251         assertWithMessage("Invalid exception message")
252                 .that(ex)
253                 .hasMessageThat()
254                         .isEqualTo("unable to create canonical path names for " + file);
255     }
256 
257     @Test
258     public void testWithFileWithoutParent() throws Exception {
259         final DefaultConfiguration moduleConfig = createModuleConfig(RegexpOnFilenameCheck.class);
260         final String path = getPath("package-info.java");
261         final File fileWithoutParent = new MockFile(path);
262         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
263         verify(createChecker(moduleConfig), new File[] {fileWithoutParent}, path, expected);
264     }
265 
266     private static final class MockFile extends File {
267 
268         /** A unique serial version identifier. */
269         @Serial
270         private static final long serialVersionUID = 8361197804062781531L;
271 
272         private MockFile(String path) {
273             super(path);
274         }
275 
276         /** This method is overridden to emulate a file without parent. */
277         @Override
278         public String getParent() {
279             return null;
280         }
281 
282     }
283 
284 }