Skip to content

Commit

Permalink
Issue #1: Create a application what parses xdocs with example config …
Browse files Browse the repository at this point in the history
…specified
  • Loading branch information
piyush kumar sadangi authored and piyush kumar sadangi committed Jun 18, 2024
1 parent 872737b commit 81313ae
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 19 deletions.
23 changes: 12 additions & 11 deletions extractor/src/main/java/com/example/extractor/ConfigSerializer.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package com.example.extractor;

import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
Expand All @@ -15,6 +16,16 @@

public class ConfigSerializer {

public static void serializeConfigToFile(String exampleFilePath, String templateFilePath, String outputFilePath) throws Exception {
String configContent = serializeConfigToString(exampleFilePath, templateFilePath);
Files.write(Path.of(outputFilePath), configContent.getBytes());
}

public static void serializeAllInOneConfigToFile(String[] exampleFilePaths, String templateFilePath, String outputFilePath) throws Exception {
String configContent = serializeAllInOneConfigToString(exampleFilePaths, templateFilePath);
Files.write(Path.of(outputFilePath), configContent.getBytes());
}

public static String serializeConfigToString(String exampleFilePath, String templateFilePath) throws Exception {
System.out.println("Loading configuration from example file...");
TestInputConfiguration testInputConfiguration = InlineConfigParser.parseWithXmlHeader(exampleFilePath);
Expand All @@ -29,11 +40,6 @@ public static String serializeConfigToString(String exampleFilePath, String temp
// Call replacePlaceholders to get the final config content
String configContent = TemplateProcessor.replacePlaceholders(template, moduleContent);

// Ensure the final content ends with a newline
if (!configContent.endsWith("\n")) {
configContent += "\n";
}

return configContent;
}

Expand Down Expand Up @@ -64,11 +70,6 @@ public static String serializeAllInOneConfigToString(String[] exampleFilePaths,
String template = new String(Files.readAllBytes(Path.of(templateFilePath)));
String configContent = TemplateProcessor.replacePlaceholders(template, combinedModuleContent);

// Ensure the final content ends with a newline
if (!configContent.endsWith("\n")) {
configContent += "\n";
}

return configContent;
}

Expand Down Expand Up @@ -207,4 +208,4 @@ private static String buildModuleContent(Configuration config, String indent) {
}
return builder.toString().trim();
}
}
}
16 changes: 8 additions & 8 deletions extractor/src/test/java/com/example/extractor/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void testParseConfigFromHeader() throws Exception {
public void testGeneratedConfigMatchesExpectedExample1() throws Exception {
String exampleFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Examples/Example1.java";
String templateFilePath = "src/main/resources/config-template-treewalker.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/config-example1-expected.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/expected-config-example1.xml";

String generatedContent = ConfigSerializer.serializeConfigToString(exampleFilePath, templateFilePath);
String expectedContent = loadToString(expectedFilePath);
Expand All @@ -43,7 +43,7 @@ public void testGeneratedConfigMatchesExpectedExample1() throws Exception {
public void testGeneratedConfigMatchesExpectedExample2() throws Exception {
String exampleFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Examples/Example2.java";
String templateFilePath = "src/main/resources/config-template-treewalker.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/config-example2-expected.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/expected-config-example2.xml";

String generatedContent = ConfigSerializer.serializeConfigToString(exampleFilePath, templateFilePath);
String expectedContent = loadToString(expectedFilePath);
Expand All @@ -55,7 +55,7 @@ public void testGeneratedConfigMatchesExpectedExample2() throws Exception {
public void testGeneratedConfigMatchesExpectedExample3() throws Exception {
String exampleFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Examples/Example3.java";
String templateFilePath = "src/main/resources/config-template-treewalker.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/config-example3-expected.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/expected-config-example3.xml";

String generatedContent = ConfigSerializer.serializeConfigToString(exampleFilePath, templateFilePath);
String expectedContent = loadToString(expectedFilePath);
Expand All @@ -67,7 +67,7 @@ public void testGeneratedConfigMatchesExpectedExample3() throws Exception {
public void testGeneratedConfigMatchesExpectedExample4() throws Exception {
String exampleFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Examples/Example4.java";
String templateFilePath = "src/main/resources/config-template-treewalker.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/config-example4-expected.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/expected-config-example4.xml";

String generatedContent = ConfigSerializer.serializeConfigToString(exampleFilePath, templateFilePath);
String expectedContent = loadToString(expectedFilePath);
Expand All @@ -79,7 +79,7 @@ public void testGeneratedConfigMatchesExpectedExample4() throws Exception {
public void testGeneratedConfigMatchesExpectedExample5() throws Exception {
String exampleFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Examples/Example5.java";
String templateFilePath = "src/main/resources/config-template-treewalker.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/config-example5-expected.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/expected-config-example5.xml";

String generatedContent = ConfigSerializer.serializeConfigToString(exampleFilePath, templateFilePath);
String expectedContent = loadToString(expectedFilePath);
Expand All @@ -91,7 +91,7 @@ public void testGeneratedConfigMatchesExpectedExample5() throws Exception {
public void testGeneratedConfigMatchesExpectedExample6() throws Exception {
String exampleFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Examples/Example6.java";
String templateFilePath = "src/main/resources/config-template-treewalker.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/config-example6-expected.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/expected-config-example6.xml";

String generatedContent = ConfigSerializer.serializeConfigToString(exampleFilePath, templateFilePath);
String expectedContent = loadToString(expectedFilePath);
Expand All @@ -103,7 +103,7 @@ public void testGeneratedConfigMatchesExpectedExample6() throws Exception {
public void testGeneratedConfigMatchesExpectedExample7() throws Exception {
String exampleFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Examples/Example7.java";
String templateFilePath = "src/main/resources/config-template-treewalker.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/config-example7-expected.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/expected-config-example7.xml";

String generatedContent = ConfigSerializer.serializeConfigToString(exampleFilePath, templateFilePath);
String expectedContent = loadToString(expectedFilePath);
Expand All @@ -124,7 +124,7 @@ public void testGeneratedConfigMatchesExpectedAllInOne() throws Exception {
};

String templateFilePath = "src/main/resources/config-template-treewalker.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/config-examples-all-in-one-expected.xml";
String expectedFilePath = "src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInName/Configs/expected-config-all-in-one.xml";

String generatedContent = ConfigSerializer.serializeAllInOneConfigToString(exampleFiles, templateFilePath);
String expectedContent = loadToString(expectedFilePath);
Expand Down

0 comments on commit 81313ae

Please sign in to comment.