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.meta;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.util.LinkedHashSet;
27 import java.util.Set;
28 import java.util.stream.Collectors;
29 import java.util.stream.Stream;
30
31 import org.itsallcode.io.Capturable;
32 import org.itsallcode.junit.sysextensions.SystemOutGuard;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35
36 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
37 import com.puppycrawl.tools.checkstyle.internal.utils.CheckUtil;
38
39 @ExtendWith(SystemOutGuard.class)
40 public final class MetadataGeneratorUtilTest extends AbstractModuleTestSupport {
41
42 private final Set<String> modulesContainingNoMetadataFile = Set.of(
43 "Checker",
44 "TreeWalker",
45 "ClassAndPropertiesSettersJavadocScraper"
46 );
47
48 @Override
49 protected String getPackageLocation() {
50 return null;
51 }
52
53
54
55
56
57
58
59
60
61
62 @Test
63 public void testMetadataFilesGenerationAllFiles(@SystemOutGuard.SysOut Capturable systemOut)
64 throws Exception {
65 systemOut.captureMuted();
66
67 MetadataGeneratorUtil.generate(System.getProperty("user.dir")
68 + "/src/main/java/com/puppycrawl/tools/checkstyle",
69 "checks", "filters", "filefilters");
70
71 final Set<String> metaFiles;
72 try (Stream<Path> fileStream = Files.walk(
73 Path.of(System.getProperty("user.dir") + "/src/main/resources/com/puppycrawl"
74 + "/tools/checkstyle/meta"))) {
75 metaFiles = fileStream
76 .filter(Files::isRegularFile)
77 .filter(path -> !path.toString().endsWith(".properties"))
78 .map(MetadataGeneratorUtilTest::getMetaFileName)
79 .sorted()
80 .collect(Collectors.toCollection(LinkedHashSet::new));
81 }
82 final Set<String> checkstyleModules =
83 CheckUtil.getSimpleNames(CheckUtil.getCheckstyleModules())
84 .stream()
85 .sorted()
86 .collect(Collectors.toCollection(LinkedHashSet::new));
87 checkstyleModules.removeAll(modulesContainingNoMetadataFile);
88 assertWithMessage("Number of generated metadata files dont match with "
89 + "number of checkstyle module")
90 .that(metaFiles)
91 .isEqualTo(checkstyleModules);
92 }
93
94
95
96
97
98
99
100 private static String getMetaFileName(Path file) {
101 final String fileName = file.getFileName().toString();
102 final int lengthToOmit;
103 if (fileName.contains("Check")) {
104 lengthToOmit = "Check.xml".length();
105 }
106 else {
107 lengthToOmit = ".xml".length();
108 }
109 return fileName.substring(0, fileName.length() - lengthToOmit);
110 }
111 }