Polish contribution

Closes gh-16047
pull/16247/head
Stephane Nicoll 6 years ago
parent 2c4afb3bd8
commit eebd906c8a

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

@ -27,7 +27,7 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
* beans. * beans.
* *
* @author Dan Zheng * @author Dan Zheng
* @since 2.1.x * @since 2.1.4
* @see BeanDefinition#setDependsOn(String[]) * @see BeanDefinition#setDependsOn(String[])
*/ */
public class NamedParameterJdbcOperationsDependsOnPostProcessor public class NamedParameterJdbcOperationsDependsOnPostProcessor

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,27 +16,15 @@
package org.springframework.boot.autoconfigure.jdbc; package org.springframework.boot.autoconfigure.jdbc;
import java.util.HashMap; import java.util.Collections;
import java.util.Map;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.Test; 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.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.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; 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.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; 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 org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
/** /**
@ -205,45 +192,14 @@ public class JdbcTemplateAutoConfigurationTests {
public void testDependencyToFlywayWithJdbcTemplateMixed() { public void testDependencyToFlywayWithJdbcTemplateMixed() {
this.contextRunner this.contextRunner
.withUserConfiguration(NamedParameterDataSourceMigrationValidator.class) .withUserConfiguration(NamedParameterDataSourceMigrationValidator.class)
.withPropertyValues("spring.flyway.locations:classpath:db/city_np") .withPropertyValues("spring.flyway.locations:classpath:db/city")
.withConfiguration(AutoConfigurations.of(FlywayAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(FlywayAutoConfiguration.class))
.run((context) -> { .run((context) -> {
assertThat(context).hasNotFailed(); assertThat(context).hasNotFailed();
assertThat(context.getBean(JdbcTemplate.class)).isNotNull(); assertThat(context.getBean(JdbcTemplate.class)).isNotNull();
assertThat(context.getBean( assertThat(context.getBean(
NamedParameterDataSourceMigrationValidator.class).count) NamedParameterDataSourceMigrationValidator.class).count)
.isEqualTo(1); .isEqualTo(0);
});
}
@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);
}); });
} }
@ -266,7 +222,7 @@ public class JdbcTemplateAutoConfigurationTests {
this.contextRunner this.contextRunner
.withUserConfiguration(NamedParameterDataSourceMigrationValidator.class) .withUserConfiguration(NamedParameterDataSourceMigrationValidator.class)
.withPropertyValues( .withPropertyValues(
"spring.liquibase.changeLog:classpath:db/changelog/db.changelog-city-np.yaml") "spring.liquibase.changeLog:classpath:db/changelog/db.changelog-city.yaml")
.withConfiguration( .withConfiguration(
AutoConfigurations.of(LiquibaseAutoConfiguration.class)) AutoConfigurations.of(LiquibaseAutoConfiguration.class))
.run((context) -> { .run((context) -> {
@ -274,34 +230,7 @@ public class JdbcTemplateAutoConfigurationTests {
assertThat(context.getBean(JdbcTemplate.class)).isNotNull(); assertThat(context.getBean(JdbcTemplate.class)).isNotNull();
assertThat(context.getBean( assertThat(context.getBean(
NamedParameterDataSourceMigrationValidator.class).count) NamedParameterDataSourceMigrationValidator.class).count)
.isEqualTo(1); .isEqualTo(0);
});
}
@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);
}); });
} }
@ -390,58 +319,8 @@ public class JdbcTemplateAutoConfigurationTests {
NamedParameterDataSourceMigrationValidator( NamedParameterDataSourceMigrationValidator(
NamedParameterJdbcTemplate namedParameterJdbcTemplate) { NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
String sql = "SELECT COUNT(*) from CITY WHERE id = :id"; this.count = namedParameterJdbcTemplate.queryForObject(
Map<String, Long> param = new HashMap<>(); "SELECT COUNT(*) from CITY", Collections.emptyMap(), Integer.class);
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
}
/**
* <p>
* we should remove the jdbc template bean definition to keep only
* NamedParameterJdbcTemplate is registerd in the bean container
* </p>
* @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);
}
}
} }
} }

@ -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

@ -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)
);

@ -1 +0,0 @@
INSERT INTO CITY(id, name, state, country, map) VALUES(1, 'Hangzhou', 'Zhejiang', 'China', 'map');
Loading…
Cancel
Save