Esperando resposta

pull/36262/head
Bernardo Bulgarelli 1 year ago
parent 45ce18e1c7
commit a8e9132500

@ -235,11 +235,21 @@ public class SpringBootContextLoader extends AbstractContextLoader implements Ao
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, getInlinedProperties(mergedConfig));
}
private boolean containsProhibitedCharacters(String str){
String regex = ".*[*&!|].*";
return str.matches(regex);
}
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles,
boolean applicationEnvironment) {
if (ObjectUtils.isEmpty(profiles)) {
return;
}
for (String profile : profiles) {
if (containsProhibitedCharacters(profile)) {
throw new IllegalArgumentException("Invalid profile: '" + profile + "'. Profile names can't contain '*', '&', '!' or '|'.");
}
}
if (!applicationEnvironment) {
environment.setActiveProfiles(profiles);
}

@ -21,6 +21,7 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@ -49,8 +50,7 @@ import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.context.WebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.*;
/**
* Tests for {@link SpringBootContextLoader}
@ -130,6 +130,34 @@ class SpringBootContextLoaderTests {
assertThat(getActiveProfiles(ActiveProfileWithComma.class)).containsExactly("profile1,2");
}
@Test
void activeProfileWithAsterisk() {
Assertions.assertThrows(Exception.class, () ->
assertThat(getActiveProfiles(ActiveProfileWithAsterisk.class)).containsExactly("foo*bar")
);
}
@Test
void activeProfileWithAmpersand() {
Assertions.assertThrows(Exception.class, () ->
assertThat(getActiveProfiles(ActiveProfileWithAmpersand.class)).containsExactly("foo&bar")
);
}
@Test
void activeProfileWithExclamationMark() {
Assertions.assertThrows(Exception.class, () ->
assertThat(getActiveProfiles(ActiveProfileWithExclamationMark.class)).containsExactly("!foobar")
);
}
@Test
void activeProfileWithPipe() {
Assertions.assertThrows(Exception.class, () ->
assertThat(getActiveProfiles(ActiveProfileWithPipe.class)).containsExactly("foobar|")
);
}
@Test // gh-28776
void testPropertyValuesShouldTakePrecedenceWhenInlinedPropertiesPresent() {
TestContext context = new ExposedTestContextManager(SimpleConfig.class).getExposedTestContext();
@ -311,6 +339,30 @@ class SpringBootContextLoaderTests {
}
@SpringBootTest(classes = Config.class)
@ActiveProfiles("foo*bar")
static class ActiveProfileWithAsterisk {
}
@SpringBootTest(classes = Config.class)
@ActiveProfiles("!foobar")
static class ActiveProfileWithExclamationMark {
}
@SpringBootTest(classes = Config.class)
@ActiveProfiles("foobar|")
static class ActiveProfileWithPipe {
}
@SpringBootTest(classes = Config.class)
@ActiveProfiles("foo&bar")
static class ActiveProfileWithAmpersand {
}
@SpringBootTest(properties = { "key=myValue" }, classes = Config.class)
@ActiveProfiles({ "profile1,2" })
static class ActiveProfileWithInlinedProperties {

Loading…
Cancel
Save