mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
[bs-22] Add more unit tests
This commit is contained in:
parent
aabff1a774
commit
729cfe813b
@ -67,7 +67,7 @@ public class AbstractDataSourceConfiguration {
|
|||||||
.getEmbeddedDatabaseType();
|
.getEmbeddedDatabaseType();
|
||||||
this.url = EmbeddedDatabaseConfiguration
|
this.url = EmbeddedDatabaseConfiguration
|
||||||
.getEmbeddedDatabaseUrl(embeddedDatabaseType);
|
.getEmbeddedDatabaseUrl(embeddedDatabaseType);
|
||||||
if (!StringUtils.hasText(this.driverClassName)) {
|
if (!StringUtils.hasText(this.url)) {
|
||||||
throw new BeanCreationException(
|
throw new BeanCreationException(
|
||||||
"Cannot determine embedded database url for database type "
|
"Cannot determine embedded database url for database type "
|
||||||
+ embeddedDatabaseType
|
+ embeddedDatabaseType
|
||||||
|
@ -39,6 +39,13 @@ public class PropertiesMergingResourceTransformer implements ResourceTransformer
|
|||||||
|
|
||||||
private Properties data = new Properties();
|
private Properties data = new Properties();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the data the properties being merged
|
||||||
|
*/
|
||||||
|
public Properties getData() {
|
||||||
|
return this.data;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canTransformResource(String resource) {
|
public boolean canTransformResource(String resource) {
|
||||||
if (this.resource != null && this.resource.equalsIgnoreCase(resource)) {
|
if (this.resource != null && this.resource.equalsIgnoreCase(resource)) {
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2013 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.bootstrap;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
import org.springframework.core.env.MapPropertySource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Dave Syer
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class TestUtils {
|
||||||
|
|
||||||
|
public static void addEnviroment(ConfigurableApplicationContext context,
|
||||||
|
String... pairs) {
|
||||||
|
Map<String, Object> map = new HashMap<String, Object>();
|
||||||
|
for (String pair : pairs) {
|
||||||
|
int index = pair.indexOf(":");
|
||||||
|
String key = pair.substring(0, index > 0 ? index : pair.length());
|
||||||
|
String value = index > 0 ? pair.substring(index + 1) : "";
|
||||||
|
map.put(key, value);
|
||||||
|
}
|
||||||
|
context.getEnvironment().getPropertySources()
|
||||||
|
.addFirst(new MapPropertySource("test", map));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -35,6 +35,7 @@ public class BasicDataSourceConfigurationTests {
|
|||||||
this.context.register(BasicDataSourceConfiguration.class);
|
this.context.register(BasicDataSourceConfiguration.class);
|
||||||
this.context.refresh();
|
this.context.refresh();
|
||||||
assertNotNull(this.context.getBean(DataSource.class));
|
assertNotNull(this.context.getBean(DataSource.class));
|
||||||
|
context.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,7 @@ public class EmbeddedDatabaseConfigurationTests {
|
|||||||
this.context.register(EmbeddedDatabaseConfiguration.class);
|
this.context.register(EmbeddedDatabaseConfiguration.class);
|
||||||
this.context.refresh();
|
this.context.refresh();
|
||||||
assertNotNull(this.context.getBean(DataSource.class));
|
assertNotNull(this.context.getBean(DataSource.class));
|
||||||
|
this.context.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,10 +15,18 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.bootstrap.autoconfigure.jdbc;
|
package org.springframework.bootstrap.autoconfigure.jdbc;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
|
import org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
@ -29,6 +37,15 @@ import static org.junit.Assert.assertNotNull;
|
|||||||
public class TomcatDataSourceConfigurationTests {
|
public class TomcatDataSourceConfigurationTests {
|
||||||
|
|
||||||
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||||
|
private Map<Object, Object> old;
|
||||||
|
private Map<Object, Object> map;
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void restore() {
|
||||||
|
if (this.map != null && this.old != null) {
|
||||||
|
this.map.putAll(this.old);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDataSourceExists() throws Exception {
|
public void testDataSourceExists() throws Exception {
|
||||||
@ -37,4 +54,33 @@ public class TomcatDataSourceConfigurationTests {
|
|||||||
assertNotNull(this.context.getBean(DataSource.class));
|
assertNotNull(this.context.getBean(DataSource.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = BeanCreationException.class)
|
||||||
|
public void testBadUrl() throws Exception {
|
||||||
|
this.map = getField(EmbeddedDatabaseConfiguration.class, "EMBEDDED_DATABASE_URLS");
|
||||||
|
this.old = new HashMap<Object, Object>(this.map);
|
||||||
|
this.map.clear();
|
||||||
|
this.context.register(TomcatDataSourceConfiguration.class,
|
||||||
|
PropertyPlaceholderAutoConfiguration.class);
|
||||||
|
this.context.refresh();
|
||||||
|
assertNotNull(this.context.getBean(DataSource.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = BeanCreationException.class)
|
||||||
|
public void testBadDriverClass() throws Exception {
|
||||||
|
this.map = getField(EmbeddedDatabaseConfiguration.class,
|
||||||
|
"EMBEDDED_DATABASE_DRIVER_CLASSES");
|
||||||
|
this.old = new HashMap<Object, Object>(this.map);
|
||||||
|
this.map.clear();
|
||||||
|
this.context.register(TomcatDataSourceConfiguration.class,
|
||||||
|
PropertyPlaceholderAutoConfiguration.class);
|
||||||
|
this.context.refresh();
|
||||||
|
assertNotNull(this.context.getBean(DataSource.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T> T getField(Class<?> target, String name) {
|
||||||
|
Field field = ReflectionUtils.findField(target, name, null);
|
||||||
|
ReflectionUtils.makeAccessible(field);
|
||||||
|
return (T) ReflectionUtils.getField(field, target);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,20 +15,21 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.bootstrap.autoconfigure.web;
|
package org.springframework.bootstrap.autoconfigure.web;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.io.File;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
import org.springframework.bootstrap.TestUtils;
|
||||||
import org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
import org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||||
import org.springframework.bootstrap.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
|
import org.springframework.bootstrap.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
|
||||||
import org.springframework.bootstrap.context.embedded.ConfigurableEmbeddedServletContainerFactory;
|
import org.springframework.bootstrap.context.embedded.ConfigurableEmbeddedServletContainerFactory;
|
||||||
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory;
|
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory;
|
||||||
|
import org.springframework.bootstrap.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
|
||||||
import org.springframework.bootstrap.properties.ServerProperties;
|
import org.springframework.bootstrap.properties.ServerProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.env.MapPropertySource;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
@ -43,27 +44,48 @@ public class ServerPropertiesConfigurationTests {
|
|||||||
|
|
||||||
private AnnotationConfigEmbeddedWebApplicationContext context;
|
private AnnotationConfigEmbeddedWebApplicationContext context;
|
||||||
|
|
||||||
private Map<String, Object> environment = new HashMap<String, Object>();
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
containerFactory = Mockito
|
containerFactory = Mockito
|
||||||
.mock(ConfigurableEmbeddedServletContainerFactory.class);
|
.mock(ConfigurableEmbeddedServletContainerFactory.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void close() {
|
||||||
|
if (this.context != null) {
|
||||||
|
this.context.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createFromConfigClass() throws Exception {
|
public void createFromConfigClass() throws Exception {
|
||||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||||
this.context.register(EmbeddedContainerConfiguration.class,
|
this.context.register(EmbeddedContainerConfiguration.class,
|
||||||
|
EmbeddedContainerCustomizerConfiguration.class,
|
||||||
ServerPropertiesConfiguration.class,
|
ServerPropertiesConfiguration.class,
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
PropertyPlaceholderAutoConfiguration.class);
|
||||||
this.environment.put("server.port", "9000");
|
TestUtils.addEnviroment(this.context, "server.port:9000");
|
||||||
this.context.getEnvironment().getPropertySources()
|
|
||||||
.addFirst(new MapPropertySource("test", this.environment));
|
|
||||||
this.context.refresh();
|
this.context.refresh();
|
||||||
ServerProperties server = this.context.getBean(ServerProperties.class);
|
ServerProperties server = this.context.getBean(ServerProperties.class);
|
||||||
assertNotNull(server);
|
assertNotNull(server);
|
||||||
assertEquals(9000, server.getPort());
|
assertEquals(9000, server.getPort());
|
||||||
|
Mockito.verify(containerFactory).setPort(9000);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void tomcatProperties() throws Exception {
|
||||||
|
containerFactory = Mockito.mock(TomcatEmbeddedServletContainerFactory.class);
|
||||||
|
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||||
|
this.context.register(EmbeddedContainerCustomizerConfiguration.class,
|
||||||
|
EmbeddedContainerConfiguration.class,
|
||||||
|
ServerPropertiesConfiguration.class,
|
||||||
|
PropertyPlaceholderAutoConfiguration.class);
|
||||||
|
TestUtils.addEnviroment(this.context, "server.tomcat.basedir:target/foo");
|
||||||
|
this.context.refresh();
|
||||||
|
ServerProperties server = this.context.getBean(ServerProperties.class);
|
||||||
|
assertNotNull(server);
|
||||||
|
assertEquals(new File("target/foo"), server.getTomcat().getBasedir());
|
||||||
|
Mockito.verify(containerFactory).setPort(8080);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@ -70,4 +70,14 @@ public class JavaLoggerConfigurerTests {
|
|||||||
JavaLoggerConfigurer.initLogging("classpath:logging-nonexistent.properties");
|
JavaLoggerConfigurer.initLogging("classpath:logging-nonexistent.properties");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testBadUrlConfigLocation() throws Exception {
|
||||||
|
JavaLoggerConfigurer.initLogging("http://nosuchhost");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testNullConfigLocation() throws Exception {
|
||||||
|
JavaLoggerConfigurer.initLogging(null);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -70,4 +70,14 @@ public class LogbackConfigurerTests {
|
|||||||
LogbackConfigurer.initLogging("classpath:logback-nonexistent.xml");
|
LogbackConfigurer.initLogging("classpath:logback-nonexistent.xml");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testBadUrlConfigLocation() throws Exception {
|
||||||
|
LogbackConfigurer.initLogging("http://nosuchhost/foo.xml");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testNullConfigLocation() throws Exception {
|
||||||
|
LogbackConfigurer.initLogging(null);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2013 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.bootstrap.maven;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.util.jar.JarOutputStream;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.core.io.ByteArrayResource;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Dave Syer
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class PropertiesMergingResourceTransformerTests {
|
||||||
|
|
||||||
|
private PropertiesMergingResourceTransformer transformer = new PropertiesMergingResourceTransformer();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testProcess() throws Exception {
|
||||||
|
assertFalse(this.transformer.hasTransformedResource());
|
||||||
|
this.transformer.processResource("foo",
|
||||||
|
new ByteArrayResource("foo=bar".getBytes()).getInputStream(), null);
|
||||||
|
assertTrue(this.transformer.hasTransformedResource());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMerge() throws Exception {
|
||||||
|
this.transformer.processResource("foo",
|
||||||
|
new ByteArrayResource("foo=bar".getBytes()).getInputStream(), null);
|
||||||
|
this.transformer.processResource("bar",
|
||||||
|
new ByteArrayResource("foo=spam".getBytes()).getInputStream(), null);
|
||||||
|
assertEquals("bar,spam", this.transformer.getData().getProperty("foo"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOutput() throws Exception {
|
||||||
|
this.transformer.resource = "foo";
|
||||||
|
this.transformer.processResource("foo",
|
||||||
|
new ByteArrayResource("foo=bar".getBytes()).getInputStream(), null);
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
JarOutputStream os = new JarOutputStream(out);
|
||||||
|
this.transformer.modifyOutputStream(os);
|
||||||
|
os.flush();
|
||||||
|
os.close();
|
||||||
|
assertNotNull(out.toByteArray());
|
||||||
|
assertTrue(out.toByteArray().length > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -17,6 +17,8 @@ package org.springframework.bootstrap.properties;
|
|||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.MutablePropertyValues;
|
import org.springframework.beans.MutablePropertyValues;
|
||||||
@ -50,4 +52,19 @@ public class ServerPropertiesTests {
|
|||||||
assertEquals(9000, this.properties.getPort());
|
assertEquals(9000, this.properties.getPort());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTomcatBinding() throws Exception {
|
||||||
|
Map<String, String> map = new HashMap<String, String>();
|
||||||
|
map.put("server.tomcat.access_log_pattern", "%h %t '%r' %s %b");
|
||||||
|
map.put("server.tomcat.protocol_header", "X-Forwarded-Protocol");
|
||||||
|
map.put("server.tomcat.remote_ip_header", "Remote-Ip");
|
||||||
|
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
|
||||||
|
map));
|
||||||
|
assertEquals("%h %t '%r' %s %b", this.properties.getTomcat()
|
||||||
|
.getAccessLogPattern());
|
||||||
|
assertEquals("Remote-Ip", this.properties.getTomcat().getRemoteIpHeader());
|
||||||
|
assertEquals("X-Forwarded-Protocol", this.properties.getTomcat()
|
||||||
|
.getProtocolHeader());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user