Skip to content

Commit bbb0b7a

Browse files
committed
Allow equal or colon in property value
Closes gh-3273
1 parent cca0b76 commit bbb0b7a

File tree

2 files changed

+113
-3
lines changed

2 files changed

+113
-3
lines changed

spring-boot/src/main/java/org/springframework/boot/test/EnvironmentTestUtils.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 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.
@@ -30,6 +30,7 @@
3030
* Test utilities for setting environment values.
3131
*
3232
* @author Dave Syer
33+
* @author Stephane Nicoll
3334
*/
3435
public abstract class EnvironmentTestUtils {
3536

@@ -79,12 +80,24 @@ public static void addEnvironment(String name, ConfigurableEnvironment environme
7980
map = value;
8081
}
8182
for (String pair : pairs) {
82-
int index = pair.indexOf(":");
83-
index = index < 0 ? index = pair.indexOf("=") : index;
83+
int index = getSeparatorIndex(pair);
8484
String key = pair.substring(0, index > 0 ? index : pair.length());
8585
String value = index > 0 ? pair.substring(index + 1) : "";
8686
map.put(key.trim(), value.trim());
8787
}
8888
}
8989

90+
private static int getSeparatorIndex(String pair) {
91+
int colonIndex = pair.indexOf(":");
92+
int equalIndex = pair.indexOf("=");
93+
if (colonIndex == -1) {
94+
return equalIndex;
95+
} else if (equalIndex == -1) {
96+
return colonIndex;
97+
}
98+
else {
99+
return Math.min(colonIndex, equalIndex);
100+
}
101+
}
102+
90103
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)