Skip to content

Commit fe25269

Browse files
kingg22DavideD
authored andcommitted
[#2492] build(hr-test-containers): replace config of 'test' with a custom test task
configuration of properties is set up before execute test and print a summary after suite. Without use of project is cacheable
1 parent fe39e26 commit fe25269

File tree

2 files changed

+146
-65
lines changed

2 files changed

+146
-65
lines changed
Lines changed: 24 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,38 @@
1-
// Print a summary of the results of the tests (number of failures, successes and skipped)
2-
def loggingSummary(db, result, desc) {
3-
if ( !desc.parent ) { // will match the outermost suite
4-
def output = "${db} results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} passed, ${result.failedTestCount} failed, ${result.skippedTestCount} skipped)"
5-
def repeatLength = output.length() + 1
6-
logger.lifecycle '\n' + ('-' * repeatLength) + '\n' + output + '\n' + ('-' * repeatLength)
7-
}
8-
}
9-
10-
// Example:
11-
// gradle test -Pdb=MySQL
12-
test {
13-
def selectedDb
14-
15-
doFirst {
16-
selectedDb = project.hasProperty( 'db' )
17-
? project.properties['db']
18-
: 'PostgreSQL'
19-
systemProperty 'db', selectedDb
20-
}
21-
afterSuite { desc, result ->
22-
loggingSummary( selectedDb, result, desc )
23-
}
24-
}
1+
import org.hibernate.reactive.env.TestDbTask
252

26-
// Configuration for the tests
27-
tasks.withType( Test ).configureEach {
28-
defaultCharacterEncoding = "UTF-8"
29-
useJUnitPlatform()
30-
testLogging {
31-
showStandardStreams = project.hasProperty('showStandardOutput')
32-
showStackTraces = true
33-
exceptionFormat = 'full'
34-
displayGranularity = 1
35-
events = ['PASSED', 'FAILED', 'SKIPPED']
36-
}
37-
systemProperty 'docker', project.hasProperty( 'docker' ) ? 'true' : 'false'
38-
systemProperty 'org.hibernate.reactive.common.InternalStateAssertions.ENFORCE', 'true'
3+
def dbs = ['MariaDB', 'MySQL', 'PostgreSQL', 'DB2', 'CockroachDB', 'MSSQLServer', 'Oracle']
394

40-
if ( project.hasProperty( 'includeTests' ) ) {
41-
// Example: ./gradlew testAll -PincludeTests=DefaultPortTest
42-
filter {
43-
includeTestsMatching project.properties['includeTests'] ?: '*' as String
44-
}
5+
// create a task for each DB, now is static configure and not dynamic with rule pattern
6+
// and run the tests on the selected db
7+
// Example:
8+
// gradlew testDbMySQL testDbDB2
9+
dbs.each { db ->
10+
tasks.register( "testDb${db}", TestDbTask ) {
11+
dbName = db
12+
description = "Run tests for ${db}"
4513
}
4614
}
4715

48-
def createTestDbTask(dbName) {
49-
tasks.register( "testDb${dbName}", Test ) {
50-
description = "Run tests for ${dbName}"
51-
52-
doFirst() {
53-
systemProperty 'db', dbName
54-
}
55-
afterSuite { desc, result ->
56-
loggingSummary( dbName, result, desc )
57-
}
16+
// replace default task 'test' with custom TestDbTask to always configure with cache safe task
17+
tasks.replace( "test", TestDbTask ).configure {
18+
dbName = project.findProperty( "db" ) ?: "PostgreSQL"
19+
dockerEnabled = project.hasProperty( "docker" )
20+
if ( project.hasProperty( "includeTests" ) ) {
21+
includeTests = project.property( "includeTests" )
5822
}
23+
description = "Default test task using TestDbTask"
5924
}
6025

61-
// Rule to recognize calls to testDb<dbName>
62-
// and run the tests on the selected db
63-
// Example:
64-
// gradle testDbMySQL testDbDB2
65-
tasks.addRule( "Pattern testDb<id>" ) { String taskName ->
66-
if ( taskName.startsWith( "testDb" ) ) {
67-
def dbName = taskName.substring( "testDb".length() )
68-
createTestDbTask( dbName )
26+
// configure all testDbTask with docker and filter test
27+
tasks.withType( TestDbTask ).configureEach {
28+
dockerEnabled = project.hasProperty( 'docker' )
29+
if ( project.hasProperty( 'includeTests' ) ) {
30+
includeTests = project.property( 'includeTests' )
6931
}
7032
}
7133

72-
// The dbs we want to test when running testAll
73-
def dbs = ['MariaDB', 'MySQL', 'PostgreSQL', 'DB2', 'CockroachDB', 'MSSQLServer', 'Oracle']
74-
dbs.forEach { createTestDbTask it }
75-
34+
// task with all database
7635
tasks.register( "testAll", Test ) {
7736
description = "Run tests for ${dbs}"
78-
dependsOn = dbs.collect( [] as HashSet ) { db -> "testDb${db}" }
37+
dependsOn( dbs.collect { "testDb${it}" } )
7938
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package org.hibernate.reactive.env;
2+
3+
import org.gradle.api.tasks.CacheableTask;
4+
import org.gradle.api.tasks.Input;
5+
import org.gradle.api.tasks.Optional;
6+
import org.gradle.api.tasks.testing.Test;
7+
import org.gradle.api.tasks.testing.TestDescriptor;
8+
import org.gradle.api.tasks.testing.TestResult;
9+
10+
import groovy.lang.Closure;
11+
import org.jetbrains.annotations.NotNull;
12+
import org.jetbrains.annotations.Nullable;
13+
14+
@CacheableTask
15+
public abstract class TestDbTask extends Test {
16+
17+
@NotNull
18+
private String dbName = "PostgreSQL";
19+
private boolean dockerEnabled = false;
20+
@Nullable
21+
private String includeTests = null;
22+
private boolean showStandardStreams = false;
23+
24+
@Input
25+
@Optional
26+
@NotNull
27+
public String getDbName() {
28+
return dbName;
29+
}
30+
31+
public void setDbName(final @NotNull String dbName) {
32+
this.dbName = dbName;
33+
}
34+
35+
@Input
36+
public boolean isDockerEnabled() {
37+
return dockerEnabled;
38+
}
39+
40+
public void setDockerEnabled(final boolean dockerEnabled) {
41+
this.dockerEnabled = dockerEnabled;
42+
}
43+
44+
@Input
45+
@Optional
46+
@Nullable
47+
public String getIncludeTests() {
48+
return includeTests;
49+
}
50+
51+
public void setIncludeTests(final @Nullable String includeTests) {
52+
this.includeTests = includeTests;
53+
}
54+
55+
@Input
56+
public boolean isShowStandardStreams() {
57+
return showStandardStreams;
58+
}
59+
60+
public void setShowStandardStreams(boolean showStandardStreams) {
61+
this.showStandardStreams = showStandardStreams;
62+
}
63+
64+
public TestDbTask() {
65+
// Default logging configuration
66+
setDefaultCharacterEncoding( "UTF-8" );
67+
useJUnitPlatform();
68+
final var testLogging = getTestLogging();
69+
testLogging.setShowStandardStreams( showStandardStreams );
70+
testLogging.setShowStackTraces( true );
71+
testLogging.setExceptionFormat( "full" );
72+
testLogging.setDisplayGranularity( 1 );
73+
testLogging.events( "PASSED", "FAILED", "SKIPPED" );
74+
75+
// enforcing Hibernate internal state
76+
systemProperty( "org.hibernate.reactive.common.InternalStateAssertions.ENFORCE", "true" );
77+
78+
// Add afterSuite hook
79+
afterSuite( new Closure<Object>( this, this ) {
80+
public Object doCall(TestDescriptor desc, TestResult result) {
81+
logSummary( desc, result );
82+
return null;
83+
}
84+
} );
85+
}
86+
87+
@Override
88+
public void executeTests() {
89+
// Apply system properties before running
90+
systemProperty( "db", dbName );
91+
systemProperty( "docker", dockerEnabled ? "true" : "false" );
92+
getTestLogging().setShowStandardStreams( showStandardStreams );
93+
94+
if ( includeTests != null && !includeTests.isEmpty() ) {
95+
getFilter().includeTestsMatching( includeTests );
96+
}
97+
98+
super.executeTests();
99+
}
100+
101+
/**
102+
* Print a summary of the results of the tests (number of failures, successes and skipped)
103+
*
104+
* @param desc the test descriptor
105+
* @param result the test result
106+
*/
107+
private void logSummary(final @NotNull TestDescriptor desc, final @NotNull TestResult result) {
108+
if ( desc.getParent() == null ) { // outermost suite
109+
final var output = String.format(
110+
"%s results: %s (%d tests, %d passed, %d failed, %d skipped)",
111+
dbName,
112+
result.getResultType(),
113+
result.getTestCount(),
114+
result.getSuccessfulTestCount(),
115+
result.getFailedTestCount(),
116+
result.getSkippedTestCount()
117+
);
118+
final var line = "-".repeat( output.length() + 1 );
119+
getLogger().lifecycle( "\n{}\n{}\n{}", line, output, line );
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)