From eebd906c8a24cb3170cc14fadb70b4c9f01829ef Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 27 Feb 2019 15:10:43 +0100 Subject: [PATCH] Polish contribution Closes gh-16047 --- .../flyway/FlywayAutoConfiguration.java | 2 +- ...rJdbcOperationsDependsOnPostProcessor.java | 2 +- .../liquibase/LiquibaseAutoConfiguration.java | 2 +- .../JdbcTemplateAutoConfigurationTests.java | 137 +----------------- .../db/changelog/db.changelog-city-np.yaml | 55 ------- .../test/resources/db/city_np/V1__init.sql | 9 -- .../resources/db/city_np/V2__add_data.sql | 1 - 7 files changed, 11 insertions(+), 197 deletions(-) delete mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/resources/db/changelog/db.changelog-city-np.yaml delete mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/resources/db/city_np/V1__init.sql delete mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/resources/db/city_np/V2__add_data.sql diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java index ff21162c61..3397315078 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 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. diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java index d39288b2e0..d4b12cc12a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/NamedParameterJdbcOperationsDependsOnPostProcessor.java @@ -27,7 +27,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; * beans. * * @author Dan Zheng - * @since 2.1.x + * @since 2.1.4 * @see BeanDefinition#setDependsOn(String[]) */ public class NamedParameterJdbcOperationsDependsOnPostProcessor diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java index 88898f3c7b..7c220f51b9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 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. diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java index 40ecc3e9d7..ad083edbbb 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 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,27 +16,15 @@ package org.springframework.boot.autoconfigure.jdbc; -import java.util.HashMap; -import java.util.Map; +import java.util.Collections; import javax.sql.DataSource; import org.junit.Test; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.NoSuchBeanDefinitionException; -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.beans.factory.support.BeanDefinitionRegistry; -import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.AutoConfigureBefore; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; -import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -47,7 +35,6 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; /** @@ -205,45 +192,14 @@ public class JdbcTemplateAutoConfigurationTests { public void testDependencyToFlywayWithJdbcTemplateMixed() { this.contextRunner .withUserConfiguration(NamedParameterDataSourceMigrationValidator.class) - .withPropertyValues("spring.flyway.locations:classpath:db/city_np") + .withPropertyValues("spring.flyway.locations:classpath:db/city") .withConfiguration(AutoConfigurations.of(FlywayAutoConfiguration.class)) .run((context) -> { assertThat(context).hasNotFailed(); assertThat(context.getBean(JdbcTemplate.class)).isNotNull(); assertThat(context.getBean( NamedParameterDataSourceMigrationValidator.class).count) - .isEqualTo(1); - }); - } - - @Test - public void testDependencyToFlywayWithOnlyNamedParameterJdbcTemplate() { - ApplicationContextRunner contextRunner1 = new ApplicationContextRunner() - .withPropertyValues("spring.datasource.initialization-mode=never", - "spring.datasource.generate-unique-name=true") - .withConfiguration( - AutoConfigurations.of(DataSourceAutoConfiguration.class, - JdbcTemplateAutoConfiguration.class, - OnlyNamedParameterJdbcTemplateAutoConfiguration.class)); - contextRunner1 - .withUserConfiguration(NamedParameterDataSourceMigrationValidator.class) - .withPropertyValues("spring.flyway.locations:classpath:db/city_np") - .withConfiguration(AutoConfigurations.of(FlywayAutoConfiguration.class)) - .run((context) -> { - assertThat(context).hasNotFailed(); - assertThat(context.containsBean("jdbcTemplate")).isFalse(); - try { - JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class); - fail("org.springframework.boot.autoconfigure.jdbc.JdcTemplate should not exist in the application context"); - } - catch (NoSuchBeanDefinitionException ex) { - - } - assertThat(context.getBean(NamedParameterJdbcTemplate.class)) - .isNotNull(); - assertThat(context.getBean( - NamedParameterDataSourceMigrationValidator.class).count) - .isEqualTo(1); + .isEqualTo(0); }); } @@ -266,7 +222,7 @@ public class JdbcTemplateAutoConfigurationTests { this.contextRunner .withUserConfiguration(NamedParameterDataSourceMigrationValidator.class) .withPropertyValues( - "spring.liquibase.changeLog:classpath:db/changelog/db.changelog-city-np.yaml") + "spring.liquibase.changeLog:classpath:db/changelog/db.changelog-city.yaml") .withConfiguration( AutoConfigurations.of(LiquibaseAutoConfiguration.class)) .run((context) -> { @@ -274,34 +230,7 @@ public class JdbcTemplateAutoConfigurationTests { assertThat(context.getBean(JdbcTemplate.class)).isNotNull(); assertThat(context.getBean( NamedParameterDataSourceMigrationValidator.class).count) - .isEqualTo(1); - }); - } - - @Test - public void testDependencyToLiquibaseWithOnlyNamedParameterJdbcTemplate() { - this.contextRunner - .withUserConfiguration(NamedParameterDataSourceMigrationValidator.class) - .withPropertyValues( - "spring.liquibase.changeLog:classpath:db/changelog/db.changelog-city-np.yaml") - .withConfiguration(AutoConfigurations.of( - OnlyNamedParameterJdbcTemplateAutoConfiguration.class, - LiquibaseAutoConfiguration.class)) - .run((context) -> { - assertThat(context).hasNotFailed(); - assertThat(context.containsBean("jdbcTemplate")).isFalse(); - try { - JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class); - fail("org.springframework.boot.autoconfigure.jdbc.JdcTemplate should not exist in the application context"); - } - catch (NoSuchBeanDefinitionException ex) { - - } - assertThat(context.getBean(NamedParameterJdbcTemplate.class)) - .isNotNull(); - assertThat(context.getBean( - NamedParameterDataSourceMigrationValidator.class).count) - .isEqualTo(1); + .isEqualTo(0); }); } @@ -390,58 +319,8 @@ public class JdbcTemplateAutoConfigurationTests { NamedParameterDataSourceMigrationValidator( NamedParameterJdbcTemplate namedParameterJdbcTemplate) { - String sql = "SELECT COUNT(*) from CITY WHERE id = :id"; - Map param = new HashMap<>(); - param.put("id", 1L); - this.count = namedParameterJdbcTemplate.queryForObject(sql, param, - Integer.class); - } - - } - - @Configuration - @ConditionalOnClass({ DataSource.class }) - @ConditionalOnSingleCandidate(DataSource.class) - @AutoConfigureAfter({ DataSourceAutoConfiguration.class, - JdbcTemplateAutoConfiguration.class }) - @AutoConfigureBefore({ FlywayAutoConfiguration.class, - LiquibaseAutoConfiguration.class }) - @EnableConfigurationProperties(JdbcProperties.class) - static class OnlyNamedParameterJdbcTemplateAutoConfiguration - implements BeanDefinitionRegistryPostProcessor { - - @Bean - public NamedParameterJdbcTemplate myNamedParameterJdbcTemplate( - DataSource dataSource) { - return new NamedParameterJdbcTemplate(dataSource); - } - - @Override - public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) - throws BeansException { - // do nothing - } - - /** - *

- * we should remove the jdbc template bean definition to keep only - * NamedParameterJdbcTemplate is registerd in the bean container - *

- * @param registry the bean definition registry. - * @throws BeansException if the bean registry have any exception. - */ - @Override - public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) - throws BeansException { - String[] excludeBeanNames = new String[] { "jdbcTemplate", - "namedParameterJdbcTemplate" }; - for (String beanName : excludeBeanNames) { - BeanDefinition beanDefinition = registry.getBeanDefinition(beanName); - if (beanDefinition != null) { - registry.removeBeanDefinition(beanName); - } - } - + this.count = namedParameterJdbcTemplate.queryForObject( + "SELECT COUNT(*) from CITY", Collections.emptyMap(), Integer.class); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/db/changelog/db.changelog-city-np.yaml b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/db/changelog/db.changelog-city-np.yaml deleted file mode 100644 index 324baff9f3..0000000000 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/db/changelog/db.changelog-city-np.yaml +++ /dev/null @@ -1,55 +0,0 @@ -databaseChangeLog: - - changeSet: - id: 1 - author: dan-zheng - changes: - - createSequence: - sequenceName: hibernate_sequence - - createTable: - tableName: city - columns: - - column: - name: id - type: bigint - autoIncrement: true - constraints: - primaryKey: true - nullable: false - - column: - name: name - type: varchar(50) - constraints: - nullable: false - - column: - name: state - type: varchar(50) - constraints: - nullable: false - - column: - name: country - type: varchar(50) - constraints: - nullable: false - - column: - name: map - type: varchar(50) - constraints: - nullable: true - - insert: - tableName: city - columns: - - column: - name: id - value: 1 - - column: - name: name - value: Hangzhou - - column: - name: state - value: Zhejiang - - column: - name: country - value: China - - column: - name: map - value: map diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/db/city_np/V1__init.sql b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/db/city_np/V1__init.sql deleted file mode 100644 index 88cc1f4919..0000000000 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/db/city_np/V1__init.sql +++ /dev/null @@ -1,9 +0,0 @@ -CREATE SEQUENCE HIBERNATE_SEQUENCE; - -CREATE TABLE CITY ( - id BIGINT GENERATED BY DEFAULT AS IDENTITY, - name VARCHAR(30), - state VARCHAR(30), - country VARCHAR(30), - map VARCHAR(30) -); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/db/city_np/V2__add_data.sql b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/db/city_np/V2__add_data.sql deleted file mode 100644 index 004b6e56cc..0000000000 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/db/city_np/V2__add_data.sql +++ /dev/null @@ -1 +0,0 @@ -INSERT INTO CITY(id, name, state, country, map) VALUES(1, 'Hangzhou', 'Zhejiang', 'China', 'map'); \ No newline at end of file