From a7c3edefe8b9df05ed184ead0bc9bcac0123ba17 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 11 Jul 2013 15:24:25 +0100 Subject: [PATCH] Tied up loose end for spring profile binding in YAML The short story: we only support comma-separated spring.profiles in YAML documents. [#51968679] --- .../config/ArrayDocumentMatcher.java | 58 +++++++++++++++++++ .../config/SpringProfileDocumentMatcher.java | 1 - .../bootstrap/config/YamlProcessor.java | 37 ------------ .../ProfileSettingDocumentMatcher.java | 6 +- .../config/ArrayDocumentMatcherTests.java | 58 +++++++++++++++++++ .../src/main/resources/application.yml | 2 +- 6 files changed, 120 insertions(+), 42 deletions(-) create mode 100644 spring-bootstrap/src/main/java/org/springframework/bootstrap/config/ArrayDocumentMatcher.java create mode 100644 spring-bootstrap/src/test/java/org/springframework/bootstrap/config/ArrayDocumentMatcherTests.java diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/config/ArrayDocumentMatcher.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/config/ArrayDocumentMatcher.java new file mode 100644 index 0000000000..afda3a46d8 --- /dev/null +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/config/ArrayDocumentMatcher.java @@ -0,0 +1,58 @@ +/* + * Copyright 2012-2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.bootstrap.config; + +import java.util.Properties; +import java.util.Set; + +import org.springframework.bootstrap.config.YamlProcessor.DocumentMatcher; +import org.springframework.bootstrap.config.YamlProcessor.MatchStatus; +import org.springframework.util.StringUtils; + +/** + * Matches a document containing a given key and where the value of that key is an array + * containing one of the given values, or where one of the values matches one of the given + * values (interpreted as regexes). + */ +public class ArrayDocumentMatcher implements DocumentMatcher { + + private String key; + + private String[] patterns; + + public ArrayDocumentMatcher(final String key, final String... patterns) { + this.key = key; + this.patterns = patterns; + + } + + @Override + public MatchStatus matches(Properties properties) { + if (!properties.containsKey(this.key)) { + return MatchStatus.ABSTAIN; + } + Set values = StringUtils.commaDelimitedListToSet(properties + .getProperty(this.key)); + for (String pattern : this.patterns) { + for (String value : values) { + if (value.matches(pattern)) { + return MatchStatus.FOUND; + } + } + } + return MatchStatus.NOT_FOUND; + } +} \ No newline at end of file diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/config/SpringProfileDocumentMatcher.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/config/SpringProfileDocumentMatcher.java index 118951dd44..802879689e 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/config/SpringProfileDocumentMatcher.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/config/SpringProfileDocumentMatcher.java @@ -17,7 +17,6 @@ package org.springframework.bootstrap.config; import java.util.Properties; -import org.springframework.bootstrap.config.YamlProcessor.ArrayDocumentMatcher; import org.springframework.bootstrap.config.YamlProcessor.DocumentMatcher; import org.springframework.bootstrap.config.YamlProcessor.MatchStatus; import org.springframework.core.env.Environment; diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/config/YamlProcessor.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/config/YamlProcessor.java index f62a77f745..079534070a 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/config/YamlProcessor.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/config/YamlProcessor.java @@ -23,7 +23,6 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; -import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -252,40 +251,4 @@ public class YamlProcessor { FOUND, NOT_FOUND, ABSTAIN } - /** - * Matches a document containing a given key and where the value of that key is an - * array containing one of the given values, or where one of the values matches one of - * the given values (interpreted as regexes). - */ - public static class ArrayDocumentMatcher implements DocumentMatcher { - - private String key; - - private String[] patterns; - - public ArrayDocumentMatcher(final String key, final String... patterns) { - this.key = key; - this.patterns = patterns; - - } - - @Override - public MatchStatus matches(Properties properties) { - if (!properties.containsKey(this.key)) { - return MatchStatus.ABSTAIN; - } - Set values = StringUtils.commaDelimitedListToSet(properties - .getProperty(this.key)); - for (String pattern : this.patterns) { - for (String value : values) { - if (value.matches(pattern)) { - return MatchStatus.FOUND; - } - } - } - return MatchStatus.NOT_FOUND; - } - - } - } diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/initializer/ProfileSettingDocumentMatcher.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/initializer/ProfileSettingDocumentMatcher.java index ce35ce3862..8d371c3e35 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/initializer/ProfileSettingDocumentMatcher.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/initializer/ProfileSettingDocumentMatcher.java @@ -25,13 +25,13 @@ import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; /** - * A {@link DocumentMatcher} that sets the active profile if it finds a document with - * a key spring.profiles.active. + * A {@link DocumentMatcher} that sets the active profile if it finds a document with a + * key spring.profiles.active. * * @author Dave Syer * */ -public final class ProfileSettingDocumentMatcher implements DocumentMatcher { +public class ProfileSettingDocumentMatcher implements DocumentMatcher { private final Environment environment; diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/config/ArrayDocumentMatcherTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/config/ArrayDocumentMatcherTests.java new file mode 100644 index 0000000000..fd8da8e8cb --- /dev/null +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/config/ArrayDocumentMatcherTests.java @@ -0,0 +1,58 @@ +/* + * Copyright 2012-2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.bootstrap.config; + +import java.io.IOException; +import java.util.Properties; + +import org.junit.Test; +import org.springframework.bootstrap.config.YamlProcessor.MatchStatus; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.support.PropertiesLoaderUtils; + +import static org.junit.Assert.assertEquals; + +/** + * @author Dave Syer + * + */ +public class ArrayDocumentMatcherTests { + + @Test + public void testMatchesSingleValue() throws IOException { + ArrayDocumentMatcher matcher = new ArrayDocumentMatcher("foo", "bar"); + assertEquals(MatchStatus.FOUND, matcher.matches(getProperties("foo: bar"))); + } + + @Test + public void testDoesNotMatchesIndexedArray() throws IOException { + ArrayDocumentMatcher matcher = new ArrayDocumentMatcher("foo", "bar"); + assertEquals(MatchStatus.ABSTAIN, + matcher.matches(getProperties("foo[0]: bar\nfoo[1]: spam"))); + } + + @Test + public void testMatchesCommaSeparatedArray() throws IOException { + ArrayDocumentMatcher matcher = new ArrayDocumentMatcher("foo", "bar"); + assertEquals(MatchStatus.FOUND, matcher.matches(getProperties("foo: bar,spam"))); + } + + private Properties getProperties(String values) throws IOException { + return PropertiesLoaderUtils.loadProperties(new ByteArrayResource(values + .getBytes())); + } + +} diff --git a/spring-zero-samples/spring-zero-sample-profile/src/main/resources/application.yml b/spring-zero-samples/spring-zero-sample-profile/src/main/resources/application.yml index 4942f3fc80..316b1f95d9 100644 --- a/spring-zero-samples/spring-zero-sample-profile/src/main/resources/application.yml +++ b/spring-zero-samples/spring-zero-sample-profile/src/main/resources/application.yml @@ -3,5 +3,5 @@ name: Phil --- spring: - profiles: [goodbye,dev] + profiles: goodbye,dev name: Everyone \ No newline at end of file