Skip to content

Commit 357ca21

Browse files
committed
Correct code example for YamlProcessor.setDocumentMatchers
Issue: SPR-16849 (cherry picked from commit 455d8ac)
1 parent 8748594 commit 357ca21

File tree

2 files changed

+37
-51
lines changed

2 files changed

+37
-51
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/config/YamlProcessor.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,16 @@ public abstract class YamlProcessor {
7676
* name: My Cool App
7777
* </pre>
7878
* when mapped with
79-
* <code>documentMatchers = YamlProcessor.mapMatcher({"environment": "prod"})</code>
79+
* <pre class="code">
80+
* setDocumentMatchers(properties ->
81+
* ("prod".equals(properties.getProperty("environment")) ? MatchStatus.FOUND : MatchStatus.NOT_FOUND));
82+
* </pre>
8083
* would end up as
8184
* <pre class="code">
8285
* environment=prod
8386
* url=http://foo.bar.com
8487
* name=My Cool App
85-
* url=http://dev.bar.com
8688
* </pre>
87-
* @param matchers a map of keys to value patterns (regular expressions)
8889
*/
8990
public void setDocumentMatchers(DocumentMatcher... matchers) {
9091
this.documentMatchers = Arrays.asList(matchers);
@@ -93,8 +94,7 @@ public void setDocumentMatchers(DocumentMatcher... matchers) {
9394
/**
9495
* Flag indicating that a document for which all the
9596
* {@link #setDocumentMatchers(DocumentMatcher...) document matchers} abstain will
96-
* nevertheless match.
97-
* @param matchDefault the flag to set (default true)
97+
* nevertheless match. Default is {@code true}.
9898
*/
9999
public void setMatchDefault(boolean matchDefault) {
100100
this.matchDefault = matchDefault;
@@ -103,9 +103,7 @@ public void setMatchDefault(boolean matchDefault) {
103103
/**
104104
* Method to use for resolving resources. Each resource will be converted to a Map,
105105
* so this property is used to decide which map entries to keep in the final output
106-
* from this factory.
107-
* @param resolutionMethod the resolution method to set (defaults to
108-
* {@link ResolutionMethod#OVERRIDE}).
106+
* from this factory. Default is {@link ResolutionMethod#OVERRIDE}.
109107
*/
110108
public void setResolutionMethod(ResolutionMethod resolutionMethod) {
111109
Assert.notNull(resolutionMethod, "ResolutionMethod must not be null");

spring-beans/src/test/java/org/springframework/beans/factory/config/YamlPropertiesFactoryBeanTests.java

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,17 +47,16 @@ public class YamlPropertiesFactoryBeanTests {
4747

4848

4949
@Test
50-
public void testLoadResource() throws Exception {
50+
public void testLoadResource() {
5151
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
52-
factory.setResources(new ByteArrayResource(
53-
"foo: bar\nspam:\n foo: baz".getBytes()));
52+
factory.setResources(new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes()));
5453
Properties properties = factory.getObject();
5554
assertThat(properties.getProperty("foo"), equalTo("bar"));
5655
assertThat(properties.getProperty("spam.foo"), equalTo("baz"));
5756
}
5857

5958
@Test
60-
public void testBadResource() throws Exception {
59+
public void testBadResource() {
6160
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
6261
factory.setResources(new ByteArrayResource(
6362
"foo: bar\ncd\nspam:\n foo: baz".getBytes()));
@@ -67,7 +66,7 @@ public void testBadResource() throws Exception {
6766
}
6867

6968
@Test
70-
public void testLoadResourcesWithOverride() throws Exception {
69+
public void testLoadResourcesWithOverride() {
7170
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
7271
factory.setResources(
7372
new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes()),
@@ -79,7 +78,7 @@ public void testLoadResourcesWithOverride() throws Exception {
7978
}
8079

8180
@Test
82-
public void testLoadResourcesWithInternalOverride() throws Exception {
81+
public void testLoadResourcesWithInternalOverride() {
8382
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
8483
factory.setResources(new ByteArrayResource(
8584
"foo: bar\nspam:\n foo: baz\nfoo: bucket".getBytes()));
@@ -89,7 +88,7 @@ public void testLoadResourcesWithInternalOverride() throws Exception {
8988

9089
@Test
9190
@Ignore("We can't fail on duplicate keys because the Map is created by the YAML library")
92-
public void testLoadResourcesWithNestedInternalOverride() throws Exception {
91+
public void testLoadResourcesWithNestedInternalOverride() {
9392
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
9493
factory.setResources(new ByteArrayResource(
9594
"foo:\n bar: spam\n foo: baz\nbreak: it\nfoo: bucket".getBytes()));
@@ -98,7 +97,7 @@ public void testLoadResourcesWithNestedInternalOverride() throws Exception {
9897
}
9998

10099
@Test
101-
public void testLoadResourceWithMultipleDocuments() throws Exception {
100+
public void testLoadResourceWithMultipleDocuments() {
102101
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
103102
factory.setResources(new ByteArrayResource(
104103
"foo: bar\nspam: baz\n---\nfoo: bag".getBytes()));
@@ -108,37 +107,29 @@ public void testLoadResourceWithMultipleDocuments() throws Exception {
108107
}
109108

110109
@Test
111-
public void testLoadResourceWithSelectedDocuments() throws Exception {
110+
public void testLoadResourceWithSelectedDocuments() {
112111
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
113112
factory.setResources(new ByteArrayResource(
114113
"foo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes()));
115-
factory.setDocumentMatchers(new DocumentMatcher() {
116-
@Override
117-
public MatchStatus matches(Properties properties) {
118-
return ("bag".equals(properties.getProperty("foo")) ?
119-
MatchStatus.FOUND : MatchStatus.NOT_FOUND);
120-
}
121-
});
114+
factory.setDocumentMatchers(properties -> ("bag".equals(properties.getProperty("foo")) ?
115+
MatchStatus.FOUND : MatchStatus.NOT_FOUND));
122116
Properties properties = factory.getObject();
123117
assertThat(properties.getProperty("foo"), equalTo("bag"));
124118
assertThat(properties.getProperty("spam"), equalTo("bad"));
125119
}
126120

127121
@Test
128-
public void testLoadResourceWithDefaultMatch() throws Exception {
122+
public void testLoadResourceWithDefaultMatch() {
129123
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
130124
factory.setMatchDefault(true);
131125
factory.setResources(new ByteArrayResource(
132126
"one: two\n---\nfoo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes()));
133-
factory.setDocumentMatchers(new DocumentMatcher() {
134-
@Override
135-
public MatchStatus matches(Properties properties) {
136-
if (!properties.containsKey("foo")) {
137-
return MatchStatus.ABSTAIN;
138-
}
139-
return ("bag".equals(properties.getProperty("foo")) ?
140-
MatchStatus.FOUND : MatchStatus.NOT_FOUND);
127+
factory.setDocumentMatchers(properties -> {
128+
if (!properties.containsKey("foo")) {
129+
return MatchStatus.ABSTAIN;
141130
}
131+
return ("bag".equals(properties.getProperty("foo")) ?
132+
MatchStatus.FOUND : MatchStatus.NOT_FOUND);
142133
});
143134
Properties properties = factory.getObject();
144135
assertThat(properties.getProperty("foo"), equalTo("bag"));
@@ -147,7 +138,7 @@ public MatchStatus matches(Properties properties) {
147138
}
148139

149140
@Test
150-
public void testLoadResourceWithoutDefaultMatch() throws Exception {
141+
public void testLoadResourceWithoutDefaultMatch() {
151142
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
152143
factory.setMatchDefault(false);
153144
factory.setResources(new ByteArrayResource(
@@ -169,20 +160,17 @@ public MatchStatus matches(Properties properties) {
169160
}
170161

171162
@Test
172-
public void testLoadResourceWithDefaultMatchSkippingMissedMatch() throws Exception {
163+
public void testLoadResourceWithDefaultMatchSkippingMissedMatch() {
173164
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
174165
factory.setMatchDefault(true);
175166
factory.setResources(new ByteArrayResource(
176167
"one: two\n---\nfoo: bag\nspam: bad\n---\nfoo: bar\nspam: baz".getBytes()));
177-
factory.setDocumentMatchers(new DocumentMatcher() {
178-
@Override
179-
public MatchStatus matches(Properties properties) {
180-
if (!properties.containsKey("foo")) {
181-
return MatchStatus.ABSTAIN;
182-
}
183-
return ("bag".equals(properties.getProperty("foo")) ?
184-
MatchStatus.FOUND : MatchStatus.NOT_FOUND);
168+
factory.setDocumentMatchers(properties -> {
169+
if (!properties.containsKey("foo")) {
170+
return MatchStatus.ABSTAIN;
185171
}
172+
return ("bag".equals(properties.getProperty("foo")) ?
173+
MatchStatus.FOUND : MatchStatus.NOT_FOUND);
186174
});
187175
Properties properties = factory.getObject();
188176
assertThat(properties.getProperty("foo"), equalTo("bag"));
@@ -191,7 +179,7 @@ public MatchStatus matches(Properties properties) {
191179
}
192180

193181
@Test
194-
public void testLoadNonExistentResource() throws Exception {
182+
public void testLoadNonExistentResource() {
195183
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
196184
factory.setResolutionMethod(ResolutionMethod.OVERRIDE_AND_IGNORE);
197185
factory.setResources(new ClassPathResource("no-such-file.yml"));
@@ -200,7 +188,7 @@ public void testLoadNonExistentResource() throws Exception {
200188
}
201189

202190
@Test
203-
public void testLoadNull() throws Exception {
191+
public void testLoadNull() {
204192
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
205193
factory.setResources(new ByteArrayResource("foo: bar\nspam:".getBytes()));
206194
Properties properties = factory.getObject();
@@ -209,7 +197,7 @@ public void testLoadNull() throws Exception {
209197
}
210198

211199
@Test
212-
public void testLoadArrayOfString() throws Exception {
200+
public void testLoadArrayOfString() {
213201
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
214202
factory.setResources(new ByteArrayResource("foo:\n- bar\n- baz".getBytes()));
215203
Properties properties = factory.getObject();
@@ -219,7 +207,7 @@ public void testLoadArrayOfString() throws Exception {
219207
}
220208

221209
@Test
222-
public void testLoadArrayOfInteger() throws Exception {
210+
public void testLoadArrayOfInteger() {
223211
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
224212
factory.setResources(new ByteArrayResource("foo:\n- 1\n- 2".getBytes()));
225213
Properties properties = factory.getObject();
@@ -229,7 +217,7 @@ public void testLoadArrayOfInteger() throws Exception {
229217
}
230218

231219
@Test
232-
public void testLoadArrayOfObject() throws Exception {
220+
public void testLoadArrayOfObject() {
233221
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
234222
factory.setResources(new ByteArrayResource(
235223
"foo:\n- bar:\n spam: crap\n- baz\n- one: two\n three: four".getBytes()
@@ -247,8 +235,8 @@ public void testLoadArrayOfObject() throws Exception {
247235
public void testYaml() {
248236
Yaml yaml = new Yaml();
249237
Map<String, ?> map = yaml.loadAs("foo: bar\nspam:\n foo: baz", Map.class);
250-
assertThat(map.get("foo"), equalTo((Object) "bar"));
251-
assertThat(((Map<String, Object>) map.get("spam")).get("foo"), equalTo((Object) "baz"));
238+
assertThat(map.get("foo"), equalTo("bar"));
239+
assertThat(((Map<String, Object>) map.get("spam")).get("foo"), equalTo("baz"));
252240
}
253241

254242
}

0 commit comments

Comments
 (0)