From 002503dbd1ef7d3dd88a8debc67a1bdf24648b9c Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Mon, 2 Sep 2024 14:53:42 +0200 Subject: [PATCH] HBX-2916: Move XMLPrettyPrinter to the API and offer the ability to specify the XMLPrettyPrinterStrategy - Move class 'org.hibernate.tool.internal.xml.XMLPrettyPrinter' to 'org.hibernate.tool.api.xml.XMLPrettyPrinter' - Add a new test class 'org.hibernate.tool.api.xml.XMLPrettyPrinterTest' Signed-off-by: Koen Aers --- .../xml/XMLPrettyPrinter.java | 4 +- .../common/DefaultArtifactCollector.java | 2 +- .../xml/XMLPrettyPrinterStrategyFactory.java | 1 - .../tool/api/xml/XMLPrettyPrinterTest.java | 46 +++++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) rename orm/src/main/java/org/hibernate/tool/{internal => api}/xml/XMLPrettyPrinter.java (90%) create mode 100644 orm/src/test/java/org/hibernate/tool/api/xml/XMLPrettyPrinterTest.java diff --git a/orm/src/main/java/org/hibernate/tool/internal/xml/XMLPrettyPrinter.java b/orm/src/main/java/org/hibernate/tool/api/xml/XMLPrettyPrinter.java similarity index 90% rename from orm/src/main/java/org/hibernate/tool/internal/xml/XMLPrettyPrinter.java rename to orm/src/main/java/org/hibernate/tool/api/xml/XMLPrettyPrinter.java index fa23fe0b21..ed5dde0fde 100644 --- a/orm/src/main/java/org/hibernate/tool/internal/xml/XMLPrettyPrinter.java +++ b/orm/src/main/java/org/hibernate/tool/api/xml/XMLPrettyPrinter.java @@ -2,7 +2,7 @@ * Created on 17-Dec-2004 * */ -package org.hibernate.tool.internal.xml; +package org.hibernate.tool.api.xml; import java.io.File; import java.io.IOException; @@ -11,6 +11,8 @@ import java.nio.file.Files; import java.nio.file.Paths; +import org.hibernate.tool.internal.xml.XMLPrettyPrinterStrategyFactory; + /** * @author max * diff --git a/orm/src/main/java/org/hibernate/tool/internal/export/common/DefaultArtifactCollector.java b/orm/src/main/java/org/hibernate/tool/internal/export/common/DefaultArtifactCollector.java index 5bd91356a4..390762ff4f 100644 --- a/orm/src/main/java/org/hibernate/tool/internal/export/common/DefaultArtifactCollector.java +++ b/orm/src/main/java/org/hibernate/tool/internal/export/common/DefaultArtifactCollector.java @@ -10,7 +10,7 @@ import java.util.Set; import org.hibernate.tool.api.export.ArtifactCollector; -import org.hibernate.tool.internal.xml.XMLPrettyPrinter; +import org.hibernate.tool.api.xml.XMLPrettyPrinter; /** * Callback class that all exporters are given to allow better feedback and diff --git a/orm/src/main/java/org/hibernate/tool/internal/xml/XMLPrettyPrinterStrategyFactory.java b/orm/src/main/java/org/hibernate/tool/internal/xml/XMLPrettyPrinterStrategyFactory.java index 748cd1adc2..80eca95301 100644 --- a/orm/src/main/java/org/hibernate/tool/internal/xml/XMLPrettyPrinterStrategyFactory.java +++ b/orm/src/main/java/org/hibernate/tool/internal/xml/XMLPrettyPrinterStrategyFactory.java @@ -30,7 +30,6 @@ private static XMLPrettyPrinterStrategy loadFromSystemProperty() { throw new RuntimeException(e); } } - return null; } } diff --git a/orm/src/test/java/org/hibernate/tool/api/xml/XMLPrettyPrinterTest.java b/orm/src/test/java/org/hibernate/tool/api/xml/XMLPrettyPrinterTest.java new file mode 100644 index 0000000000..176be73829 --- /dev/null +++ b/orm/src/test/java/org/hibernate/tool/api/xml/XMLPrettyPrinterTest.java @@ -0,0 +1,46 @@ +package org.hibernate.tool.api.xml; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.File; +import java.io.PrintWriter; +import java.nio.file.Files; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +public class XMLPrettyPrinterTest { + + private static final String XML_BEFORE = "foobar"; + + private static final String XML_AFTER = + "\n" + + "\n" + + " foobar\n" + + "\n"; + + private static final String fileName = "foobarfile.xml"; + + @TempDir + private File tempDir; + + private File xmlFile = null; + + @BeforeEach + public void beforeEach() throws Exception { + xmlFile = new File(tempDir, fileName); + PrintWriter writer = new PrintWriter(xmlFile); + writer.print(XML_BEFORE); + writer.flush(); + writer.close(); + } + + @Test + public void testXmlPrettyPrintDefault() throws Exception { + XMLPrettyPrinter.prettyPrintFile(xmlFile); + String result = Files.readString(xmlFile.toPath()); + assertEquals(XML_AFTER, result); + } + +}