View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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      @Override
41      protected String getPackageLocation() {
42          return "com/puppycrawl/tools/checkstyle/checks/regexp/regexponfilename";
43      }
44  
45      @Test
46      public void testDefaultConfigurationOnValidInput() throws Exception {
47          final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
48          verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
49                  CommonUtils.EMPTY_STRING_ARRAY);
50      }
51  
52      @Test
53      public void testDefaultProperties() throws Exception {
54          final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
55          final String path = getPath("InputRegexpOnFilename Space.properties");
56          final String[] expected = {
57              "0: " + getCheckMessage(MSG_MATCH, "", "\\s"),
58          };
59          verify(checkConfig, path, expected);
60      }
61  
62      @Test
63      public void testMatchFileMatches() throws Exception {
64          final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
65          checkConfig.addAttribute("match", "true");
66          checkConfig.addAttribute("fileNamePattern", ".*\\.java");
67          final String path = getPath("InputRegexpOnFilenameSemantic.java");
68          final String[] expected = {
69              "0: " + getCheckMessage(MSG_MATCH, "", ".*\\.java"),
70          };
71          verify(checkConfig, path, expected);
72      }
73  
74      @Test
75      public void testMatchFileNotMatches() throws Exception {
76          final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
77          checkConfig.addAttribute("match", "true");
78          checkConfig.addAttribute("fileNamePattern", "BAD.*");
79          verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
80                  CommonUtils.EMPTY_STRING_ARRAY);
81      }
82  
83      @Test
84      public void testNotMatchFileMatches() throws Exception {
85          final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
86          checkConfig.addAttribute("match", "false");
87          checkConfig.addAttribute("fileNamePattern", ".*\\.properties");
88          final String path = getPath("InputRegexpOnFilenameSemantic.java");
89          final String[] expected = {
90              "0: " + getCheckMessage(MSG_MISMATCH, "", ".*\\.properties"),
91          };
92          verify(checkConfig, path, expected);
93      }
94  
95      @Test
96      public void testNotMatchFileNotMatches() throws Exception {
97          final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
98          checkConfig.addAttribute("match", "false");
99          checkConfig.addAttribute("fileNamePattern", ".*\\.java");
100         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
101                 CommonUtils.EMPTY_STRING_ARRAY);
102     }
103 
104     @Test
105     public void testMatchFolderMatches() throws Exception {
106         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
107         checkConfig.addAttribute("match", "true");
108         checkConfig.addAttribute("folderPattern", ".*[\\\\/]resources[\\\\/].*");
109         final String path = getPath("InputRegexpOnFilenameSemantic.java");
110         final String[] expected = {
111             "0: " + getCheckMessage(MSG_MATCH, ".*[\\\\/]resources[\\\\/].*", ""),
112         };
113         verify(checkConfig, path, expected);
114     }
115 
116     @Test
117     public void testMatchFolderNotMatches() throws Exception {
118         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
119         checkConfig.addAttribute("match", "true");
120         checkConfig.addAttribute("folderPattern", "BAD.*");
121         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
122                 CommonUtils.EMPTY_STRING_ARRAY);
123     }
124 
125     @Test
126     public void testNotMatchFolderMatches() throws Exception {
127         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
128         checkConfig.addAttribute("match", "false");
129         checkConfig.addAttribute("folderPattern", ".*[\\\\/]gov[\\\\/].*");
130         final String path = getPath("InputRegexpOnFilenameSemantic.java");
131         final String[] expected = {
132             "0: " + getCheckMessage(MSG_MISMATCH, ".*[\\\\/]gov[\\\\/].*", ""),
133         };
134         verify(checkConfig, path, expected);
135     }
136 
137     @Test
138     public void testNotMatchFolderNotMatches() throws Exception {
139         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
140         checkConfig.addAttribute("match", "false");
141         checkConfig.addAttribute("folderPattern", ".*[\\\\/]resources[\\\\/].*");
142         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
143                 CommonUtils.EMPTY_STRING_ARRAY);
144     }
145 
146     @Test
147     public void testMatchFolderAndFileMatches() throws Exception {
148         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
149         checkConfig.addAttribute("match", "true");
150         checkConfig.addAttribute("folderPattern", ".*[\\\\/]resources[\\\\/].*");
151         checkConfig.addAttribute("fileNamePattern", ".*\\.java");
152         final String path = getPath("InputRegexpOnFilenameSemantic.java");
153         final String[] expected = {
154             "0: " + getCheckMessage(MSG_MATCH, ".*[\\\\/]resources[\\\\/].*", ".*\\.java"),
155         };
156         verify(checkConfig, path, expected);
157     }
158 
159     @Test
160     public void testMatchFolderAndFileNotMatchesBoth() throws Exception {
161         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
162         checkConfig.addAttribute("match", "true");
163         checkConfig.addAttribute("folderPattern", "BAD.*");
164         checkConfig.addAttribute("fileNamePattern", ".*\\.properties");
165         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
166                 CommonUtils.EMPTY_STRING_ARRAY);
167     }
168 
169     @Test
170     public void testMatchFolderAndFileNotMatchesFile() throws Exception {
171         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
172         checkConfig.addAttribute("match", "true");
173         checkConfig.addAttribute("folderPattern", ".*[\\\\/]resources[\\\\/].*");
174         checkConfig.addAttribute("fileNamePattern", ".*\\.properties");
175         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
176                 CommonUtils.EMPTY_STRING_ARRAY);
177     }
178 
179     @Test
180     public void testMatchFolderAndFileNotMatchesFolder() throws Exception {
181         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
182         checkConfig.addAttribute("match", "true");
183         checkConfig.addAttribute("folderPattern", "BAD.*");
184         checkConfig.addAttribute("fileNamePattern", ".*\\.java");
185         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
186                 CommonUtils.EMPTY_STRING_ARRAY);
187     }
188 
189     @Test
190     public void testNotMatchFolderAndFileMatches() throws Exception {
191         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
192         checkConfig.addAttribute("match", "false");
193         checkConfig.addAttribute("folderPattern", ".*[\\\\/]com[\\\\/].*");
194         checkConfig.addAttribute("fileNamePattern", ".*\\.dat");
195         final String path = getPath("InputRegexpOnFilenameSemantic.java");
196         final String[] expected = {
197             "0: " + getCheckMessage(MSG_MISMATCH, ".*[\\\\/]com[\\\\/].*", ".*\\.dat"),
198         };
199         verify(checkConfig, path, expected);
200     }
201 
202     @Test
203     public void testNotMatchFolderAndFileNotMatchesFolder() throws Exception {
204         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
205         checkConfig.addAttribute("match", "false");
206         checkConfig.addAttribute("folderPattern", ".*[\\\\/]javastrangefolder[\\\\/].*");
207         checkConfig.addAttribute("fileNamePattern", ".*\\.dat");
208         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
209                 CommonUtils.EMPTY_STRING_ARRAY);
210     }
211 
212     @Test
213     public void testNotMatchFolderAndFileNotMatchesFile() throws Exception {
214         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
215         checkConfig.addAttribute("match", "false");
216         checkConfig.addAttribute("folderPattern", ".*[\\\\/]govstrangefolder[\\\\/].*");
217         checkConfig.addAttribute("fileNamePattern", ".*\\.java");
218         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
219                 CommonUtils.EMPTY_STRING_ARRAY);
220     }
221 
222     @Test
223     public void testIgnoreExtension() throws Exception {
224         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
225         checkConfig.addAttribute("fileNamePattern", ".*\\.java");
226         checkConfig.addAttribute("ignoreFileNameExtensions", "true");
227         verify(checkConfig, getPath("InputRegexpOnFilenameSemantic.java"),
228                 CommonUtils.EMPTY_STRING_ARRAY);
229     }
230 
231     @Test
232     public void testIgnoreExtensionNoExtension() throws Exception {
233         final DefaultConfiguration checkConfig = createModuleConfig(RegexpOnFilenameCheck.class);
234         checkConfig.addAttribute("fileNamePattern", "\\.");
235         checkConfig.addAttribute("ignoreFileNameExtensions", "true");
236         verify(checkConfig, getPath("InputRegexpOnFilenameNoExtension"),
237                 CommonUtils.EMPTY_STRING_ARRAY);
238     }
239 
240     @Test
241     public void testException() throws Exception {
242         // escape character needed for testing IOException from File.getCanonicalPath on all OSes
243         final File file = new File(getPath("") + "\u0000" + File.separatorChar + "Test");
244         try {
245             final RegexpOnFilenameCheck check = new RegexpOnFilenameCheck();
246             check.setFileNamePattern(Pattern.compile("BAD"));
247             check.process(file, null);
248             fail("CheckstyleException expected");
249         }
250         catch (CheckstyleException ex) {
251             assertEquals("Invalid exception message",
252                 "unable to create canonical path names for " + file.getAbsolutePath(),
253                 ex.getMessage());
254         }
255     }
256 
257     /**
258      * Test require readable file with no parent to be used.
259      * Usage of Mockito.spy() is the only way to satisfy these requirements
260      * without the need to create new file in current working directory.
261      *
262      * @throws Exception if error occurs
263      */
264     @Test
265     public void testWithFileWithoutParent() throws Exception {
266         final DefaultConfiguration moduleConfig = createModuleConfig(RegexpOnFilenameCheck.class);
267         final File fileWithoutParent = spy(new File(getPath("package-info.java")));
268         when(fileWithoutParent.getParent()).thenReturn(null);
269         when(fileWithoutParent.getParentFile()).thenReturn(null);
270         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
271         verify(createChecker(moduleConfig),
272                 new File[] {fileWithoutParent},
273                 getPath("package-info.java"), expected);
274     }
275 }