Skip to content

Commit 8143dad

Browse files
committed
HBX-3050: Add a functional tests for the Maven generateJava mojo guarding the sanity of the 5 minute tutorial
Signed-off-by: Koen Aers <[email protected]>
1 parent e212bdf commit 8143dad

File tree

5 files changed

+290
-0
lines changed

5 files changed

+290
-0
lines changed
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.tool</groupId>
38+
<artifactId>hibernate-tools-maven</artifactId>
39+
<version>@project.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+

maven/pom.xml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@
7070
<artifactId>maven-plugin-annotations</artifactId>
7171
<scope>provided</scope>
7272
</dependency>
73+
<dependency>
74+
<groupId>org.apache.maven</groupId>
75+
<artifactId>maven-embedder</artifactId>
76+
<scope>test</scope>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.apache.maven</groupId>
80+
<artifactId>maven-compat</artifactId>
81+
<scope>test</scope>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.apache.maven.resolver</groupId>
85+
<artifactId>maven-resolver-transport-http</artifactId>
86+
<scope>test</scope>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.apache.maven.resolver</groupId>
90+
<artifactId>maven-resolver-connector-basic</artifactId>
91+
<scope>test</scope>
92+
</dependency>
7393
</dependencies>
7494

7595
<build>
@@ -156,6 +176,83 @@
156176
</execution>
157177
</executions>
158178
</plugin>
179+
<!-- run the integration tests -->
180+
<plugin>
181+
<groupId>org.apache.maven.plugins</groupId>
182+
<artifactId>maven-failsafe-plugin</artifactId>
183+
<executions>
184+
<execution>
185+
<goals>
186+
<goal>integration-test</goal>
187+
<goal>verify</goal>
188+
</goals>
189+
</execution>
190+
</executions>
191+
</plugin>
192+
<!-- add the integration test source folder -->
193+
<plugin>
194+
<groupId>org.codehaus.mojo</groupId>
195+
<artifactId>build-helper-maven-plugin</artifactId>
196+
<executions>
197+
<execution>
198+
<id>add-test-source</id>
199+
<phase>generate-test-sources</phase>
200+
<goals>
201+
<goal>add-test-source</goal>
202+
</goals>
203+
<configuration>
204+
<sources>
205+
<source>src/functionalTest/java</source>
206+
</sources>
207+
</configuration>
208+
</execution>
209+
<execution>
210+
<id>add-test-resource</id>
211+
<phase>generate-test-resources</phase>
212+
<goals>
213+
<goal>add-test-resource</goal>
214+
</goals>
215+
<configuration>
216+
<resources>
217+
<resource>
218+
<directory>docs/examples</directory>
219+
<excludes>
220+
<!-- we will not use the Sakila database for the purpose of the integration test -->
221+
<exclude>**/hibernate.properties</exclude>
222+
</excludes>
223+
</resource>
224+
</resources>
225+
</configuration>
226+
</execution>
227+
</executions>
228+
</plugin>
229+
<plugin>
230+
<groupId>org.apache.maven.plugins</groupId>
231+
<artifactId>maven-resources-plugin</artifactId>
232+
<executions>
233+
<execution>
234+
<id>filter-test-resources</id>
235+
<phase>process-test-resources</phase>
236+
<goals>
237+
<goal>testResources</goal>
238+
</goals>
239+
<configuration>
240+
<resources>
241+
<resource>
242+
<directory>${project.build.testOutputDirectory}</directory>
243+
<filtering>true</filtering>
244+
<includes>
245+
<include>**/pom.xml</include>
246+
</includes>
247+
</resource>
248+
</resources>
249+
<delimiters>
250+
<delimiter>@*@</delimiter>
251+
</delimiters>
252+
</configuration>
253+
</execution>
254+
</executions>
255+
</plugin>
159256
</plugins>
160257
</build>
161258

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.hibernate.tool.maven;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
import static org.junit.jupiter.api.Assertions.fail;
6+
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
10+
import org.apache.maven.cli.MavenCli;
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+
public class ExamplesTestIT {
19+
20+
public static final String MVN_HOME = "maven.multiModuleProjectDirectory";
21+
private static File baseFolder;
22+
23+
@BeforeAll
24+
public static void beforeAll() throws Exception {
25+
// The needed resource for this test are put in place
26+
// in the 'baseFolder' (normally 'target/test-classes')
27+
// by the 'build-helper-maven-plugin' execution.
28+
// See the 'pom.xml'
29+
baseFolder = determineBaseFolder();
30+
createDatabase();
31+
}
32+
33+
private MavenCli mavenCli;
34+
35+
@Test
36+
public void test5MinuteTutorial() throws Exception {
37+
File projectFolder = prepareProjectFolder("5-minute-tutorial");
38+
File generatedPersonFile = new File(projectFolder, "target/generated-sources/Person.java");
39+
assertFalse(generatedPersonFile.exists());
40+
new MavenCli().doMain(
41+
new String[]{"generate-sources"},
42+
projectFolder.getAbsolutePath(),
43+
null,
44+
null);
45+
assertTrue(generatedPersonFile.exists());
46+
String personFileContents = new String(Files.readAllBytes(generatedPersonFile.toPath()));
47+
assertTrue(personFileContents.contains("public class Person"));
48+
}
49+
50+
private File prepareProjectFolder(String projectName) throws Exception {
51+
File projectFolder = new File(baseFolder, projectName);
52+
assertTrue(projectFolder.exists());
53+
System.setProperty(MVN_HOME, projectFolder.getAbsolutePath());
54+
createHibernatePropertiesFile(projectFolder);
55+
return projectFolder;
56+
}
57+
58+
private void createHibernatePropertiesFile(File projectFolder) throws Exception {
59+
File projectResourcesFolder = new File(projectFolder, "src/main/resources");
60+
projectResourcesFolder.mkdirs();
61+
File hibernatePropertiesFile = new File(projectResourcesFolder, "hibernate.properties");
62+
assertFalse(hibernatePropertiesFile.exists());
63+
String hibernatePropertiesFileContents =
64+
"hibernate.connection.driver_class=org.h2.Driver\n" +
65+
"hibernate.connection.url=" + constructJdbcConnectionString() + "\n" +
66+
"hibernate.connection.username=\n" +
67+
"hibernate.connection.password=\n" +
68+
"hibernate.default_catalog=TEST\n" +
69+
"hibernate.default_schema=PUBLIC\n";
70+
Files.writeString(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents);
71+
assertTrue(hibernatePropertiesFile.exists());
72+
}
73+
74+
private static File determineBaseFolder() throws Exception {
75+
return new File(ExamplesTestIT.class.getClassLoader().getResource("5-minute-tutorial/pom.xml").toURI())
76+
.getParentFile().getParentFile();
77+
}
78+
79+
private static void createDatabase() throws Exception {
80+
File databaseFile = new File(baseFolder, "database/test.mv.db");
81+
assertFalse(databaseFile.exists());
82+
assertFalse(databaseFile.isFile());
83+
Connection connection = DriverManager.getConnection(constructJdbcConnectionString());
84+
Statement statement = connection.createStatement();
85+
statement.execute("create table PERSON (ID int not null, NAME varchar(20), primary key (ID))");
86+
statement.close();
87+
connection.close();
88+
assertTrue(databaseFile.exists());
89+
assertTrue(databaseFile.isFile());
90+
}
91+
92+
private static String constructJdbcConnectionString() {
93+
return "jdbc:h2:" + baseFolder.getAbsolutePath() + "/database/test;AUTO_SERVER=TRUE";
94+
}
95+
96+
}

pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
<oracle.version>19.3.0.0</oracle.version>
118118
<sqlserver.version>9.2.1.jre8</sqlserver.version>
119119
<jakarta.xml.bind-api.version>4.0.2</jakarta.xml.bind-api.version>
120+
<maven-resolver.version>1.9.24</maven-resolver.version>
120121

121122
<!-- Plugins not managed by the JBoss parent POM: -->
122123
<maven-plugin-api.version>3.9.11</maven-plugin-api.version>
@@ -234,10 +235,30 @@
234235
<artifactId>maven-plugin-api</artifactId>
235236
<version>${maven-plugin-api.version}</version>
236237
</dependency>
238+
<dependency>
239+
<groupId>org.apache.maven</groupId>
240+
<artifactId>maven-embedder</artifactId>
241+
<version>${maven-core.version}</version>
242+
</dependency>
243+
<dependency>
244+
<groupId>org.apache.maven</groupId>
245+
<artifactId>maven-compat</artifactId>
246+
<version>${maven-core.version}</version>
247+
</dependency>
237248
<dependency>
238249
<groupId>org.apache.maven.plugin-tools</groupId>
239250
<artifactId>maven-plugin-annotations</artifactId>
240251
<version>${maven-plugin-annotations.version}</version>
252+
</dependency>
253+
<dependency>
254+
<groupId>org.apache.maven.resolver</groupId>
255+
<artifactId>maven-resolver-transport-http</artifactId>
256+
<version>${maven-resolver.version}</version>
257+
</dependency>
258+
<dependency>
259+
<groupId>org.apache.maven.resolver</groupId>
260+
<artifactId>maven-resolver-connector-basic</artifactId>
261+
<version>${maven-resolver.version}</version>
241262
</dependency>
242263
<dependency>
243264
<groupId>org.freemarker</groupId>

0 commit comments

Comments
 (0)