|
| 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