pull/23084/head
Stephane Nicoll 4 years ago
parent 598b7a9137
commit f191b7513d

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,18 +16,17 @@
package org.springframework.boot.autoconfigure.jdbc;
import java.util.UUID;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
@ -41,82 +40,65 @@ import static org.mockito.Mockito.mock;
*/
class DataSourceTransactionManagerAutoConfigurationTests {
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@Test
void testDataSourceExists() {
this.context.register(EmbeddedDataSourceConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class, TransactionAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(DataSource.class)).isNotNull();
assertThat(this.context.getBean(DataSourceTransactionManager.class)).isNotNull();
}
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(TransactionAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class))
.withPropertyValues("spring.datasource.url:jdbc:hsqldb:mem:test-" + UUID.randomUUID());
@Test
void testNoDataSourceExists() {
this.context.register(DataSourceTransactionManagerAutoConfiguration.class, TransactionAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeanNamesForType(DataSource.class)).isEmpty();
assertThat(this.context.getBeanNamesForType(DataSourceTransactionManager.class)).isEmpty();
void transactionManagerWithoutDataSourceIsNotConfigured() {
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(DataSourceTransactionManager.class));
}
@Test
void testManualConfiguration() {
this.context.register(EmbeddedDataSourceConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class, TransactionAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(DataSource.class)).isNotNull();
assertThat(this.context.getBean(DataSourceTransactionManager.class)).isNotNull();
void transactionManagerWithExistingDataSourceIsConfigured() {
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
.run((context) -> {
assertThat(context).hasSingleBean(PlatformTransactionManager.class)
.hasSingleBean(DataSourceTransactionManager.class);
assertThat(context.getBean(DataSourceTransactionManager.class).getDataSource())
.isSameAs(context.getBean(DataSource.class));
});
}
@Test
void testExistingTransactionManager() {
this.context.register(TransactionManagerConfiguration.class, EmbeddedDataSourceConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class, TransactionAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeansOfType(PlatformTransactionManager.class)).hasSize(1);
assertThat(this.context.getBean(PlatformTransactionManager.class))
.isEqualTo(this.context.getBean("myTransactionManager"));
void transactionManagerWithCustomizationIsConfigured() {
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
.withPropertyValues("spring.transaction.default-timeout=1m",
"spring.transaction.rollback-on-commit-failure=true")
.run((context) -> {
assertThat(context).hasSingleBean(PlatformTransactionManager.class)
.hasSingleBean(DataSourceTransactionManager.class);
DataSourceTransactionManager transactionManager = context
.getBean(DataSourceTransactionManager.class);
assertThat(transactionManager.getDefaultTimeout()).isEqualTo(60);
assertThat(transactionManager.isRollbackOnCommitFailure()).isTrue();
});
}
@Test
void testMultiDataSource() {
this.context.register(MultiDataSourceConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class,
TransactionAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeansOfType(PlatformTransactionManager.class)).isEmpty();
void transactionManagerWithExistingTransactionManagerIsNotOverridden() {
this.contextRunner
.withBean("myTransactionManager", PlatformTransactionManager.class,
() -> mock(PlatformTransactionManager.class))
.run((context) -> assertThat(context).hasSingleBean(PlatformTransactionManager.class)
.hasBean("myTransactionManager"));
}
@Test
void testMultiDataSourceUsingPrimary() {
this.context.register(MultiDataSourceUsingPrimaryConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class, TransactionAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBean(DataSourceTransactionManager.class)).isNotNull();
assertThat(this.context.getBean(AbstractTransactionManagementConfiguration.class)).isNotNull();
void transactionWithMultipleDataSourcesIsNotConfigured() {
this.contextRunner.withUserConfiguration(MultiDataSourceConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean(PlatformTransactionManager.class));
}
@Test
void testCustomizeDataSourceTransactionManagerUsingProperties() {
TestPropertyValues
.of("spring.transaction.default-timeout:30", "spring.transaction.rollback-on-commit-failure:true")
.applyTo(this.context);
this.context.register(EmbeddedDataSourceConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class, TransactionAutoConfiguration.class);
this.context.refresh();
DataSourceTransactionManager transactionManager = this.context.getBean(DataSourceTransactionManager.class);
assertThat(transactionManager.getDefaultTimeout()).isEqualTo(30);
assertThat(transactionManager.isRollbackOnCommitFailure()).isTrue();
}
@Configuration(proxyBeanMethods = false)
static class TransactionManagerConfiguration {
@Bean
PlatformTransactionManager myTransactionManager() {
return mock(PlatformTransactionManager.class);
}
void transactionWithMultipleDataSourcesAndPrimaryCandidateIsConfigured() {
this.contextRunner.withUserConfiguration(MultiDataSourceUsingPrimaryConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(PlatformTransactionManager.class)
.hasSingleBean(DataSourceTransactionManager.class);
assertThat(context.getBean(DataSourceTransactionManager.class).getDataSource())
.isSameAs(context.getBean("test1DataSource"));
});
}
}

Loading…
Cancel
Save