Skip to content

Commit 33be78a

Browse files
mbhavephilwebb
authored andcommitted
Add additional profile negation tests
Closes gh-8011
1 parent 4bde6b8 commit 33be78a

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.context.config;
18+
19+
import org.junit.After;
20+
import org.junit.Test;
21+
22+
import org.springframework.boot.SpringApplication;
23+
import org.springframework.boot.WebApplicationType;
24+
import org.springframework.context.ConfigurableApplicationContext;
25+
import org.springframework.context.annotation.Configuration;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link ConfigFileApplicationListener} handling of negated profiles in yaml
31+
* configuration files.
32+
*
33+
* @author Madhura Bhave
34+
*/
35+
public class ConfigFileApplicationListenerYamlProfileNegationTests {
36+
37+
private ConfigurableApplicationContext context;
38+
39+
@After
40+
public void cleanUp() {
41+
if (this.context != null) {
42+
this.context.close();
43+
}
44+
}
45+
46+
@Test
47+
public void yamlProfileNegationDefaultProfile() {
48+
SpringApplication application = new SpringApplication(Config.class);
49+
application.setWebApplicationType(WebApplicationType.NONE);
50+
String configName = "--spring.config.name=profilenegation";
51+
this.context = application.run(configName);
52+
assertVersionProperty(this.context, "NOT A");
53+
}
54+
55+
@Test
56+
public void yamlProfileNegationWithActiveProfile() {
57+
SpringApplication application = new SpringApplication(Config.class);
58+
application.setWebApplicationType(WebApplicationType.NONE);
59+
String configName = "--spring.config.name=profilenegation";
60+
this.context = application.run(configName, "--spring.profiles.active=C,A");
61+
assertVersionProperty(this.context, null, "C", "A");
62+
}
63+
64+
@Test
65+
public void yamlProfileNegationLocalActiveProfiles() {
66+
SpringApplication application = new SpringApplication(Config.class);
67+
application.setWebApplicationType(WebApplicationType.NONE);
68+
String configName = "--spring.config.name=profilenegation-local-active-profiles";
69+
this.context = application.run(configName);
70+
assertVersionProperty(this.context, "NOT A", "B");
71+
}
72+
73+
@Test
74+
public void yamlProfileNegationOverrideLocalActiveProfiles() {
75+
SpringApplication application = new SpringApplication(Config.class);
76+
application.setWebApplicationType(WebApplicationType.NONE);
77+
String configName = "--spring.config.name=profilenegation-local-active-profiles";
78+
this.context = application.run(configName, "--spring.profiles.active=C,A");
79+
assertVersionProperty(this.context, null, "C", "A");
80+
}
81+
82+
@Test
83+
public void yamlProfileNegationWithProfileSpecificFile() {
84+
SpringApplication application = new SpringApplication(Config.class);
85+
application.setWebApplicationType(WebApplicationType.NONE);
86+
String configName = "--spring.config.name=profilenegation";
87+
this.context = application.run(configName, "--spring.profiles.active=C,B");
88+
assertVersionProperty(this.context, "NOT A", "C", "B");
89+
}
90+
91+
private void assertVersionProperty(ConfigurableApplicationContext context,
92+
String expectedVersion, String... expectedActiveProfiles) {
93+
assertThat(context.getEnvironment().getActiveProfiles())
94+
.isEqualTo(expectedActiveProfiles);
95+
assertThat(context.getEnvironment().getProperty("version")).as("version mismatch")
96+
.isEqualTo(expectedVersion);
97+
context.close();
98+
}
99+
100+
@Configuration
101+
public static class Config {
102+
103+
}
104+
105+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
version: NOT A
2+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
spring:
2+
profiles:
3+
active:
4+
- B
5+
---
6+
7+
spring.profiles: "!A"
8+
9+
version: NOT A
10+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
spring.profiles: "!A"
3+
4+
version: NOT A

0 commit comments

Comments
 (0)