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.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       * Generates metadata for checkstyle modules and verifies number of
55       * generated metadata modules match the number of checkstyle modules.
56       * Also verifies whether every checkstyle module contains description.
57       *
58       * @param systemOut wrapper for {@code System.out}
59       * @throws Exception if exception occurs during generating metadata or
60       *                   if an I/O error is thrown when accessing the starting f
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       * Get meta file name from full file name.
96       *
97       * @param file file to process
98       * @return meta file name
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 }