Merge branch '3.1.x'

This commit is contained in:
spencergibb
2022-11-28 19:34:35 -05:00
3 changed files with 61 additions and 7 deletions

View File

@@ -39,7 +39,7 @@ public class CompositeEnvironmentRepository implements EnvironmentRepository {
protected List<EnvironmentRepository> environmentRepositories;
private boolean failOnError;
protected boolean failOnError;
/**
* Creates a new {@link CompositeEnvironmentRepository}.

View File

@@ -46,13 +46,23 @@ public class SearchPathCompositeEnvironmentRepository extends CompositeEnvironme
public Locations getLocations(String application, String profile, String label) {
List<String> locations = new ArrayList<>();
for (EnvironmentRepository repo : this.environmentRepositories) {
if (repo instanceof SearchPathLocator searchPathLocator) {
addForSearchPathLocators(application, profile, label, locations, searchPathLocator);
}
else if (repo instanceof ObservationEnvironmentRepositoryWrapper wrapper
&& wrapper.getDelegate() instanceof SearchPathLocator searchPathLocator) {
addForSearchPathLocators(application, profile, label, locations, searchPathLocator);
try {
if (repo instanceof SearchPathLocator searchPathLocator) {
addForSearchPathLocators(application, profile, label, locations, searchPathLocator);
}
else if (repo instanceof ObservationEnvironmentRepositoryWrapper wrapper
&& wrapper.getDelegate() instanceof SearchPathLocator searchPathLocator) {
addForSearchPathLocators(application, profile, label, locations, searchPathLocator);
}
}
catch (RepositoryException ex) {
if (failOnError) {
throw ex;
}
else {
log.info("Error finding locations for " + repo, ex);
}
}
}
return new Locations(application, profile, label, null, locations.toArray(new String[locations.size()]));
}

View File

@@ -34,6 +34,7 @@ import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
@@ -176,6 +177,36 @@ public class CompositeEnvironmentRepositoryTests {
assertThat(propertySources.get(0).getName()).isEqualTo("p1");
}
@Test
public void testFailOnErrorFlagFalseForGetLocations() {
String sLoc1 = "loc1";
Environment e1 = new Environment("app", "dev");
SearchPathLocator.Locations loc1 = new SearchPathLocator.Locations("app", "dev", "label", "version",
new String[] { sLoc1 });
List<EnvironmentRepository> repos = new ArrayList<EnvironmentRepository>();
repos.add(new TestFailingLocationRepository(1, e1, loc1));
SearchPathCompositeEnvironmentRepository compositeRepo = new SearchPathCompositeEnvironmentRepository(repos,
false);
SearchPathLocator.Locations locations = compositeRepo.getLocations("app", "dev", "label");
assertThat(locations.getLocations()).isEmpty();
}
@Test
public void testFailOnErrorFlagTrueForGetLocations() {
String sLoc1 = "loc1";
Environment e1 = new Environment("app", "dev");
SearchPathLocator.Locations loc1 = new SearchPathLocator.Locations("app", "dev", "label", "version",
new String[] { sLoc1 });
List<EnvironmentRepository> repos = new ArrayList<EnvironmentRepository>();
repos.add(new TestFailingLocationRepository(1, e1, loc1));
SearchPathCompositeEnvironmentRepository compositeRepo = new SearchPathCompositeEnvironmentRepository(repos,
true);
assertThatExceptionOfType(RepositoryException.class)
.isThrownBy(() -> compositeRepo.getLocations("app", "dev", "label"));
}
private static class TestOrderedEnvironmentRepository implements EnvironmentRepository, SearchPathLocator, Ordered {
private Environment env;
@@ -230,6 +261,19 @@ public class CompositeEnvironmentRepositoryTests {
}
private static class TestFailingLocationRepository extends TestOrderedEnvironmentRepository {
TestFailingLocationRepository(int order, Environment env, Locations locations) {
super(order, env, locations);
}
@Override
public Locations getLocations(String application, String profile, String label) {
throw new RepositoryException("Failing for some reason");
}
}
@Configuration(proxyBeanMethods = false)
static class OverrideCompositeConfig {