|
| 1 | +/* |
| 2 | + * Copyright 2012-2015 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.test; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 25 | +import org.springframework.core.env.MapPropertySource; |
| 26 | +import org.springframework.core.env.StandardEnvironment; |
| 27 | + |
| 28 | +import static org.junit.Assert.assertEquals; |
| 29 | +import static org.junit.Assert.assertFalse; |
| 30 | +import static org.junit.Assert.assertTrue; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests for {@link EnvironmentTestUtils}. |
| 34 | + * |
| 35 | + * @author Stephane Nicoll |
| 36 | + */ |
| 37 | +public class EnvironmentTestUtilsTests { |
| 38 | + |
| 39 | + private final ConfigurableEnvironment environment = new StandardEnvironment(); |
| 40 | + |
| 41 | + @Test |
| 42 | + public void addSimplePairEqual() { |
| 43 | + testAddSimplePair("my.foo", "bar", "="); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void addSimplePairColon() { |
| 48 | + testAddSimplePair("my.foo", "bar", ":"); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void addSimplePairEqualWithEqualInValue() { |
| 53 | + testAddSimplePair("my.foo", "b=ar", "="); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void addSimplePairEqualWithColonInValue() { |
| 58 | + testAddSimplePair("my.foo", "b:ar", "="); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void addSimplePairColonWithColonInValue() { |
| 63 | + testAddSimplePair("my.foo", "b:ar", ":"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void addSimplePairColonWithEqualInValue() { |
| 68 | + testAddSimplePair("my.foo", "b=ar", ":"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void addPairNoValue() { |
| 73 | + String propertyName = "my.foo+bar"; |
| 74 | + assertFalse(environment.containsProperty(propertyName)); |
| 75 | + EnvironmentTestUtils.addEnvironment(environment, propertyName); |
| 76 | + assertTrue(environment.containsProperty(propertyName)); |
| 77 | + assertEquals("", environment.getProperty(propertyName)); |
| 78 | + } |
| 79 | + |
| 80 | + private void testAddSimplePair(String key, String value, String delimiter) { |
| 81 | + assertFalse("Property '" + key + "' should not exist", environment.containsProperty(key)); |
| 82 | + EnvironmentTestUtils.addEnvironment(environment, key + delimiter + value); |
| 83 | + assertEquals("Wrong value for property '" + key + "'", value, environment.getProperty(key)); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testConfigHasHigherPrecedence() { |
| 88 | + Map<String, Object> map = new HashMap<String, Object>(); |
| 89 | + map.put("my.foo", "bar"); |
| 90 | + MapPropertySource source = new MapPropertySource("sample", map); |
| 91 | + environment.getPropertySources().addFirst(source); |
| 92 | + assertEquals("bar", environment.getProperty("my.foo")); |
| 93 | + EnvironmentTestUtils.addEnvironment(environment, "my.foo=bar2"); |
| 94 | + assertEquals("bar2", environment.getProperty("my.foo")); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments