Migrate missed tests to new Binder

Migrate a few tests that were missed to use the new `Binder`.

See gh-9000
This commit is contained in:
Phillip Webb 2017-04-28 14:04:10 -07:00
parent 528d776d2b
commit 3f71b8453f
3 changed files with 67 additions and 104 deletions

View File

@ -16,16 +16,12 @@
package org.springframework.boot.autoconfigure.security; package org.springframework.boot.autoconfigure.security;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.MutablePropertyValues; import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.bind.RelaxedDataBinder; import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.MockConfigurationPropertySource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -38,88 +34,71 @@ public class SecurityPropertiesTests {
private SecurityProperties security = new SecurityProperties(); private SecurityProperties security = new SecurityProperties();
private RelaxedDataBinder binder = new RelaxedDataBinder(this.security, "security");
@Before
public void init() {
this.binder.setIgnoreUnknownFields(false);
this.binder.setConversionService(new DefaultConversionService());
}
@Test @Test
public void testBindingIgnoredSingleValued() { public void testBindingIgnoredSingleValued() {
this.binder.bind(new MutablePropertyValues( bind("security.ignored", "/css/**");
Collections.singletonMap("security.ignored", "/css/**")));
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
assertThat(this.security.getIgnored()).hasSize(1); assertThat(this.security.getIgnored()).hasSize(1);
} }
@Test @Test
public void testBindingIgnoredEmpty() { public void testBindingIgnoredEmpty() {
this.binder.bind(new MutablePropertyValues( bind("security.ignored", "");
Collections.singletonMap("security.ignored", "")));
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
assertThat(this.security.getIgnored()).isEmpty(); assertThat(this.security.getIgnored()).isEmpty();
} }
@Test @Test
public void testBindingIgnoredDisable() { public void testBindingIgnoredDisable() {
this.binder.bind(new MutablePropertyValues( bind("security.ignored", "none");
Collections.singletonMap("security.ignored", "none")));
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
assertThat(this.security.getIgnored()).hasSize(1); assertThat(this.security.getIgnored()).hasSize(1);
} }
@Test @Test
public void testBindingIgnoredMultiValued() { public void testBindingIgnoredMultiValued() {
this.binder.bind(new MutablePropertyValues( bind("security.ignored", "/css/**,/images/**");
Collections.singletonMap("security.ignored", "/css/**,/images/**")));
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
assertThat(this.security.getIgnored()).hasSize(2); assertThat(this.security.getIgnored()).hasSize(2);
} }
@Test @Test
public void testBindingIgnoredMultiValuedList() { public void testBindingIgnoredMultiValuedList() {
Map<String, String> map = new HashMap<>(); MockConfigurationPropertySource source = new MockConfigurationPropertySource();
map.put("security.ignored[0]", "/css/**"); source.put("security.ignored[0]", "/css/**");
map.put("security.ignored[1]", "/foo/**"); source.put("security.ignored[1]", "/foo/**");
this.binder.bind(new MutablePropertyValues(map)); bind(source);
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
assertThat(this.security.getIgnored()).hasSize(2); assertThat(this.security.getIgnored()).hasSize(2);
assertThat(this.security.getIgnored().contains("/foo/**")).isTrue(); assertThat(this.security.getIgnored().contains("/foo/**")).isTrue();
} }
@Test @Test
public void testDefaultPasswordAutogeneratedIfUnresolvedPlaceholder() { public void testDefaultPasswordAutogeneratedIfUnresolvedPlaceholder() {
this.binder.bind(new MutablePropertyValues( bind("security.user.password", "${ADMIN_PASSWORD}");
Collections.singletonMap("security.user.password", "${ADMIN_PASSWORD}")));
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
assertThat(this.security.getUser().isDefaultPassword()).isTrue(); assertThat(this.security.getUser().isDefaultPassword()).isTrue();
} }
@Test @Test
public void testDefaultPasswordAutogeneratedIfEmpty() { public void testDefaultPasswordAutogeneratedIfEmpty() {
this.binder.bind(new MutablePropertyValues( bind("security.user.password", "");
Collections.singletonMap("security.user.password", "")));
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
assertThat(this.security.getUser().isDefaultPassword()).isTrue(); assertThat(this.security.getUser().isDefaultPassword()).isTrue();
} }
@Test @Test
public void testRoles() { public void testRoles() {
this.binder.bind(new MutablePropertyValues( bind("security.user.role", "USER,ADMIN");
Collections.singletonMap("security.user.role", "USER,ADMIN")));
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
assertThat(this.security.getUser().getRole().toString()) assertThat(this.security.getUser().getRole().toString())
.isEqualTo("[USER, ADMIN]"); .isEqualTo("[USER, ADMIN]");
} }
@Test @Test
public void testRole() { public void testRole() {
this.binder.bind(new MutablePropertyValues( bind("security.user.role", "ADMIN");
Collections.singletonMap("security.user.role", "ADMIN")));
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
assertThat(this.security.getUser().getRole().toString()).isEqualTo("[ADMIN]"); assertThat(this.security.getUser().getRole().toString()).isEqualTo("[ADMIN]");
} }
private void bind(String name, String value) {
bind(new MockConfigurationPropertySource(name, value));
}
private void bind(ConfigurationPropertySource source) {
new Binder(source).bind("security", Bindable.ofInstance(this.security));
}
} }

View File

@ -24,8 +24,9 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.MutablePropertyValues; import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.bind.RelaxedDataBinder; import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.MockConfigurationPropertySource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -46,18 +47,14 @@ public class ServerPropertiesTests {
@Test @Test
public void testAddressBinding() throws Exception { public void testAddressBinding() throws Exception {
RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server"); bind("server.address", "127.0.0.1");
binder.bind(new MutablePropertyValues(
Collections.singletonMap("server.address", "127.0.0.1")));
assertThat(binder.getBindingResult().hasErrors()).isFalse();
assertThat(this.properties.getAddress()) assertThat(this.properties.getAddress())
.isEqualTo(InetAddress.getByName("127.0.0.1")); .isEqualTo(InetAddress.getByName("127.0.0.1"));
} }
@Test @Test
public void testPortBinding() throws Exception { public void testPortBinding() throws Exception {
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues( bind("server.port", "9000");
Collections.singletonMap("server.port", "9000")));
assertThat(this.properties.getPort().intValue()).isEqualTo(9000); assertThat(this.properties.getPort().intValue()).isEqualTo(9000);
} }
@ -68,36 +65,26 @@ public class ServerPropertiesTests {
@Test @Test
public void testServerHeader() throws Exception { public void testServerHeader() throws Exception {
RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server"); bind("server.server-header", "Custom Server");
binder.bind(new MutablePropertyValues(
Collections.singletonMap("server.server-header", "Custom Server")));
assertThat(this.properties.getServerHeader()).isEqualTo("Custom Server"); assertThat(this.properties.getServerHeader()).isEqualTo("Custom Server");
} }
@Test @Test
public void testConnectionTimeout() throws Exception { public void testConnectionTimeout() throws Exception {
Map<String, String> map = new HashMap<>(); bind("server.connection-timeout", "60000");
map.put("server.connection-timeout", "60000");
bindProperties(map);
assertThat(this.properties.getConnectionTimeout()).isEqualTo(60000); assertThat(this.properties.getConnectionTimeout()).isEqualTo(60000);
} }
@Test @Test
public void testServletPathAsMapping() throws Exception { public void testServletPathAsMapping() throws Exception {
RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server"); bind("server.servlet.path", "/foo/*");
binder.bind(new MutablePropertyValues(
Collections.singletonMap("server.servlet.path", "/foo/*")));
assertThat(binder.getBindingResult().hasErrors()).isFalse();
assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*"); assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*");
assertThat(this.properties.getServlet().getServletPrefix()).isEqualTo("/foo"); assertThat(this.properties.getServlet().getServletPrefix()).isEqualTo("/foo");
} }
@Test @Test
public void testServletPathAsPrefix() throws Exception { public void testServletPathAsPrefix() throws Exception {
RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server"); bind("server.servlet.path", "/foo");
binder.bind(new MutablePropertyValues(
Collections.singletonMap("server.servlet.path", "/foo")));
assertThat(binder.getBindingResult().hasErrors()).isFalse();
assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*"); assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*");
assertThat(this.properties.getServlet().getServletPrefix()).isEqualTo("/foo"); assertThat(this.properties.getServlet().getServletPrefix()).isEqualTo("/foo");
} }
@ -111,11 +98,11 @@ public class ServerPropertiesTests {
map.put("server.tomcat.accesslog.rename-on-rotate", "true"); map.put("server.tomcat.accesslog.rename-on-rotate", "true");
map.put("server.tomcat.accesslog.request-attributes-enabled", "true"); map.put("server.tomcat.accesslog.request-attributes-enabled", "true");
map.put("server.tomcat.accesslog.suffix", "-bar.log"); map.put("server.tomcat.accesslog.suffix", "-bar.log");
map.put("server.tomcat.protocol_header", "X-Forwarded-Protocol"); map.put("server.tomcat.protocol-header", "X-Forwarded-Protocol");
map.put("server.tomcat.remote_ip_header", "Remote-Ip"); map.put("server.tomcat.remote-ip-header", "Remote-Ip");
map.put("server.tomcat.internal_proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"); map.put("server.tomcat.internal-proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
map.put("server.tomcat.background_processor_delay", "10"); map.put("server.tomcat.background-processor-delay", "10");
bindProperties(map); bind(map);
ServerProperties.Tomcat tomcat = this.properties.getTomcat(); ServerProperties.Tomcat tomcat = this.properties.getTomcat();
assertThat(tomcat.getAccesslog().getPattern()).isEqualTo("%h %t '%r' %s %b"); assertThat(tomcat.getAccesslog().getPattern()).isEqualTo("%h %t '%r' %s %b");
assertThat(tomcat.getAccesslog().getPrefix()).isEqualTo("foo"); assertThat(tomcat.getAccesslog().getPrefix()).isEqualTo("foo");
@ -132,55 +119,45 @@ public class ServerPropertiesTests {
@Test @Test
public void redirectContextRootIsNotConfiguredByDefault() throws Exception { public void redirectContextRootIsNotConfiguredByDefault() throws Exception {
bindProperties(new HashMap<String, String>()); bind(new HashMap<String, String>());
ServerProperties.Tomcat tomcat = this.properties.getTomcat(); ServerProperties.Tomcat tomcat = this.properties.getTomcat();
assertThat(tomcat.getRedirectContextRoot()).isNull(); assertThat(tomcat.getRedirectContextRoot()).isNull();
} }
@Test @Test
public void testTrailingSlashOfContextPathIsRemoved() { public void testTrailingSlashOfContextPathIsRemoved() {
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues( bind("server.servlet.context-path", "/foo/");
Collections.singletonMap("server.servlet.contextPath", "/foo/")));
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/foo"); assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/foo");
} }
@Test @Test
public void testSlashOfContextPathIsDefaultValue() { public void testSlashOfContextPathIsDefaultValue() {
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues( bind("server.servlet.context-path", "/");
Collections.singletonMap("server.servlet.contextPath", "/")));
assertThat(this.properties.getServlet().getContextPath()).isEqualTo(""); assertThat(this.properties.getServlet().getContextPath()).isEqualTo("");
} }
@Test @Test
public void testCustomizeUriEncoding() throws Exception { public void testCustomizeUriEncoding() throws Exception {
Map<String, String> map = new HashMap<>(); bind("server.tomcat.uri-encoding", "US-ASCII");
map.put("server.tomcat.uriEncoding", "US-ASCII");
bindProperties(map);
assertThat(this.properties.getTomcat().getUriEncoding()) assertThat(this.properties.getTomcat().getUriEncoding())
.isEqualTo(Charset.forName("US-ASCII")); .isEqualTo(Charset.forName("US-ASCII"));
} }
@Test @Test
public void testCustomizeHeaderSize() throws Exception { public void testCustomizeHeaderSize() throws Exception {
Map<String, String> map = new HashMap<>(); bind("server.max-http-header-size", "9999");
map.put("server.maxHttpHeaderSize", "9999");
bindProperties(map);
assertThat(this.properties.getMaxHttpHeaderSize()).isEqualTo(9999); assertThat(this.properties.getMaxHttpHeaderSize()).isEqualTo(9999);
} }
@Test @Test
public void testCustomizeJettyAcceptors() throws Exception { public void testCustomizeJettyAcceptors() throws Exception {
Map<String, String> map = new HashMap<>(); bind("server.jetty.acceptors", "10");
map.put("server.jetty.acceptors", "10");
bindProperties(map);
assertThat(this.properties.getJetty().getAcceptors()).isEqualTo(10); assertThat(this.properties.getJetty().getAcceptors()).isEqualTo(10);
} }
@Test @Test
public void testCustomizeJettySelectors() throws Exception { public void testCustomizeJettySelectors() throws Exception {
Map<String, String> map = new HashMap<>(); bind("server.jetty.selectors", "10");
map.put("server.jetty.selectors", "10");
bindProperties(map);
assertThat(this.properties.getJetty().getSelectors()).isEqualTo(10); assertThat(this.properties.getJetty().getSelectors()).isEqualTo(10);
} }
@ -192,7 +169,7 @@ public class ServerPropertiesTests {
map.put("server.jetty.accesslog.file-date-format", "yyyymmdd"); map.put("server.jetty.accesslog.file-date-format", "yyyymmdd");
map.put("server.jetty.accesslog.retention-period", "4"); map.put("server.jetty.accesslog.retention-period", "4");
map.put("server.jetty.accesslog.append", "true"); map.put("server.jetty.accesslog.append", "true");
bindProperties(map); bind(map);
ServerProperties.Jetty jetty = this.properties.getJetty(); ServerProperties.Jetty jetty = this.properties.getJetty();
assertThat(jetty.getAccesslog().isEnabled()).isEqualTo(true); assertThat(jetty.getAccesslog().isEnabled()).isEqualTo(true);
assertThat(jetty.getAccesslog().getFilename()).isEqualTo("foo.txt"); assertThat(jetty.getAccesslog().getFilename()).isEqualTo("foo.txt");
@ -201,9 +178,14 @@ public class ServerPropertiesTests {
assertThat(jetty.getAccesslog().isAppend()).isEqualTo(true); assertThat(jetty.getAccesslog().isAppend()).isEqualTo(true);
} }
private void bindProperties(Map<String, String> map) { private void bind(String name, String value) {
new RelaxedDataBinder(this.properties, "server") bind(Collections.singletonMap(name, value));
.bind(new MutablePropertyValues(map)); }
private void bind(Map<String, String> map) {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
map.forEach(source::put);
new Binder(source).bind("server", Bindable.ofInstance(this.properties));
} }
} }

View File

@ -45,9 +45,10 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Captor; import org.mockito.Captor;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.bind.RelaxedDataBinder; import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.MockConfigurationPropertySource;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.embedded.jetty.JettyWebServer; import org.springframework.boot.web.embedded.jetty.JettyWebServer;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer; import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
@ -255,8 +256,8 @@ public class DefaultServletWebServerFactoryCustomizerTests {
@Test @Test
public void disableTomcatRemoteIpValve() throws Exception { public void disableTomcatRemoteIpValve() throws Exception {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("server.tomcat.remote_ip_header", ""); map.put("server.tomcat.remote-ip-header", "");
map.put("server.tomcat.protocol_header", ""); map.put("server.tomcat.protocol-header", "");
bindProperties(map); bindProperties(map);
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
this.customizer.customize(factory); this.customizer.customize(factory);
@ -286,8 +287,8 @@ public class DefaultServletWebServerFactoryCustomizerTests {
public void defaultTomcatRemoteIpValve() throws Exception { public void defaultTomcatRemoteIpValve() throws Exception {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
// Since 1.1.7 you need to specify at least the protocol // Since 1.1.7 you need to specify at least the protocol
map.put("server.tomcat.protocol_header", "X-Forwarded-Proto"); map.put("server.tomcat.protocol-header", "X-Forwarded-Proto");
map.put("server.tomcat.remote_ip_header", "X-Forwarded-For"); map.put("server.tomcat.remote-ip-header", "X-Forwarded-For");
bindProperties(map); bindProperties(map);
testRemoteIpValveConfigured(); testRemoteIpValveConfigured();
} }
@ -328,9 +329,9 @@ public class DefaultServletWebServerFactoryCustomizerTests {
@Test @Test
public void customTomcatRemoteIpValve() throws Exception { public void customTomcatRemoteIpValve() throws Exception {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("server.tomcat.remote_ip_header", "x-my-remote-ip-header"); map.put("server.tomcat.remote-ip-header", "x-my-remote-ip-header");
map.put("server.tomcat.protocol_header", "x-my-protocol-header"); map.put("server.tomcat.protocol-header", "x-my-protocol-header");
map.put("server.tomcat.internal_proxies", "192.168.0.1"); map.put("server.tomcat.internal-proxies", "192.168.0.1");
map.put("server.tomcat.port-header", "x-my-forward-port"); map.put("server.tomcat.port-header", "x-my-forward-port");
map.put("server.tomcat.protocol-header-https-value", "On"); map.put("server.tomcat.protocol-header-https-value", "On");
bindProperties(map); bindProperties(map);
@ -623,8 +624,9 @@ public class DefaultServletWebServerFactoryCustomizerTests {
} }
private void bindProperties(Map<String, String> map) { private void bindProperties(Map<String, String> map) {
new RelaxedDataBinder(this.properties, "server") MockConfigurationPropertySource source = new MockConfigurationPropertySource();
.bind(new MutablePropertyValues(map)); map.forEach(source::put);
new Binder(source).bind("server", Bindable.ofInstance(this.properties));
} }
} }