diff --git a/RegexpHeader/Example2/extra-config-files.txt b/RegexpHeader/Example2/extra-config-files.txt
new file mode 100644
index 00000000..93925e67
--- /dev/null
+++ b/RegexpHeader/Example2/extra-config-files.txt
@@ -0,0 +1 @@
+java.header
\ No newline at end of file
diff --git a/extractor/src/main/java/com/example/extractor/CheckstyleExampleExtractor.java b/extractor/src/main/java/com/example/extractor/CheckstyleExampleExtractor.java
index de14e8ac..3952389d 100644
--- a/extractor/src/main/java/com/example/extractor/CheckstyleExampleExtractor.java
+++ b/extractor/src/main/java/com/example/extractor/CheckstyleExampleExtractor.java
@@ -112,6 +112,31 @@ public final class CheckstyleExampleExtractor {
*/
private static final int BUFFER_SIZE = 1024;
+ /**
+ * Number of expected arguments when processing a configuration file.
+ */
+ private static final int CONFIG_FILE_ARG_COUNT = 5;
+
+ /**
+ * Index of the "--config-file" flag in the argument array.
+ */
+ private static final int CONFIG_FILE_FLAG_INDEX = 1;
+
+ /**
+ * Index of the configuration file path in the argument array.
+ */
+ private static final int CONFIG_FILE_PATH_INDEX = 2;
+
+ /**
+ * Index of the output file path in the argument array.
+ */
+ private static final int CONFIG_OUTPUT_PATH_INDEX = 3;
+
+ /**
+ * Index of the projects output path in the argument array.
+ */
+ private static final int CONFIG_PROJECTS_PATH_INDEX = 4;
+
/**
* Private constructor to prevent instantiation of this utility class.
*/
@@ -147,6 +172,19 @@ public static void main(final String[] args) throws Exception {
// Output default projects list
outputDefaultProjectsList(projectsOutputPath);
}
+ else if (args.length == CONFIG_FILE_ARG_COUNT
+ && "--config-file".equals(args[CONFIG_FILE_FLAG_INDEX])) {
+ // New functionality: process external config file
+ final String configFilePath = args[CONFIG_FILE_PATH_INDEX];
+ final String configOutputPath = args[CONFIG_OUTPUT_PATH_INDEX];
+ final String projectsOutputPath = args[CONFIG_PROJECTS_PATH_INDEX];
+
+ // Process config file and generate output
+ processConfigFile(Paths.get(configFilePath), Paths.get(configOutputPath));
+
+ // Output default projects list
+ outputDefaultProjectsList(projectsOutputPath);
+ }
else {
// Functionality: process all examples
final String checkstyleRepoPath = args[0];
@@ -162,6 +200,33 @@ public static void main(final String[] args) throws Exception {
}
}
+ /**
+ * Processes a configuration file and generates an output file.
+ *
+ * @param configFile The path to the configuration file
+ * @param outputFile The path to the output file
+ * @throws Exception If an error occurs during processing
+ * @throws IOException If an error occurs during processing
+ */
+ public static void processConfigFile(
+ final Path configFile,
+ final Path outputFile)
+ throws Exception {
+ // Check if the config file exists
+ if (!Files.exists(configFile)) {
+ LOGGER.severe("Config file does not exist: " + configFile);
+ throw new IOException("Config file does not exist: " + configFile);
+ }
+
+ // Read the config file content
+ final String configContent = Files.readString(configFile, StandardCharsets.UTF_8);
+
+ // Write the config content to the output file
+ Files.writeString(outputFile, configContent, StandardCharsets.UTF_8);
+
+ LOGGER.info("Generated configuration at " + outputFile);
+ }
+
/**
* Writes the default projects list to the specified file.
*
diff --git a/extractor/src/test/java/com/example/extractor/MainsLauncherTest.java b/extractor/src/test/java/com/example/extractor/MainsLauncherTest.java
index 55f95e4d..35f26b5b 100644
--- a/extractor/src/test/java/com/example/extractor/MainsLauncherTest.java
+++ b/extractor/src/test/java/com/example/extractor/MainsLauncherTest.java
@@ -40,22 +40,20 @@
*/
class MainsLauncherTest {
- /**
- * The base path to the Checkstyle repository.
- */
+ /** The base path to the Checkstyle repository. */
private static final String CHECKSTYLE_REPO_PATH = "../.ci-temp/checkstyle";
- /**
- * Base path for Checkstyle checks package used in test resource files.
- */
+ /** Base path for Checkstyle checks package used in test resource files. */
private static final String CHECKSTYLE_CHECKS_BASE_PATH =
"com/puppycrawl/tools/checkstyle/checks";
- /**
- * The constant for default projects file.
- */
+ /** The constant for default projects file. */
private static final String DEFAULT_PROJECTS_FILE = "list-of-projects.yml";
+ /** Temporary folder for test files. */
+ @TempDir
+ Path temporaryFolder;
+
/**
* Tests the main method of CheckstyleExampleExtractor.
* This test ensures that the main method runs without throwing any exceptions.
@@ -122,7 +120,6 @@ private String loadDefaultProjectsList() throws IOException {
/**
* Tests the getTemplateFilePathForInputFile method.
- *
*/
@Test
void testGetTemplateFilePathForInputFile() throws Exception {
@@ -143,4 +140,84 @@ private String loadToString(final String filePath) throws IOException {
final byte[] encoded = Files.readAllBytes(Paths.get(filePath));
return new String(encoded, StandardCharsets.UTF_8);
}
+
+ /**
+ * Tests the external config files functionality.
+ */
+ @Test
+ void testMainWithExternalConfigFiles(@TempDir final Path tempDir) throws Exception {
+ // Create the 'config' directory inside the tempDir
+ final Path configDir = tempDir.resolve("config");
+ Files.createDirectories(configDir);
+
+ // Write the java.header file inside the 'config' directory
+ final String headerFileContent = """
+ ^// Copyright \\(C\\) (\\d\\d\\d\\d -)? 2004 MyCompany$
+ ^// All rights reserved$
+ """;
+
+ final Path headerFile = configDir.resolve("java.header");
+ Files.writeString(headerFile, headerFileContent);
+
+ // Write the config.xml file with ${execution.path} - this is key for DiffTool
+ final String configXmlContent = """
+
+
+
+
+
+
+
+
+ """;
+
+ final Path configXmlFile = tempDir.resolve("config.xml");
+ Files.writeString(configXmlFile, configXmlContent);
+
+ // Write the test input file (could be an empty Java file)
+ final String testFileContent = """
+ // Sample header content
+ // That should be matched
+
+ public class TestClass {
+ // class content
+ }
+ """;
+
+ final Path testFile = tempDir.resolve("TestClass.java");
+ Files.writeString(testFile, testFileContent);
+
+ // Define the output files for the extractor
+ final Path outputConfigFile = tempDir.resolve("output-config.xml");
+ final Path outputProjectsFile = tempDir.resolve("output-projects.yml");
+
+ // Modify the CheckstyleExampleExtractor to accept a config file
+ assertDoesNotThrow(() -> {
+ CheckstyleExampleExtractor.main(new String[]{
+ CHECKSTYLE_REPO_PATH,
+ "--config-file",
+ configXmlFile.toString(),
+ outputConfigFile.toString(),
+ outputProjectsFile.toString(),
+ });
+ });
+
+ // Verify that the output config file is created
+ assertTrue(Files.exists(outputConfigFile), "Config output file should be created");
+
+ // Load the generated config content
+ final String generatedConfigContent = Files.readString(outputConfigFile);
+
+ // Verify that the generated config includes the external file reference with execution.path
+ assertThat(generatedConfigContent)
+ .contains("");
+
+ // Verify that the projects file is created and not empty
+ assertTrue(Files.exists(outputProjectsFile), "Projects output file should be created");
+ final String generatedProjectsContent = Files.readString(outputProjectsFile);
+ assertFalse(generatedProjectsContent.isEmpty(), "Projects file should not be empty");
+ }
}