-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fix: onCreated should not return unwrapped datasource close: #96 * Update DataSourceMigrationRunnerSpec.groovy
- Loading branch information
Showing
5 changed files
with
108 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
flyway/src/test/groovy/io/micronaut/flyway/ApplicationContextSpecification.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.micronaut.flyway | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
abstract class ApplicationContextSpecification extends Specification implements ConfigurationFixture { | ||
|
||
@Shared | ||
@AutoCleanup | ||
ApplicationContext applicationContext = ApplicationContext.run(configuration) | ||
} |
41 changes: 41 additions & 0 deletions
41
flyway/src/test/groovy/io/micronaut/flyway/ConfigurationFixture.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.micronaut.flyway | ||
|
||
trait ConfigurationFixture { | ||
|
||
Map<String, Object> getConfiguration() { | ||
Map<String, Object> m = [:] | ||
if (specName) { | ||
m['spec.name'] = specName | ||
} | ||
m | ||
} | ||
|
||
Map<String, Object> getJpaConfiguration(List<String> packages = ['example.micronaut']) { | ||
[ | ||
'jpa.default.packages-to-scan' : packages, | ||
'jpa.default.properties.hibernate.hbm2ddl.auto': 'none', | ||
'jpa.default.properties.hibernate.show_sql' : true, | ||
] | ||
} | ||
|
||
Map<String, Object> getDataSourceConfiguration(String database, | ||
String dataSourceName = 'default', | ||
String driverClassName = 'org.h2.Driver', | ||
String username = 'sa', | ||
String password = '') { | ||
Map<String, Object> m = [:] | ||
m['datasources.' + dataSourceName + '.url'] = driverClassName == 'org.h2.Driver' ? h2JdbcUrl(database) : '' | ||
m['datasources.' + dataSourceName + '.username'] = username | ||
m['datasources.' + dataSourceName + '.password'] = password | ||
m['datasources.' + dataSourceName + '.driverClassName'] = driverClassName | ||
m | ||
} | ||
|
||
String h2JdbcUrl(String dbName) { | ||
'jdbc:h2:mem:' + dbName + ';DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE' | ||
} | ||
|
||
String getSpecName() { | ||
null | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
flyway/src/test/groovy/io/micronaut/flyway/DataSourceMigrationRunnerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.micronaut.flyway | ||
|
||
import io.micronaut.context.annotation.Primary | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.jdbc.DataSourceResolver | ||
|
||
import javax.inject.Singleton | ||
import javax.sql.DataSource | ||
|
||
class DataSourceMigrationRunnerSpec extends ApplicationContextSpecification { | ||
|
||
@Override | ||
String getSpecName() { | ||
'DataSourceMigrationRunnerSpec' | ||
} | ||
@Override | ||
Map<String, Object> getConfiguration() { | ||
super.configuration + | ||
getDataSourceConfiguration('datasourcemigrationrunner') + | ||
getJpaConfiguration(['example.micronaut']) | ||
} | ||
void "DataSourceMigrationRunner::onCreated returns wrapped Datasource"() { | ||
when: | ||
DataSource dataSource = applicationContext.getBean(DataSource) | ||
|
||
then: | ||
!(dataSource instanceof ReturnedByDataSourceResolver) | ||
} | ||
|
||
@Primary | ||
@Requires(property = 'spec.name', value = 'DataSourceMigrationRunnerSpec') | ||
@Singleton | ||
static class MockDataSourceResolver implements DataSourceResolver { | ||
@Override | ||
DataSource resolve(DataSource dataSource) { | ||
new ReturnedByDataSourceResolver(dataSource) | ||
} | ||
} | ||
|
||
static class ReturnedByDataSourceResolver implements DataSource { | ||
|
||
@Delegate | ||
private final DataSource dataSource | ||
|
||
ReturnedByDataSourceResolver(DataSource dataSource) { | ||
this.dataSource = dataSource | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters