|
|
|
@ -19,13 +19,15 @@ package org.springframework.boot.yaml; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.Properties; |
|
|
|
import java.util.Properties; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Assert; |
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.config.YamlProcessor.DocumentMatcher; |
|
|
|
import org.springframework.beans.factory.config.YamlProcessor.MatchStatus; |
|
|
|
import org.springframework.beans.factory.config.YamlProcessor.MatchStatus; |
|
|
|
import org.springframework.core.io.ByteArrayResource; |
|
|
|
import org.springframework.core.io.ByteArrayResource; |
|
|
|
import org.springframework.core.io.support.PropertiesLoaderUtils; |
|
|
|
import org.springframework.core.io.support.PropertiesLoaderUtils; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Tests for {@link SpringProfileDocumentMatcher}. |
|
|
|
* Tests for {@link SpringProfileDocumentMatcher}. |
|
|
|
* |
|
|
|
* |
|
|
|
@ -34,79 +36,71 @@ import org.springframework.core.io.support.PropertiesLoaderUtils; |
|
|
|
public class SpringProfileDocumentMatcherTests { |
|
|
|
public class SpringProfileDocumentMatcherTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testMatchesSingleProfile() throws IOException { |
|
|
|
public void matchesSingleProfile() throws IOException { |
|
|
|
SpringProfileDocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", |
|
|
|
DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "bar"); |
|
|
|
"bar"); |
|
|
|
Properties properties = getProperties("spring.profiles: foo"); |
|
|
|
Assert.assertSame(MatchStatus.FOUND, |
|
|
|
assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.FOUND); |
|
|
|
matcher.matches(getProperties("spring.profiles: foo"))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testAbstainNoConfiguredProfiles() throws IOException { |
|
|
|
public void abstainNoConfiguredProfiles() throws IOException { |
|
|
|
SpringProfileDocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", |
|
|
|
DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "bar"); |
|
|
|
"bar"); |
|
|
|
Properties properties = getProperties("some.property: spam"); |
|
|
|
Assert.assertSame(MatchStatus.ABSTAIN, |
|
|
|
assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.ABSTAIN); |
|
|
|
matcher.matches(getProperties("some.property: spam"))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testNoActiveProfiles() throws IOException { |
|
|
|
public void noActiveProfiles() throws IOException { |
|
|
|
SpringProfileDocumentMatcher matcher = new SpringProfileDocumentMatcher(); |
|
|
|
DocumentMatcher matcher = new SpringProfileDocumentMatcher(); |
|
|
|
Assert.assertSame(MatchStatus.NOT_FOUND, |
|
|
|
Properties properties = getProperties("spring.profiles: bar,spam"); |
|
|
|
matcher.matches(getProperties("spring.profiles: bar,spam"))); |
|
|
|
assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.NOT_FOUND); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testMatchesCommaSeparatedArray() throws IOException { |
|
|
|
public void matchesCommaSeparatedArray() throws IOException { |
|
|
|
SpringProfileDocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", |
|
|
|
DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "bar"); |
|
|
|
"bar"); |
|
|
|
Properties properties = getProperties("spring.profiles: bar,spam"); |
|
|
|
Assert.assertSame(MatchStatus.FOUND, |
|
|
|
assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.FOUND); |
|
|
|
matcher.matches(getProperties("spring.profiles: bar,spam"))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testNoMatchingProfiles() throws IOException { |
|
|
|
public void noMatchingProfiles() throws IOException { |
|
|
|
SpringProfileDocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", |
|
|
|
DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "bar"); |
|
|
|
"bar"); |
|
|
|
Properties properties = getProperties("spring.profiles: baz,blah"); |
|
|
|
Assert.assertSame(MatchStatus.NOT_FOUND, |
|
|
|
assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.NOT_FOUND); |
|
|
|
matcher.matches(getProperties("spring.profiles: baz,blah"))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testInverseMatchSingle() throws IOException { |
|
|
|
public void inverseMatchSingle() throws IOException { |
|
|
|
SpringProfileDocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", |
|
|
|
DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "bar"); |
|
|
|
"bar"); |
|
|
|
Properties properties = getProperties("spring.profiles: !baz"); |
|
|
|
Assert.assertSame(MatchStatus.FOUND, |
|
|
|
assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.FOUND); |
|
|
|
matcher.matches(getProperties("spring.profiles: !baz"))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testInverseMatchMulti() throws IOException { |
|
|
|
public void testInverseMatchMulti() throws IOException { |
|
|
|
SpringProfileDocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", |
|
|
|
DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "bar"); |
|
|
|
"bar"); |
|
|
|
Properties properties = getProperties("spring.profiles: !baz,!blah"); |
|
|
|
Assert.assertSame(MatchStatus.FOUND, |
|
|
|
assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.FOUND); |
|
|
|
matcher.matches(getProperties("spring.profiles: !baz,!blah"))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testNegatedAndNonNegated() throws IOException { |
|
|
|
public void negatedAndNonNegated() throws IOException { |
|
|
|
SpringProfileDocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", |
|
|
|
DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "bar", "blah"); |
|
|
|
"bar", "blah"); |
|
|
|
Properties properties = getProperties("spring.profiles: !baz,blah"); |
|
|
|
Assert.assertSame(MatchStatus.FOUND, |
|
|
|
assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.FOUND); |
|
|
|
matcher.matches(getProperties("spring.profiles: !baz,blah"))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testNegatedTrumpsMatching() throws IOException { |
|
|
|
public void negatedTrumpsMatching() throws IOException { |
|
|
|
SpringProfileDocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", |
|
|
|
DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "baz", "blah"); |
|
|
|
"baz", "blah"); |
|
|
|
Properties properties = getProperties("spring.profiles: !baz,blah"); |
|
|
|
Assert.assertSame(MatchStatus.NOT_FOUND, |
|
|
|
assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.NOT_FOUND); |
|
|
|
matcher.matches(getProperties("spring.profiles: !baz,blah"))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Properties getProperties(String values) throws IOException { |
|
|
|
private Properties getProperties(String values) throws IOException { |
|
|
|
return PropertiesLoaderUtils |
|
|
|
ByteArrayResource resource = new ByteArrayResource(values.getBytes()); |
|
|
|
.loadProperties(new ByteArrayResource(values.getBytes())); |
|
|
|
return PropertiesLoaderUtils.loadProperties(resource); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|