-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow disabling of datasources by excluding packages (#1323)
This allows the testing of non-db beans without requiring the connection pools to be fully configured. Does not cover r2dbc as that is defined elsewhere. micronaut-projects/micronaut-data#2893 It is done this way (with package requirements) as there is no way currently to allow EachBean to skip factory creation based on a property of the bean (ie Toggleable#isEnabled).
- Loading branch information
Showing
14 changed files
with
272 additions
and
20 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
You can disable Micronaut Data Sources, for example in a test, by setting `datasources.enabled` to `false`. |
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
53 changes: 53 additions & 0 deletions
53
.../hibernate6/hibernate6-mysql/src/test/java/example/hibernate6/sync/DisabledDbAppTest.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,53 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package example.hibernate6.sync; | ||
|
||
import io.micronaut.context.annotation.Property; | ||
import io.micronaut.context.exceptions.NoSuchBeanException; | ||
import io.micronaut.core.convert.ConversionService; | ||
import io.micronaut.runtime.server.EmbeddedServer; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@MicronautTest | ||
@Property(name = "datasources.enabled", value = "false") | ||
@Property(name = "datasources.default.db-type", value = "mysql") | ||
@Property(name = "jpa.default.properties.hibernate.dialect", value = "org.hibernate.dialect.MySQLDialect") | ||
class DisabledDbAppTest { | ||
|
||
@Test | ||
void serverRunning(EmbeddedServer embeddedServer) { | ||
assertTrue(embeddedServer.isRunning()); | ||
} | ||
|
||
@Test | ||
void canTestNonDbBeans(EmbeddedServer embeddedServer) { | ||
Integer i = embeddedServer.getApplicationContext().getBean(ConversionService.class).convert("10", Integer.class).orElse(999); | ||
assertEquals(10, i); | ||
} | ||
|
||
@Test | ||
void noDatasourceDefined(EmbeddedServer embeddedServer) { | ||
assertThrows(NoSuchBeanException.class, () -> embeddedServer.getApplicationContext().getBean(DataSource.class)); | ||
} | ||
} | ||
|
54 changes: 54 additions & 0 deletions
54
...jdbc-ucp-tests/jdbc-ucp-oracle/src/test/java/example/jdbc/ucp/sync/DisabledDbAppTest.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,54 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package example.jdbc.ucp.sync; | ||
|
||
import io.micronaut.context.annotation.Property; | ||
import io.micronaut.context.exceptions.NoSuchBeanException; | ||
import io.micronaut.core.convert.ConversionService; | ||
import io.micronaut.runtime.server.EmbeddedServer; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@MicronautTest | ||
@Property(name = "datasources.enabled", value = "false") | ||
@Property(name = "datasources.default.db-type", value = "oracle") | ||
@Property(name = "datasources.default.connection-factory-class-name", value = "oracle.jdbc.pool.OracleDataSource") | ||
@Property(name = "test-resources.containers.oracle.startup-timeout", value = "600s") | ||
class DisabledDbAppTest { | ||
|
||
@Test | ||
void serverRunning(EmbeddedServer embeddedServer) { | ||
assertTrue(embeddedServer.isRunning()); | ||
} | ||
|
||
@Test | ||
void canTestNonDbBeans(EmbeddedServer embeddedServer) { | ||
Integer i = embeddedServer.getApplicationContext().getBean(ConversionService.class).convert("10", Integer.class).orElse(999); | ||
assertEquals(10, i); | ||
} | ||
|
||
@Test | ||
void noDatasourceDefined(EmbeddedServer embeddedServer) { | ||
assertThrows(NoSuchBeanException.class, () -> embeddedServer.getApplicationContext().getBean(DataSource.class)); | ||
} | ||
} | ||
|
Oops, something went wrong.