forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes spring-projectsgh-28724
- Loading branch information
Showing
6 changed files
with
197 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
.../java/org/springframework/boot/test/context/SpringBootTestWithCustomEnvironmentTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2012-2021 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.test.context; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.mock.env.MockEnvironment; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Integration tests for {@link SpringBootTest @SpringBootTest} with a custom | ||
* {@link Environment}. | ||
* | ||
* @author Madhura Bhave | ||
*/ | ||
@SpringBootTest | ||
@ActiveProfiles({ "test1", "test2" }) | ||
@ContextConfiguration(loader = SpringBootTestWithCustomEnvironmentTests.Loader.class) | ||
class SpringBootTestWithCustomEnvironmentTests { | ||
|
||
@Autowired | ||
private Environment environment; | ||
|
||
@Test | ||
void getActiveProfiles() { | ||
assertThat(this.environment).isInstanceOf(MockEnvironment.class); | ||
assertThat(this.environment.getActiveProfiles()).containsOnly("test1", "test2"); | ||
} | ||
|
||
@Configuration | ||
static class Config { | ||
|
||
} | ||
|
||
static class Loader extends SpringBootContextLoader { | ||
|
||
@Override | ||
protected ConfigurableEnvironment getEnvironment() { | ||
return new MockEnvironment(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...-test-profile/src/main/java/smoketest/profile/ActiveProfilesEnvironmentPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2012-2021 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package smoketest.profile; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.env.EnvironmentPostProcessor; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
|
||
/** | ||
* {@link EnvironmentPostProcessor} that adds an active profile. | ||
* | ||
* @author Madhura Bhave | ||
*/ | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
class ActiveProfilesEnvironmentPostProcessor implements EnvironmentPostProcessor { | ||
|
||
@Override | ||
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { | ||
if (environment.getProperty("enableEnvironmentPostProcessor") != null) { | ||
environment.addActiveProfile("dev"); | ||
} | ||
} | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
...t-smoke-tests/spring-boot-smoke-test-profile/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
org.springframework.boot.env.EnvironmentPostProcessor=\ | ||
smoketest.profile.ActiveProfilesEnvironmentPostProcessor |
47 changes: 47 additions & 0 deletions
47
...s/spring-boot-smoke-test-profile/src/test/java/smoketest/profile/ActiveProfilesTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2012-2021 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package smoketest.profile; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.env.EnvironmentPostProcessor; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests that profiles are activited in the correct order from an | ||
* {@link EnvironmentPostProcessor}. | ||
* | ||
* @author Madhura Bhave | ||
*/ | ||
@SpringBootTest(properties = "enableEnvironmentPostProcessor=true") // gh-28530 | ||
@ActiveProfiles("hello") | ||
class ActiveProfilesTests { | ||
|
||
@Autowired | ||
private Environment environment; | ||
|
||
@Test | ||
void activeProfileShouldTakePrecedenceOverProgrammaticallySetProfile() { | ||
assertThat(this.environment.getActiveProfiles()).containsExactly("dev", "hello"); | ||
} | ||
|
||
} |