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/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/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 new file mode 100644 index 0000000000..1b5e86ca62 --- /dev/null +++ b/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java @@ -0,0 +1,99 @@ +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.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 localRepo; + + @TempDir + private Path projectPath; + + @BeforeAll + public static void beforeAll() throws Exception { + localRepo = new File(determineBaseFolder().getParentFile(), "local-repo"); + } + @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() 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[] { + "-Dmaven.repo.local=" + localRepo.getAbsolutePath(), + "org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm" + }, + projectPath.toAbsolutePath().toString(), + null, + null); + // 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 { + 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 = + """ + + 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 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); } }