mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-20 04:09:04 +00:00
Expose non-existent resources at the end of the sorted result
Closes gh-35895
(cherry picked from commit c1b6bfb681)
This commit is contained in:
+9
-6
@@ -72,20 +72,22 @@ public class SortedResourcesFactoryBean extends AbstractFactoryBean<Resource[]>
|
||||
|
||||
@Override
|
||||
protected Resource[] createInstance() throws Exception {
|
||||
List<Resource> scripts = new ArrayList<>();
|
||||
List<Resource> result = new ArrayList<>();
|
||||
for (String location : this.locations) {
|
||||
Resource[] resources = this.resourcePatternResolver.getResources(location);
|
||||
|
||||
// Cache URLs to avoid repeated I/O during sorting
|
||||
Map<Resource, String> urlCache = new LinkedHashMap<>(resources.length);
|
||||
List<Resource> failingResources = new ArrayList<>();
|
||||
for (Resource resource : resources) {
|
||||
try {
|
||||
urlCache.put(resource, resource.getURL().toString());
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException(
|
||||
"Failed to resolve URL for resource [" + resource +
|
||||
"] from location pattern [" + location + "]", ex);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Failed to resolve " + resource + " for sorting purposes: " + ex);
|
||||
}
|
||||
failingResources.add(resource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,9 +95,10 @@ public class SortedResourcesFactoryBean extends AbstractFactoryBean<Resource[]>
|
||||
List<Resource> sortedResources = new ArrayList<>(urlCache.keySet());
|
||||
sortedResources.sort(Comparator.comparing(urlCache::get));
|
||||
|
||||
scripts.addAll(sortedResources);
|
||||
result.addAll(sortedResources);
|
||||
result.addAll(failingResources);
|
||||
}
|
||||
return scripts.toArray(new Resource[0]);
|
||||
return result.toArray(new Resource[0]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+9
@@ -21,6 +21,7 @@ import javax.sql.DataSource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.PropertyValue;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
@@ -32,6 +33,7 @@ import org.springframework.jdbc.BadSqlGrammarException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.AbstractDriverBasedDataSource;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean;
|
||||
import org.springframework.jdbc.datasource.init.CannotReadScriptException;
|
||||
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
@@ -67,6 +69,13 @@ class JdbcNamespaceIntegrationTests {
|
||||
assertCorrectSetup("jdbc-config-pattern.xml", "dataSource");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWithNonExistentResource() {
|
||||
assertThatExceptionOfType(BeanCreationException.class)
|
||||
.isThrownBy(() -> assertCorrectSetup("jdbc-config-nonexistent.xml", "dataSource"))
|
||||
.withCauseInstanceOf(CannotReadScriptException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWithAnonymousDataSourceAndDefaultDatabaseName() {
|
||||
assertThat(extractDataSourceUrl("jdbc-config-db-name-default-and-anonymous-datasource.xml"))
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/jdbc https://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
|
||||
|
||||
<jdbc:embedded-database id="dataSource" type="HSQL">
|
||||
<jdbc:script location="classpath:org/springframework/jdbc/config/db-schema.sql"/>
|
||||
<jdbc:script location="classpath:org/springframework/jdbc/config/db-test-data.sql"/>
|
||||
<jdbc:script location="classpath:org/springframework/jdbc/config/custom-data.sql"/> <!-- does not exist -->
|
||||
</jdbc:embedded-database>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user