Skip to content

Commit c0a581a

Browse files
committed
HHH-19879: Move Hibernate Tools' reveng module to Hibernate ORM and merge the relevant ant/gradle/maven plugins
- Add exporter to generate mapping.xml files - Use above exporter in TransformHbmMojo - Add integration tests for TransformHbmMojo (TransformHbmTestIT and ExamplesTestIT) Signed-off-by: Koen Aers <[email protected]>
1 parent b2d3bbd commit c0a581a

File tree

17 files changed

+946
-159
lines changed

17 files changed

+946
-159
lines changed

tooling/hibernate-maven-plugin/src/intTest/java/org/hibernate/tool/maven/ExamplesTestIT.java

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,33 @@
44
*/
55
package org.hibernate.tool.maven;
66

7-
import org.apache.maven.cli.MavenCli;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertFalse;
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
10+
11+
import org.hibernate.tool.reveng.api.version.Version;
812
import org.junit.jupiter.api.BeforeAll;
913
import org.junit.jupiter.api.Test;
1014
import org.junit.jupiter.api.io.TempDir;
1115

16+
17+
import org.apache.maven.cli.MavenCli;
18+
1219
import java.io.File;
20+
import java.net.URL;
1321
import java.nio.file.Files;
1422
import java.sql.Connection;
1523
import java.sql.DriverManager;
1624
import java.sql.Statement;
17-
18-
import static org.junit.jupiter.api.Assertions.assertEquals;
19-
import static org.junit.jupiter.api.Assertions.assertFalse;
20-
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
import java.util.Objects;
2126

2227
public class ExamplesTestIT {
2328

2429
public static final String MVN_HOME = "maven.multiModuleProjectDirectory";
2530
private static File baseFolder;
26-
// private static File localRepo;
31+
private static File localRepo;
2732

2833
private File projectFolder;
29-
private MavenCli mavenCli;
3034

3135
@TempDir
3236
private File tempFolder;
@@ -43,7 +47,7 @@ public static void beforeAll() throws Exception {
4347
// by the 'build-helper-maven-plugin' execution.
4448
// See the 'pom.xml'
4549
baseFolder = determineBaseFolder();
46-
// localRepo = new File(baseFolder.getParentFile(), "local-repo");
50+
localRepo = new File(baseFolder.getParentFile(), "local-repo");
4751
}
4852

4953
@Test
@@ -78,7 +82,7 @@ public void testNoGenerics() throws Exception {
7882
databaseCreationScript = new String[] {
7983
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))",
8084
"create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " +
81-
" primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
85+
" primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
8286
};
8387
prepareProject("hbm2java/no-generics");
8488
assertNotGeneratedYet("Person.java");
@@ -96,7 +100,7 @@ public void testOutputDirectory() throws Exception {
96100
assertFalse(outputDirectory.exists());
97101
assertFalse(personFile.exists());
98102
runGenerateSources();
99-
assertEquals(1, outputDirectory.list().length); // 1 file is generated in 'generated-classes'
103+
assertEquals(1, Objects.requireNonNull(outputDirectory.list()).length); // 1 file is generated in 'generated-classes'
100104
assertTrue(personFile.exists()); // The Person.java file should have been generated
101105
}
102106

@@ -115,7 +119,7 @@ public void testUseGenerics() throws Exception {
115119
databaseCreationScript = new String[] {
116120
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))",
117121
"create table ITEM (ID int not null, NAME varchar(20), OWNER_ID int not null, " +
118-
" primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
122+
" primary key (ID), foreign key (OWNER_ID) references PERSON(ID))"
119123
};
120124
prepareProject("hbm2java/use-generics");
121125
assertNotGeneratedYet("Person.java");
@@ -124,34 +128,50 @@ public void testUseGenerics() throws Exception {
124128
assertGeneratedContains("Person.java", "Set<Item>");
125129
}
126130

131+
@Test
132+
public void testHbm2OrmSimpleDefault() throws Exception {
133+
projectFolder = new File(baseFolder, "hbm2orm/simple-default");
134+
File ormXmlFile = new File(projectFolder, "src/main/resources/simple.mapping.xml");
135+
assertFalse(ormXmlFile.exists());
136+
runMavenCommand("org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm");
137+
assertTrue(ormXmlFile.exists());
138+
String ormXmlContents = Files.readString( ormXmlFile.toPath() );
139+
assertTrue(ormXmlContents.contains("entity-mappings"));
140+
}
141+
127142
private void prepareProject(String projectName) throws Exception {
128143
projectFolder = new File(baseFolder, projectName);
129144
assertTrue(projectFolder.exists());
130145
System.setProperty(MVN_HOME, projectFolder.getAbsolutePath());
131-
editPomFile(projectFolder);
132146
createHibernatePropertiesFile(projectFolder);
133147
createDatabase();
134148
}
135149

136150
private void createHibernatePropertiesFile(File projectFolder) throws Exception {
137151
File projectResourcesFolder = new File(projectFolder, "src/main/resources");
138-
projectResourcesFolder.mkdirs();
152+
assertTrue(projectResourcesFolder.mkdirs());
139153
File hibernatePropertiesFile = new File(projectResourcesFolder, "hibernate.properties");
140-
// assertFalse(hibernatePropertiesFile.exists());
154+
assertFalse(hibernatePropertiesFile.exists());
141155
String hibernatePropertiesFileContents =
142156
"hibernate.connection.driver_class=org.h2.Driver\n" +
143-
"hibernate.connection.url=" + constructJdbcConnectionString() + "\n" +
144-
"hibernate.connection.username=\n" +
145-
"hibernate.connection.password=\n" +
146-
"hibernate.default_catalog=TEST\n" +
147-
"hibernate.default_schema=PUBLIC\n";
157+
"hibernate.connection.url=" + constructJdbcConnectionString() + "\n" +
158+
"hibernate.connection.username=\n" +
159+
"hibernate.connection.password=\n" +
160+
"hibernate.default_catalog=TEST\n" +
161+
"hibernate.default_schema=PUBLIC\n";
148162
Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents);
149163
assertTrue(hibernatePropertiesFile.exists());
150164
}
151165

152166
private void runGenerateSources() {
167+
runMavenCommand("generate-sources");
168+
}
169+
170+
private void runMavenCommand(String command) {
153171
new MavenCli().doMain(
154-
new String[]{"generate-sources"},
172+
new String[]{
173+
"-Dmaven.repo.local=" + localRepo.getAbsolutePath(),
174+
command},
155175
projectFolder.getAbsolutePath(),
156176
null,
157177
null);
@@ -169,8 +189,11 @@ private void assertGeneratedDoesNotContain(String fileName, String contents) thr
169189
assertFalse(readGeneratedContents(fileName).contains(contents));
170190
}
171191

172-
private void assertNumberOfGeneratedFiles(int amount) throws Exception {
173-
assertEquals(amount, new File(projectFolder, "target/generated-sources").list().length);
192+
private void assertNumberOfGeneratedFiles(int amount) {
193+
assertEquals(
194+
amount,
195+
Objects.requireNonNull(
196+
new File(projectFolder, "target/generated-sources").list()).length);
174197
}
175198

176199
private String readGeneratedContents(String fileName) throws Exception {
@@ -180,8 +203,14 @@ private String readGeneratedContents(String fileName) throws Exception {
180203
}
181204

182205
private static File determineBaseFolder() throws Exception {
183-
return new File(ExamplesTestIT.class.getClassLoader().getResource("5-minute-tutorial/pom.xml").toURI())
184-
.getParentFile().getParentFile();
206+
Class<?> thisClass = ExamplesTestIT.class;
207+
URL classUrl = thisClass.getResource("/" + thisClass.getName().replace(".", "/") + ".class");
208+
assert classUrl != null;
209+
File result = new File(classUrl.toURI());
210+
for (int i = 0; i < thisClass.getName().chars().filter(ch -> ch == '.').count() + 1; i++) {
211+
result = result.getParentFile();
212+
}
213+
return result;
185214
}
186215

187216
private void createDatabase() throws Exception {
@@ -203,15 +232,4 @@ private String constructJdbcConnectionString() {
203232
return "jdbc:h2:" + tempFolder.getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
204233
}
205234

206-
private void editPomFile(File projectFolder) throws Exception {
207-
System.out.println("Editing pom file");
208-
File pomFile = new File(projectFolder, "pom.xml");
209-
assertTrue(pomFile.exists());
210-
String pomFileContents = Files.readString( pomFile.toPath() );
211-
pomFileContents = pomFileContents
212-
.replace( "${h2.version}", System.getenv("h2Version") )
213-
.replace( "${hibernate.version}", System.getenv("hibernateVersion") );
214-
Files.writeString( pomFile.toPath(), pomFileContents );
215-
}
216-
217235
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.tool.maven;
6+
7+
import org.apache.maven.cli.MavenCli;
8+
import org.hibernate.tool.reveng.api.version.Version;
9+
import org.junit.jupiter.api.BeforeAll;
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.api.io.TempDir;
12+
13+
import java.io.File;
14+
import java.net.URL;
15+
import java.nio.file.Files;
16+
import java.nio.file.Path;
17+
import java.nio.file.Paths;
18+
import java.util.Objects;
19+
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
import static org.junit.jupiter.api.Assertions.assertTrue;
23+
24+
public class TransformHbmTestIT {
25+
26+
public static final String MVN_HOME = "maven.multiModuleProjectDirectory";
27+
private static File localRepo;
28+
29+
@TempDir
30+
private Path projectPath;
31+
32+
@BeforeAll
33+
public static void beforeAll() throws Exception {
34+
localRepo = new File(determineBaseFolder().getParentFile(), "local-repo");
35+
}
36+
@Test
37+
public void testSimpleHbmTransformation() throws Exception {
38+
System.setProperty(MVN_HOME, projectPath.toAbsolutePath().toString());
39+
writePomFile();
40+
copyHbmFile();
41+
runTransformHbmToOrm();
42+
}
43+
44+
private void writePomFile() throws Exception {
45+
File pomFile = new File(projectPath.toFile(), "pom.xml");
46+
assertFalse(pomFile.exists());
47+
Path pomPath = projectPath.resolve("pom.xml");
48+
Files.writeString(pomPath, simplePomContents);
49+
assertTrue(pomFile.exists());
50+
}
51+
52+
private void copyHbmFile() throws Exception {
53+
URL originUrl = TransformHbmTestIT.class.getResource("simple.hbm.xml");
54+
assertNotNull(originUrl);
55+
Path originPath = Paths.get(Objects.requireNonNull(originUrl).toURI());
56+
File destinationDir = new File(projectPath.toFile(), "src/main/resources/");
57+
assertTrue(destinationDir.mkdirs());
58+
File destinationFile = new File(destinationDir, "simple.hbm.xml");
59+
assertFalse(destinationFile.exists());
60+
Files.copy(originPath, destinationFile.toPath());
61+
assertTrue(destinationFile.exists());
62+
}
63+
64+
private void runTransformHbmToOrm() throws Exception {
65+
File destinationDir = new File(projectPath.toFile(), "src/main/resources/");
66+
File ormXmlFile = new File(destinationDir, "simple.mapping.xml");
67+
assertFalse(ormXmlFile.exists());
68+
new MavenCli().doMain(
69+
new String[] {
70+
"-Dmaven.repo.local=" + localRepo.getAbsolutePath(),
71+
"compile",
72+
"org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm"
73+
},
74+
projectPath.toAbsolutePath().toString(),
75+
null,
76+
null);
77+
// Check the existence of the transformed file
78+
assertTrue(ormXmlFile.exists());
79+
// Check if it's pretty printed
80+
assertTrue(Files.readString(ormXmlFile.toPath()).contains("\n <table name=\"Foo\"/>\n"));
81+
}
82+
83+
private static File determineBaseFolder() throws Exception {
84+
Class<?> thisClass = TransformHbmTestIT.class;
85+
URL classUrl = thisClass.getResource("/" + thisClass.getName().replace(".", "/") + ".class");
86+
assert classUrl != null;
87+
File result = new File(classUrl.toURI());
88+
for (int i = 0; i < thisClass.getName().chars().filter(ch -> ch == '.').count() + 1; i++) {
89+
result = result.getParentFile();
90+
}
91+
return result;
92+
}
93+
94+
private static final String simplePomContents =
95+
"""
96+
<project>
97+
<modelVersion>4.0.0</modelVersion>
98+
<groupId>org.hibernate.tool.maven.test</groupId>
99+
<artifactId>simplest</artifactId>
100+
<version>0.1-SNAPSHOT</version>
101+
</project>
102+
""";
103+
104+
}
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>
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 compile org.hibernate.tool:hibernate-tools-maven:${hibernate.version}:hbm2orm`

0 commit comments

Comments
 (0)