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