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.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.List;
28  import java.util.Set;
29  import java.util.stream.Collectors;
30  import java.util.stream.Stream;
31  
32  import org.junit.jupiter.api.Test;
33  
34  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35  import com.puppycrawl.tools.checkstyle.internal.utils.CheckUtil;
36  
37  public final class MetadataGeneratorUtilTest extends AbstractModuleTestSupport {
38  
39      private static final Set<String> MODULES_CONTAINING_NO_METADATA_FILE = Set.of(
40              "Checker",
41              "TreeWalker"
42      );
43  
44      @Override
45      public String getPackageLocation() {
46          return null;
47      }
48  
49      /**
50       * Verifies number of generated metadata modules match the number of Checkstyle modules.
51       * Also verifies whether every Checkstyle module contains description.
52       *
53       * <p>When running this test directly from an IDE, run
54       * {@code ./mvnw process-classes} first to generate the metadata files
55       * under {@code target/classes}.
56       *
57       * @throws Exception if an I/O error occurs while reading metadata files
58       */
59      @Test
60      public void testMetadataFilesGenerationAllFiles() throws Exception {
61          final Path sourceMetadataDirectory = Path.of(System.getProperty("user.dir"),
62                  "src", "main", "resources", "com", "puppycrawl", "tools", "checkstyle", "meta");
63          final List<Path> metadataFiles;
64          try (Stream<Path> fileStream = Files.walk(sourceMetadataDirectory)) {
65              metadataFiles = fileStream
66                      .filter(Files::isRegularFile)
67                      .filter(path -> !path.toString().endsWith(".properties"))
68                      .sorted()
69                      .toList();
70          }
71          final Set<String> metaFiles = metadataFiles.stream()
72                  .map(MetadataGeneratorUtilTest::getMetaFileName)
73                  .collect(Collectors.toCollection(LinkedHashSet::new));
74  
75          final Path outputMetadataDirectory = Path.of(System.getProperty("user.dir"),
76                  "target", "classes", "com", "puppycrawl", "tools", "checkstyle", "meta");
77          for (Path metadataFile : metadataFiles) {
78              final Path relativePath = sourceMetadataDirectory.relativize(metadataFile);
79              final Path outputMetadataFile = outputMetadataDirectory.resolve(relativePath);
80              assertWithMessage("Metadata differs in target/classes for %s", relativePath)
81                      .that(Files.readString(outputMetadataFile))
82                      .isEqualTo(Files.readString(metadataFile));
83          }
84          final Set<String> checkstyleModules =
85                  CheckUtil.getSimpleNames(CheckUtil.getCheckstyleModules())
86                          .stream()
87                          .sorted()
88                          .collect(Collectors.toCollection(LinkedHashSet::new));
89          checkstyleModules.removeAll(MODULES_CONTAINING_NO_METADATA_FILE);
90          assertWithMessage("Number of generated metadata files dont match with "
91                  + "number of checkstyle module")
92                  .that(metaFiles)
93                  .isEqualTo(checkstyleModules);
94      }
95  
96      /**
97       * Get meta file name from full file name.
98       *
99       * @param file file to process
100      * @return meta file name
101      */
102     private static String getMetaFileName(Path file) {
103         final String fileName = file.getFileName().toString();
104         final int lengthToOmit;
105         if (fileName.contains("Check")) {
106             lengthToOmit = "Check.xml".length();
107         }
108         else {
109             lengthToOmit = ".xml".length();
110         }
111         return fileName.substring(0, fileName.length() - lengthToOmit);
112     }
113 
114 }