Polish "Add profile expression support"
Issue: SPR-12458
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -38,7 +38,7 @@ public class CustomEnvironmentTests {
|
||||
@Test
|
||||
public void control() {
|
||||
Environment env = new AbstractEnvironment() { };
|
||||
assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(true));
|
||||
assertThat(env.acceptsProfiles(defaultProfile()), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -51,7 +51,7 @@ public class CustomEnvironmentTests {
|
||||
}
|
||||
|
||||
Environment env = new CustomEnvironment();
|
||||
assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(false));
|
||||
assertThat(env.acceptsProfiles(defaultProfile()), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -64,8 +64,8 @@ public class CustomEnvironmentTests {
|
||||
}
|
||||
|
||||
Environment env = new CustomEnvironment();
|
||||
assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(false));
|
||||
assertThat(env.acceptsProfiles("rd1"), is(true));
|
||||
assertThat(env.acceptsProfiles(defaultProfile()), is(false));
|
||||
assertThat(env.acceptsProfiles(Profiles.of("rd1")), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -79,28 +79,32 @@ public class CustomEnvironmentTests {
|
||||
}
|
||||
|
||||
ConfigurableEnvironment env = new CustomEnvironment();
|
||||
assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(false));
|
||||
assertThat(env.acceptsProfiles("rd1", "rd2"), is(true));
|
||||
assertThat(env.acceptsProfiles(defaultProfile()), is(false));
|
||||
assertThat(env.acceptsProfiles(Profiles.of("rd1 | rd2")), is(true));
|
||||
|
||||
// finally, issue additional assertions to cover all combinations of calling these
|
||||
// methods, however unlikely.
|
||||
env.setDefaultProfiles("d1");
|
||||
assertThat(env.acceptsProfiles("rd1", "rd2"), is(false));
|
||||
assertThat(env.acceptsProfiles("d1"), is(true));
|
||||
assertThat(env.acceptsProfiles(Profiles.of("rd1 | rd2")), is(false));
|
||||
assertThat(env.acceptsProfiles(Profiles.of("d1")), is(true));
|
||||
|
||||
env.setActiveProfiles("a1", "a2");
|
||||
assertThat(env.acceptsProfiles("d1"), is(false));
|
||||
assertThat(env.acceptsProfiles("a1", "a2"), is(true));
|
||||
assertThat(env.acceptsProfiles(Profiles.of("d1")), is(false));
|
||||
assertThat(env.acceptsProfiles(Profiles.of("a1 | a2")), is(true));
|
||||
|
||||
env.setActiveProfiles();
|
||||
assertThat(env.acceptsProfiles("d1"), is(true));
|
||||
assertThat(env.acceptsProfiles("a1", "a2"), is(false));
|
||||
assertThat(env.acceptsProfiles(Profiles.of("d1")), is(true));
|
||||
assertThat(env.acceptsProfiles(Profiles.of("a1 | a2")), is(false));
|
||||
|
||||
env.setDefaultProfiles();
|
||||
assertThat(env.acceptsProfiles(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME), is(false));
|
||||
assertThat(env.acceptsProfiles("rd1", "rd2"), is(false));
|
||||
assertThat(env.acceptsProfiles("d1"), is(false));
|
||||
assertThat(env.acceptsProfiles("a1", "a2"), is(false));
|
||||
assertThat(env.acceptsProfiles(defaultProfile()), is(false));
|
||||
assertThat(env.acceptsProfiles(Profiles.of("rd1 | rd2")), is(false));
|
||||
assertThat(env.acceptsProfiles(Profiles.of("d1")), is(false));
|
||||
assertThat(env.acceptsProfiles(Profiles.of("a1 | a2")), is(false));
|
||||
}
|
||||
|
||||
private Profiles defaultProfile() {
|
||||
return Profiles.of(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -73,8 +73,14 @@ public class DummyEnvironment implements Environment {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean acceptsProfiles(String... profiles) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean acceptsProfiles(Profiles profiles) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,12 +21,13 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.core.env.Profiles.ActiveProfiles;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
@@ -35,6 +36,7 @@ import static org.junit.Assert.*;
|
||||
* Tests for {@link Profiles}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class ProfilesTests {
|
||||
|
||||
@@ -56,10 +58,10 @@ public class ProfilesTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofNullElement() throws Exception {
|
||||
public void ofNullElement() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
this.thrown.expectMessage("must contain text");
|
||||
Profiles.of((String)null);
|
||||
Profiles.of((String) null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -103,22 +105,32 @@ public class ProfilesTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofSingleExpression() throws Exception {
|
||||
public void ofSingleExpression() {
|
||||
Profiles profiles = Profiles.of("(spring)");
|
||||
assertTrue(profiles.matches(new MockActiveProfiles("spring")));
|
||||
assertFalse(profiles.matches(new MockActiveProfiles("framework")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofSingleInvertedExpression() throws Exception {
|
||||
public void ofSingleInvertedExpression() {
|
||||
Profiles profiles = Profiles.of("(!spring)");
|
||||
assertFalse(profiles.matches(new MockActiveProfiles("spring")));
|
||||
assertTrue(profiles.matches(new MockActiveProfiles("framework")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofOrExpression() throws Exception {
|
||||
public void ofOrExpression() {
|
||||
Profiles profiles = Profiles.of("(spring | framework)");
|
||||
assertOrExpression(profiles);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofOrExpressionWithoutSpace() {
|
||||
Profiles profiles = Profiles.of("(spring|framework)");
|
||||
assertOrExpression(profiles);
|
||||
}
|
||||
|
||||
private void assertOrExpression(Profiles profiles) {
|
||||
assertTrue(profiles.matches(new MockActiveProfiles("spring")));
|
||||
assertTrue(profiles.matches(new MockActiveProfiles("framework")));
|
||||
assertTrue(profiles.matches(new MockActiveProfiles("spring", "framework")));
|
||||
@@ -126,17 +138,24 @@ public class ProfilesTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofAndExpression() throws Exception {
|
||||
public void ofAndExpression() {
|
||||
Profiles profiles = Profiles.of("(spring & framework)");
|
||||
assertFalse(profiles.matches(new MockActiveProfiles("spring")));
|
||||
assertFalse(profiles.matches(new MockActiveProfiles("framework")));
|
||||
assertTrue(profiles.matches(new MockActiveProfiles("spring", "framework")));
|
||||
assertFalse(profiles.matches(new MockActiveProfiles("java")));
|
||||
assertAndExpression(profiles);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofAndExpressionWithoutBraces() throws Exception {
|
||||
public void ofAndExpressionWithoutSpace() {
|
||||
Profiles profiles = Profiles.of("spring&framework)");
|
||||
assertAndExpression(profiles);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofAndExpressionWithoutBraces() {
|
||||
Profiles profiles = Profiles.of("spring & framework");
|
||||
assertAndExpression(profiles);
|
||||
}
|
||||
|
||||
private void assertAndExpression(Profiles profiles) {
|
||||
assertFalse(profiles.matches(new MockActiveProfiles("spring")));
|
||||
assertFalse(profiles.matches(new MockActiveProfiles("framework")));
|
||||
assertTrue(profiles.matches(new MockActiveProfiles("spring", "framework")));
|
||||
@@ -144,8 +163,18 @@ public class ProfilesTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofNotAndExpression() throws Exception {
|
||||
public void ofNotAndExpression() {
|
||||
Profiles profiles = Profiles.of("!(spring & framework)");
|
||||
assertOfNotAndExpression(profiles);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofNotAndExpressionWithoutSpace() {
|
||||
Profiles profiles = Profiles.of("!(spring&framework)");
|
||||
assertOfNotAndExpression(profiles);
|
||||
}
|
||||
|
||||
private void assertOfNotAndExpression(Profiles profiles) {
|
||||
assertTrue(profiles.matches(new MockActiveProfiles("spring")));
|
||||
assertTrue(profiles.matches(new MockActiveProfiles("framework")));
|
||||
assertFalse(profiles.matches(new MockActiveProfiles("spring", "framework")));
|
||||
@@ -153,8 +182,18 @@ public class ProfilesTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofNotOrExpression() throws Exception {
|
||||
public void ofNotOrExpression() {
|
||||
Profiles profiles = Profiles.of("!(spring | framework)");
|
||||
assertOfNotOrExpression(profiles);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofNotOrExpressionWithoutSpace() {
|
||||
Profiles profiles = Profiles.of("!(spring|framework)");
|
||||
assertOfNotOrExpression(profiles);
|
||||
}
|
||||
|
||||
private void assertOfNotOrExpression(Profiles profiles) {
|
||||
assertFalse(profiles.matches(new MockActiveProfiles("spring")));
|
||||
assertFalse(profiles.matches(new MockActiveProfiles("framework")));
|
||||
assertFalse(profiles.matches(new MockActiveProfiles("spring", "framework")));
|
||||
@@ -162,8 +201,18 @@ public class ProfilesTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofComplex() throws Exception {
|
||||
public void ofComplexExpression() {
|
||||
Profiles profiles = Profiles.of("(spring & framework) | (spring & java)");
|
||||
assertComplexExpression(profiles);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofComplexExpressionWithoutSpace() {
|
||||
Profiles profiles = Profiles.of("(spring&framework)|(spring&java)");
|
||||
assertComplexExpression(profiles);
|
||||
}
|
||||
|
||||
private void assertComplexExpression(Profiles profiles) {
|
||||
assertFalse(profiles.matches(new MockActiveProfiles("spring")));
|
||||
assertTrue(profiles.matches(new MockActiveProfiles("spring", "framework")));
|
||||
assertTrue(profiles.matches(new MockActiveProfiles("spring", "java")));
|
||||
@@ -171,12 +220,18 @@ public class ProfilesTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void malformedExpressions() throws Exception {
|
||||
public void malformedExpressions() {
|
||||
assertMalformed(() -> Profiles.of("("));
|
||||
assertMalformed(() -> Profiles.of(")"));
|
||||
assertMalformed(() -> Profiles.of("a & b | c"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sensibleToString() {
|
||||
assertEquals("spring & framework or java | kotlin",
|
||||
Profiles.of("spring & framework", "java | kotlin").toString());
|
||||
}
|
||||
|
||||
private void assertMalformed(Supplier<Profiles> supplier) {
|
||||
try {
|
||||
supplier.get();
|
||||
@@ -187,7 +242,7 @@ public class ProfilesTests {
|
||||
}
|
||||
}
|
||||
|
||||
private static class MockActiveProfiles implements ActiveProfiles {
|
||||
private static class MockActiveProfiles implements Predicate<String> {
|
||||
|
||||
private Set<String> activeProfiles;
|
||||
|
||||
@@ -210,13 +265,14 @@ public class ProfilesTests {
|
||||
|
||||
|
||||
@Override
|
||||
public boolean contains(String profile) {
|
||||
public boolean test(String profile) {
|
||||
if (!StringUtils.hasText(profile) || profile.charAt(0) == '!') {
|
||||
throw new IllegalArgumentException("Invalid profile [" + profile + "]");
|
||||
}
|
||||
return (this.activeProfiles.contains(profile)
|
||||
|| (this.activeProfiles.isEmpty() && this.defaultProfiles.contains(profile)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -326,12 +326,12 @@ public class StandardEnvironmentTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void acceptsProfiles_withProfileExpression() throws Exception {
|
||||
assertThat(environment.acceptsProfiles("p1 & p2"), is(false));
|
||||
public void acceptsProfiles_withProfileExpression() {
|
||||
assertThat(environment.acceptsProfiles(Profiles.of("p1 & p2")), is(false));
|
||||
environment.addActiveProfile("p1");
|
||||
assertThat(environment.acceptsProfiles("p1 & p2"), is(false));
|
||||
assertThat(environment.acceptsProfiles(Profiles.of("p1 & p2")), is(false));
|
||||
environment.addActiveProfile("p2");
|
||||
assertThat(environment.acceptsProfiles("p1 & p2"), is(true));
|
||||
assertThat(environment.acceptsProfiles(Profiles.of("p1 & p2")), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user