Skip to content

Commit 145f946

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 01ac6e0 commit 145f946

File tree

5 files changed

+304
-0
lines changed

5 files changed

+304
-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: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,31 @@
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>
93+
<dependency>
94+
<groupId>org.slf4j</groupId>
95+
<artifactId>slf4j-simple</artifactId>
96+
<scope>test</scope>
97+
</dependency>
7398
</dependencies>
7499

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

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

pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@
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>
121+
<slf4j.version>2.0.17</slf4j.version>
120122

121123
<!-- Plugins not managed by the JBoss parent POM: -->
122124
<maven-plugin-api.version>3.9.11</maven-plugin-api.version>
@@ -234,10 +236,30 @@
234236
<artifactId>maven-plugin-api</artifactId>
235237
<version>${maven-plugin-api.version}</version>
236238
</dependency>
239+
<dependency>
240+
<groupId>org.apache.maven</groupId>
241+
<artifactId>maven-embedder</artifactId>
242+
<version>${maven-core.version}</version>
243+
</dependency>
244+
<dependency>
245+
<groupId>org.apache.maven</groupId>
246+
<artifactId>maven-compat</artifactId>
247+
<version>${maven-core.version}</version>
248+
</dependency>
237249
<dependency>
238250
<groupId>org.apache.maven.plugin-tools</groupId>
239251
<artifactId>maven-plugin-annotations</artifactId>
240252
<version>${maven-plugin-annotations.version}</version>
253+
</dependency>
254+
<dependency>
255+
<groupId>org.apache.maven.resolver</groupId>
256+
<artifactId>maven-resolver-transport-http</artifactId>
257+
<version>${maven-resolver.version}</version>
258+
</dependency>
259+
<dependency>
260+
<groupId>org.apache.maven.resolver</groupId>
261+
<artifactId>maven-resolver-connector-basic</artifactId>
262+
<version>${maven-resolver.version}</version>
241263
</dependency>
242264
<dependency>
243265
<groupId>org.freemarker</groupId>
@@ -305,6 +327,12 @@
305327
<artifactId>jboss-logging</artifactId>
306328
<version>${jboss-logging.version}</version>
307329
</dependency>
330+
<dependency>
331+
<groupId>org.slf4j</groupId>
332+
<artifactId>slf4j-simple</artifactId>
333+
<version>1.7.5</version>
334+
<scope>test</scope>
335+
</dependency>
308336
<dependency>
309337
<groupId>org.junit.jupiter</groupId>
310338
<artifactId>junit-jupiter-engine</artifactId>

0 commit comments

Comments
 (0)