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