Skip to content

Commit ab96576

Browse files
committed
Expose non-existent resources at the end of the sorted result
Closes gh-35895 (cherry picked from commit c1b6bfb)
1 parent 19b080b commit ab96576

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/config/SortedResourcesFactoryBean.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,33 @@ public Class<? extends Resource[]> getObjectType() {
7272

7373
@Override
7474
protected Resource[] createInstance() throws Exception {
75-
List<Resource> scripts = new ArrayList<>();
75+
List<Resource> result = new ArrayList<>();
7676
for (String location : this.locations) {
7777
Resource[] resources = this.resourcePatternResolver.getResources(location);
7878

7979
// Cache URLs to avoid repeated I/O during sorting
8080
Map<Resource, String> urlCache = new LinkedHashMap<>(resources.length);
81+
List<Resource> failingResources = new ArrayList<>();
8182
for (Resource resource : resources) {
8283
try {
8384
urlCache.put(resource, resource.getURL().toString());
8485
}
8586
catch (IOException ex) {
86-
throw new IllegalStateException(
87-
"Failed to resolve URL for resource [" + resource +
88-
"] from location pattern [" + location + "]", ex);
87+
if (logger.isDebugEnabled()) {
88+
logger.debug("Failed to resolve " + resource + " for sorting purposes: " + ex);
89+
}
90+
failingResources.add(resource);
8991
}
9092
}
9193

9294
// Sort using cached URLs
9395
List<Resource> sortedResources = new ArrayList<>(urlCache.keySet());
9496
sortedResources.sort(Comparator.comparing(urlCache::get));
9597

96-
scripts.addAll(sortedResources);
98+
result.addAll(sortedResources);
99+
result.addAll(failingResources);
97100
}
98-
return scripts.toArray(new Resource[0]);
101+
return result.toArray(new Resource[0]);
99102
}
100103

101104
}

spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import org.springframework.beans.PropertyValue;
24+
import org.springframework.beans.factory.BeanCreationException;
2425
import org.springframework.beans.factory.config.BeanDefinition;
2526
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
2627
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
@@ -32,6 +33,7 @@
3233
import org.springframework.jdbc.core.JdbcTemplate;
3334
import org.springframework.jdbc.datasource.AbstractDriverBasedDataSource;
3435
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean;
36+
import org.springframework.jdbc.datasource.init.CannotReadScriptException;
3537
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
3638
import org.springframework.lang.Nullable;
3739

@@ -67,6 +69,13 @@ void createWithResourcePattern() {
6769
assertCorrectSetup("jdbc-config-pattern.xml", "dataSource");
6870
}
6971

72+
@Test
73+
void createWithNonExistentResource() {
74+
assertThatExceptionOfType(BeanCreationException.class)
75+
.isThrownBy(() -> assertCorrectSetup("jdbc-config-nonexistent.xml", "dataSource"))
76+
.withCauseInstanceOf(CannotReadScriptException.class);
77+
}
78+
7079
@Test
7180
void createWithAnonymousDataSourceAndDefaultDatabaseName() {
7281
assertThat(extractDataSourceUrl("jdbc-config-db-name-default-and-anonymous-datasource.xml"))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
6+
http://www.springframework.org/schema/jdbc https://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
7+
8+
<jdbc:embedded-database id="dataSource" type="HSQL">
9+
<jdbc:script location="classpath:org/springframework/jdbc/config/db-schema.sql"/>
10+
<jdbc:script location="classpath:org/springframework/jdbc/config/db-test-data.sql"/>
11+
<jdbc:script location="classpath:org/springframework/jdbc/config/custom-data.sql"/> <!-- does not exist -->
12+
</jdbc:embedded-database>
13+
14+
</beans>

0 commit comments

Comments
 (0)