Skip to content

Commit 274cc2d

Browse files
committed
HHH-19879: Move Hibernate Tools' reveng module to Hibernate ORM and merge the relevant ant/gradle/maven plugins
- Add tests and integration tests for the Maven Mojos Signed-off-by: Koen Aers <koen.aers@gmail.com>
1 parent 682fc9a commit 274cc2d

File tree

25 files changed

+1121
-0
lines changed

25 files changed

+1121
-0
lines changed

tooling/hibernate-maven-plugin/hibernate-maven-plugin.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ plugins {
1111
}
1212

1313
description = 'Maven plugin to integrate aspects of Hibernate into your build.'
14+
def h2Version = '2.4.240'
1415

1516
sourceSets {
1617
intTest {
@@ -42,6 +43,8 @@ dependencies {
4243
intTestRuntimeOnly 'org.apache.maven:maven-compat:3.9.11'
4344
intTestRuntimeOnly 'org.apache.maven.resolver:maven-resolver-transport-http:1.9.24'
4445
intTestRuntimeOnly 'org.apache.maven.resolver:maven-resolver-connector-basic:1.9.24'
46+
intTestRuntimeOnly 'com.h2database:h2:' + h2Version
47+
// intTestRuntimeOnly 'org.slf4j:slf4j-simple:2.0.17'
4548
}
4649

4750
def releasePrepareTask = tasks.register("releasePrepare") {
@@ -104,6 +107,7 @@ publishingExtension.publications.named("publishedArtifacts") {
104107

105108
integrationTest {
106109
environment "hibernateVersion", project.version
110+
environment "h2Version", h2Version
107111
}
108112

109113
integrationTest.dependsOn ':hibernate-core:publishToMavenLocal'
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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.junit.jupiter.api.BeforeAll;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.io.TempDir;
11+
12+
import java.io.File;
13+
import java.nio.file.Files;
14+
import java.sql.Connection;
15+
import java.sql.DriverManager;
16+
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;
21+
22+
public class ExamplesTestIT {
23+
24+
public static final String MVN_HOME = "maven.multiModuleProjectDirectory";
25+
private static File baseFolder;
26+
// private static File localRepo;
27+
28+
private File projectFolder;
29+
private MavenCli mavenCli;
30+
31+
@TempDir
32+
private File tempFolder;
33+
34+
private String[] databaseCreationScript = new String[] {
35+
// This is the default database which can be overridden per test
36+
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))"
37+
};
38+
39+
@BeforeAll
40+
public static void beforeAll() throws Exception {
41+
// The needed resource for this test are put in place
42+
// in the 'baseFolder' (normally 'target/test-classes')
43+
// by the 'build-helper-maven-plugin' execution.
44+
// See the 'pom.xml'
45+
baseFolder = determineBaseFolder();
46+
// localRepo = new File(baseFolder.getParentFile(), "local-repo");
47+
}
48+
49+
@Test
50+
public void test5MinuteTutorial() throws Exception {
51+
prepareProject("5-minute-tutorial");
52+
assertNotGeneratedYet("Person.java");
53+
runGenerateSources();
54+
assertNumberOfGeneratedFiles(1);
55+
assertGeneratedContains("Person.java", "public class Person");
56+
}
57+
58+
@Test
59+
public void testJpaDefault() throws Exception {
60+
prepareProject("hbm2java/jpa-default");
61+
assertNotGeneratedYet("Person.java");
62+
runGenerateSources();
63+
assertNumberOfGeneratedFiles(1);
64+
assertGeneratedContains("Person.java","import jakarta.persistence.Entity;");
65+
}
66+
67+
@Test
68+
public void testNoAnnotations() throws Exception {
69+
prepareProject("hbm2java/no-annotations");
70+
assertNotGeneratedYet("Person.java");
71+
runGenerateSources();
72+
assertNumberOfGeneratedFiles(1);
73+
assertGeneratedDoesNotContain("Person.java", "import jakarta.persistence.Entity;");
74+
}
75+
76+
@Test
77+
public void testNoGenerics() throws Exception {
78+
databaseCreationScript = new String[] {
79+
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))",
80+
"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))"
82+
};
83+
prepareProject("hbm2java/no-generics");
84+
assertNotGeneratedYet("Person.java");
85+
runGenerateSources();
86+
assertNumberOfGeneratedFiles(2);
87+
assertGeneratedDoesNotContain("Person.java", "Set<Item>");
88+
}
89+
90+
@Test
91+
public void testOutputDirectory() throws Exception {
92+
System.setProperty("output.dir", "${project.basedir}/generated-classes");
93+
prepareProject("hbm2java/output-directory");
94+
File outputDirectory = new File(projectFolder, "generated-classes");
95+
File personFile = new File(outputDirectory, "Person.java");
96+
assertFalse(outputDirectory.exists());
97+
assertFalse(personFile.exists());
98+
runGenerateSources();
99+
assertEquals(1, outputDirectory.list().length); // 1 file is generated in 'generated-classes'
100+
assertTrue(personFile.exists()); // The Person.java file should have been generated
101+
}
102+
103+
@Test
104+
public void testTemplatePath() throws Exception {
105+
System.setProperty("template.dir", "${project.basedir}/templates");
106+
prepareProject("hbm2java/template-path");
107+
assertNotGeneratedYet("Person.java");
108+
runGenerateSources();
109+
assertNumberOfGeneratedFiles(1);
110+
assertGeneratedContains("Person.java", "// This is just an example of a custom template");
111+
}
112+
113+
@Test
114+
public void testUseGenerics() throws Exception {
115+
databaseCreationScript = new String[] {
116+
"create table PERSON (ID int not null, NAME varchar(20), primary key (ID))",
117+
"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))"
119+
};
120+
prepareProject("hbm2java/use-generics");
121+
assertNotGeneratedYet("Person.java");
122+
runGenerateSources();
123+
assertNumberOfGeneratedFiles(2);
124+
assertGeneratedContains("Person.java", "Set<Item>");
125+
}
126+
127+
private void prepareProject(String projectName) throws Exception {
128+
projectFolder = new File(baseFolder, projectName);
129+
assertTrue(projectFolder.exists());
130+
System.setProperty(MVN_HOME, projectFolder.getAbsolutePath());
131+
editPomFile(projectFolder);
132+
createHibernatePropertiesFile(projectFolder);
133+
createDatabase();
134+
}
135+
136+
private void createHibernatePropertiesFile(File projectFolder) throws Exception {
137+
File projectResourcesFolder = new File(projectFolder, "src/main/resources");
138+
projectResourcesFolder.mkdirs();
139+
File hibernatePropertiesFile = new File(projectResourcesFolder, "hibernate.properties");
140+
// assertFalse(hibernatePropertiesFile.exists());
141+
String hibernatePropertiesFileContents =
142+
"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";
148+
Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents);
149+
assertTrue(hibernatePropertiesFile.exists());
150+
}
151+
152+
private void runGenerateSources() {
153+
new MavenCli().doMain(
154+
new String[]{"generate-sources"},
155+
projectFolder.getAbsolutePath(),
156+
null,
157+
null);
158+
}
159+
160+
private void assertNotGeneratedYet(String fileName) {
161+
assertFalse(new File(projectFolder, "target/generated-sources/" + fileName).exists());
162+
}
163+
164+
private void assertGeneratedContains(String fileName, String contents) throws Exception {
165+
assertTrue(readGeneratedContents(fileName).contains(contents));
166+
}
167+
168+
private void assertGeneratedDoesNotContain(String fileName, String contents) throws Exception {
169+
assertFalse(readGeneratedContents(fileName).contains(contents));
170+
}
171+
172+
private void assertNumberOfGeneratedFiles(int amount) throws Exception {
173+
assertEquals(amount, new File(projectFolder, "target/generated-sources").list().length);
174+
}
175+
176+
private String readGeneratedContents(String fileName) throws Exception {
177+
File generatedPersonFile = new File(projectFolder, "target/generated-sources/" + fileName);
178+
assertTrue(generatedPersonFile.exists());
179+
return new String(Files.readAllBytes(generatedPersonFile.toPath()));
180+
}
181+
182+
private static File determineBaseFolder() throws Exception {
183+
return new File(ExamplesTestIT.class.getClassLoader().getResource("5-minute-tutorial/pom.xml").toURI())
184+
.getParentFile().getParentFile();
185+
}
186+
187+
private void createDatabase() throws Exception {
188+
File databaseFile = new File(tempFolder, "database/test.mv.db");
189+
assertFalse(databaseFile.exists());
190+
assertFalse(databaseFile.isFile());
191+
Connection connection = DriverManager.getConnection(constructJdbcConnectionString());
192+
Statement statement = connection.createStatement();
193+
for (String s : databaseCreationScript) {
194+
statement.execute(s);
195+
}
196+
statement.close();
197+
connection.close();
198+
assertTrue(databaseFile.exists());
199+
assertTrue(databaseFile.isFile());
200+
}
201+
202+
private String constructJdbcConnectionString() {
203+
return "jdbc:h2:" + tempFolder.getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
204+
}
205+
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+
217+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
- Have [H2 Sakila database](https://github.com/hibernate/sakila-h2) running
19+
- Issue one of the following commands from a command-line window opened in this folder:
20+
- `mvn generate-sources -Dh2.version=${h2.version} -Dproject.version=${hibernate.version}`
21+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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>five-minute-tutorial</artifactId>
24+
<version>0.0.1-SNAPSHOT</version>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>com.h2database</groupId>
29+
<artifactId>h2</artifactId>
30+
<version>${h2.version}</version>
31+
</dependency>
32+
</dependencies>
33+
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.hibernate.orm</groupId>
38+
<artifactId>hibernate-maven-plugin</artifactId>
39+
<version>${hibernate.version}</version>
40+
<executions>
41+
<execution>
42+
<id>Entity generation</id>
43+
<phase>generate-sources</phase>
44+
<goals>
45+
<goal>hbm2java</goal>
46+
</goals>
47+
</execution>
48+
</executions>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
53+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
############################################################################
2+
# Hibernate Tools, Tooling for your Hibernate Projects #
3+
# #
4+
# Copyright 2004-2025 Red Hat, Inc. #
5+
# #
6+
# Licensed under the Apache License, Version 2.0 (the "License"); #
7+
# you may not use this file except in compliance with the License. #
8+
# You may obtain a copy of the License at #
9+
# #
10+
# http://www.apache.org/licenses/LICENSE-2.0 #
11+
# #
12+
# Unless required by applicable law or agreed to in writing, software #
13+
# distributed under the License is distributed on an "AS IS" basis, #
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15+
# See the License for the specific language governing permissions and #
16+
# limitations under the License. #
17+
############################################################################
18+
hibernate.connection.driver_class=org.h2.Driver
19+
hibernate.connection.url=jdbc:h2:tcp://localhost/./sakila
20+
hibernate.connection.username=sa
21+
hibernate.default_catalog=SAKILA
22+
hibernate.default_schema=PUBLIC
23+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
- Have [H2 Sakila database](https://github.com/hibernate/sakila-h2) running
19+
- Issue the following commands from a command-line window opened in this folder:
20+
`mvn generate-sources -Dh2.version=${h2.version} -Dhibernate.version=${hibernate.version}`
21+

0 commit comments

Comments
 (0)