Browse Source

Add unit tests for YAML parsing errors

Test for gh-235
pull/244/head
Dave Syer 12 years ago
parent
commit
e7675a0630
  1. 19
      spring-boot/src/test/java/org/springframework/boot/config/YamlProcessorTests.java
  2. 16
      spring-boot/src/test/java/org/springframework/boot/config/YamlPropertiesFactoryBeanTests.java

19
spring-boot/src/test/java/org/springframework/boot/config/YamlProcessorTests.java

@ -18,10 +18,13 @@ package org.springframework.boot.config;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.config.YamlProcessor.MatchCallback; import org.springframework.boot.config.YamlProcessor.MatchCallback;
import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.yaml.snakeyaml.scanner.ScannerException;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -34,6 +37,9 @@ public class YamlProcessorTests {
private YamlProcessor processor = new YamlProcessor(); private YamlProcessor processor = new YamlProcessor();
@Rule
public ExpectedException exception = ExpectedException.none();
@Test @Test
public void arrayConvertedToIndexedBeanReference() { public void arrayConvertedToIndexedBeanReference() {
this.processor.setResources(new Resource[] { new ByteArrayResource( this.processor.setResources(new Resource[] { new ByteArrayResource(
@ -49,6 +55,19 @@ public class YamlProcessorTests {
}); });
} }
@Test
public void testBadResource() throws Exception {
this.processor.setResources(new Resource[] { new ByteArrayResource(
"foo: bar\ncd\nspam:\n foo: baz".getBytes()) });
this.exception.expect(ScannerException.class);
this.exception.expectMessage("line 3, column 1");
this.processor.process(new MatchCallback() {
@Override
public void process(Properties properties, Map<String, Object> map) {
}
});
}
@Test @Test
public void mapConvertedToIndexedBeanReference() { public void mapConvertedToIndexedBeanReference() {
this.processor.setResources(new Resource[] { new ByteArrayResource( this.processor.setResources(new Resource[] { new ByteArrayResource(

16
spring-boot/src/test/java/org/springframework/boot/config/YamlPropertiesFactoryBeanTests.java

@ -21,7 +21,9 @@ import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.config.YamlProcessor.DocumentMatcher; import org.springframework.boot.config.YamlProcessor.DocumentMatcher;
import org.springframework.boot.config.YamlProcessor.MatchStatus; import org.springframework.boot.config.YamlProcessor.MatchStatus;
import org.springframework.boot.config.YamlProcessor.ResolutionMethod; import org.springframework.boot.config.YamlProcessor.ResolutionMethod;
@ -29,6 +31,7 @@ import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.scanner.ScannerException;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -39,6 +42,9 @@ import static org.junit.Assert.assertEquals;
*/ */
public class YamlPropertiesFactoryBeanTests { public class YamlPropertiesFactoryBeanTests {
@Rule
public ExpectedException exception = ExpectedException.none();
@Test @Test
public void testLoadResource() throws Exception { public void testLoadResource() throws Exception {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
@ -49,6 +55,16 @@ public class YamlPropertiesFactoryBeanTests {
assertEquals("baz", properties.get("spam.foo")); assertEquals("baz", properties.get("spam.foo"));
} }
@Test
public void testBadResource() throws Exception {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(new Resource[] { new ByteArrayResource(
"foo: bar\ncd\nspam:\n foo: baz".getBytes()) });
this.exception.expect(ScannerException.class);
this.exception.expectMessage("line 3, column 1");
factory.getObject();
}
@Test @Test
public void testLoadResourcesWithOverride() throws Exception { public void testLoadResourcesWithOverride() throws Exception {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();

Loading…
Cancel
Save