Browse Source

Polish CompositePropertySource[Tests]

pull/31121/head
Sam Brannen 2 years ago
parent
commit
d189e169cc
  1. 4
      spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java
  2. 18
      spring-core/src/test/java/org/springframework/core/env/CompositePropertySourceTests.java

4
spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java vendored

@ -17,8 +17,8 @@ @@ -17,8 +17,8 @@
package org.springframework.core.env;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@ -90,7 +90,7 @@ public class CompositePropertySource extends EnumerablePropertySource<Object> { @@ -90,7 +90,7 @@ public class CompositePropertySource extends EnumerablePropertySource<Object> {
total += names.length;
}
Set<String> allNames = new LinkedHashSet<>(total);
namesList.forEach(names -> allNames.addAll(Arrays.asList(names)));
namesList.forEach(names -> Collections.addAll(allNames, names));
return StringUtils.toStringArray(allNames);
}

18
spring-core/src/test/java/org/springframework/core/env/CompositePropertySourceTests.java vendored

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,7 +16,6 @@ @@ -16,7 +16,6 @@
package org.springframework.core.env;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
@ -27,24 +26,23 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -27,24 +26,23 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link CompositePropertySource}.
*
* @author Phillip Webb
* @author Sam Brannen
*/
class CompositePropertySourceTests {
@Test
void addFirst() {
PropertySource<?> p1 = new MapPropertySource("p1", Collections.emptyMap());
PropertySource<?> p2 = new MapPropertySource("p2", Collections.emptyMap());
PropertySource<?> p3 = new MapPropertySource("p3", Collections.emptyMap());
PropertySource<?> p1 = new MapPropertySource("p1", Map.of());
PropertySource<?> p2 = new MapPropertySource("p2", Map.of());
PropertySource<?> p3 = new MapPropertySource("p3", Map.of());
CompositePropertySource composite = new CompositePropertySource("c");
composite.addPropertySource(p2);
composite.addPropertySource(p3);
composite.addPropertySource(p1);
composite.addFirstPropertySource(p1);
String s = composite.toString();
int i1 = s.indexOf("name='p1'");
int i2 = s.indexOf("name='p2'");
int i3 = s.indexOf("name='p3'");
assertThat(((i1 < i2) && (i2 < i3))).as("Bad order: " + s).isTrue();
assertThat(composite.getPropertySources()).extracting(PropertySource::getName).containsExactly("p1", "p2", "p3");
assertThat(composite).asString().containsSubsequence("name='p1'", "name='p2'", "name='p3'");
}
@Test

Loading…
Cancel
Save