Add test for binding to Hikari DataSource

This commit is contained in:
Dave Syer
2018-03-02 13:38:35 +00:00
parent 8f4439e8e5
commit d14e8ffa1a
4 changed files with 122 additions and 13 deletions

View File

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

View File

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