Skip to content

Commit eebd906

Browse files
committed
Polish contribution
Closes gh-16047
1 parent 2c4afb3 commit eebd906

File tree

7 files changed

+11
-197
lines changed

7 files changed

+11
-197
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* beans.
2828
*
2929
* @author Dan Zheng
30-
* @since 2.1.x
30+
* @since 2.1.4
3131
* @see BeanDefinition#setDependsOn(String[])
3232
*/
3333
public class NamedParameterJdbcOperationsDependsOnPostProcessor

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java

Lines changed: 8 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,27 +16,15 @@
1616

1717
package org.springframework.boot.autoconfigure.jdbc;
1818

19-
import java.util.HashMap;
20-
import java.util.Map;
19+
import java.util.Collections;
2120

2221
import javax.sql.DataSource;
2322

2423
import org.junit.Test;
2524

26-
import org.springframework.beans.BeansException;
27-
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
28-
import org.springframework.beans.factory.config.BeanDefinition;
29-
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
30-
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
31-
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
3225
import org.springframework.boot.autoconfigure.AutoConfigurations;
33-
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
34-
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
35-
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
36-
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
3726
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
3827
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
39-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4028
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
4129
import org.springframework.context.annotation.Bean;
4230
import org.springframework.context.annotation.Configuration;
@@ -47,7 +35,6 @@
4735
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
4836

4937
import static org.assertj.core.api.Assertions.assertThat;
50-
import static org.junit.Assert.fail;
5138
import static org.mockito.Mockito.mock;
5239

5340
/**
@@ -205,45 +192,14 @@ public void testDependencyToFlyway() {
205192
public void testDependencyToFlywayWithJdbcTemplateMixed() {
206193
this.contextRunner
207194
.withUserConfiguration(NamedParameterDataSourceMigrationValidator.class)
208-
.withPropertyValues("spring.flyway.locations:classpath:db/city_np")
195+
.withPropertyValues("spring.flyway.locations:classpath:db/city")
209196
.withConfiguration(AutoConfigurations.of(FlywayAutoConfiguration.class))
210197
.run((context) -> {
211198
assertThat(context).hasNotFailed();
212199
assertThat(context.getBean(JdbcTemplate.class)).isNotNull();
213200
assertThat(context.getBean(
214201
NamedParameterDataSourceMigrationValidator.class).count)
215-
.isEqualTo(1);
216-
});
217-
}
218-
219-
@Test
220-
public void testDependencyToFlywayWithOnlyNamedParameterJdbcTemplate() {
221-
ApplicationContextRunner contextRunner1 = new ApplicationContextRunner()
222-
.withPropertyValues("spring.datasource.initialization-mode=never",
223-
"spring.datasource.generate-unique-name=true")
224-
.withConfiguration(
225-
AutoConfigurations.of(DataSourceAutoConfiguration.class,
226-
JdbcTemplateAutoConfiguration.class,
227-
OnlyNamedParameterJdbcTemplateAutoConfiguration.class));
228-
contextRunner1
229-
.withUserConfiguration(NamedParameterDataSourceMigrationValidator.class)
230-
.withPropertyValues("spring.flyway.locations:classpath:db/city_np")
231-
.withConfiguration(AutoConfigurations.of(FlywayAutoConfiguration.class))
232-
.run((context) -> {
233-
assertThat(context).hasNotFailed();
234-
assertThat(context.containsBean("jdbcTemplate")).isFalse();
235-
try {
236-
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
237-
fail("org.springframework.boot.autoconfigure.jdbc.JdcTemplate should not exist in the application context");
238-
}
239-
catch (NoSuchBeanDefinitionException ex) {
240-
241-
}
242-
assertThat(context.getBean(NamedParameterJdbcTemplate.class))
243-
.isNotNull();
244-
assertThat(context.getBean(
245-
NamedParameterDataSourceMigrationValidator.class).count)
246-
.isEqualTo(1);
202+
.isEqualTo(0);
247203
});
248204
}
249205

@@ -266,42 +222,15 @@ public void testDependencyToLiquibaseWithJdbcTemplateMixed() {
266222
this.contextRunner
267223
.withUserConfiguration(NamedParameterDataSourceMigrationValidator.class)
268224
.withPropertyValues(
269-
"spring.liquibase.changeLog:classpath:db/changelog/db.changelog-city-np.yaml")
225+
"spring.liquibase.changeLog:classpath:db/changelog/db.changelog-city.yaml")
270226
.withConfiguration(
271227
AutoConfigurations.of(LiquibaseAutoConfiguration.class))
272228
.run((context) -> {
273229
assertThat(context).hasNotFailed();
274230
assertThat(context.getBean(JdbcTemplate.class)).isNotNull();
275231
assertThat(context.getBean(
276232
NamedParameterDataSourceMigrationValidator.class).count)
277-
.isEqualTo(1);
278-
});
279-
}
280-
281-
@Test
282-
public void testDependencyToLiquibaseWithOnlyNamedParameterJdbcTemplate() {
283-
this.contextRunner
284-
.withUserConfiguration(NamedParameterDataSourceMigrationValidator.class)
285-
.withPropertyValues(
286-
"spring.liquibase.changeLog:classpath:db/changelog/db.changelog-city-np.yaml")
287-
.withConfiguration(AutoConfigurations.of(
288-
OnlyNamedParameterJdbcTemplateAutoConfiguration.class,
289-
LiquibaseAutoConfiguration.class))
290-
.run((context) -> {
291-
assertThat(context).hasNotFailed();
292-
assertThat(context.containsBean("jdbcTemplate")).isFalse();
293-
try {
294-
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
295-
fail("org.springframework.boot.autoconfigure.jdbc.JdcTemplate should not exist in the application context");
296-
}
297-
catch (NoSuchBeanDefinitionException ex) {
298-
299-
}
300-
assertThat(context.getBean(NamedParameterJdbcTemplate.class))
301-
.isNotNull();
302-
assertThat(context.getBean(
303-
NamedParameterDataSourceMigrationValidator.class).count)
304-
.isEqualTo(1);
233+
.isEqualTo(0);
305234
});
306235
}
307236

@@ -390,58 +319,8 @@ static class NamedParameterDataSourceMigrationValidator {
390319

391320
NamedParameterDataSourceMigrationValidator(
392321
NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
393-
String sql = "SELECT COUNT(*) from CITY WHERE id = :id";
394-
Map<String, Long> param = new HashMap<>();
395-
param.put("id", 1L);
396-
this.count = namedParameterJdbcTemplate.queryForObject(sql, param,
397-
Integer.class);
398-
}
399-
400-
}
401-
402-
@Configuration
403-
@ConditionalOnClass({ DataSource.class })
404-
@ConditionalOnSingleCandidate(DataSource.class)
405-
@AutoConfigureAfter({ DataSourceAutoConfiguration.class,
406-
JdbcTemplateAutoConfiguration.class })
407-
@AutoConfigureBefore({ FlywayAutoConfiguration.class,
408-
LiquibaseAutoConfiguration.class })
409-
@EnableConfigurationProperties(JdbcProperties.class)
410-
static class OnlyNamedParameterJdbcTemplateAutoConfiguration
411-
implements BeanDefinitionRegistryPostProcessor {
412-
413-
@Bean
414-
public NamedParameterJdbcTemplate myNamedParameterJdbcTemplate(
415-
DataSource dataSource) {
416-
return new NamedParameterJdbcTemplate(dataSource);
417-
}
418-
419-
@Override
420-
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
421-
throws BeansException {
422-
// do nothing
423-
}
424-
425-
/**
426-
* <p>
427-
* we should remove the jdbc template bean definition to keep only
428-
* NamedParameterJdbcTemplate is registerd in the bean container
429-
* </p>
430-
* @param registry the bean definition registry.
431-
* @throws BeansException if the bean registry have any exception.
432-
*/
433-
@Override
434-
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
435-
throws BeansException {
436-
String[] excludeBeanNames = new String[] { "jdbcTemplate",
437-
"namedParameterJdbcTemplate" };
438-
for (String beanName : excludeBeanNames) {
439-
BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
440-
if (beanDefinition != null) {
441-
registry.removeBeanDefinition(beanName);
442-
}
443-
}
444-
322+
this.count = namedParameterJdbcTemplate.queryForObject(
323+
"SELECT COUNT(*) from CITY", Collections.emptyMap(), Integer.class);
445324
}
446325

447326
}

spring-boot-project/spring-boot-autoconfigure/src/test/resources/db/changelog/db.changelog-city-np.yaml

Lines changed: 0 additions & 55 deletions
This file was deleted.

spring-boot-project/spring-boot-autoconfigure/src/test/resources/db/city_np/V1__init.sql

Lines changed: 0 additions & 9 deletions
This file was deleted.

spring-boot-project/spring-boot-autoconfigure/src/test/resources/db/city_np/V2__add_data.sql

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)