Skip to content

Commit 5a3b881

Browse files
committed
Merge pull request #7217 from gdpotter/master
* pr/7217: Respect 'primary' flag when replacing databases
2 parents dbf6d3d + 39d5881 commit 5a3b881

File tree

2 files changed

+103
-6
lines changed

2 files changed

+103
-6
lines changed

spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfiguration.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,20 @@ private void process(BeanDefinitionRegistry registry,
101101
ConfigurableListableBeanFactory beanFactory) {
102102
BeanDefinitionHolder holder = getDataSourceBeanDefinition(beanFactory);
103103
if (holder != null) {
104-
logger.info("Replacing '" + holder.getBeanName()
105-
+ "' DataSource bean with embedded version");
106-
registry.registerBeanDefinition(holder.getBeanName(),
107-
createEmbeddedBeanDefinition());
104+
String beanName = holder.getBeanName();
105+
boolean primary = holder.getBeanDefinition().isPrimary();
106+
logger.info("Replacing '" + beanName + "' DataSource bean with "
107+
+ (primary ? "primary " : "") + "embedded version");
108+
registry.registerBeanDefinition(beanName,
109+
createEmbeddedBeanDefinition(primary));
108110
}
109111
}
110112

111-
private BeanDefinition createEmbeddedBeanDefinition() {
112-
return new RootBeanDefinition(EmbeddedDataSourceFactoryBean.class);
113+
private BeanDefinition createEmbeddedBeanDefinition(boolean primary) {
114+
BeanDefinition beanDefinition = new RootBeanDefinition(
115+
EmbeddedDataSourceFactoryBean.class);
116+
beanDefinition.setPrimary(primary);
117+
return beanDefinition;
113118
}
114119

115120
private BeanDefinitionHolder getDataSourceBeanDefinition(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.orm.jpa;
18+
19+
import javax.sql.DataSource;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.context.annotation.Primary;
29+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
30+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
31+
import org.springframework.test.context.junit4.SpringRunner;
32+
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
35+
/**
36+
* Integration tests for {@link AutoConfigureTestDatabase} when there are multiple
37+
* datasources.
38+
*
39+
* @author Greg Potter
40+
*/
41+
@RunWith(SpringRunner.class)
42+
@DataJpaTest
43+
@AutoConfigureTestDatabase
44+
public class AutoConfigureTestDatabaseWithMultipleDatasourcesIntegrationTests {
45+
46+
@Autowired
47+
private TestEntityManager entities;
48+
49+
@Autowired
50+
private ExampleRepository repository;
51+
52+
@Autowired
53+
private DataSource dataSource;
54+
55+
@Test
56+
public void testRepository() throws Exception {
57+
this.entities.persist(new ExampleEntity("boot", "124"));
58+
this.entities.flush();
59+
ExampleEntity found = this.repository.findByReference("124");
60+
assertThat(found.getName()).isEqualTo("boot");
61+
}
62+
63+
@Test
64+
public void replacesDefinedDataSourceWithExplicit() throws Exception {
65+
// Look that the datasource is replaced with an H2 DB.
66+
String product = this.dataSource.getConnection().getMetaData()
67+
.getDatabaseProductName();
68+
assertThat(product).startsWith("H2");
69+
}
70+
71+
@Configuration
72+
@EnableAutoConfiguration
73+
static class Config {
74+
75+
@Bean
76+
@Primary
77+
public DataSource dataSource() {
78+
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
79+
.setType(EmbeddedDatabaseType.HSQL);
80+
return builder.build();
81+
}
82+
83+
@Bean
84+
public DataSource secondaryDataSource() {
85+
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
86+
.setType(EmbeddedDatabaseType.HSQL);
87+
return builder.build();
88+
}
89+
90+
}
91+
92+
}

0 commit comments

Comments
 (0)