|
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 |
25 | 2 |
|
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'] |
39 | 4 |
|
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}" |
45 | 13 | } |
46 | 14 | } |
47 | 15 |
|
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" ) |
58 | 22 | } |
| 23 | + description = "Default test task using TestDbTask" |
59 | 24 | } |
60 | 25 |
|
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' ) |
69 | 31 | } |
70 | 32 | } |
71 | 33 |
|
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 |
76 | 35 | tasks.register( "testAll", Test ) { |
77 | 36 | description = "Run tests for ${dbs}" |
78 | | - dependsOn = dbs.collect( [] as HashSet ) { db -> "testDb${db}" } |
| 37 | + dependsOn( dbs.collect { "testDb${it}" } ) |
79 | 38 | } |
0 commit comments