|
|
|
|
@ -19,11 +19,11 @@ package org.springframework.beans.factory.config;
@@ -19,11 +19,11 @@ package org.springframework.beans.factory.config;
|
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Properties; |
|
|
|
|
|
|
|
|
|
import org.junit.Ignore; |
|
|
|
|
import org.junit.Rule; |
|
|
|
|
import org.junit.Test; |
|
|
|
|
import org.junit.rules.ExpectedException; |
|
|
|
|
import org.yaml.snakeyaml.Yaml; |
|
|
|
|
import org.yaml.snakeyaml.constructor.DuplicateKeyException; |
|
|
|
|
import org.yaml.snakeyaml.scanner.ScannerException; |
|
|
|
|
|
|
|
|
|
import org.springframework.core.io.ByteArrayResource; |
|
|
|
|
@ -46,17 +46,16 @@ public class YamlPropertiesFactoryBeanTests {
@@ -46,17 +46,16 @@ public class YamlPropertiesFactoryBeanTests {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testLoadResource() throws Exception { |
|
|
|
|
public void testLoadResource() { |
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
|
|
|
|
factory.setResources(new ByteArrayResource( |
|
|
|
|
"foo: bar\nspam:\n foo: baz".getBytes())); |
|
|
|
|
factory.setResources(new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); |
|
|
|
|
Properties properties = factory.getObject(); |
|
|
|
|
assertThat(properties.getProperty("foo"), equalTo("bar")); |
|
|
|
|
assertThat(properties.getProperty("spam.foo"), equalTo("baz")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testBadResource() throws Exception { |
|
|
|
|
public void testBadResource() { |
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
|
|
|
|
factory.setResources(new ByteArrayResource( |
|
|
|
|
"foo: bar\ncd\nspam:\n foo: baz".getBytes())); |
|
|
|
|
@ -66,7 +65,7 @@ public class YamlPropertiesFactoryBeanTests {
@@ -66,7 +65,7 @@ public class YamlPropertiesFactoryBeanTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testLoadResourcesWithOverride() throws Exception { |
|
|
|
|
public void testLoadResourcesWithOverride() { |
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
|
|
|
|
factory.setResources( |
|
|
|
|
new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes()), |
|
|
|
|
@ -77,27 +76,24 @@ public class YamlPropertiesFactoryBeanTests {
@@ -77,27 +76,24 @@ public class YamlPropertiesFactoryBeanTests {
|
|
|
|
|
assertThat(properties.getProperty("foo.bar"), equalTo("spam")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testLoadResourcesWithInternalOverride() throws Exception { |
|
|
|
|
@Test(expected = DuplicateKeyException.class) |
|
|
|
|
public void testLoadResourcesWithInternalOverride() { |
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
|
|
|
|
factory.setResources(new ByteArrayResource( |
|
|
|
|
"foo: bar\nspam:\n foo: baz\nfoo: bucket".getBytes())); |
|
|
|
|
Properties properties = factory.getObject(); |
|
|
|
|
assertThat(properties.getProperty("foo"), equalTo("bucket")); |
|
|
|
|
factory.getObject(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
@Ignore("We can't fail on duplicate keys because the Map is created by the YAML library") |
|
|
|
|
public void testLoadResourcesWithNestedInternalOverride() throws Exception { |
|
|
|
|
@Test(expected = DuplicateKeyException.class) |
|
|
|
|
public void testLoadResourcesWithNestedInternalOverride() { |
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
|
|
|
|
factory.setResources(new ByteArrayResource( |
|
|
|
|
"foo:\n bar: spam\n foo: baz\nbreak: it\nfoo: bucket".getBytes())); |
|
|
|
|
Properties properties = factory.getObject(); |
|
|
|
|
assertThat(properties.getProperty("foo.bar"), equalTo("spam")); |
|
|
|
|
factory.getObject(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testLoadResourceWithMultipleDocuments() throws Exception { |
|
|
|
|
public void testLoadResourceWithMultipleDocuments() { |
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
|
|
|
|
factory.setResources(new ByteArrayResource( |
|
|
|
|
"foo: bar\nspam: baz\n---\nfoo: bag".getBytes())); |
|
|
|
|
@ -107,37 +103,29 @@ public class YamlPropertiesFactoryBeanTests {
@@ -107,37 +103,29 @@ public class YamlPropertiesFactoryBeanTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testLoadResourceWithSelectedDocuments() throws Exception { |
|
|
|
|
public void testLoadResourceWithSelectedDocuments() { |
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
|
|
|
|
factory.setResources(new ByteArrayResource( |
|
|
|
|
"foo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes())); |
|
|
|
|
factory.setDocumentMatchers(new DocumentMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public MatchStatus matches(Properties properties) { |
|
|
|
|
return ("bag".equals(properties.getProperty("foo")) ? |
|
|
|
|
MatchStatus.FOUND : MatchStatus.NOT_FOUND); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
factory.setDocumentMatchers((DocumentMatcher) properties -> ("bag".equals(properties.getProperty("foo")) ? |
|
|
|
|
MatchStatus.FOUND : MatchStatus.NOT_FOUND)); |
|
|
|
|
Properties properties = factory.getObject(); |
|
|
|
|
assertThat(properties.getProperty("foo"), equalTo("bag")); |
|
|
|
|
assertThat(properties.getProperty("spam"), equalTo("bad")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testLoadResourceWithDefaultMatch() throws Exception { |
|
|
|
|
public void testLoadResourceWithDefaultMatch() { |
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
|
|
|
|
factory.setMatchDefault(true); |
|
|
|
|
factory.setResources(new ByteArrayResource( |
|
|
|
|
"one: two\n---\nfoo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes())); |
|
|
|
|
factory.setDocumentMatchers(new DocumentMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public MatchStatus matches(Properties properties) { |
|
|
|
|
if (!properties.containsKey("foo")) { |
|
|
|
|
return MatchStatus.ABSTAIN; |
|
|
|
|
} |
|
|
|
|
return ("bag".equals(properties.getProperty("foo")) ? |
|
|
|
|
MatchStatus.FOUND : MatchStatus.NOT_FOUND); |
|
|
|
|
factory.setDocumentMatchers((DocumentMatcher) properties -> { |
|
|
|
|
if (!properties.containsKey("foo")) { |
|
|
|
|
return MatchStatus.ABSTAIN; |
|
|
|
|
} |
|
|
|
|
return ("bag".equals(properties.getProperty("foo")) ? |
|
|
|
|
MatchStatus.FOUND : MatchStatus.NOT_FOUND); |
|
|
|
|
}); |
|
|
|
|
Properties properties = factory.getObject(); |
|
|
|
|
assertThat(properties.getProperty("foo"), equalTo("bag")); |
|
|
|
|
@ -146,7 +134,7 @@ public class YamlPropertiesFactoryBeanTests {
@@ -146,7 +134,7 @@ public class YamlPropertiesFactoryBeanTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testLoadResourceWithoutDefaultMatch() throws Exception { |
|
|
|
|
public void testLoadResourceWithoutDefaultMatch() { |
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
|
|
|
|
factory.setMatchDefault(false); |
|
|
|
|
factory.setResources(new ByteArrayResource( |
|
|
|
|
@ -168,20 +156,17 @@ public class YamlPropertiesFactoryBeanTests {
@@ -168,20 +156,17 @@ public class YamlPropertiesFactoryBeanTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testLoadResourceWithDefaultMatchSkippingMissedMatch() throws Exception { |
|
|
|
|
public void testLoadResourceWithDefaultMatchSkippingMissedMatch() { |
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
|
|
|
|
factory.setMatchDefault(true); |
|
|
|
|
factory.setResources(new ByteArrayResource( |
|
|
|
|
"one: two\n---\nfoo: bag\nspam: bad\n---\nfoo: bar\nspam: baz".getBytes())); |
|
|
|
|
factory.setDocumentMatchers(new DocumentMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public MatchStatus matches(Properties properties) { |
|
|
|
|
if (!properties.containsKey("foo")) { |
|
|
|
|
return MatchStatus.ABSTAIN; |
|
|
|
|
} |
|
|
|
|
return ("bag".equals(properties.getProperty("foo")) ? |
|
|
|
|
MatchStatus.FOUND : MatchStatus.NOT_FOUND); |
|
|
|
|
factory.setDocumentMatchers((DocumentMatcher) properties -> { |
|
|
|
|
if (!properties.containsKey("foo")) { |
|
|
|
|
return MatchStatus.ABSTAIN; |
|
|
|
|
} |
|
|
|
|
return ("bag".equals(properties.getProperty("foo")) ? |
|
|
|
|
MatchStatus.FOUND : MatchStatus.NOT_FOUND); |
|
|
|
|
}); |
|
|
|
|
Properties properties = factory.getObject(); |
|
|
|
|
assertThat(properties.getProperty("foo"), equalTo("bag")); |
|
|
|
|
@ -190,7 +175,7 @@ public class YamlPropertiesFactoryBeanTests {
@@ -190,7 +175,7 @@ public class YamlPropertiesFactoryBeanTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testLoadNonExistentResource() throws Exception { |
|
|
|
|
public void testLoadNonExistentResource() { |
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
|
|
|
|
factory.setResolutionMethod(ResolutionMethod.OVERRIDE_AND_IGNORE); |
|
|
|
|
factory.setResources(new ClassPathResource("no-such-file.yml")); |
|
|
|
|
@ -199,7 +184,7 @@ public class YamlPropertiesFactoryBeanTests {
@@ -199,7 +184,7 @@ public class YamlPropertiesFactoryBeanTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testLoadNull() throws Exception { |
|
|
|
|
public void testLoadNull() { |
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
|
|
|
|
factory.setResources(new ByteArrayResource("foo: bar\nspam:".getBytes())); |
|
|
|
|
Properties properties = factory.getObject(); |
|
|
|
|
@ -217,7 +202,7 @@ public class YamlPropertiesFactoryBeanTests {
@@ -217,7 +202,7 @@ public class YamlPropertiesFactoryBeanTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testLoadArrayOfString() throws Exception { |
|
|
|
|
public void testLoadArrayOfString() { |
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
|
|
|
|
factory.setResources(new ByteArrayResource("foo:\n- bar\n- baz".getBytes())); |
|
|
|
|
Properties properties = factory.getObject(); |
|
|
|
|
@ -227,7 +212,7 @@ public class YamlPropertiesFactoryBeanTests {
@@ -227,7 +212,7 @@ public class YamlPropertiesFactoryBeanTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testLoadArrayOfInteger() throws Exception { |
|
|
|
|
public void testLoadArrayOfInteger() { |
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
|
|
|
|
factory.setResources(new ByteArrayResource("foo:\n- 1\n- 2".getBytes())); |
|
|
|
|
Properties properties = factory.getObject(); |
|
|
|
|
@ -237,7 +222,7 @@ public class YamlPropertiesFactoryBeanTests {
@@ -237,7 +222,7 @@ public class YamlPropertiesFactoryBeanTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testLoadArrayOfObject() throws Exception { |
|
|
|
|
public void testLoadArrayOfObject() { |
|
|
|
|
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); |
|
|
|
|
factory.setResources(new ByteArrayResource( |
|
|
|
|
"foo:\n- bar:\n spam: crap\n- baz\n- one: two\n three: four".getBytes() |
|
|
|
|
@ -255,8 +240,8 @@ public class YamlPropertiesFactoryBeanTests {
@@ -255,8 +240,8 @@ public class YamlPropertiesFactoryBeanTests {
|
|
|
|
|
public void testYaml() { |
|
|
|
|
Yaml yaml = new Yaml(); |
|
|
|
|
Map<String, ?> map = yaml.loadAs("foo: bar\nspam:\n foo: baz", Map.class); |
|
|
|
|
assertThat(map.get("foo"), equalTo((Object) "bar")); |
|
|
|
|
assertThat(((Map<String, Object>) map.get("spam")).get("foo"), equalTo((Object) "baz")); |
|
|
|
|
assertThat(map.get("foo"), equalTo("bar")); |
|
|
|
|
assertThat(((Map<String, Object>) map.get("spam")).get("foo"), equalTo("baz")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|