Skip to content

Commit 7d72ab5

Browse files
committed
Return null correctly from MutablePropertySources#get
Prior to this commit, MutablePropertySources#get(String) would throw IndexArrayOutOfBoundsException if the named property source does not actually exist. This is a violation of the PropertySource#get contract as described in its Javadoc. The implementation now correctly checks for the existence of the named property source, returning null if non-existent and otherwise returning the associated PropertySource. Other changes - Rename PropertySourcesTests => MutablePropertySourcesTests - Polish MutablePropertySourcesTests for style, formatting - Refactor MutablePropertySources for consistency Issue: SPR-9185 Backport-Issue: SPR-9179 Backport-Commit: 15d1d82
1 parent 40a6769 commit 7d72ab5

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

org.springframework.core/src/main/java/org/springframework/core/env/MutablePropertySources.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -79,7 +79,8 @@ public boolean contains(String name) {
7979
}
8080

8181
public PropertySource<?> get(String name) {
82-
return this.propertySourceList.get(this.propertySourceList.indexOf(PropertySource.named(name)));
82+
int index = this.propertySourceList.indexOf(PropertySource.named(name));
83+
return index == -1 ? null : this.propertySourceList.get(index);
8384
}
8485

8586
public Iterator<PropertySource<?>> iterator() {
@@ -146,10 +147,7 @@ public int precedenceOf(PropertySource<?> propertySource) {
146147
public PropertySource<?> remove(String name) {
147148
logger.debug(String.format("Removing [%s] PropertySource", name));
148149
int index = this.propertySourceList.indexOf(PropertySource.named(name));
149-
if (index >= 0) {
150-
return this.propertySourceList.remove(index);
151-
}
152-
return null;
150+
return index == -1 ? null : this.propertySourceList.remove(index);
153151
}
154152

155153
/**
@@ -210,11 +208,13 @@ private void addAtIndex(int index, PropertySource<?> propertySource) {
210208

211209
/**
212210
* Assert that the named property source is present and return its index.
211+
* @param name the {@linkplain PropertySource#getName() name of the property source}
212+
* to find
213213
* @throws IllegalArgumentException if the named property source is not present
214214
*/
215-
private int assertPresentAndGetIndex(String propertySourceName) {
216-
int index = this.propertySourceList.indexOf(PropertySource.named(propertySourceName));
217-
Assert.isTrue(index >= 0, String.format(NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, propertySourceName));
215+
private int assertPresentAndGetIndex(String name) {
216+
int index = this.propertySourceList.indexOf(PropertySource.named(name));
217+
Assert.isTrue(index >= 0, String.format(NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, name));
218218
return index;
219219
}
220220

org.springframework.core/src/test/java/org/springframework/core/env/PropertySourcesTests.java renamed to org.springframework.core/src/test/java/org/springframework/core/env/MutablePropertySourcesTests.java

+23-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -16,18 +16,21 @@
1616

1717
package org.springframework.core.env;
1818

19-
import static org.hamcrest.CoreMatchers.equalTo;
20-
import static org.hamcrest.CoreMatchers.is;
21-
import static org.hamcrest.CoreMatchers.not;
22-
import static org.hamcrest.CoreMatchers.nullValue;
23-
import static org.junit.Assert.assertEquals;
24-
import static org.junit.Assert.assertThat;
25-
import static org.junit.Assert.fail;
26-
2719
import org.junit.Test;
2820
import org.springframework.mock.env.MockPropertySource;
2921

30-
public class PropertySourcesTests {
22+
import static java.lang.String.*;
23+
import static org.hamcrest.CoreMatchers.*;
24+
import static org.junit.Assert.*;
25+
import static org.springframework.core.env.MutablePropertySources.*;
26+
27+
/**
28+
* Unit tests for {@link MutablePropertySources}
29+
*
30+
* @author Chris Beams
31+
*/
32+
public class MutablePropertySourcesTests {
33+
3134
@Test
3235
public void test() {
3336
MutablePropertySources sources = new MutablePropertySources();
@@ -104,7 +107,7 @@ public void test() {
104107
fail("expected non-existent PropertySource exception");
105108
} catch (IllegalArgumentException ex) {
106109
assertThat(ex.getMessage(),
107-
equalTo(String.format(MutablePropertySources.NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, bogusPS)));
110+
equalTo(format(NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, bogusPS)));
108111
}
109112

110113
sources.addFirst(new MockPropertySource("a"));
@@ -126,24 +129,30 @@ public void test() {
126129
fail("expected non-existent PropertySource exception");
127130
} catch (IllegalArgumentException ex) {
128131
assertThat(ex.getMessage(),
129-
equalTo(String.format(MutablePropertySources.NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, bogusPS)));
132+
equalTo(format(NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, bogusPS)));
130133
}
131134

132135
try {
133136
sources.addBefore("b", new MockPropertySource("b"));
134137
fail("expected exception");
135138
} catch (IllegalArgumentException ex) {
136139
assertThat(ex.getMessage(),
137-
equalTo(String.format(MutablePropertySources.ILLEGAL_RELATIVE_ADDITION_MESSAGE, "b")));
140+
equalTo(format(ILLEGAL_RELATIVE_ADDITION_MESSAGE, "b")));
138141
}
139142

140143
try {
141144
sources.addAfter("b", new MockPropertySource("b"));
142145
fail("expected exception");
143146
} catch (IllegalArgumentException ex) {
144147
assertThat(ex.getMessage(),
145-
equalTo(String.format(MutablePropertySources.ILLEGAL_RELATIVE_ADDITION_MESSAGE, "b")));
148+
equalTo(format(ILLEGAL_RELATIVE_ADDITION_MESSAGE, "b")));
146149
}
147150
}
148151

152+
@Test
153+
public void getNonExistentPropertySourceReturnsNull() {
154+
MutablePropertySources sources = new MutablePropertySources();
155+
assertThat(sources.get("bogus"), nullValue());
156+
}
157+
149158
}

0 commit comments

Comments
 (0)