forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#37906 from yrodiere/datasource-unconfigu…
…red-basics Fix type/unremovable for reactive client CDI beans, make DataSource beans application-scoped, more consistent exceptions for unconfigured datasources
- Loading branch information
Showing
51 changed files
with
1,351 additions
and
349 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
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
41 changes: 41 additions & 0 deletions
41
extensions/agroal/deployment/src/test/java/io/quarkus/agroal/test/EagerStartupTest.java
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.quarkus.agroal.test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import jakarta.inject.Singleton; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.agroal.runtime.DataSources; | ||
import io.quarkus.arc.Arc; | ||
import io.quarkus.datasource.common.runtime.DataSourceUtil; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
/** | ||
* Check that datasources are created eagerly on application startup. | ||
* <p> | ||
* This has always been the case historically, so we want to keep it that way. | ||
*/ | ||
public class EagerStartupTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withConfigurationResource("base.properties"); | ||
|
||
@Test | ||
public void shouldStartEagerly() { | ||
var container = Arc.container(); | ||
var instanceHandle = container.instance(DataSources.class); | ||
// Check that the following call won't trigger a lazy initialization: | ||
// the DataSources bean must be eagerly initialized. | ||
assertThat(container.getActiveContext(Singleton.class).getState() | ||
.getContextualInstances().get(instanceHandle.getBean())) | ||
.as("Eagerly instantiated DataSources bean") | ||
.isNotNull(); | ||
// Check that the datasource has already been eagerly created. | ||
assertThat(instanceHandle.get().isDataSourceCreated(DataSourceUtil.DEFAULT_DATASOURCE_NAME)) | ||
.isTrue(); | ||
} | ||
|
||
} |
81 changes: 78 additions & 3 deletions
81
extensions/agroal/deployment/src/test/java/io/quarkus/agroal/test/NoConfigTest.java
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 |
---|---|---|
@@ -1,19 +1,94 @@ | ||
package io.quarkus.agroal.test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.sql.SQLException; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.agroal.api.AgroalDataSource; | ||
import io.quarkus.arc.Arc; | ||
import io.quarkus.runtime.configuration.ConfigurationException; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
/** | ||
* We should be able to start the application, even with no configuration at all. | ||
*/ | ||
public class NoConfigTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest(); | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
// The datasource won't be truly "unconfigured" if dev services are enabled | ||
.overrideConfigKey("quarkus.devservices.enabled", "false"); | ||
|
||
@Inject | ||
MyBean myBean; | ||
|
||
@Test | ||
public void testNoConfig() throws SQLException { | ||
// we should be able to start the application, even with no configuration at all | ||
public void dataSource_default() { | ||
DataSource ds = Arc.container().instance(DataSource.class).get(); | ||
|
||
// The default datasource is a bit special; | ||
// it's historically always been considered as "present" even if there was no explicit configuration. | ||
// So the bean will never be null. | ||
assertThat(ds).isNotNull(); | ||
// However, if unconfigured, any attempt to use it at runtime will fail. | ||
assertThatThrownBy(() -> ds.getConnection()) | ||
.isInstanceOf(ConfigurationException.class) | ||
.hasMessageContaining("quarkus.datasource.jdbc.url has not been defined"); | ||
} | ||
|
||
@Test | ||
public void agroalDataSource_default() { | ||
AgroalDataSource ds = Arc.container().instance(AgroalDataSource.class).get(); | ||
|
||
// The default datasource is a bit special; | ||
// it's historically always been considered as "present" even if there was no explicit configuration. | ||
// So the bean will never be null. | ||
assertThat(ds).isNotNull(); | ||
// However, if unconfigured, any attempt to use it at runtime will fail. | ||
assertThatThrownBy(() -> ds.getConnection()) | ||
.isInstanceOf(ConfigurationException.class) | ||
.hasMessageContaining("quarkus.datasource.jdbc.url has not been defined"); | ||
} | ||
|
||
@Test | ||
public void dataSource_named() { | ||
DataSource ds = Arc.container().instance(DataSource.class, | ||
new io.quarkus.agroal.DataSource.DataSourceLiteral("ds-1")).get(); | ||
// An unconfigured, named datasource has no corresponding bean. | ||
assertThat(ds).isNull(); | ||
} | ||
|
||
@Test | ||
public void agroalDataSource_named() { | ||
AgroalDataSource ds = Arc.container().instance(AgroalDataSource.class, | ||
new io.quarkus.agroal.DataSource.DataSourceLiteral("ds-1")).get(); | ||
// An unconfigured, named datasource has no corresponding bean. | ||
assertThat(ds).isNull(); | ||
} | ||
|
||
@Test | ||
public void injectedBean_default() { | ||
assertThatThrownBy(() -> myBean.useDataSource()) | ||
.isInstanceOf(ConfigurationException.class) | ||
.hasMessageContaining("quarkus.datasource.jdbc.url has not been defined"); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyBean { | ||
@Inject | ||
DataSource ds; | ||
|
||
public void useDataSource() throws SQLException { | ||
ds.getConnection(); | ||
} | ||
} | ||
} |
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
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
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
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
62 changes: 62 additions & 0 deletions
62
...src/test/java/io/quarkus/flyway/test/FlywayExtensionConfigEmptyDefaultDatasourceTest.java
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,62 @@ | ||
package io.quarkus.flyway.test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.inject.CreationException; | ||
import jakarta.enterprise.inject.Instance; | ||
import jakarta.inject.Inject; | ||
|
||
import org.flywaydb.core.Flyway; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class FlywayExtensionConfigEmptyDefaultDatasourceTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
// The datasource won't be truly "unconfigured" if dev services are enabled | ||
.overrideConfigKey("quarkus.devservices.enabled", "false"); | ||
|
||
@Inject | ||
Instance<Flyway> flywayForDefaultDatasource; | ||
|
||
@Inject | ||
MyBean myBean; | ||
|
||
@Test | ||
@DisplayName("If there is no config for the default datasource, the application should boot, but Flyway should be deactivated for that datasource") | ||
public void testBootSucceedsButFlywayDeactivated() { | ||
assertThatThrownBy(flywayForDefaultDatasource::get) | ||
.isInstanceOf(CreationException.class) | ||
.cause() | ||
.hasMessageContainingAll("Unable to find datasource '<default>' for Flyway", | ||
"Datasource '<default>' is not configured.", | ||
"To solve this, configure datasource '<default>'.", | ||
"Refer to https://quarkus.io/guides/datasource for guidance."); | ||
} | ||
|
||
@Test | ||
@DisplayName("If there is no config for the default datasource, the application should boot even if we inject a bean that depends on Liquibase, but actually using Liquibase should fail") | ||
public void testBootSucceedsWithInjectedBeanDependingOnFlywayButFlywayDeactivated() { | ||
assertThatThrownBy(() -> myBean.useFlyway()) | ||
.cause() | ||
.hasMessageContainingAll("Unable to find datasource '<default>' for Flyway", | ||
"Datasource '<default>' is not configured.", | ||
"To solve this, configure datasource '<default>'.", | ||
"Refer to https://quarkus.io/guides/datasource for guidance."); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyBean { | ||
@Inject | ||
Flyway flywayForDefaultDatasource; | ||
|
||
public void useFlyway() { | ||
flywayForDefaultDatasource.getConfiguration(); | ||
} | ||
} | ||
} |
Oops, something went wrong.