From d14e8ffa1a60167d18f1ddf42acd5bec9bf739a1 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 2 Mar 2018 13:38:35 +0000 Subject: [PATCH] Add test for binding to Hikari DataSource --- .../ConfigurationPropertiesRebinder.java | 9 +- .../ContextRefresherIntegrationTests.java | 114 ++++++++++++++++++ .../refresh/RefreshScopeIntegrationTests.java | 8 -- .../src/test/resources/schema.sql | 4 + 4 files changed, 122 insertions(+), 13 deletions(-) create mode 100644 spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherIntegrationTests.java create mode 100644 spring-cloud-context/src/test/resources/schema.sql diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinder.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinder.java index 85b7b8c2..6d6890b6 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinder.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinder.java @@ -96,11 +96,6 @@ public class ConfigurationPropertiesRebinder if (AopUtils.isAopProxy(bean)) { bean = getTargetObject(bean); } - // TODO: determine a more general approach to fix this. - // see https://github.com/spring-cloud/spring-cloud-commons/issues/318 - if (bean.getClass().getName().equals("com.zaxxer.hikari.HikariDataSource")) { - return false; //ignore - } this.applicationContext.getAutowireCapableBeanFactory().destroyBean(bean); this.applicationContext.getAutowireCapableBeanFactory() .initializeBean(bean, name); @@ -110,6 +105,10 @@ public class ConfigurationPropertiesRebinder this.errors.put(name, e); throw e; } + catch (Exception e) { + this.errors.put(name, e); + throw new IllegalStateException("Cannot rebind to " + name, e); + } } return false; } diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherIntegrationTests.java new file mode 100644 index 00000000..f43083be --- /dev/null +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherIntegrationTests.java @@ -0,0 +1,114 @@ +/* + * Copyright 2006-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cloud.context.refresh; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.cloud.context.refresh.ContextRefresherIntegrationTests.TestConfiguration; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.jmx.export.annotation.ManagedAttribute; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = TestConfiguration.class, properties = { + "spring.datasource.hikari.read-only=false" }) +public class ContextRefresherIntegrationTests { + + @Autowired + private TestProperties properties; + + @Autowired + private ConfigurableEnvironment environment; + + @Autowired + private ContextRefresher refresher; + + @Test + @DirtiesContext + public void testSimpleProperties() throws Exception { + assertEquals("Hello scope!", this.properties.getMessage()); + // Change the dynamic property source... + this.properties.setMessage("Foo"); + // ...but don't refresh, so the bean stays the same: + assertEquals("Foo", this.properties.getMessage()); + } + + @Test + @DirtiesContext + public void testRefreshBean() throws Exception { + assertEquals("Hello scope!", this.properties.getMessage()); + // Change the dynamic property source... + this.properties.setMessage("Foo"); + // ...and then refresh, so the bean is re-initialized: + this.refresher.refresh(); + assertEquals("Hello scope!", this.properties.getMessage()); + } + + @Test + @DirtiesContext + public void testUpdateHikari() throws Exception { + assertEquals("Hello scope!", this.properties.getMessage()); + TestPropertyValues.of("spring.datasource.hikari.read-only=true") + .applyTo(environment); + // ...and then refresh, so the bean is re-initialized: + this.refresher.refresh(); + assertEquals("Hello scope!", this.properties.getMessage()); + } + + @Configuration + @EnableConfigurationProperties(TestProperties.class) + @EnableAutoConfiguration + protected static class TestConfiguration { + } + + @ConfigurationProperties + @ManagedResource + protected static class TestProperties { + private String message; + private int delay; + + @ManagedAttribute + public String getMessage() { + return this.message; + } + + public void setMessage(String message) { + this.message = message; + } + + @ManagedAttribute + public int getDelay() { + return this.delay; + } + + public void setDelay(int delay) { + this.delay = delay; + } + } + +} diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeIntegrationTests.java index a717d6fe..b39a1642 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeIntegrationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeIntegrationTests.java @@ -27,7 +27,6 @@ import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.test.context.SpringBootTest; @@ -42,8 +41,6 @@ import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; -import javax.sql.DataSource; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; @@ -215,11 +212,6 @@ public class RefreshScopeIntegrationTests { return service; } - @Bean - @RefreshScope - public DataSource dataSource(DataSourceProperties dataSourceProperties) { - return dataSourceProperties.initializeDataSourceBuilder().build(); - } } @ConfigurationProperties diff --git a/spring-cloud-context/src/test/resources/schema.sql b/spring-cloud-context/src/test/resources/schema.sql new file mode 100644 index 00000000..53b892cf --- /dev/null +++ b/spring-cloud-context/src/test/resources/schema.sql @@ -0,0 +1,4 @@ +create table foos ( + id INTEGER IDENTITY PRIMARY KEY, + value VARCHAR(30) +); \ No newline at end of file