Skip to content

Commit f11e94d

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 200e1c4 commit f11e94d

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
@@ -66,6 +66,31 @@
6666
<artifactId>maven-plugin-annotations</artifactId>
6767
<scope>provided</scope>
6868
</dependency>
69+
<dependency>
70+
<groupId>org.apache.maven</groupId>
71+
<artifactId>maven-embedder</artifactId>
72+
<scope>test</scope>
73+
</dependency>
74+
<dependency>
75+
<groupId>org.apache.maven</groupId>
76+
<artifactId>maven-compat</artifactId>
77+
<scope>test</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.apache.maven.resolver</groupId>
81+
<artifactId>maven-resolver-transport-http</artifactId>
82+
<scope>test</scope>
83+
</dependency>
84+
<dependency>
85+
<groupId>org.apache.maven.resolver</groupId>
86+
<artifactId>maven-resolver-connector-basic</artifactId>
87+
<scope>test</scope>
88+
</dependency>
89+
<dependency>
90+
<groupId>org.slf4j</groupId>
91+
<artifactId>slf4j-simple</artifactId>
92+
<scope>test</scope>
93+
</dependency>
6994
</dependencies>
7095

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

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
<mysql.version>8.0.22</mysql.version>
118118
<oracle.version>19.3.0.0</oracle.version>
119119
<sqlserver.version>9.2.1.jre8</sqlserver.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>
@@ -228,10 +230,30 @@
228230
<artifactId>maven-plugin-api</artifactId>
229231
<version>${maven-plugin-api.version}</version>
230232
</dependency>
233+
<dependency>
234+
<groupId>org.apache.maven</groupId>
235+
<artifactId>maven-embedder</artifactId>
236+
<version>${maven-core.version}</version>
237+
</dependency>
238+
<dependency>
239+
<groupId>org.apache.maven</groupId>
240+
<artifactId>maven-compat</artifactId>
241+
<version>${maven-core.version}</version>
242+
</dependency>
231243
<dependency>
232244
<groupId>org.apache.maven.plugin-tools</groupId>
233245
<artifactId>maven-plugin-annotations</artifactId>
234246
<version>${maven-plugin-annotations.version}</version>
247+
</dependency>
248+
<dependency>
249+
<groupId>org.apache.maven.resolver</groupId>
250+
<artifactId>maven-resolver-transport-http</artifactId>
251+
<version>${maven-resolver.version}</version>
252+
</dependency>
253+
<dependency>
254+
<groupId>org.apache.maven.resolver</groupId>
255+
<artifactId>maven-resolver-connector-basic</artifactId>
256+
<version>${maven-resolver.version}</version>
235257
</dependency>
236258
<dependency>
237259
<groupId>org.freemarker</groupId>
@@ -289,6 +311,12 @@
289311
<artifactId>jboss-logging</artifactId>
290312
<version>${jboss-logging.version}</version>
291313
</dependency>
314+
<dependency>
315+
<groupId>org.slf4j</groupId>
316+
<artifactId>slf4j-simple</artifactId>
317+
<version>1.7.5</version>
318+
<scope>test</scope>
319+
</dependency>
292320
<dependency>
293321
<groupId>org.junit.jupiter</groupId>
294322
<artifactId>junit-jupiter-engine</artifactId>

0 commit comments

Comments
 (0)