-
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.
Refactor datasource tests related to active/inactive
So that further changes are shorter. This is only a refactoring and doesn't change the test expectations.
- Loading branch information
Showing
48 changed files
with
1,500 additions
and
1,314 deletions.
There are no files selected for viewing
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."); | ||
} | ||
} |
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."); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...test/java/io/quarkus/agroal/test/ConfigActiveFalseNamedDatasourceStaticInjectionTest.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,51 @@ | ||
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 ConfigActiveFalseNamedDatasourceStaticInjectionTest { | ||
|
||
@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 | ||
MyBean myBean; | ||
|
||
@Test | ||
public void test() { | ||
assertThatThrownBy(() -> myBean.useDatasource()) | ||
.isInstanceOf(CreationException.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."); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyBean { | ||
@Inject | ||
@io.quarkus.agroal.DataSource("users") | ||
DataSource ds; | ||
|
||
public void useDatasource() throws SQLException { | ||
ds.getConnection(); | ||
} | ||
} | ||
} |
Oops, something went wrong.