Skip to content

Commit 7b6125f

Browse files
committed
HBX-3234: Improve the Maven hbm2orm Mojo
- Create an initial integration test for the TransformHbm Mojo Signed-off-by: Koen Aers <[email protected]>
1 parent c093a51 commit 7b6125f

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

maven/pom.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@
189189
</sources>
190190
</configuration>
191191
</execution>
192-
<!-- add the examples as test resources -->
192+
<!-- add the examples along with the other resources of the functional tests as test resources -->
193193
<execution>
194194
<id>add-test-resource</id>
195195
<phase>generate-test-resources</phase>
@@ -205,6 +205,9 @@
205205
<exclude>**/hibernate.properties</exclude>
206206
</excludes>
207207
</resource>
208+
<resource>
209+
<directory>src/functionalTest/resources</directory>
210+
</resource>
208211
</resources>
209212
</configuration>
210213
</execution>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.hibernate.tool.maven;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.io.File;
8+
import java.net.URL;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.util.Objects;
13+
14+
import org.apache.maven.cli.MavenCli;
15+
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.api.io.TempDir;
17+
18+
public class TransformHbmTestIT {
19+
20+
public static final String MVN_HOME = "maven.multiModuleProjectDirectory";
21+
22+
@TempDir
23+
private Path projectPath;
24+
25+
@Test
26+
public void testSimpleHbmTransformation() throws Exception {
27+
System.setProperty(MVN_HOME, projectPath.toAbsolutePath().toString());
28+
writePomFile();
29+
copyHbmFile();
30+
31+
runTransformHbmToOrm();
32+
}
33+
34+
private void writePomFile() throws Exception {
35+
File pomFile = new File(projectPath.toFile(), "pom.xml");
36+
assertFalse(pomFile.exists());
37+
Path pomPath = projectPath.resolve("pom.xml");
38+
Files.writeString(pomPath, simplePomContents);
39+
assertTrue(pomFile.exists());
40+
}
41+
42+
private void copyHbmFile() throws Exception {
43+
URL originUrl = TransformHbmTestIT.class.getResource("simple.hbm.xml");
44+
assertNotNull(originUrl);
45+
Path originPath = Paths.get(Objects.requireNonNull(originUrl).toURI());
46+
File destinationDir = new File(projectPath.toFile(), "src/main/resources/");
47+
assertTrue(destinationDir.mkdirs());
48+
File destinationFile = new File(destinationDir, "simple.hbm.xml");
49+
assertFalse(destinationFile.exists());
50+
Files.copy(originPath, destinationFile.toPath());
51+
assertTrue(destinationFile.exists());
52+
}
53+
54+
private void runTransformHbmToOrm() {
55+
File destinationDir = new File(projectPath.toFile(), "src/main/resources/");
56+
File ormXmlFile = new File(destinationDir, "simple.mapping.xml");
57+
assertFalse(ormXmlFile.exists());
58+
new MavenCli().doMain(
59+
new String[]{"org.hibernate.tool:hibernate-tools-maven:7.2.0.CR2:hbm2orm", "generate-sources"},
60+
projectPath.toAbsolutePath().toString(),
61+
null,
62+
null);
63+
assertTrue(ormXmlFile.exists());
64+
}
65+
66+
private static final String simplePomContents =
67+
"""
68+
<project>
69+
<modelVersion>4.0.0</modelVersion>
70+
<groupId>org.hibernate.tool.maven.test</groupId>
71+
<artifactId>simplest</artifactId>
72+
<version>0.1-SNAPSHOT</version>
73+
</project>
74+
""";
75+
76+
}
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>

0 commit comments

Comments
 (0)