View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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  
25  import java.io.File;
26  import java.io.IOException;
27  import java.nio.file.Files;
28  import java.nio.file.Path;
29  import java.nio.file.Paths;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.stream.Stream;
34  
35  import org.junit.jupiter.api.Test;
36  
37  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
38  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
39  import com.puppycrawl.tools.checkstyle.bdd.InlineConfigParser;
40  import com.puppycrawl.tools.checkstyle.bdd.TestInputConfiguration;
41  
42  public class RegexpOnFilenameCheckExamplesTest extends AbstractExamplesModuleTestSupport {
43  
44      @Override
45      public String getPackageLocation() {
46          return "com/puppycrawl/tools/checkstyle/checks/regexp/regexponfilename";
47      }
48  
49      @Test
50      public void testExample1() throws Exception {
51          final Map<String, List<String>> expected = new HashMap<>();
52          final List<String> messages = List.of("1: " + getCheckMessage(MSG_MATCH, "", "\\s"));
53          expected.put(getPath("Test Example1.xml"), messages);
54  
55          final Path path = Paths.get("src/xdocs-examples/resources/" + getPackageLocation() + "/");
56  
57          final String configFilePath = getPath("Example1.java");
58          final TestInputConfiguration testInputConfiguration1 =
59                  InlineConfigParser.parse(configFilePath);
60          final DefaultConfiguration parsedConfig =
61                  testInputConfiguration1.createConfiguration();
62  
63          verify(createChecker(parsedConfig), getFilesInFolder(path), expected);
64      }
65  
66      @Test
67      public void testExample2() throws Exception {
68          final Map<String, List<String>> expected = new HashMap<>();
69          final List<String> messages =
70                  List.of("1: xml files should not match '^TestExample\\d+\\.xml$'");
71          expected.put(getPath("TestExample2.xml"), messages);
72          expected.put(getPath("TestExample4.xml"), messages);
73  
74          final Path path = Paths.get("src/xdocs-examples/resources/" + getPackageLocation() + "/");
75  
76          final String configFilePath = getPath("Example2.java");
77          final TestInputConfiguration testInputConfiguration1 =
78                  InlineConfigParser.parse(configFilePath);
79          final DefaultConfiguration parsedConfig =
80                  testInputConfiguration1.createConfiguration();
81  
82          verify(createChecker(parsedConfig), getFilesInFolder(path), expected);
83      }
84  
85      @Test
86      public void testExample3() throws Exception {
87          final Map<String, List<String>> expected = new HashMap<>();
88          expected.put(getPath("TestExample3.md"),
89                  List.of("1: No *.md files other than README.md"));
90  
91          final Path path = Paths.get("src/xdocs-examples/resources/" + getPackageLocation() + "/");
92  
93          final String configFilePath = getPath("Example3.java");
94          final TestInputConfiguration testInputConfiguration1 =
95                  InlineConfigParser.parse(configFilePath);
96          final DefaultConfiguration parsedConfig =
97                  testInputConfiguration1.createConfiguration();
98  
99          verify(createChecker(parsedConfig), getFilesInFolder(path), expected);
100     }
101 
102     @Test
103     public void testExample4() throws Exception {
104         final Map<String, List<String>> expected = new HashMap<>();
105         final List<String> messages =
106                 List.of("1: Only property and xml files to be located in the resource folder");
107         expected.put(getPath("TestExample3.md"), messages);
108         // java files should not be mentioned in user visible output Example4.java
109         expected.put(getPath("Example1.java"), messages);
110         expected.put(getPath("Example2.java"), messages);
111         expected.put(getPath("Example3.java"), messages);
112         expected.put(getPath("Example4.java"), messages);
113         expected.put(getPath("Example5.java"), messages);
114         expected.put(getPath("Example6.java"), messages);
115 
116         final Path path = Paths.get("src/xdocs-examples/resources/" + getPackageLocation() + "/");
117 
118         final String configFilePath = getPath("Example4.java");
119         final TestInputConfiguration testInputConfiguration1 =
120                 InlineConfigParser.parse(configFilePath);
121         final DefaultConfiguration parsedConfig =
122                 testInputConfiguration1.createConfiguration();
123 
124         verify(createChecker(parsedConfig), getFilesInFolder(path), expected);
125     }
126 
127     @Test
128     public void testExample5() throws Exception {
129         final Map<String, List<String>> expected = new HashMap<>();
130         final List<String> messages = List.of("1: only filenames in camelcase is allowed");
131         expected.put(getPath("checkstyle.xml"), messages);
132         expected.put(getPath("Test Example1.xml"), messages);
133 
134         final Path path = Paths.get("src/xdocs-examples/resources/" + getPackageLocation() + "/");
135 
136         final String configFilePath = getPath("Example5.java");
137         final TestInputConfiguration testInputConfiguration1 =
138                 InlineConfigParser.parse(configFilePath);
139         final DefaultConfiguration parsedConfig =
140                 testInputConfiguration1.createConfiguration();
141 
142         verify(createChecker(parsedConfig), getFilesInFolder(path), expected);
143     }
144 
145     private static File[] getFilesInFolder(Path path) throws IOException {
146         try (Stream<Path> stream = Files.walk(path.toAbsolutePath())) {
147             return stream.filter(Files::isRegularFile)
148                     .map(Path::toFile)
149                     .toArray(File[]::new);
150         }
151     }
152 
153     @Test
154     public void testExample6() throws Exception {
155         final Map<String, List<String>> expected = new HashMap<>();
156         final List<String> messages = List.of(
157                 "1: " + getCheckMessage(MSG_MISMATCH,
158                         "[\\\\/]regexponfilename$", "^Test.*\\.xml$"));
159 
160         expected.put(getPath("checkstyle.xml"), messages);
161         expected.put(getPath("TestExample3.md"), messages);
162         expected.put(getPath("Example1.java"), messages);
163         expected.put(getPath("Example2.java"), messages);
164         expected.put(getPath("Example3.java"), messages);
165         expected.put(getPath("Example4.java"), messages);
166         expected.put(getPath("Example5.java"), messages);
167         expected.put(getPath("Example6.java"), messages);
168 
169         final Path path = Paths.get("src/xdocs-examples/resources/" + getPackageLocation() + "/");
170 
171         final String configFilePath = getPath("Example6.java");
172         final TestInputConfiguration testInputConfiguration1 =
173                 InlineConfigParser.parse(configFilePath);
174         final DefaultConfiguration parsedConfig =
175                 testInputConfiguration1.createConfiguration();
176 
177         verify(createChecker(parsedConfig), getFilesInFolder(path), expected);
178     }
179 
180 }