This commit is contained in:
2026-09-29 18:48:57 +08:00
parent a530205d2b
commit 7bc4c52ba4
5 changed files with 0 additions and 23 deletions
@@ -11,14 +11,12 @@ import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import static org.assertj.core.api.Assertions.*;
class EventDeliveryObservationAutoConfigurationTest {
@Test void configuresMandatoryAuditAndMetricsForServiceLocalJdbc() {
var jdbc = new JdbcTemplate(new DriverManagerDataSource("jdbc:h2:mem:auto", "sa", ""));
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(EventDeliveryObservationAutoConfiguration.class))
.withPropertyValues("baiyue.event-delivery.observation-enabled=true")
.withBean(EventDeliveryProducer.class, () -> new EventDeliveryProducer() { })
.withBean(JdbcTemplate.class, () -> jdbc)
.withBean(PlatformTransactionManager.class, () -> new DataSourceTransactionManager(jdbc.getDataSource()))
@@ -30,7 +28,6 @@ class EventDeliveryObservationAutoConfigurationTest {
}
@Test void otherJdbcServicesDoNotAcquireTransactionOrMetricsRequirements() {
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(EventDeliveryObservationAutoConfiguration.class))
.withPropertyValues("baiyue.event-delivery.observation-enabled=true")
.withBean(JdbcTemplate.class, () -> new JdbcTemplate(new DriverManagerDataSource("jdbc:h2:mem:other", "sa", "")))
.run(context -> {
assertThat(context).hasNotFailed();
@@ -41,28 +38,13 @@ class EventDeliveryObservationAutoConfigurationTest {
@Test void producerWithoutMeterRegistryFailsInsteadOfSilentlyDisablingObservation() {
var jdbc = new JdbcTemplate(new DriverManagerDataSource("jdbc:h2:mem:noRegistry", "sa", ""));
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(EventDeliveryObservationAutoConfiguration.class))
.withPropertyValues("baiyue.event-delivery.observation-enabled=true")
.withBean(EventDeliveryProducer.class, () -> new EventDeliveryProducer() { })
.withBean(JdbcTemplate.class, () -> jdbc)
.withBean(PlatformTransactionManager.class, () -> new DataSourceTransactionManager(jdbc.getDataSource()))
.run(context -> assertThat(context).hasFailed());
}
static class RequiredProducer implements EventDeliveryProducer {
@Autowired EventDeliveryJournal journal;
@Autowired EventDeliveryMetrics metrics;
}
@Test void producerMissingTheExplicitObservationSwitchCannotStart() {
var jdbc = new JdbcTemplate(new DriverManagerDataSource("jdbc:h2:mem:noSwitch", "sa", ""));
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(EventDeliveryObservationAutoConfiguration.class))
.withBean(RequiredProducer.class, RequiredProducer::new)
.withBean(JdbcTemplate.class, () -> jdbc)
.withBean(PlatformTransactionManager.class, () -> new DataSourceTransactionManager(jdbc.getDataSource()))
.withBean(MeterRegistry.class, SimpleMeterRegistry::new)
.run(context -> assertThat(context).hasFailed());
}
@Test void missingTransactionManagerFailsRatherThanInstallingNonTransactionalFallback() {
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(EventDeliveryObservationAutoConfiguration.class))
.withPropertyValues("baiyue.event-delivery.observation-enabled=true")
.withBean(EventDeliveryProducer.class, () -> new EventDeliveryProducer() { })
.withBean(JdbcTemplate.class, () -> new JdbcTemplate(new DriverManagerDataSource("jdbc:h2:mem:missing", "sa", "")))
.withBean(MeterRegistry.class, SimpleMeterRegistry::new)