|
| 1 | +/* |
| 2 | + * Copyright 2012-2020 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 | + * https://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.env; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.function.Consumer; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.mockito.ArgumentCaptor; |
| 25 | +import org.mockito.Captor; |
| 26 | +import org.mockito.Mock; |
| 27 | +import org.mockito.MockitoAnnotations; |
| 28 | + |
| 29 | +import org.springframework.core.env.MutablePropertySources; |
| 30 | +import org.springframework.core.env.PropertySource; |
| 31 | +import org.springframework.mock.env.MockEnvironment; |
| 32 | +import org.springframework.mock.env.MockPropertySource; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | +import static org.mockito.Mockito.verify; |
| 36 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 37 | + |
| 38 | +/** |
| 39 | + * Tests for {@link DefaultPropertiesPropertySource}. |
| 40 | + * |
| 41 | + * @author Phillip Webb |
| 42 | + */ |
| 43 | +class DefaultPropertiesPropertySourceTests { |
| 44 | + |
| 45 | + @Mock |
| 46 | + private Consumer<DefaultPropertiesPropertySource> action; |
| 47 | + |
| 48 | + @Captor |
| 49 | + private ArgumentCaptor<DefaultPropertiesPropertySource> captor; |
| 50 | + |
| 51 | + @BeforeEach |
| 52 | + void setup() { |
| 53 | + MockitoAnnotations.initMocks(this); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void nameIsDefaultProperties() { |
| 58 | + assertThat(DefaultPropertiesPropertySource.NAME).isEqualTo("defaultProperties"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void createCreatesSource() { |
| 63 | + DefaultPropertiesPropertySource propertySource = new DefaultPropertiesPropertySource( |
| 64 | + Collections.singletonMap("spring", "boot")); |
| 65 | + assertThat(propertySource.getName()).isEqualTo("defaultProperties"); |
| 66 | + assertThat(propertySource.getProperty("spring")).isEqualTo("boot"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void hasMatchingNameWhenNameMatchesReturnsTrue() { |
| 71 | + MockPropertySource propertySource = new MockPropertySource("defaultProperties"); |
| 72 | + assertThat(DefaultPropertiesPropertySource.hasMatchingName(propertySource)).isTrue(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void hasMatchingNameWhenNameDoesNotMatchReturnsFalse() { |
| 77 | + MockPropertySource propertySource = new MockPropertySource("normalProperties"); |
| 78 | + assertThat(DefaultPropertiesPropertySource.hasMatchingName(propertySource)).isFalse(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void hasMatchingNameWhenPropertySourceIsNullReturnsFalse() { |
| 83 | + assertThat(DefaultPropertiesPropertySource.hasMatchingName(null)).isFalse(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void ifNotEmptyWhenNullDoesNotCallAction() { |
| 88 | + DefaultPropertiesPropertySource.ifNotEmpty(null, this.action); |
| 89 | + verifyNoInteractions(this.action); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void ifNotEmptyWhenEmptyDoesNotCallAction() { |
| 94 | + DefaultPropertiesPropertySource.ifNotEmpty(Collections.emptyMap(), this.action); |
| 95 | + verifyNoInteractions(this.action); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void ifNotEmptyHasValueCallsAction() { |
| 100 | + DefaultPropertiesPropertySource.ifNotEmpty(Collections.singletonMap("spring", "boot"), this.action); |
| 101 | + verify(this.action).accept(this.captor.capture()); |
| 102 | + assertThat(this.captor.getValue().getProperty("spring")).isEqualTo("boot"); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void moveToEndWhenNotPresentDoesNothing() { |
| 107 | + MockEnvironment environment = new MockEnvironment(); |
| 108 | + DefaultPropertiesPropertySource.moveToEnd(environment); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void moveToEndWhenPresentMovesToEnd() { |
| 113 | + MockEnvironment environment = new MockEnvironment(); |
| 114 | + MutablePropertySources propertySources = environment.getPropertySources(); |
| 115 | + propertySources.addLast(new DefaultPropertiesPropertySource(Collections.singletonMap("spring", "boot"))); |
| 116 | + propertySources.addLast(new MockPropertySource("test")); |
| 117 | + DefaultPropertiesPropertySource.moveToEnd(environment); |
| 118 | + String[] names = propertySources.stream().map(PropertySource::getName).toArray(String[]::new); |
| 119 | + assertThat(names).containsExactly(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME, "test", |
| 120 | + DefaultPropertiesPropertySource.NAME); |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments