Skip to content

Commit 9842407

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 9842407

File tree

5 files changed

+269
-0
lines changed

5 files changed

+269
-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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@
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>
7383
</dependencies>
7484

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

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,16 @@
234234
<artifactId>maven-plugin-api</artifactId>
235235
<version>${maven-plugin-api.version}</version>
236236
</dependency>
237+
<dependency>
238+
<groupId>org.apache.maven</groupId>
239+
<artifactId>maven-embedder</artifactId>
240+
<version>${maven-core.version}</version>
241+
</dependency>
242+
<dependency>
243+
<groupId>org.apache.maven</groupId>
244+
<artifactId>maven-compat</artifactId>
245+
<version>${maven-core.version}</version>
246+
</dependency>
237247
<dependency>
238248
<groupId>org.apache.maven.plugin-tools</groupId>
239249
<artifactId>maven-plugin-annotations</artifactId>

0 commit comments

Comments
 (0)