Correct code example for YamlProcessor.setDocumentMatchers

Issue: SPR-16849

(cherry picked from commit 455d8ac)
This commit is contained in:
Juergen Hoeller
2018-06-11 16:46:45 +02:00
parent 874859493b
commit 357ca21675
2 changed files with 37 additions and 51 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@@ -47,17 +47,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()));
@@ -67,7 +66,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()),
@@ -79,7 +78,7 @@ public class YamlPropertiesFactoryBeanTests {
}
@Test
public void testLoadResourcesWithInternalOverride() throws Exception {
public void testLoadResourcesWithInternalOverride() {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(new ByteArrayResource(
"foo: bar\nspam:\n foo: baz\nfoo: bucket".getBytes()));
@@ -89,7 +88,7 @@ public class YamlPropertiesFactoryBeanTests {
@Test
@Ignore("We can't fail on duplicate keys because the Map is created by the YAML library")
public void testLoadResourcesWithNestedInternalOverride() throws Exception {
public void testLoadResourcesWithNestedInternalOverride() {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(new ByteArrayResource(
"foo:\n bar: spam\n foo: baz\nbreak: it\nfoo: bucket".getBytes()));
@@ -98,7 +97,7 @@ public class YamlPropertiesFactoryBeanTests {
}
@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()));
@@ -108,37 +107,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(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(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"));
@@ -147,7 +138,7 @@ public class YamlPropertiesFactoryBeanTests {
}
@Test
public void testLoadResourceWithoutDefaultMatch() throws Exception {
public void testLoadResourceWithoutDefaultMatch() {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setMatchDefault(false);
factory.setResources(new ByteArrayResource(
@@ -169,20 +160,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(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"));
@@ -191,7 +179,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"));
@@ -200,7 +188,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();
@@ -209,7 +197,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();
@@ -219,7 +207,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();
@@ -229,7 +217,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()
@@ -247,8 +235,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"));
}
}