Browse Source

Expose non-existent resources at the end of the sorted result

Closes gh-35895
pull/35899/head
Juergen Hoeller 3 weeks ago
parent
commit
c1b6bfb681
  1. 15
      spring-jdbc/src/main/java/org/springframework/jdbc/config/SortedResourcesFactoryBean.java
  2. 9
      spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java
  3. 14
      spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-config-nonexistent.xml

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

@ -72,20 +72,22 @@ public class SortedResourcesFactoryBean extends AbstractFactoryBean<Resource[]> @@ -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[]> @@ -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
spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java

@ -22,6 +22,7 @@ import org.jspecify.annotations.Nullable; @@ -22,6 +22,7 @@ import org.jspecify.annotations.Nullable;
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;
@ -33,6 +34,7 @@ import org.springframework.jdbc.BadSqlGrammarException; @@ -33,6 +34,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 static org.assertj.core.api.Assertions.assertThat;
@ -67,6 +69,13 @@ class JdbcNamespaceIntegrationTests { @@ -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
spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-config-nonexistent.xml

@ -0,0 +1,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>
Loading…
Cancel
Save