Skip to content

Commit 1dfa9d3

Browse files
committed
HBX-3080: Refactor the Gradle integration tests to factor out common code
Signed-off-by: Koen Aers <[email protected]>
1 parent 9b2bfdb commit 1dfa9d3

File tree

13 files changed

+576
-314
lines changed

13 files changed

+576
-314
lines changed

gradle/plugin/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ repositories {
2020

2121
dependencies {
2222

23-
implementation "org.hibernate.tool:hibernate-tools-orm:$version"
23+
implementation "org.hibernate.tool:hibernate-tools-orm:$projectVersion"
2424

2525
testImplementation libs.junit.jupiter
2626

2727
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
28+
testRuntimeOnly "com.h2database:h2:$h2Version"
2829
}
2930

3031
rootProject.ext.set("gradle.publish.key", System.env.PLUGIN_PORTAL_USERNAME)
@@ -56,6 +57,8 @@ tasks.register('functionalTest', Test) {
5657
testClassesDirs = sourceSets.functionalTest.output.classesDirs
5758
classpath = sourceSets.functionalTest.runtimeClasspath
5859
useJUnitPlatform()
60+
environment("HIBERNATE_TOOLS_VERSION", projectVersion);
61+
environment("H2_VERSION", h2Version);
5962
}
6063

6164
gradlePlugin.testSourceSets.add(sourceSets.functionalTest)
Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,51 @@
1+
/*
2+
* Hibernate Tools, Tooling for your Hibernate Projects
3+
*
4+
* Copyright 2024-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+
*/
118
package org.hibernate.tool.gradle;
219

320
import static org.junit.jupiter.api.Assertions.assertTrue;
421

522
import java.io.File;
6-
import java.io.IOException;
723
import java.nio.file.Files;
824

9-
import org.gradle.testkit.runner.BuildResult;
10-
import org.hibernate.tool.gradle.test.func.utils.FuncTestConstants;
11-
import org.hibernate.tool.gradle.test.func.utils.FuncTestTemplate;
25+
import org.hibernate.tool.it.gradle.TestTemplate;
26+
import org.junit.jupiter.api.BeforeEach;
1227
import org.junit.jupiter.api.Test;
1328

14-
class GenerateCfgTest extends FuncTestTemplate implements FuncTestConstants {
29+
class GenerateCfgTest extends TestTemplate {
1530

16-
@Test
17-
void testGenerateCfg() throws IOException {
18-
performTask("generateCfg", true);
19-
}
20-
21-
@Override
22-
public void verifyBuild(BuildResult buildResult) {
23-
try {
24-
File generatedSourcesFolder = new File(projectDir, "generated-sources");
25-
assertTrue(buildResult.getOutput().contains("Starting CFG export to directory: " + generatedSourcesFolder.getCanonicalPath()));
26-
assertTrue(generatedSourcesFolder.exists());
27-
assertTrue(generatedSourcesFolder.isDirectory());
28-
File cfgFile = new File(generatedSourcesFolder, "hibernate.cfg.xml");
29-
assertTrue(cfgFile.exists());
30-
assertTrue(cfgFile.isFile());
31-
String cfgContents = Files.readString(cfgFile.toPath());
32-
assertTrue(cfgContents.contains("<mapping resource=\"Foo.hbm.xml\"/>"));
33-
} catch (Exception e) {
34-
throw new RuntimeException(e);
35-
}
36-
}
31+
@BeforeEach
32+
public void beforeEach() {
33+
setGradleTaskToPerform("generateCfg");
34+
setDatabaseCreationScript(new String[] {
35+
"create table FOO (ID int not null, BAR varchar(20), primary key (ID))"
36+
});
37+
}
3738

38-
}
39+
@Test
40+
void testGenerateCfg() throws Exception {
41+
createProjectAndExecuteGradleCommand();
42+
File generatedSourcesFolder = new File(getProjectDir(), "app/generated-sources");
43+
assertTrue(getBuildResult().getOutput().contains("Starting CFG export to directory: "));
44+
File cfgFile = new File(generatedSourcesFolder, "hibernate.cfg.xml");
45+
assertTrue(cfgFile.exists());
46+
assertTrue(cfgFile.isFile());
47+
String cfgContents = Files.readString(cfgFile.toPath());
48+
assertTrue(cfgContents.contains("<mapping resource=\"Foo.hbm.xml\"/>"));
49+
}
50+
51+
}
Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,55 @@
1+
/*
2+
* Hibernate Tools, Tooling for your Hibernate Projects
3+
*
4+
* Copyright 2024-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+
*/
118
package org.hibernate.tool.gradle;
219

320
import static org.junit.jupiter.api.Assertions.assertTrue;
421

522
import java.io.File;
6-
import java.io.IOException;
723

8-
import org.gradle.testkit.runner.BuildResult;
9-
import org.hibernate.tool.gradle.test.func.utils.FuncTestConstants;
10-
import org.hibernate.tool.gradle.test.func.utils.FuncTestTemplate;
24+
import org.hibernate.tool.it.gradle.TestTemplate;
25+
import org.junit.jupiter.api.BeforeEach;
1126
import org.junit.jupiter.api.Test;
1227

13-
class GenerateDaoTest extends FuncTestTemplate implements FuncTestConstants {
28+
class GenerateDaoTest extends TestTemplate {
1429

15-
private static final String BUILD_FILE_HIBERNATE_TOOLS_SECTION =
16-
"hibernateTools {\n" +
17-
" packageName = 'foo.model'\n" +
18-
"}\n";
30+
@BeforeEach
31+
public void beforeEach() {
32+
setGradleTaskToPerform("generateDao");
33+
setDatabaseCreationScript(new String[] {
34+
"create table FOO (ID int not null, BAR varchar(20), primary key (ID))"
35+
});
36+
}
1937

20-
@Override
21-
public String getBuildFileHibernateToolsSection() {
22-
return BUILD_FILE_HIBERNATE_TOOLS_SECTION;
38+
@Test
39+
void testGenerateDao() throws Exception {
40+
setHibernateToolsExtensionSection(
41+
"hibernateTools { \n" +
42+
" packageName = 'foo.model'\n" +
43+
"}"
44+
);
45+
createProjectAndExecuteGradleCommand();
46+
File generatedSourcesFolder = new File(getProjectDir(), "app/generated-sources");
47+
assertTrue(getBuildResult().getOutput().contains("Starting DAO export to directory: "));
48+
assertTrue(generatedSourcesFolder.exists());
49+
assertTrue(generatedSourcesFolder.isDirectory());
50+
File fooFile = new File(generatedSourcesFolder, "foo/model/FooHome.java");
51+
assertTrue(fooFile.exists());
52+
assertTrue(fooFile.isFile());
2353
}
2454

25-
@Test
26-
void testGenerateJava() throws IOException {
27-
performTask("generateDao", true);
28-
}
29-
30-
@Override
31-
protected void verifyBuild(BuildResult buildResult) {
32-
try {
33-
File generatedSourcesFolder = new File(projectDir, "generated-sources");
34-
assertTrue(buildResult.getOutput().contains(
35-
"Starting DAO export to directory: " + generatedSourcesFolder.getCanonicalPath()));
36-
assertTrue(generatedSourcesFolder.exists());
37-
assertTrue(generatedSourcesFolder.isDirectory());
38-
File fooFile = new File(generatedSourcesFolder, "foo/model/FooHome.java");
39-
assertTrue(fooFile.exists());
40-
assertTrue(fooFile.isFile());
41-
} catch (Exception e) {
42-
throw new RuntimeException(e);
43-
}
44-
}
45-
46-
}
55+
}
Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,55 @@
1+
/*
2+
* Hibernate Tools, Tooling for your Hibernate Projects
3+
*
4+
* Copyright 2024-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+
*/
118
package org.hibernate.tool.gradle;
219

320
import static org.junit.jupiter.api.Assertions.assertTrue;
421

522
import java.io.File;
6-
import java.io.IOException;
723

8-
import org.gradle.testkit.runner.BuildResult;
9-
import org.hibernate.tool.gradle.test.func.utils.FuncTestConstants;
10-
import org.hibernate.tool.gradle.test.func.utils.FuncTestTemplate;
24+
import org.hibernate.tool.it.gradle.TestTemplate;
25+
import org.junit.jupiter.api.BeforeEach;
1126
import org.junit.jupiter.api.Test;
1227

13-
class GenerateHbmTest extends FuncTestTemplate implements FuncTestConstants {
28+
class GenerateHbmTest extends TestTemplate {
1429

15-
private static final String BUILD_FILE_HIBERNATE_TOOLS_SECTION =
16-
"hibernateTools {\n" +
17-
" packageName = 'foo.model'\n" +
18-
"}\n";
19-
20-
@Override
21-
public String getBuildFileHibernateToolsSection() {
22-
return BUILD_FILE_HIBERNATE_TOOLS_SECTION;
30+
@BeforeEach
31+
public void beforeEach() {
32+
setGradleTaskToPerform("generateHbm");
33+
setDatabaseCreationScript(new String[] {
34+
"create table FOO (ID int not null, BAR varchar(20), primary key (ID))"
35+
});
2336
}
2437

25-
@Test
26-
void testGenerateHbm() throws IOException {
27-
performTask("generateHbm", true);
28-
}
29-
30-
@Override
31-
protected void verifyBuild(BuildResult buildResult) {
32-
try {
33-
File generatedSourcesFolder = new File(projectDir, "generated-sources");
34-
assertTrue(buildResult.getOutput().contains(
35-
"Starting HBM export to directory: " + generatedSourcesFolder.getCanonicalPath()));
36-
assertTrue(generatedSourcesFolder.exists());
37-
assertTrue(generatedSourcesFolder.isDirectory());
38-
File fooFile = new File(generatedSourcesFolder, "foo/model/Foo.hbm.xml");
39-
assertTrue(fooFile.exists());
40-
assertTrue(fooFile.isFile());
41-
} catch (Exception e) {
42-
throw new RuntimeException(e);
43-
}
38+
@Test
39+
void testGenerateHbm() throws Exception {
40+
setHibernateToolsExtensionSection(
41+
"hibernateTools { \n" +
42+
" packageName = 'foo.model'\n" +
43+
"}"
44+
);
45+
createProjectAndExecuteGradleCommand();
46+
File generatedSourcesFolder = new File(getProjectDir(), "app/generated-sources");
47+
assertTrue(getBuildResult().getOutput().contains("Starting HBM export to directory: "));
48+
assertTrue(generatedSourcesFolder.exists());
49+
assertTrue(generatedSourcesFolder.isDirectory());
50+
File fooFile = new File(generatedSourcesFolder, "foo/model/Foo.hbm.xml");
51+
assertTrue(fooFile.exists());
52+
assertTrue(fooFile.isFile());
4453
}
4554

4655
}

0 commit comments

Comments
 (0)