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.filefilters;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.nio.file.Paths;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.stream.Stream;
31  
32  import org.junit.jupiter.api.Test;
33  
34  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
35  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
36  import com.puppycrawl.tools.checkstyle.bdd.InlineConfigParser;
37  import com.puppycrawl.tools.checkstyle.bdd.TestInputConfiguration;
38  
39  public class BeforeExecutionExclusionFileFilterExamplesTest
40          extends AbstractExamplesModuleTestSupport {
41      @Override
42      protected String getPackageLocation() {
43          return "com/puppycrawl/tools/checkstyle/filefilters/beforeexecutionexclusionfilefilter";
44      }
45  
46      @Test
47      public void testExample1() throws Exception {
48          final Map<String, List<String>> expected = new HashMap<>();
49          final List<String> messages =
50                  List.of("1: File name must start with an uppercase.");
51          expected.put(getNonCompilablePath("test/generated_StubBankRemote.java"), messages);
52          expected.put(getNonCompilablePath("test/generated_TestCase1.java"), messages);
53          expected.put(getNonCompilablePath("module-info.java"), messages);
54  
55          final Path path = Paths.get("src/xdocs-examples/resources-noncompilable/"
56                  + getPackageLocation() + "/");
57  
58          final String fileWithConfig = getNonCompilablePath("Example1.java");
59          final TestInputConfiguration testInputConfiguration =
60                  InlineConfigParser.parse(fileWithConfig);
61          final DefaultConfiguration parsedConfig =
62                  testInputConfiguration.createConfiguration();
63  
64          verify(createChecker(parsedConfig), getFilesInFolder(path), expected);
65      }
66  
67      @Test
68      public void testExample2() throws Exception {
69          final Map<String, List<String>> expected = new HashMap<>();
70          final List<String> messages =
71                  List.of("1: File name must start with an uppercase.");
72          expected.put(getNonCompilablePath("test/generated_StubBankRemote.java"), messages);
73          expected.put(getNonCompilablePath("test/generated_TestCase1.java"), messages);
74  
75          final Path path = Paths.get("src/xdocs-examples/resources-noncompilable/"
76                  + getPackageLocation() + "/");
77  
78          final String fileWithConfig = getNonCompilablePath("Example2.java");
79          final TestInputConfiguration testInputConfiguration =
80                  InlineConfigParser.parse(fileWithConfig);
81          final DefaultConfiguration parsedConfig =
82                  testInputConfiguration.createConfiguration();
83  
84          verify(createChecker(parsedConfig), getFilesInFolder(path), expected);
85      }
86  
87      @Test
88      public void testExample3() throws Exception {
89          final Map<String, List<String>> expected = new HashMap<>();
90          final List<String> messages =
91                  List.of("1: File name must start with an uppercase.");
92          expected.put(getNonCompilablePath("test/generated_StubBankRemote.java"), messages);
93  
94          final Path path = Paths.get("src/xdocs-examples/resources-noncompilable/"
95                  + getPackageLocation() + "/");
96  
97          final String fileWithConfig = getNonCompilablePath("Example3.java");
98          final TestInputConfiguration testInputConfiguration =
99                  InlineConfigParser.parse(fileWithConfig);
100         final DefaultConfiguration parsedConfig =
101                 testInputConfiguration.createConfiguration();
102 
103         verify(createChecker(parsedConfig), getFilesInFolder(path), expected);
104     }
105 
106     @Test
107     public void testExample4() throws Exception {
108         final Map<String, List<String>> expected = new HashMap<>();
109 
110         final Path path = Paths.get("src/xdocs-examples/resources-noncompilable/"
111                 + getPackageLocation() + "/");
112 
113         final String fileWithConfig = getNonCompilablePath("Example4.java");
114         final TestInputConfiguration testInputConfiguration =
115                 InlineConfigParser.parse(fileWithConfig);
116         final DefaultConfiguration parsedConfig =
117                 testInputConfiguration.createConfiguration();
118 
119         verify(createChecker(parsedConfig), getFilesInFolder(path), expected);
120     }
121 
122     private static File[] getFilesInFolder(Path path) throws IOException {
123         try (Stream<Path> stream = Files.walk(path.toAbsolutePath())) {
124             return stream.filter(Files::isRegularFile)
125                     .map(Path::toFile)
126                     .toArray(File[]::new);
127         }
128     }
129 
130 }