Skip to content

Commit d4613c5

Browse files
committed
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 <[email protected]>
1 parent b0d134e commit d4613c5

File tree

6 files changed

+114
-11
lines changed

6 files changed

+114
-11
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.mapping.xml
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!--
2+
~ Copyright 2004 - 2025 Red Hat, Inc.
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" basis,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
To run this example:
17+
- Have [Apache Maven](https://maven.apache.org) installed
18+
- Issue the following commands from a command-line window opened in this folder:
19+
`mvn org.hibernate.tool:hibernate-tools-maven:${hibernate.version}:hbm2orm`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2018 - 2025 Red Hat, Inc.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" basis,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
22+
<groupId>org.hibernate.tool.maven.test</groupId>
23+
<artifactId>hbm2orm-simple-default</artifactId>
24+
<version>0.0.1-SNAPSHOT</version>
25+
26+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2004 - 2025 Red Hat, Inc.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" basis,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<!DOCTYPE hibernate-mapping PUBLIC
18+
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
19+
"https://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
20+
<hibernate-mapping>
21+
22+
<class name="org.bar.Foo">
23+
<id name="id" type="long">
24+
<generator class="assigned"/>
25+
</id>
26+
<property name="name" type="string"/>
27+
</class>
28+
29+
</hibernate-mapping>

maven/src/functionalTest/java/org/hibernate/tool/maven/ExamplesTestIT.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66

7+
import org.hibernate.tool.api.version.Version;
78
import org.junit.jupiter.api.BeforeAll;
89
import org.junit.jupiter.api.Test;
910
import org.junit.jupiter.api.io.TempDir;
@@ -12,10 +13,12 @@
1213
import org.apache.maven.cli.MavenCli;
1314

1415
import java.io.File;
16+
import java.net.URL;
1517
import java.nio.file.Files;
1618
import java.sql.Connection;
1719
import java.sql.DriverManager;
1820
import java.sql.Statement;
21+
import java.util.Objects;
1922

2023
public class ExamplesTestIT {
2124

@@ -24,7 +27,6 @@ public class ExamplesTestIT {
2427
private static File localRepo;
2528

2629
private File projectFolder;
27-
private MavenCli mavenCli;
2830

2931
@TempDir
3032
private File tempFolder;
@@ -94,7 +96,7 @@ public void testOutputDirectory() throws Exception {
9496
assertFalse(outputDirectory.exists());
9597
assertFalse(personFile.exists());
9698
runGenerateSources();
97-
assertEquals(1, outputDirectory.list().length); // 1 file is generated in 'generated-classes'
99+
assertEquals(1, Objects.requireNonNull(outputDirectory.list()).length); // 1 file is generated in 'generated-classes'
98100
assertTrue(personFile.exists()); // The Person.java file should have been generated
99101
}
100102

@@ -122,6 +124,17 @@ public void testUseGenerics() throws Exception {
122124
assertGeneratedContains("Person.java", "Set<Item>");
123125
}
124126

127+
@Test
128+
public void testHbm2OrmSimpleDefault() throws Exception {
129+
projectFolder = new File(baseFolder, "hbm2orm/simple-default");
130+
File ormXmlFile = new File(projectFolder, "src/main/resources/simple.mapping.xml");
131+
assertFalse(ormXmlFile.exists());
132+
runMavenCommand("org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm");
133+
assertTrue(ormXmlFile.exists());
134+
String ormXmlContents = Files.readString( ormXmlFile.toPath() );
135+
assertTrue(ormXmlContents.contains("entity-mappings"));
136+
}
137+
125138
private void prepareProject(String projectName) throws Exception {
126139
projectFolder = new File(baseFolder, projectName);
127140
assertTrue(projectFolder.exists());
@@ -132,7 +145,7 @@ private void prepareProject(String projectName) throws Exception {
132145

133146
private void createHibernatePropertiesFile(File projectFolder) throws Exception {
134147
File projectResourcesFolder = new File(projectFolder, "src/main/resources");
135-
projectResourcesFolder.mkdirs();
148+
assertTrue(projectResourcesFolder.mkdirs());
136149
File hibernatePropertiesFile = new File(projectResourcesFolder, "hibernate.properties");
137150
assertFalse(hibernatePropertiesFile.exists());
138151
String hibernatePropertiesFileContents =
@@ -147,8 +160,14 @@ private void createHibernatePropertiesFile(File projectFolder) throws Exception
147160
}
148161

149162
private void runGenerateSources() {
163+
runMavenCommand("generate-sources");
164+
}
165+
166+
private void runMavenCommand(String command) {
150167
new MavenCli().doMain(
151-
new String[]{"-Dmaven.repo.local=" + localRepo.getAbsolutePath(), "generate-sources"},
168+
new String[]{
169+
"-Dmaven.repo.local=" + localRepo.getAbsolutePath(),
170+
command},
152171
projectFolder.getAbsolutePath(),
153172
null,
154173
null);
@@ -166,8 +185,11 @@ private void assertGeneratedDoesNotContain(String fileName, String contents) thr
166185
assertFalse(readGeneratedContents(fileName).contains(contents));
167186
}
168187

169-
private void assertNumberOfGeneratedFiles(int amount) throws Exception {
170-
assertEquals(amount, new File(projectFolder, "target/generated-sources").list().length);
188+
private void assertNumberOfGeneratedFiles(int amount) {
189+
assertEquals(
190+
amount,
191+
Objects.requireNonNull(
192+
new File(projectFolder, "target/generated-sources").list()).length);
171193
}
172194

173195
private String readGeneratedContents(String fileName) throws Exception {
@@ -177,8 +199,14 @@ private String readGeneratedContents(String fileName) throws Exception {
177199
}
178200

179201
private static File determineBaseFolder() throws Exception {
180-
return new File(ExamplesTestIT.class.getClassLoader().getResource("5-minute-tutorial/pom.xml").toURI())
181-
.getParentFile().getParentFile();
202+
Class<?> thisClass = ExamplesTestIT.class;
203+
URL classUrl = thisClass.getResource("/" + thisClass.getName().replace(".", "/") + ".class");
204+
assert classUrl != null;
205+
File result = new File(classUrl.toURI());
206+
for (int i = 0; i < thisClass.getName().chars().filter(ch -> ch == '.').count() + 1; i++) {
207+
result = result.getParentFile();
208+
}
209+
return result;
182210
}
183211

184212
private void createDatabase() throws Exception {

maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ private void runTransformHbmToOrm() throws Exception {
6262
File ormXmlFile = new File(destinationDir, "simple.mapping.xml");
6363
assertFalse(ormXmlFile.exists());
6464
new MavenCli().doMain(
65-
new String[]{
65+
new String[] {
6666
"-Dmaven.repo.local=" + localRepo.getAbsolutePath(),
67-
"org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm",
68-
"generate-sources"},
67+
"org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm"
68+
},
6969
projectPath.toAbsolutePath().toString(),
7070
null,
7171
null);

0 commit comments

Comments
 (0)