Skip to content

Commit 3c08c43

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 336b8df commit 3c08c43

File tree

5 files changed

+334
-5
lines changed

5 files changed

+334
-5
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</groupId>
38+
<artifactId>hibernate-tools-maven-plugin</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-plugin/pom.xml

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@
2727
<groupId>org.hibernate</groupId>
2828
<artifactId>hibernate-tools</artifactId>
2929
</dependency>
30+
<dependency>
31+
<groupId>org.junit.jupiter</groupId>
32+
<artifactId>junit-jupiter-engine</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.h2database</groupId>
37+
<artifactId>h2</artifactId>
38+
<scope>test</scope>
39+
</dependency>
3040
<!-- Maven Plugins -->
3141
<dependency>
3242
<groupId>org.apache.maven</groupId>
@@ -41,6 +51,31 @@
4151
<artifactId>maven-plugin-annotations</artifactId>
4252
<scope>provided</scope>
4353
</dependency>
54+
<dependency>
55+
<groupId>org.apache.maven</groupId>
56+
<artifactId>maven-embedder</artifactId>
57+
<scope>test</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.apache.maven</groupId>
61+
<artifactId>maven-compat</artifactId>
62+
<scope>test</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.apache.maven.resolver</groupId>
66+
<artifactId>maven-resolver-transport-http</artifactId>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.apache.maven.resolver</groupId>
71+
<artifactId>maven-resolver-connector-basic</artifactId>
72+
<scope>test</scope>
73+
</dependency>
74+
<dependency>
75+
<groupId>org.slf4j</groupId>
76+
<artifactId>slf4j-simple</artifactId>
77+
<scope>test</scope>
78+
</dependency>
4479
</dependencies>
4580

4681
<build>
@@ -93,6 +128,98 @@
93128
</execution>
94129
</executions>
95130
</plugin>
131+
<plugin>
132+
<artifactId>maven-invoker-plugin</artifactId>
133+
<version>${maven-invoker-plugin.version}</version>
134+
<configuration>
135+
<localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
136+
</configuration>
137+
<executions>
138+
<execution>
139+
<id>integration-test</id>
140+
<goals>
141+
<goal>install</goal>
142+
</goals>
143+
</execution>
144+
</executions>
145+
</plugin>
146+
<!-- run the integration tests -->
147+
<plugin>
148+
<groupId>org.apache.maven.plugins</groupId>
149+
<artifactId>maven-failsafe-plugin</artifactId>
150+
<executions>
151+
<execution>
152+
<goals>
153+
<goal>integration-test</goal>
154+
<goal>verify</goal>
155+
</goals>
156+
</execution>
157+
</executions>
158+
</plugin>
159+
<!-- add the integration test source folder -->
160+
<plugin>
161+
<groupId>org.codehaus.mojo</groupId>
162+
<artifactId>build-helper-maven-plugin</artifactId>
163+
<executions>
164+
<execution>
165+
<id>add-test-source</id>
166+
<phase>generate-test-sources</phase>
167+
<goals>
168+
<goal>add-test-source</goal>
169+
</goals>
170+
<configuration>
171+
<sources>
172+
<source>src/functionalTest/java</source>
173+
</sources>
174+
</configuration>
175+
</execution>
176+
<execution>
177+
<id>add-test-resource</id>
178+
<phase>generate-test-resources</phase>
179+
<goals>
180+
<goal>add-test-resource</goal>
181+
</goals>
182+
<configuration>
183+
<resources>
184+
<resource>
185+
<directory>docs/examples</directory>
186+
<excludes>
187+
<!-- we will not use the Sakila database for the purpose of the integration test -->
188+
<exclude>**/hibernate.properties</exclude>
189+
</excludes>
190+
</resource>
191+
</resources>
192+
</configuration>
193+
</execution>
194+
</executions>
195+
</plugin>
196+
<plugin>
197+
<groupId>org.apache.maven.plugins</groupId>
198+
<artifactId>maven-resources-plugin</artifactId>
199+
<executions>
200+
<execution>
201+
<id>filter-test-resources</id>
202+
<phase>process-test-resources</phase>
203+
<goals>
204+
<goal>testResources</goal>
205+
</goals>
206+
<configuration>
207+
<resources>
208+
<resource>
209+
<directory>${project.build.testOutputDirectory}</directory>
210+
<filtering>true</filtering>
211+
<includes>
212+
<include>**/pom.xml</include>
213+
</includes>
214+
</resource>
215+
</resources>
216+
<delimiters>
217+
<delimiter>@*@</delimiter>
218+
</delimiters>
219+
</configuration>
220+
</execution>
221+
</executions>
222+
</plugin>
96223
</plugins>
97224
</build>
98225

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.write(hibernatePropertiesFile.toPath(), hibernatePropertiesFileContents.getBytes());
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: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@
111111
<mysql.version>8.0.22</mysql.version>
112112
<oracle.version>19.3.0.0</oracle.version>
113113
<sqlserver.version>9.2.1.jre8</sqlserver.version>
114+
<maven-resolver.version>1.9.24</maven-resolver.version>
115+
<slf4j.version>2.0.17</slf4j.version>
114116

115117
<!-- Plugins not managed by the JBoss parent POM: -->
116118
<maven-plugin-api.version>3.9.11</maven-plugin-api.version>
@@ -171,11 +173,36 @@
171173
<artifactId>maven-plugin-api</artifactId>
172174
<version>${maven-plugin-api.version}</version>
173175
</dependency>
176+
<dependency>
177+
<groupId>org.apache.maven</groupId>
178+
<artifactId>maven-embedder</artifactId>
179+
<version>${maven-core.version}</version>
180+
</dependency>
181+
<dependency>
182+
<groupId>org.apache.maven</groupId>
183+
<artifactId>maven-compat</artifactId>
184+
<version>${maven-core.version}</version>
185+
</dependency>
174186
<dependency>
175187
<groupId>org.apache.maven.plugin-tools</groupId>
176188
<artifactId>maven-plugin-annotations</artifactId>
177189
<version>${maven-plugin-annotations.version}</version>
178190
</dependency>
191+
<dependency>
192+
<groupId>org.apache.maven.resolver</groupId>
193+
<artifactId>maven-resolver-transport-http</artifactId>
194+
<version>${maven-resolver.version}</version>
195+
</dependency>
196+
<dependency>
197+
<groupId>org.apache.maven.resolver</groupId>
198+
<artifactId>maven-resolver-connector-basic</artifactId>
199+
<version>${maven-resolver.version}</version>
200+
</dependency>
201+
<dependency>
202+
<groupId>org.freemarker</groupId>
203+
<artifactId>freemarker</artifactId>
204+
<version>${freemarker.version}</version>
205+
</dependency>
179206
<dependency>
180207
<groupId>com.microsoft.sqlserver</groupId>
181208
<artifactId>mssql-jdbc</artifactId>
@@ -215,11 +242,6 @@
215242
<artifactId>ant</artifactId>
216243
<version>${ant.version}</version>
217244
</dependency>
218-
<dependency>
219-
<groupId>org.freemarker</groupId>
220-
<artifactId>freemarker</artifactId>
221-
<version>${freemarker.version}</version>
222-
</dependency>
223245
<dependency>
224246
<groupId>org.hibernate</groupId>
225247
<artifactId>hibernate-core</artifactId>
@@ -251,6 +273,12 @@
251273
<version>${hsqldb.version}</version>
252274
<scope>test</scope>
253275
</dependency>
276+
<dependency>
277+
<groupId>org.slf4j</groupId>
278+
<artifactId>slf4j-simple</artifactId>
279+
<version>1.7.5</version>
280+
<scope>test</scope>
281+
</dependency>
254282
<dependency>
255283
<groupId>org.junit.jupiter</groupId>
256284
<artifactId>junit-jupiter-engine</artifactId>

0 commit comments

Comments
 (0)