1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }