From 15d6662c0d2531d37901bcc2d488961cbee5e419 Mon Sep 17 00:00:00 2001 From: Matt Benson Date: Wed, 20 Apr 2016 10:20:02 -0500 Subject: [PATCH] Fix SpringProfileDocumentMatcher negation bug Prior to this commit SpringProfileDocumentMatcher was returning 'found' anytime a negated profile was not found, even if there were positive profile matches required and unsatisfied. Closes gh-5747 See gh-4953 --- .../boot/yaml/SpringProfileDocumentMatcher.java | 6 +++--- .../boot/yaml/SpringProfileDocumentMatcherTests.java | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/yaml/SpringProfileDocumentMatcher.java b/spring-boot/src/main/java/org/springframework/boot/yaml/SpringProfileDocumentMatcher.java index 771086af83a..e1b5e1db34a 100644 --- a/spring-boot/src/main/java/org/springframework/boot/yaml/SpringProfileDocumentMatcher.java +++ b/spring-boot/src/main/java/org/springframework/boot/yaml/SpringProfileDocumentMatcher.java @@ -68,10 +68,10 @@ public class SpringProfileDocumentMatcher implements DocumentMatcher { if (StringUtils.hasLength(negative)) { properties = new Properties(properties); properties.setProperty(SPRING_PROFILES, negative); - switch (activeProfilesMatcher.matches(properties)) { - case FOUND: + if (activeProfilesMatcher.matches(properties) == MatchStatus.FOUND) { return MatchStatus.NOT_FOUND; - case NOT_FOUND: + } + if (StringUtils.isEmpty(positive)) { return MatchStatus.FOUND; } properties.setProperty(SPRING_PROFILES, positive); diff --git a/spring-boot/src/test/java/org/springframework/boot/yaml/SpringProfileDocumentMatcherTests.java b/spring-boot/src/test/java/org/springframework/boot/yaml/SpringProfileDocumentMatcherTests.java index b246b18957e..c180d496e94 100644 --- a/spring-boot/src/test/java/org/springframework/boot/yaml/SpringProfileDocumentMatcherTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/yaml/SpringProfileDocumentMatcherTests.java @@ -85,12 +85,19 @@ public class SpringProfileDocumentMatcherTests { } @Test - public void negatedAndNonNegated() throws IOException { + public void negatedWithMatch() throws Exception { DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "bar", "blah"); Properties properties = getProperties("spring.profiles: !baz,blah"); assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.FOUND); } + @Test + public void negatedWithNoMatch() throws IOException { + DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "bar", "blah"); + Properties properties = getProperties("spring.profiles: !baz,another"); + assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.NOT_FOUND); + } + @Test public void negatedTrumpsMatching() throws IOException { DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "baz", "blah");