mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Fix placeholder support in <springProfile>'s name attribute
Closes gh-13450
This commit is contained in:
parent
6081db5c34
commit
92f62043d4
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-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.
|
||||
@ -69,14 +69,13 @@ class SpringProfileAction extends Action implements InPlayListener {
|
||||
private boolean acceptsProfiles(InterpretationContext ic, Attributes attributes) {
|
||||
String[] profileNames = StringUtils.trimArrayElements(StringUtils
|
||||
.commaDelimitedListToStringArray(attributes.getValue(NAME_ATTRIBUTE)));
|
||||
if (profileNames.length != 0) {
|
||||
for (String profileName : profileNames) {
|
||||
OptionHelper.substVars(profileName, ic, this.context);
|
||||
}
|
||||
return this.environment != null
|
||||
&& this.environment.acceptsProfiles(profileNames);
|
||||
if (this.environment == null || profileNames.length == 0) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
for (int i = 0; i < profileNames.length; i++) {
|
||||
profileNames[i] = OptionHelper.substVars(profileNames[i], ic, this.context);
|
||||
}
|
||||
return this.environment.acceptsProfiles(profileNames);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright 2012-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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://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.logging.logback;
|
||||
|
||||
import ch.qos.logback.core.Context;
|
||||
import ch.qos.logback.core.ContextBase;
|
||||
import ch.qos.logback.core.joran.action.Action;
|
||||
import ch.qos.logback.core.joran.spi.ActionException;
|
||||
import ch.qos.logback.core.joran.spi.InterpretationContext;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.xml.sax.Attributes;
|
||||
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringProfileAction}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class SpringProfileActionTests {
|
||||
|
||||
private final Environment environment = mock(Environment.class);
|
||||
|
||||
private final SpringProfileAction action = new SpringProfileAction(this.environment);
|
||||
|
||||
private final Context context = new ContextBase();
|
||||
|
||||
private final InterpretationContext interpretationContext = new InterpretationContext(
|
||||
this.context, null);
|
||||
|
||||
private final Attributes attributes = mock(Attributes.class);
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.action.setContext(this.context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void environmentIsQueriedWithProfileFromNameAttribute()
|
||||
throws ActionException {
|
||||
given(this.attributes.getValue(Action.NAME_ATTRIBUTE)).willReturn("dev");
|
||||
this.action.begin(this.interpretationContext, null, this.attributes);
|
||||
verify(this.environment).acceptsProfiles("dev");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void environmentIsQueriedWithMultipleProfilesFromCommaSeparatedNameAttribute()
|
||||
throws ActionException {
|
||||
given(this.attributes.getValue(Action.NAME_ATTRIBUTE)).willReturn("dev,qa");
|
||||
this.action.begin(this.interpretationContext, null, this.attributes);
|
||||
verify(this.environment).acceptsProfiles("dev", "qa");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void environmentIsQueriedWithResolvedValueWhenNameAttributeUsesAPlaceholder()
|
||||
throws ActionException {
|
||||
given(this.attributes.getValue(Action.NAME_ATTRIBUTE)).willReturn("${profile}");
|
||||
this.context.putProperty("profile", "dev");
|
||||
this.action.begin(this.interpretationContext, null, this.attributes);
|
||||
verify(this.environment).acceptsProfiles("dev");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void environmentIsQueriedWithResolvedValuesFromCommaSeparatedNameNameAttributeWithPlaceholders()
|
||||
throws ActionException {
|
||||
given(this.attributes.getValue(Action.NAME_ATTRIBUTE))
|
||||
.willReturn("${profile1},${profile2}");
|
||||
this.context.putProperty("profile1", "dev");
|
||||
this.context.putProperty("profile2", "qa");
|
||||
this.action.begin(this.interpretationContext, null, this.attributes);
|
||||
verify(this.environment).acceptsProfiles("dev", "qa");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user