@Override public MatchStatus matches(Properties properties) { DocumentMatcher activeProfilesMatcher = getActiveProfilesDocumentMatcher(); String profiles = properties.getProperty(SPRING_PROFILES); String negative = extractProfiles(profiles, ProfileType.NEGATIVE); String positive = extractProfiles(profiles, ProfileType.POSITIVE); if (StringUtils.hasLength(negative)) { properties = new Properties(properties); properties.setProperty(SPRING_PROFILES, negative); if (activeProfilesMatcher.matches(properties) == MatchStatus.FOUND) { return MatchStatus.NOT_FOUND; } if (StringUtils.isEmpty(positive)) { return MatchStatus.FOUND; } properties.setProperty(SPRING_PROFILES, positive); } return activeProfilesMatcher.matches(properties); }
private DocumentMatcher getActiveProfilesDocumentMatcher() { String[] profiles = this.activeProfiles; if (profiles.length == 0) { profiles = DEFAULT_PROFILES; } return new ArrayDocumentMatcher(SPRING_PROFILES, profiles); }
@Test public void matchesSingleProfile() throws IOException { DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "bar"); Properties properties = getProperties("spring.profiles: foo"); assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.FOUND); }
@Test public void abstainNoConfiguredProfiles() throws IOException { DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "bar"); Properties properties = getProperties("some.property: spam"); assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.ABSTAIN); }
@Test public void noActiveProfiles() throws IOException { DocumentMatcher matcher = new SpringProfileDocumentMatcher(); Properties properties = getProperties("spring.profiles: bar,spam"); assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.NOT_FOUND); }
@Test public void matchesCommaSeparatedArray() throws IOException { DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "bar"); Properties properties = getProperties("spring.profiles: bar,spam"); assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.FOUND); }
@Test public void noMatchingProfiles() throws IOException { DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "bar"); Properties properties = getProperties("spring.profiles: baz,blah"); assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.NOT_FOUND); }
@Test public void inverseMatchSingle() throws IOException { DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "bar"); Properties properties = getProperties("spring.profiles: !baz"); assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.FOUND); }
@Test public void testInverseMatchMulti() throws IOException { DocumentMatcher matcher = new SpringProfileDocumentMatcher("foo", "bar"); Properties properties = getProperties("spring.profiles: !baz,!blah"); assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.FOUND); }
@Test 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"); Properties properties = getProperties("spring.profiles: !baz,blah"); assertThat(matcher.matches(properties)).isEqualTo(MatchStatus.NOT_FOUND); }