Browse Source

Add explicit test for config file and SpringApplication

pull/138/head
Dave Syer 12 years ago
parent
commit
9db55a3b71
  1. 1
      spring-boot-samples/spring-boot-sample-xml/src/main/java/org/springframework/boot/sample/xml/SampleSpringXmlApplication.java
  2. 9
      spring-boot/src/main/java/org/springframework/boot/config/PropertiesPropertySourceLoader.java
  3. 3
      spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java
  4. 12
      spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java
  5. 2
      spring-boot/src/test/java/org/springframework/boot/context/initializer/LoggingApplicationContextInitializerTests.java
  6. 3
      spring-boot/src/test/resources/logback-test.xml

1
spring-boot-samples/spring-boot-sample-xml/src/main/java/org/springframework/boot/sample/xml/SampleSpringXmlApplication.java

@ -32,7 +32,6 @@ public class SampleSpringXmlApplication implements CommandLineRunner { @@ -32,7 +32,6 @@ public class SampleSpringXmlApplication implements CommandLineRunner {
}
public static void main(String[] args) throws Exception {
// TODO: to make this a pure XML example, will need <boot:auto-configure/>
SpringApplication.run("classpath:/META-INF/application-context.xml", args);
}
}

9
spring-boot/src/main/java/org/springframework/boot/config/PropertiesPropertySourceLoader.java

@ -19,6 +19,8 @@ package org.springframework.boot.config; @@ -19,6 +19,8 @@ package org.springframework.boot.config;
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
@ -31,6 +33,8 @@ import org.springframework.core.io.support.PropertiesLoaderUtils; @@ -31,6 +33,8 @@ import org.springframework.core.io.support.PropertiesLoaderUtils;
*/
public class PropertiesPropertySourceLoader implements PropertySourceLoader {
private static Log logger = LogFactory.getLog(PropertiesPropertySourceLoader.class);
@Override
public boolean supports(Resource resource) {
return resource.getFilename().endsWith(".properties");
@ -40,6 +44,11 @@ public class PropertiesPropertySourceLoader implements PropertySourceLoader { @@ -40,6 +44,11 @@ public class PropertiesPropertySourceLoader implements PropertySourceLoader {
public PropertySource<?> load(Resource resource) {
try {
Properties properties = loadProperties(resource);
// N.B. this is off by default unless user has supplied logback config in
// standard location
if (logger.isDebugEnabled()) {
logger.debug("Properties loaded from " + resource + ": " + properties);
}
return new PropertiesPropertySource(resource.getDescription(), properties);
}
catch (IOException ex) {

3
spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java

@ -59,7 +59,8 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem { @@ -59,7 +59,8 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem {
}
public LogbackLoggingSystem(ClassLoader classLoader) {
super(classLoader, "logback.xml");
super(classLoader, "logback-test.groovy", "logback-test.xml", "logback.groovy",
"logback.xml");
}
@Override

12
spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

@ -273,6 +273,16 @@ public class SpringApplicationTests { @@ -273,6 +273,16 @@ public class SpringApplicationTests {
assertEquals("bar", environment.getProperty("foo"));
}
@Test
public void proprtiesFileEnhancesEnvironment() throws Exception {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
ConfigurableEnvironment environment = new StandardEnvironment();
application.setEnvironment(environment);
application.run();
assertEquals("bucket", environment.getProperty("foo"));
}
@Test
public void emptyCommandLinePropertySourceNotAdded() throws Exception {
SpringApplication application = new SpringApplication(ExampleConfig.class);
@ -404,8 +414,6 @@ public class SpringApplicationTests { @@ -404,8 +414,6 @@ public class SpringApplicationTests {
// FIXME test initializers
// FIXME test config files?
public static class SpyApplicationContext extends AnnotationConfigApplicationContext {
ConfigurableApplicationContext applicationContext = spy(new AnnotationConfigApplicationContext());

2
spring-boot/src/test/java/org/springframework/boot/context/initializer/LoggingApplicationContextInitializerTests.java

@ -153,7 +153,7 @@ public class LoggingApplicationContextInitializerTests { @@ -153,7 +153,7 @@ public class LoggingApplicationContextInitializerTests {
@Test
public void parseArgsDisabled() throws Exception {
this.initializer.setParseArgs(false);
this.initializer.initialize(this.springApplication, new String[] { "--debug" });
TestUtils.addEnviroment(this.context, "debug");
this.initializer.initialize(this.context);
this.logger.debug("testatdebug");
assertThat(this.outputCapture.toString(), not(containsString("testatdebug")));

3
spring-boot/src/test/resources/logback-test.xml

@ -1,14 +1,11 @@ @@ -1,14 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_PATTERN" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] spring - ${PID:-????} %5p [%t] --- %c{1}: %m%n"/>
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-/tmp/logs/service.log}}"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<logger name="org.springframework.boot.context.annotation" level="TRACE" />
<logger name="org.springframework.boot.context.initializer" level="TRACE" />
<logger name="org.springframework.boot.config" level="TRACE" />
<logger name="org.thymeleaf" level="TRACE" />
<root level="INFO">

Loading…
Cancel
Save