From 718dbf72d9e3554836a1585f417a2fb5f0e525ef Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Mon, 24 Nov 2025 17:12:17 +0100 Subject: [PATCH 1/4] HBX-3234: Improve the Maven hbm2orm Mojo - Create an initial integration test for the TransformHbm Mojo Signed-off-by: Koen Aers --- maven/pom.xml | 5 +- .../tool/maven/TransformHbmTestIT.java | 76 +++++++++++++++++++ .../org/hibernate/tool/maven/simple.hbm.xml | 29 +++++++ 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java create mode 100644 maven/src/functionalTest/resources/org/hibernate/tool/maven/simple.hbm.xml diff --git a/maven/pom.xml b/maven/pom.xml index 17946891de..1be0104baa 100644 --- a/maven/pom.xml +++ b/maven/pom.xml @@ -189,7 +189,7 @@ - + add-test-resource generate-test-resources @@ -205,6 +205,9 @@ **/hibernate.properties + + src/functionalTest/resources + diff --git a/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java b/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java new file mode 100644 index 0000000000..a276996338 --- /dev/null +++ b/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java @@ -0,0 +1,76 @@ +package org.hibernate.tool.maven; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Objects; + +import org.apache.maven.cli.MavenCli; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +public class TransformHbmTestIT { + + public static final String MVN_HOME = "maven.multiModuleProjectDirectory"; + + @TempDir + private Path projectPath; + + @Test + public void testSimpleHbmTransformation() throws Exception { + System.setProperty(MVN_HOME, projectPath.toAbsolutePath().toString()); + writePomFile(); + copyHbmFile(); + + runTransformHbmToOrm(); + } + + private void writePomFile() throws Exception { + File pomFile = new File(projectPath.toFile(), "pom.xml"); + assertFalse(pomFile.exists()); + Path pomPath = projectPath.resolve("pom.xml"); + Files.writeString(pomPath, simplePomContents); + assertTrue(pomFile.exists()); + } + + private void copyHbmFile() throws Exception { + URL originUrl = TransformHbmTestIT.class.getResource("simple.hbm.xml"); + assertNotNull(originUrl); + Path originPath = Paths.get(Objects.requireNonNull(originUrl).toURI()); + File destinationDir = new File(projectPath.toFile(), "src/main/resources/"); + assertTrue(destinationDir.mkdirs()); + File destinationFile = new File(destinationDir, "simple.hbm.xml"); + assertFalse(destinationFile.exists()); + Files.copy(originPath, destinationFile.toPath()); + assertTrue(destinationFile.exists()); + } + + private void runTransformHbmToOrm() { + File destinationDir = new File(projectPath.toFile(), "src/main/resources/"); + File ormXmlFile = new File(destinationDir, "simple.mapping.xml"); + assertFalse(ormXmlFile.exists()); + new MavenCli().doMain( + new String[]{"org.hibernate.tool:hibernate-tools-maven:7.2.0.CR2:hbm2orm", "generate-sources"}, + projectPath.toAbsolutePath().toString(), + null, + null); + assertTrue(ormXmlFile.exists()); + } + + private static final String simplePomContents = + """ + + 4.0.0 + org.hibernate.tool.maven.test + simplest + 0.1-SNAPSHOT + + """; + +} diff --git a/maven/src/functionalTest/resources/org/hibernate/tool/maven/simple.hbm.xml b/maven/src/functionalTest/resources/org/hibernate/tool/maven/simple.hbm.xml new file mode 100644 index 0000000000..c45e11bd01 --- /dev/null +++ b/maven/src/functionalTest/resources/org/hibernate/tool/maven/simple.hbm.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + \ No newline at end of file From 745137abd0d7a1e724722b6e8aed7b0761aedcd8 Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Tue, 25 Nov 2025 10:34:09 +0100 Subject: [PATCH 2/4] HBX-3234: Improve the Maven hbm2orm Mojo - Add default pretty printing of the resulting mapping.xml file Signed-off-by: Koen Aers --- .../tool/maven/TransformHbmTestIT.java | 33 ++++++++++++++++--- .../tool/maven/TransformHbmMojo.java | 15 +++++++-- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java b/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java index a276996338..4ed3d7d16a 100644 --- a/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java +++ b/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java @@ -12,22 +12,34 @@ import java.util.Objects; import org.apache.maven.cli.MavenCli; +import org.hibernate.tool.api.version.Version; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; public class TransformHbmTestIT { public static final String MVN_HOME = "maven.multiModuleProjectDirectory"; + private static File baseFolder; + private static File localRepo; @TempDir private Path projectPath; + @BeforeAll + public static void beforeAll() throws Exception { + // The needed resource for this test are put in place + // in the 'baseFolder' (normally 'target/test-classes') + // by the 'build-helper-maven-plugin' execution. + // See the 'pom.xml' + baseFolder = determineBaseFolder(); + localRepo = new File(baseFolder.getParentFile(), "local-repo"); + } @Test public void testSimpleHbmTransformation() throws Exception { System.setProperty(MVN_HOME, projectPath.toAbsolutePath().toString()); writePomFile(); - copyHbmFile(); - + copyHbmFile(); runTransformHbmToOrm(); } @@ -51,16 +63,29 @@ private void copyHbmFile() throws Exception { assertTrue(destinationFile.exists()); } - private void runTransformHbmToOrm() { + private void runTransformHbmToOrm() throws Exception { File destinationDir = new File(projectPath.toFile(), "src/main/resources/"); File ormXmlFile = new File(destinationDir, "simple.mapping.xml"); assertFalse(ormXmlFile.exists()); new MavenCli().doMain( - new String[]{"org.hibernate.tool:hibernate-tools-maven:7.2.0.CR2:hbm2orm", "generate-sources"}, + new String[]{ + "-Dmaven.repo.local=" + localRepo.getAbsolutePath(), + "org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm", + "generate-sources"}, projectPath.toAbsolutePath().toString(), null, null); + // Check the existaence of the transformed file assertTrue(ormXmlFile.exists()); + // Check if it's pretty printed + assertTrue(Files.readString(ormXmlFile.toPath()).contains("\n \n")); + } + + private static File determineBaseFolder() throws Exception { + URL classUrl = TransformHbmTestIT.class.getResource( + "/" + TransformHbmTestIT.class.getName().replace(".", "/") + ".class"); + return new File(classUrl.toURI()) + .getParentFile().getParentFile().getParentFile().getParentFile().getParentFile(); } private static final String simplePomContents = diff --git a/maven/src/main/java/org/hibernate/tool/maven/TransformHbmMojo.java b/maven/src/main/java/org/hibernate/tool/maven/TransformHbmMojo.java index 8276d8b5e4..173c15f4e9 100644 --- a/maven/src/main/java/org/hibernate/tool/maven/TransformHbmMojo.java +++ b/maven/src/main/java/org/hibernate/tool/maven/TransformHbmMojo.java @@ -48,6 +48,7 @@ import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.Marshaller; +import org.hibernate.tool.api.xml.XMLPrettyPrinter; @Mojo( name = "hbm2orm", @@ -58,6 +59,9 @@ public class TransformHbmMojo extends AbstractMojo { @Parameter(defaultValue = "${project.basedir}/src/main/resources") private File inputFolder; + @Parameter(defaultValue = "true") + private boolean format; + @Override public void execute() { MappingBinder mappingBinder = new MappingBinder( @@ -119,12 +123,19 @@ private void marshall(Marshaller marshaller, JaxbEntityMappingsImpl mappings, Fi getLog().info("Marshalling file: " + hbmXmlFile.getAbsolutePath() + " into " + mappingXmlFile.getAbsolutePath()); try { marshaller.marshal( mappings, mappingXmlFile ); + if (format) { + XMLPrettyPrinter.prettyPrintFile(mappingXmlFile); + } } catch (JAXBException e) { throw new RuntimeException( "Unable to marshall mapping JAXB representation to file `" + mappingXmlFile.getAbsolutePath() + "`", - e - ); + e); + } + catch (IOException e) { + throw new RuntimeException( + "Unable to format XML file `" + mappingXmlFile.getAbsolutePath() + "`", + e); } } From b0d134e3b97981e9ddf4b0c33e6498df6e70533d Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Tue, 25 Nov 2025 11:03:16 +0100 Subject: [PATCH 3/4] HBX-3234: Improve the Maven hbm2orm Mojo - Better determination of the base folder in TransformHbmTestIT Signed-off-by: Koen Aers --- .../tool/maven/TransformHbmTestIT.java | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java b/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java index 4ed3d7d16a..0ec7323aba 100644 --- a/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java +++ b/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java @@ -20,7 +20,6 @@ public class TransformHbmTestIT { public static final String MVN_HOME = "maven.multiModuleProjectDirectory"; - private static File baseFolder; private static File localRepo; @TempDir @@ -28,12 +27,7 @@ public class TransformHbmTestIT { @BeforeAll public static void beforeAll() throws Exception { - // The needed resource for this test are put in place - // in the 'baseFolder' (normally 'target/test-classes') - // by the 'build-helper-maven-plugin' execution. - // See the 'pom.xml' - baseFolder = determineBaseFolder(); - localRepo = new File(baseFolder.getParentFile(), "local-repo"); + localRepo = new File(determineBaseFolder().getParentFile(), "local-repo"); } @Test public void testSimpleHbmTransformation() throws Exception { @@ -75,17 +69,21 @@ private void runTransformHbmToOrm() throws Exception { projectPath.toAbsolutePath().toString(), null, null); - // Check the existaence of the transformed file + // Check the existence of the transformed file assertTrue(ormXmlFile.exists()); // Check if it's pretty printed assertTrue(Files.readString(ormXmlFile.toPath()).contains("\n
\n")); } private static File determineBaseFolder() throws Exception { - URL classUrl = TransformHbmTestIT.class.getResource( - "/" + TransformHbmTestIT.class.getName().replace(".", "/") + ".class"); - return new File(classUrl.toURI()) - .getParentFile().getParentFile().getParentFile().getParentFile().getParentFile(); + Class thisClass = TransformHbmTestIT.class; + URL classUrl = thisClass.getResource("/" + thisClass.getName().replace(".", "/") + ".class"); + assert classUrl != null; + File result = new File(classUrl.toURI()); + for (int i = 0; i < thisClass.getName().chars().filter(ch -> ch == '.').count() + 1; i++) { + result = result.getParentFile(); + } + return result; } private static final String simplePomContents = From d4613c5ceee5c98578ec00572ac90d753cbbf9df Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Wed, 26 Nov 2025 07:38:28 +0100 Subject: [PATCH 4/4] HBX-3234: Improve the Maven hbm2orm Mojo - Add an example for a simple transformation - Verify the proper execution of the example by adding an integration test for it Signed-off-by: Koen Aers --- maven/docs/examples/hbm2orm/.gitignore | 1 + .../examples/hbm2orm/simple-default/README.md | 19 ++++++++ .../examples/hbm2orm/simple-default/pom.xml | 26 +++++++++++ .../src/main/resources/simple.hbm.xml | 29 ++++++++++++ .../hibernate/tool/maven/ExamplesTestIT.java | 44 +++++++++++++++---- .../tool/maven/TransformHbmTestIT.java | 6 +-- 6 files changed, 114 insertions(+), 11 deletions(-) create mode 100644 maven/docs/examples/hbm2orm/.gitignore create mode 100644 maven/docs/examples/hbm2orm/simple-default/README.md create mode 100644 maven/docs/examples/hbm2orm/simple-default/pom.xml create mode 100644 maven/docs/examples/hbm2orm/simple-default/src/main/resources/simple.hbm.xml diff --git a/maven/docs/examples/hbm2orm/.gitignore b/maven/docs/examples/hbm2orm/.gitignore new file mode 100644 index 0000000000..34aa788d0e --- /dev/null +++ b/maven/docs/examples/hbm2orm/.gitignore @@ -0,0 +1 @@ +*.mapping.xml \ No newline at end of file diff --git a/maven/docs/examples/hbm2orm/simple-default/README.md b/maven/docs/examples/hbm2orm/simple-default/README.md new file mode 100644 index 0000000000..40d4212b98 --- /dev/null +++ b/maven/docs/examples/hbm2orm/simple-default/README.md @@ -0,0 +1,19 @@ + +To run this example: +- Have [Apache Maven](https://maven.apache.org) installed +- Issue the following commands from a command-line window opened in this folder: + `mvn org.hibernate.tool:hibernate-tools-maven:${hibernate.version}:hbm2orm` \ No newline at end of file diff --git a/maven/docs/examples/hbm2orm/simple-default/pom.xml b/maven/docs/examples/hbm2orm/simple-default/pom.xml new file mode 100644 index 0000000000..8f2461a63d --- /dev/null +++ b/maven/docs/examples/hbm2orm/simple-default/pom.xml @@ -0,0 +1,26 @@ + + + + 4.0.0 + + org.hibernate.tool.maven.test + hbm2orm-simple-default + 0.0.1-SNAPSHOT + + \ No newline at end of file diff --git a/maven/docs/examples/hbm2orm/simple-default/src/main/resources/simple.hbm.xml b/maven/docs/examples/hbm2orm/simple-default/src/main/resources/simple.hbm.xml new file mode 100644 index 0000000000..c45e11bd01 --- /dev/null +++ b/maven/docs/examples/hbm2orm/simple-default/src/main/resources/simple.hbm.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/maven/src/functionalTest/java/org/hibernate/tool/maven/ExamplesTestIT.java b/maven/src/functionalTest/java/org/hibernate/tool/maven/ExamplesTestIT.java index b11f0b2c2b..6579244062 100644 --- a/maven/src/functionalTest/java/org/hibernate/tool/maven/ExamplesTestIT.java +++ b/maven/src/functionalTest/java/org/hibernate/tool/maven/ExamplesTestIT.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import org.hibernate.tool.api.version.Version; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -12,10 +13,12 @@ import org.apache.maven.cli.MavenCli; import java.io.File; +import java.net.URL; import java.nio.file.Files; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; +import java.util.Objects; public class ExamplesTestIT { @@ -24,7 +27,6 @@ public class ExamplesTestIT { private static File localRepo; private File projectFolder; - private MavenCli mavenCli; @TempDir private File tempFolder; @@ -94,7 +96,7 @@ public void testOutputDirectory() throws Exception { assertFalse(outputDirectory.exists()); assertFalse(personFile.exists()); runGenerateSources(); - assertEquals(1, outputDirectory.list().length); // 1 file is generated in 'generated-classes' + assertEquals(1, Objects.requireNonNull(outputDirectory.list()).length); // 1 file is generated in 'generated-classes' assertTrue(personFile.exists()); // The Person.java file should have been generated } @@ -122,6 +124,17 @@ public void testUseGenerics() throws Exception { assertGeneratedContains("Person.java", "Set"); } + @Test + public void testHbm2OrmSimpleDefault() throws Exception { + projectFolder = new File(baseFolder, "hbm2orm/simple-default"); + File ormXmlFile = new File(projectFolder, "src/main/resources/simple.mapping.xml"); + assertFalse(ormXmlFile.exists()); + runMavenCommand("org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm"); + assertTrue(ormXmlFile.exists()); + String ormXmlContents = Files.readString( ormXmlFile.toPath() ); + assertTrue(ormXmlContents.contains("entity-mappings")); + } + private void prepareProject(String projectName) throws Exception { projectFolder = new File(baseFolder, projectName); assertTrue(projectFolder.exists()); @@ -132,7 +145,7 @@ private void prepareProject(String projectName) throws Exception { private void createHibernatePropertiesFile(File projectFolder) throws Exception { File projectResourcesFolder = new File(projectFolder, "src/main/resources"); - projectResourcesFolder.mkdirs(); + assertTrue(projectResourcesFolder.mkdirs()); File hibernatePropertiesFile = new File(projectResourcesFolder, "hibernate.properties"); assertFalse(hibernatePropertiesFile.exists()); String hibernatePropertiesFileContents = @@ -147,8 +160,14 @@ private void createHibernatePropertiesFile(File projectFolder) throws Exception } private void runGenerateSources() { + runMavenCommand("generate-sources"); + } + + private void runMavenCommand(String command) { new MavenCli().doMain( - new String[]{"-Dmaven.repo.local=" + localRepo.getAbsolutePath(), "generate-sources"}, + new String[]{ + "-Dmaven.repo.local=" + localRepo.getAbsolutePath(), + command}, projectFolder.getAbsolutePath(), null, null); @@ -166,8 +185,11 @@ private void assertGeneratedDoesNotContain(String fileName, String contents) thr assertFalse(readGeneratedContents(fileName).contains(contents)); } - private void assertNumberOfGeneratedFiles(int amount) throws Exception { - assertEquals(amount, new File(projectFolder, "target/generated-sources").list().length); + private void assertNumberOfGeneratedFiles(int amount) { + assertEquals( + amount, + Objects.requireNonNull( + new File(projectFolder, "target/generated-sources").list()).length); } private String readGeneratedContents(String fileName) throws Exception { @@ -177,8 +199,14 @@ private String readGeneratedContents(String fileName) throws Exception { } private static File determineBaseFolder() throws Exception { - return new File(ExamplesTestIT.class.getClassLoader().getResource("5-minute-tutorial/pom.xml").toURI()) - .getParentFile().getParentFile(); + Class thisClass = ExamplesTestIT.class; + URL classUrl = thisClass.getResource("/" + thisClass.getName().replace(".", "/") + ".class"); + assert classUrl != null; + File result = new File(classUrl.toURI()); + for (int i = 0; i < thisClass.getName().chars().filter(ch -> ch == '.').count() + 1; i++) { + result = result.getParentFile(); + } + return result; } private void createDatabase() throws Exception { diff --git a/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java b/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java index 0ec7323aba..1b5e86ca62 100644 --- a/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java +++ b/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java @@ -62,10 +62,10 @@ private void runTransformHbmToOrm() throws Exception { File ormXmlFile = new File(destinationDir, "simple.mapping.xml"); assertFalse(ormXmlFile.exists()); new MavenCli().doMain( - new String[]{ + new String[] { "-Dmaven.repo.local=" + localRepo.getAbsolutePath(), - "org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm", - "generate-sources"}, + "org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm" + }, projectPath.toAbsolutePath().toString(), null, null);