-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 #43537 from yrodiere/datasource-more-tests
Datasource-related refactorings of tests (and some utils)
- Loading branch information
Showing
129 changed files
with
3,771 additions
and
1,769 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...roal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalDataSourceBuildUtil.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,33 @@ | ||
package io.quarkus.agroal.deployment; | ||
|
||
import jakarta.enterprise.inject.Default; | ||
|
||
import org.jboss.jandex.AnnotationInstance; | ||
|
||
import io.quarkus.agroal.DataSource; | ||
import io.quarkus.arc.processor.DotNames; | ||
import io.quarkus.datasource.common.runtime.DataSourceUtil; | ||
|
||
public final class AgroalDataSourceBuildUtil { | ||
private AgroalDataSourceBuildUtil() { | ||
} | ||
|
||
public static AnnotationInstance qualifier(String dataSourceName) { | ||
if (DataSourceUtil.isDefault(dataSourceName)) { | ||
return AnnotationInstance.builder(Default.class).build(); | ||
} else { | ||
return AnnotationInstance.builder(DataSource.class).value(dataSourceName).build(); | ||
} | ||
} | ||
|
||
public static AnnotationInstance[] qualifiers(String dataSourceName) { | ||
if (DataSourceUtil.isDefault(dataSourceName)) { | ||
return new AnnotationInstance[] { AnnotationInstance.builder(Default.class).build() }; | ||
} else { | ||
return new AnnotationInstance[] { | ||
AnnotationInstance.builder(DotNames.NAMED).value(dataSourceName).build(), | ||
AnnotationInstance.builder(DataSource.class).value(dataSourceName).build(), | ||
}; | ||
} | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
...t/java/io/quarkus/agroal/test/ConfigActiveFalseDefaultDatasourceDynamicInjectionTest.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,57 @@ | ||
package io.quarkus.agroal.test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import javax.sql.DataSource; | ||
|
||
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.InjectableInstance; | ||
import io.quarkus.runtime.configuration.ConfigurationException; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ConfigActiveFalseDefaultDatasourceDynamicInjectionTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.overrideConfigKey("quarkus.datasource.active", "false"); | ||
|
||
@Inject | ||
InjectableInstance<DataSource> dataSource; | ||
|
||
@Inject | ||
InjectableInstance<AgroalDataSource> agroalDataSource; | ||
|
||
@Test | ||
public void dataSource() { | ||
doTest(dataSource); | ||
} | ||
|
||
@Test | ||
public void agroalDataSource() { | ||
doTest(agroalDataSource); | ||
} | ||
|
||
private void doTest(InjectableInstance<? extends DataSource> instance) { | ||
// The bean is always available to be injected during static init | ||
// since we don't know whether the datasource will be active at runtime. | ||
// So the bean proxy cannot be null. | ||
var ds = instance.get(); | ||
assertThat(ds).isNotNull(); | ||
// However, any attempt to use it at runtime will fail. | ||
assertThatThrownBy(() -> ds.getConnection()) | ||
.isInstanceOf(RuntimeException.class) | ||
.cause() | ||
.isInstanceOf(ConfigurationException.class) | ||
.hasMessageContainingAll("Datasource '<default>' was deactivated through configuration properties.", | ||
"To solve this, avoid accessing this datasource at runtime, for instance by deactivating consumers (persistence units, ...).", | ||
"Alternatively, activate the datasource by setting configuration property 'quarkus.datasource.active'" | ||
+ " to 'true' and configure datasource '<default>'", | ||
"Refer to https://quarkus.io/guides/datasource for guidance."); | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
...st/java/io/quarkus/agroal/test/ConfigActiveFalseDefaultDatasourceStaticInjectionTest.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,47 @@ | ||
package io.quarkus.agroal.test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.sql.SQLException; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.inject.CreationException; | ||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ConfigActiveFalseDefaultDatasourceStaticInjectionTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.overrideConfigKey("quarkus.datasource.active", "false"); | ||
|
||
@Inject | ||
MyBean myBean; | ||
|
||
@Test | ||
public void test() { | ||
assertThatThrownBy(() -> myBean.useDatasource()) | ||
.isInstanceOf(CreationException.class) | ||
.hasMessageContainingAll("Datasource '<default>' was deactivated through configuration properties.", | ||
"To solve this, avoid accessing this datasource at runtime, for instance by deactivating consumers (persistence units, ...).", | ||
"Alternatively, activate the datasource by setting configuration property 'quarkus.datasource.active'" | ||
+ " to 'true' and configure datasource '<default>'", | ||
"Refer to https://quarkus.io/guides/datasource for guidance."); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyBean { | ||
@Inject | ||
DataSource ds; | ||
|
||
public void useDatasource() throws SQLException { | ||
ds.getConnection(); | ||
} | ||
} | ||
} |
91 changes: 0 additions & 91 deletions
91
...ployment/src/test/java/io/quarkus/agroal/test/ConfigActiveFalseDefaultDatasourceTest.java
This file was deleted.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
...est/java/io/quarkus/agroal/test/ConfigActiveFalseNamedDatasourceDynamicInjectionTest.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.agroal.test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import javax.sql.DataSource; | ||
|
||
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.InjectableInstance; | ||
import io.quarkus.runtime.configuration.ConfigurationException; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ConfigActiveFalseNamedDatasourceDynamicInjectionTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.overrideConfigKey("quarkus.datasource.users.active", "false") | ||
// We need at least one build-time property for the datasource, | ||
// otherwise it's considered unconfigured at build time... | ||
.overrideConfigKey("quarkus.datasource.users.db-kind", "h2"); | ||
|
||
@Inject | ||
@io.quarkus.agroal.DataSource("users") | ||
InjectableInstance<DataSource> dataSource; | ||
|
||
@Inject | ||
@io.quarkus.agroal.DataSource("users") | ||
InjectableInstance<AgroalDataSource> agroalDataSource; | ||
|
||
@Test | ||
public void dataSource() { | ||
doTest(dataSource); | ||
} | ||
|
||
@Test | ||
public void agroalDataSource() { | ||
doTest(agroalDataSource); | ||
} | ||
|
||
private void doTest(InjectableInstance<? extends DataSource> instance) { | ||
// The bean is always available to be injected during static init | ||
// since we don't know whether the datasource will be active at runtime. | ||
// So the bean cannot be null. | ||
var ds = instance.get(); | ||
assertThat(ds).isNotNull(); | ||
// However, any attempt to use it at runtime will fail. | ||
assertThatThrownBy(() -> ds.getConnection()) | ||
.isInstanceOf(RuntimeException.class) | ||
.cause() | ||
.isInstanceOf(ConfigurationException.class) | ||
.hasMessageContainingAll("Datasource 'users' was deactivated through configuration properties.", | ||
"To solve this, avoid accessing this datasource at runtime, for instance by deactivating consumers (persistence units, ...).", | ||
"Alternatively, activate the datasource by setting configuration property 'quarkus.datasource.\"users\".active'" | ||
+ " to 'true' and configure datasource 'users'", | ||
"Refer to https://quarkus.io/guides/datasource for guidance."); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...src/test/java/io/quarkus/agroal/test/ConfigActiveFalseNamedDatasourceHealthCheckTest.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,32 @@ | ||
package io.quarkus.agroal.test; | ||
|
||
import org.hamcrest.CoreMatchers; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class ConfigActiveFalseNamedDatasourceHealthCheckTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.overrideConfigKey("quarkus.datasource.health.enabled", "true") | ||
.overrideConfigKey("quarkus.datasource.users.active", "false") | ||
// We need at least one build-time property for the datasource, | ||
// otherwise it's considered unconfigured at build time... | ||
.overrideConfigKey("quarkus.datasource.users.db-kind", "h2") | ||
// this data source is broken, but will be deactivated, | ||
// so the overall check should pass | ||
.overrideConfigKey("quarkus.datasource.users.jdbc.url", "BROKEN"); | ||
|
||
@Test | ||
public void testDataSourceHealthCheckExclusion() { | ||
RestAssured.when().get("/q/health/ready") | ||
.then() | ||
.body("status", CoreMatchers.equalTo("UP")) | ||
// If the datasource is inactive, there should not be a health check | ||
.body("checks[0].data.\"users\"", CoreMatchers.nullValue()); | ||
} | ||
|
||
} |
Oops, something went wrong.