View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck.MSG_MATCH;
23  import static com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheck.MSG_MISMATCH;
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.fail;
26  import static org.mockito.Mockito.spy;
27  import static org.mockito.Mockito.when;
28  
29  import java.io.File;
30  import java.util.regex.Pattern;
31  
32  import org.junit.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.utils.CommonUtils;
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                  CommonUtils.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              "0: " + 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.addAttribute("match", "true");
67          checkConfig.addAttribute("fileNamePattern", ".*\\.java");
68          final String path = getPath("InputRegexpOnFilenameSemantic.java");
69          final String[] expected = {
70              "0: " + 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.addAttribute("match", "true");
79          checkConfig.addAttribute("fileNamePattern", "BAD.*");
80          verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
81                  CommonUtils.EMPTY_STRING_ARRAY);
82      }
83  
84      @Test
85      public void testNotMatchFileMatches() throws Exception {
86          final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
87          checkConfig.addAttribute("match", "false");
88          checkConfig.addAttribute("fileNamePattern", ".*\\.properties");
89          final String path = getPath("InputRegexpOnFilenameSemantic.java");
90          final String[] expected = {
91              "0: " + 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.addAttribute("match", "false");
100         checkConfig.addAttribute("fileNamePattern", ".*\\.java");
101         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
102                 CommonUtils.EMPTY_STRING_ARRAY);
103     }
104 
105     @Test
106     public void testMatchFolderMatches() throws Exception {
107         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
108         checkConfig.addAttribute("match", "true");
109         checkConfig.addAttribute("folderPattern", ".*[\\\\/]resources[\\\\/].*");
110         final String path = getPath("InputRegexpOnFilenameSemantic.java");
111         final String[] expected = {
112             "0: " + 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.addAttribute("match", "true");
121         checkConfig.addAttribute("folderPattern", "BAD.*");
122         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
123                 CommonUtils.EMPTY_STRING_ARRAY);
124     }
125 
126     @Test
127     public void testNotMatchFolderMatches() throws Exception {
128         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
129         checkConfig.addAttribute("match", "false");
130         checkConfig.addAttribute("folderPattern", ".*[\\\\/]gov[\\\\/].*");
131         final String path = getPath("InputRegexpOnFilenameSemantic.java");
132         final String[] expected = {
133             "0: " + 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.addAttribute("match", "false");
142         checkConfig.addAttribute("folderPattern", ".*[\\\\/]resources[\\\\/].*");
143         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
144                 CommonUtils.EMPTY_STRING_ARRAY);
145     }
146 
147     @Test
148     public void testMatchFolderAndFileMatches() throws Exception {
149         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
150         checkConfig.addAttribute("match", "true");
151         checkConfig.addAttribute("folderPattern", ".*[\\\\/]resources[\\\\/].*");
152         checkConfig.addAttribute("fileNamePattern", ".*\\.java");
153         final String path = getPath("InputRegexpOnFilenameSemantic.java");
154         final String[] expected = {
155             "0: " + 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.addAttribute("match", "true");
164         checkConfig.addAttribute("folderPattern", "BAD.*");
165         checkConfig.addAttribute("fileNamePattern", ".*\\.properties");
166         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
167                 CommonUtils.EMPTY_STRING_ARRAY);
168     }
169 
170     @Test
171     public void testMatchFolderAndFileNotMatchesFile() throws Exception {
172         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
173         checkConfig.addAttribute("match", "true");
174         checkConfig.addAttribute("folderPattern", ".*[\\\\/]resources[\\\\/].*");
175         checkConfig.addAttribute("fileNamePattern", ".*\\.properties");
176         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
177                 CommonUtils.EMPTY_STRING_ARRAY);
178     }
179 
180     @Test
181     public void testMatchFolderAndFileNotMatchesFolder() throws Exception {
182         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
183         checkConfig.addAttribute("match", "true");
184         checkConfig.addAttribute("folderPattern", "BAD.*");
185         checkConfig.addAttribute("fileNamePattern", ".*\\.java");
186         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
187                 CommonUtils.EMPTY_STRING_ARRAY);
188     }
189 
190     @Test
191     public void testNotMatchFolderAndFileMatches() throws Exception {
192         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
193         checkConfig.addAttribute("match", "false");
194         checkConfig.addAttribute("folderPattern", ".*[\\\\/]com[\\\\/].*");
195         checkConfig.addAttribute("fileNamePattern", ".*\\.dat");
196         final String path = getPath("InputRegexpOnFilenameSemantic.java");
197         final String[] expected = {
198             "0: " + 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.addAttribute("match", "false");
207         checkConfig.addAttribute("folderPattern", ".*[\\\\/]javastrangefolder[\\\\/].*");
208         checkConfig.addAttribute("fileNamePattern", ".*\\.dat");
209         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
210                 CommonUtils.EMPTY_STRING_ARRAY);
211     }
212 
213     @Test
214     public void testNotMatchFolderAndFileNotMatchesFile() throws Exception {
215         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
216         checkConfig.addAttribute("match", "false");
217         checkConfig.addAttribute("folderPattern", ".*[\\\\/]govstrangefolder[\\\\/].*");
218         checkConfig.addAttribute("fileNamePattern", ".*\\.java");
219         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
220                 CommonUtils.EMPTY_STRING_ARRAY);
221     }
222 
223     @Test
224     public void testIgnoreExtension() throws Exception {
225         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
226         checkConfig.addAttribute("fileNamePattern", ".*\\.java");
227         checkConfig.addAttribute("ignoreFileNameExtensions", "true");
228         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
229                 CommonUtils.EMPTY_STRING_ARRAY);
230     }
231 
232     @Test
233     public void testIgnoreExtensionNoExtension() throws Exception {
234         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
235         checkConfig.addAttribute("fileNamePattern", "\\.");
236         checkConfig.addAttribute("ignoreFileNameExtensions", "true");
237         verify(checkConfig, getPath("InputRegexpOnFilenameNoExtension"),
238                 CommonUtils.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         try {
246             final RegexpOnFilenameCheck check = new RegexpOnFilenameCheck();
247             check.setFileNamePattern(Pattern.compile("BAD"));
248             check.process(file, null);
249             fail("CheckstyleException expected");
250         }
251         catch (CheckstyleException ex) {
252             assertEquals("Invalid exception message",
253                 "unable to create canonical path names for " + file.getAbsolutePath(),
254                 ex.getMessage());
255         }
256     }
257 
258     /**
259      * Test require readable file with no parent to be used.
260      * Usage of Mockito.spy() is the only way to satisfy these requirements
261      * without the need to create new file in current working directory.
262      *
263      * @throws Exception if error occurs
264      */
265     @Test
266     public void testWithFileWithoutParent() throws Exception {
267         final DefaultConfiguration moduleConfig = createModuleConfig(RegexpOnFilenameCheck.class);
268         final File fileWithoutParent = spy(new File(getPath("package-info.java")));
269         when(fileWithoutParent.getParent()).thenReturn(null);
270         when(fileWithoutParent.getParentFile()).thenReturn(null);
271         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
272         verify(createChecker(moduleConfig),
273                 new File[] {fileWithoutParent},
274                 getPath("package-info.java"), expected);
275     }
276 
277 }