diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/jdbc/AbstractDataSourceConfiguration.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/jdbc/AbstractDataSourceConfiguration.java index c1e58eca88..249c85f19f 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/jdbc/AbstractDataSourceConfiguration.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/jdbc/AbstractDataSourceConfiguration.java @@ -67,7 +67,7 @@ public class AbstractDataSourceConfiguration { .getEmbeddedDatabaseType(); this.url = EmbeddedDatabaseConfiguration .getEmbeddedDatabaseUrl(embeddedDatabaseType); - if (!StringUtils.hasText(this.driverClassName)) { + if (!StringUtils.hasText(this.url)) { throw new BeanCreationException( "Cannot determine embedded database url for database type " + embeddedDatabaseType diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/maven/PropertiesMergingResourceTransformer.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/maven/PropertiesMergingResourceTransformer.java index d9bb18b6a2..7369cb9d98 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/maven/PropertiesMergingResourceTransformer.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/maven/PropertiesMergingResourceTransformer.java @@ -39,6 +39,13 @@ public class PropertiesMergingResourceTransformer implements ResourceTransformer private Properties data = new Properties(); + /** + * @return the data the properties being merged + */ + public Properties getData() { + return this.data; + } + @Override public boolean canTransformResource(String resource) { if (this.resource != null && this.resource.equalsIgnoreCase(resource)) { diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/TestUtils.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/TestUtils.java new file mode 100644 index 0000000000..3cb7d00277 --- /dev/null +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/TestUtils.java @@ -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 map = new HashMap(); + 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)); + } + +} diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/BasicDataSourceConfigurationTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/BasicDataSourceConfigurationTests.java index e7dc1e0af3..5bd94ec791 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/BasicDataSourceConfigurationTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/BasicDataSourceConfigurationTests.java @@ -35,6 +35,7 @@ public class BasicDataSourceConfigurationTests { this.context.register(BasicDataSourceConfiguration.class); this.context.refresh(); assertNotNull(this.context.getBean(DataSource.class)); + context.close(); } } diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/EmbeddedDatabaseConfigurationTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/EmbeddedDatabaseConfigurationTests.java index c8fabe2658..8880b6a770 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/EmbeddedDatabaseConfigurationTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/EmbeddedDatabaseConfigurationTests.java @@ -36,6 +36,7 @@ public class EmbeddedDatabaseConfigurationTests { this.context.register(EmbeddedDatabaseConfiguration.class); this.context.refresh(); assertNotNull(this.context.getBean(DataSource.class)); + this.context.close(); } } diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/TomcatDataSourceConfigurationTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/TomcatDataSourceConfigurationTests.java index 300f5c4815..3335f43c00 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/TomcatDataSourceConfigurationTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/TomcatDataSourceConfigurationTests.java @@ -15,10 +15,18 @@ */ package org.springframework.bootstrap.autoconfigure.jdbc; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + import javax.sql.DataSource; +import org.junit.After; 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.util.ReflectionUtils; import static org.junit.Assert.assertNotNull; @@ -29,6 +37,15 @@ import static org.junit.Assert.assertNotNull; public class TomcatDataSourceConfigurationTests { private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + private Map old; + private Map map; + + @After + public void restore() { + if (this.map != null && this.old != null) { + this.map.putAll(this.old); + } + } @Test public void testDataSourceExists() throws Exception { @@ -37,4 +54,33 @@ public class TomcatDataSourceConfigurationTests { 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(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(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 getField(Class target, String name) { + Field field = ReflectionUtils.findField(target, name, null); + ReflectionUtils.makeAccessible(field); + return (T) ReflectionUtils.getField(field, target); + } } diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/web/ServerPropertiesConfigurationTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/web/ServerPropertiesConfigurationTests.java index b9d6f295f0..969726fe46 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/web/ServerPropertiesConfigurationTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/web/ServerPropertiesConfigurationTests.java @@ -15,20 +15,21 @@ */ package org.springframework.bootstrap.autoconfigure.web; -import java.util.HashMap; -import java.util.Map; +import java.io.File; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; +import org.springframework.bootstrap.TestUtils; import org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.bootstrap.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.bootstrap.context.embedded.ConfigurableEmbeddedServletContainerFactory; import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory; +import org.springframework.bootstrap.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.bootstrap.properties.ServerProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.MapPropertySource; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -43,27 +44,48 @@ public class ServerPropertiesConfigurationTests { private AnnotationConfigEmbeddedWebApplicationContext context; - private Map environment = new HashMap(); - @Before public void init() { containerFactory = Mockito .mock(ConfigurableEmbeddedServletContainerFactory.class); } + @After + public void close() { + if (this.context != null) { + this.context.close(); + } + } + @Test public void createFromConfigClass() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(EmbeddedContainerConfiguration.class, + EmbeddedContainerCustomizerConfiguration.class, ServerPropertiesConfiguration.class, PropertyPlaceholderAutoConfiguration.class); - this.environment.put("server.port", "9000"); - this.context.getEnvironment().getPropertySources() - .addFirst(new MapPropertySource("test", this.environment)); + TestUtils.addEnviroment(this.context, "server.port:9000"); this.context.refresh(); ServerProperties server = this.context.getBean(ServerProperties.class); assertNotNull(server); 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 diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/logging/JavaLoggerConfigurerTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/logging/JavaLoggerConfigurerTests.java index f15dab1a9a..89611cf4b8 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/logging/JavaLoggerConfigurerTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/logging/JavaLoggerConfigurerTests.java @@ -70,4 +70,14 @@ public class JavaLoggerConfigurerTests { 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); + } + } diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/logging/LogbackConfigurerTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/logging/LogbackConfigurerTests.java index 2b170fdd5d..2f5d787150 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/logging/LogbackConfigurerTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/logging/LogbackConfigurerTests.java @@ -70,4 +70,14 @@ public class LogbackConfigurerTests { 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); + } + } diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/maven/PropertiesMergingResourceTransformerTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/maven/PropertiesMergingResourceTransformerTests.java new file mode 100644 index 0000000000..b8fc198049 --- /dev/null +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/maven/PropertiesMergingResourceTransformerTests.java @@ -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); + } + +} diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/properties/ServerPropertiesTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/properties/ServerPropertiesTests.java index 2cde04b9cd..c3011b3546 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/properties/ServerPropertiesTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/properties/ServerPropertiesTests.java @@ -17,6 +17,8 @@ package org.springframework.bootstrap.properties; import java.net.InetAddress; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import org.junit.Test; import org.springframework.beans.MutablePropertyValues; @@ -50,4 +52,19 @@ public class ServerPropertiesTests { assertEquals(9000, this.properties.getPort()); } + @Test + public void testTomcatBinding() throws Exception { + Map map = new HashMap(); + 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()); + } + }