Skip to content

Commit 92f6204

Browse files
committed
Fix placeholder support in <springProfile>'s name attribute
Closes gh-13450
1 parent 6081db5 commit 92f6204

File tree

2 files changed

+100
-8
lines changed

2 files changed

+100
-8
lines changed

spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringProfileAction.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,14 +69,13 @@ public void begin(InterpretationContext ic, String name, Attributes attributes)
6969
private boolean acceptsProfiles(InterpretationContext ic, Attributes attributes) {
7070
String[] profileNames = StringUtils.trimArrayElements(StringUtils
7171
.commaDelimitedListToStringArray(attributes.getValue(NAME_ATTRIBUTE)));
72-
if (profileNames.length != 0) {
73-
for (String profileName : profileNames) {
74-
OptionHelper.substVars(profileName, ic, this.context);
75-
}
76-
return this.environment != null
77-
&& this.environment.acceptsProfiles(profileNames);
72+
if (this.environment == null || profileNames.length == 0) {
73+
return false;
7874
}
79-
return false;
75+
for (int i = 0; i < profileNames.length; i++) {
76+
profileNames[i] = OptionHelper.substVars(profileNames[i], ic, this.context);
77+
}
78+
return this.environment.acceptsProfiles(profileNames);
8079
}
8180

8281
@Override
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.logging.logback;
18+
19+
import ch.qos.logback.core.Context;
20+
import ch.qos.logback.core.ContextBase;
21+
import ch.qos.logback.core.joran.action.Action;
22+
import ch.qos.logback.core.joran.spi.ActionException;
23+
import ch.qos.logback.core.joran.spi.InterpretationContext;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.xml.sax.Attributes;
27+
28+
import org.springframework.core.env.Environment;
29+
30+
import static org.mockito.BDDMockito.given;
31+
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.verify;
33+
34+
/**
35+
* Tests for {@link SpringProfileAction}.
36+
*
37+
* @author Andy Wilkinson
38+
*/
39+
public class SpringProfileActionTests {
40+
41+
private final Environment environment = mock(Environment.class);
42+
43+
private final SpringProfileAction action = new SpringProfileAction(this.environment);
44+
45+
private final Context context = new ContextBase();
46+
47+
private final InterpretationContext interpretationContext = new InterpretationContext(
48+
this.context, null);
49+
50+
private final Attributes attributes = mock(Attributes.class);
51+
52+
@Before
53+
public void setUp() {
54+
this.action.setContext(this.context);
55+
}
56+
57+
@Test
58+
public void environmentIsQueriedWithProfileFromNameAttribute()
59+
throws ActionException {
60+
given(this.attributes.getValue(Action.NAME_ATTRIBUTE)).willReturn("dev");
61+
this.action.begin(this.interpretationContext, null, this.attributes);
62+
verify(this.environment).acceptsProfiles("dev");
63+
}
64+
65+
@Test
66+
public void environmentIsQueriedWithMultipleProfilesFromCommaSeparatedNameAttribute()
67+
throws ActionException {
68+
given(this.attributes.getValue(Action.NAME_ATTRIBUTE)).willReturn("dev,qa");
69+
this.action.begin(this.interpretationContext, null, this.attributes);
70+
verify(this.environment).acceptsProfiles("dev", "qa");
71+
}
72+
73+
@Test
74+
public void environmentIsQueriedWithResolvedValueWhenNameAttributeUsesAPlaceholder()
75+
throws ActionException {
76+
given(this.attributes.getValue(Action.NAME_ATTRIBUTE)).willReturn("${profile}");
77+
this.context.putProperty("profile", "dev");
78+
this.action.begin(this.interpretationContext, null, this.attributes);
79+
verify(this.environment).acceptsProfiles("dev");
80+
}
81+
82+
@Test
83+
public void environmentIsQueriedWithResolvedValuesFromCommaSeparatedNameNameAttributeWithPlaceholders()
84+
throws ActionException {
85+
given(this.attributes.getValue(Action.NAME_ATTRIBUTE))
86+
.willReturn("${profile1},${profile2}");
87+
this.context.putProperty("profile1", "dev");
88+
this.context.putProperty("profile2", "qa");
89+
this.action.begin(this.interpretationContext, null, this.attributes);
90+
verify(this.environment).acceptsProfiles("dev", "qa");
91+
}
92+
93+
}

0 commit comments

Comments
 (0)