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