From 2e9b6a6a46aa5f260030a47cec512168b18ad7bb Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Wed, 10 Sep 2025 17:29:54 +0200 Subject: [PATCH] HBX-3110: Add a functional test to guard the sanity of the Ant examples - Add a test verifying the '5-minute-tutorial' example Signed-off-by: Koen Aers --- ant/pom.xml | 9 ++ .../hibernate/tool/ant/ExamplesTestIT.java | 104 +++++++++++++++++- 2 files changed, 108 insertions(+), 5 deletions(-) diff --git a/ant/pom.xml b/ant/pom.xml index 1876630fbb..ebee82a56a 100644 --- a/ant/pom.xml +++ b/ant/pom.xml @@ -58,6 +58,12 @@ h2 test + + org.apache.ivy + ivy + 2.5.2 + test + @@ -115,6 +121,9 @@ docs/examples + + **/README.md + diff --git a/ant/src/it/java/org/hibernate/tool/ant/ExamplesTestIT.java b/ant/src/it/java/org/hibernate/tool/ant/ExamplesTestIT.java index 2e304bc24e..8c832d3126 100644 --- a/ant/src/it/java/org/hibernate/tool/ant/ExamplesTestIT.java +++ b/ant/src/it/java/org/hibernate/tool/ant/ExamplesTestIT.java @@ -1,17 +1,111 @@ package org.hibernate.tool.ant; -import static org.junit.jupiter.api.Assertions.assertTrue; - +import org.apache.tools.ant.DefaultLogger; +import org.apache.tools.ant.MagicNames; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.ProjectHelper; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import java.io.File; +import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.Files; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.Statement; + +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertFalse; public class ExamplesTestIT { + private static File baseFolder; + + @BeforeAll + public static void beforeAll() throws Exception { + baseFolder = determineBaseFolder(); + editIncludedXml(); + overwriteHibernateProperties(); + createDatabase(); + } + @Test - public void testSomethin() { - URL url = getClass().getClassLoader().getResource("5-minute-tutorial/build.xml"); - System.out.println(url); + public void test5MinuteTutorial() throws Exception { + File buildFile = new File(baseFolder, "5-minute-tutorial/build.xml"); + Project project = createProject(buildFile); + assertNotNull(project); + File personFile = new File(baseFolder, "5-minute-tutorial/generated/Person.java"); + assertFalse(personFile.exists()); + project.executeTarget("reveng"); + assertTrue(personFile.exists()); + } + + private Project createProject(File buildXmlFile) throws Exception { + Project result = new Project(); + ProjectHelper projectHelper = ProjectHelper.getProjectHelper(); + result.addReference(MagicNames.REFID_PROJECT_HELPER, projectHelper); + result.setBaseDir(buildXmlFile.getParentFile()); + result.addBuildListener(createConsoleLogger()); + projectHelper.parse(result, buildXmlFile); + return result; + } + + private DefaultLogger createConsoleLogger() { + DefaultLogger consoleLogger = new DefaultLogger(); + consoleLogger.setErrorPrintStream(System.err); + consoleLogger.setOutputPrintStream(System.out); + consoleLogger.setMessageOutputLevel(Project.MSG_INFO); + return consoleLogger; + } + + private static void editIncludedXml() throws Exception { + File xmlFile = new File(baseFolder, "common/included.xml"); + StringBuffer xmlFileContents = new StringBuffer( + new String(Files.readAllBytes(xmlFile.toPath()))); + int start = xmlFileContents.indexOf(""); + end = xmlFileContents.indexOf("