|
|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2014 the original author or authors. |
|
|
|
|
* Copyright 2002-2016 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. |
|
|
|
|
@ -13,6 +13,7 @@
@@ -13,6 +13,7 @@
|
|
|
|
|
* See the License for the specific language governing permissions and |
|
|
|
|
* limitations under the License. |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
package org.springframework.beans.factory.config; |
|
|
|
|
|
|
|
|
|
import java.util.LinkedHashMap; |
|
|
|
|
@ -24,6 +25,7 @@ import org.junit.Test;
@@ -24,6 +25,7 @@ import org.junit.Test;
|
|
|
|
|
import org.junit.rules.ExpectedException; |
|
|
|
|
import org.yaml.snakeyaml.parser.ParserException; |
|
|
|
|
import org.yaml.snakeyaml.scanner.ScannerException; |
|
|
|
|
|
|
|
|
|
import org.springframework.core.io.ByteArrayResource; |
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
@ -33,34 +35,38 @@ import static org.springframework.beans.factory.config.YamlProcessor.*;
@@ -33,34 +35,38 @@ import static org.springframework.beans.factory.config.YamlProcessor.*;
|
|
|
|
|
* Tests for {@link YamlProcessor}. |
|
|
|
|
* |
|
|
|
|
* @author Dave Syer |
|
|
|
|
* @author Juergen Hoeller |
|
|
|
|
*/ |
|
|
|
|
public class YamlProcessorTests { |
|
|
|
|
|
|
|
|
|
private final YamlProcessor processor = new YamlProcessor() { |
|
|
|
|
}; |
|
|
|
|
private final YamlProcessor processor = new YamlProcessor() {}; |
|
|
|
|
|
|
|
|
|
@Rule |
|
|
|
|
public ExpectedException exception = ExpectedException.none(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void arrayConvertedToIndexedBeanReference() { |
|
|
|
|
this.processor.setResources(new ByteArrayResource( |
|
|
|
|
"foo: bar\nbar: [1,2,3]".getBytes())); |
|
|
|
|
this.processor.setResources(new ByteArrayResource("foo: bar\nbar: [1,2,3]".getBytes())); |
|
|
|
|
this.processor.process(new MatchCallback() { |
|
|
|
|
@Override |
|
|
|
|
public void process(Properties properties, Map<String, Object> map) { |
|
|
|
|
assertEquals(4, properties.size()); |
|
|
|
|
assertEquals("bar", properties.get("foo")); |
|
|
|
|
assertEquals("bar", properties.getProperty("foo")); |
|
|
|
|
assertEquals(1, properties.get("bar[0]")); |
|
|
|
|
assertEquals("1", properties.getProperty("bar[0]")); |
|
|
|
|
assertEquals(2, properties.get("bar[1]")); |
|
|
|
|
assertEquals("2", properties.getProperty("bar[1]")); |
|
|
|
|
assertEquals(3, properties.get("bar[2]")); |
|
|
|
|
assertEquals(4, properties.size()); |
|
|
|
|
assertEquals("3", properties.getProperty("bar[2]")); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testStringResource() throws Exception { |
|
|
|
|
this.processor.setResources(new ByteArrayResource( |
|
|
|
|
"foo # a document that is a literal".getBytes())); |
|
|
|
|
this.processor.setResources(new ByteArrayResource("foo # a document that is a literal".getBytes())); |
|
|
|
|
this.processor.process(new MatchCallback() { |
|
|
|
|
@Override |
|
|
|
|
public void process(Properties properties, Map<String, Object> map) { |
|
|
|
|
@ -71,8 +77,7 @@ public class YamlProcessorTests {
@@ -71,8 +77,7 @@ public class YamlProcessorTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testBadDocumentStart() throws Exception { |
|
|
|
|
this.processor.setResources(new ByteArrayResource( |
|
|
|
|
"foo # a document\nbar: baz".getBytes())); |
|
|
|
|
this.processor.setResources(new ByteArrayResource("foo # a document\nbar: baz".getBytes())); |
|
|
|
|
this.exception.expect(ParserException.class); |
|
|
|
|
this.exception.expectMessage("line 2, column 1"); |
|
|
|
|
this.processor.process(new MatchCallback() { |
|
|
|
|
@ -84,8 +89,7 @@ public class YamlProcessorTests {
@@ -84,8 +89,7 @@ public class YamlProcessorTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testBadResource() throws Exception { |
|
|
|
|
this.processor.setResources(new ByteArrayResource( |
|
|
|
|
"foo: bar\ncd\nspam:\n foo: baz".getBytes())); |
|
|
|
|
this.processor.setResources(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() { |
|
|
|
|
@ -97,8 +101,7 @@ public class YamlProcessorTests {
@@ -97,8 +101,7 @@ public class YamlProcessorTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void mapConvertedToIndexedBeanReference() { |
|
|
|
|
this.processor.setResources(new ByteArrayResource( |
|
|
|
|
"foo: bar\nbar:\n spam: bucket".getBytes())); |
|
|
|
|
this.processor.setResources(new ByteArrayResource("foo: bar\nbar:\n spam: bucket".getBytes())); |
|
|
|
|
this.processor.process(new MatchCallback() { |
|
|
|
|
@Override |
|
|
|
|
public void process(Properties properties, Map<String, Object> map) { |
|
|
|
|
@ -111,8 +114,7 @@ public class YamlProcessorTests {
@@ -111,8 +114,7 @@ public class YamlProcessorTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void integerKeyBehaves() { |
|
|
|
|
this.processor.setResources(new ByteArrayResource( |
|
|
|
|
"foo: bar\n1: bar".getBytes())); |
|
|
|
|
this.processor.setResources(new ByteArrayResource("foo: bar\n1: bar".getBytes())); |
|
|
|
|
this.processor.process(new MatchCallback() { |
|
|
|
|
@Override |
|
|
|
|
public void process(Properties properties, Map<String, Object> map) { |
|
|
|
|
@ -124,10 +126,8 @@ public class YamlProcessorTests {
@@ -124,10 +126,8 @@ public class YamlProcessorTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void integerDeepKeyBehaves() { |
|
|
|
|
this.processor.setResources(new ByteArrayResource( |
|
|
|
|
"foo:\n 1: bar".getBytes())); |
|
|
|
|
this.processor.setResources(new ByteArrayResource("foo:\n 1: bar".getBytes())); |
|
|
|
|
this.processor.process(new MatchCallback() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void process(Properties properties, Map<String, Object> map) { |
|
|
|
|
assertEquals("bar", properties.get("foo[1]")); |
|
|
|
|
@ -139,8 +139,7 @@ public class YamlProcessorTests {
@@ -139,8 +139,7 @@ public class YamlProcessorTests {
|
|
|
|
|
@Test |
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
public void flattenedMapIsSameAsPropertiesButOrdered() { |
|
|
|
|
this.processor.setResources(new ByteArrayResource( |
|
|
|
|
"foo: bar\nbar:\n spam: bucket".getBytes())); |
|
|
|
|
this.processor.setResources(new ByteArrayResource("foo: bar\nbar:\n spam: bucket".getBytes())); |
|
|
|
|
this.processor.process(new MatchCallback() { |
|
|
|
|
@Override |
|
|
|
|
public void process(Properties properties, Map<String, Object> map) { |
|
|
|
|
@ -155,4 +154,5 @@ public class YamlProcessorTests {
@@ -155,4 +154,5 @@ public class YamlProcessorTests {
|
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|