Avoid questionable use of ClassPathResource#getPath() in tests

Prior to this commit, several tests used ClassPathResource#getPath()
based on the knowledge that the ClassPathResource had been created
using the ClassPathResource(String,Class) constructor. However, making
such an assumption seems ill advised in light of the abstraction that
ClassPathResource provides.

In light of that, this commit avoids questionable use of
ClassPathResource#getPath() in tests by refactoring those tests to use
the proper abstractions provided by ClassPathResource.
This commit is contained in:
Sam Brannen
2022-09-07 14:59:05 +02:00
parent 4cc91e46b2
commit 1688becd73
3 changed files with 27 additions and 33 deletions

View File

@@ -22,8 +22,6 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
@@ -173,7 +171,7 @@ class XmlBeanFactoryTests {
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
try (InputStream inputStream = getClass().getResourceAsStream(REFTYPES_CONTEXT.getPath())) {
try (InputStream inputStream = REFTYPES_CONTEXT.getInputStream()) {
reader.loadBeanDefinitions(new InputSource(inputStream));
}
@@ -1172,10 +1170,9 @@ class XmlBeanFactoryTests {
}
@Test
void urlResourceWithImport() {
URL url = getClass().getResource(RESOURCE_CONTEXT.getPath());
void urlResourceWithImport() throws Exception {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new UrlResource(url));
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new UrlResource(RESOURCE_CONTEXT.getURL()));
// comes from "resourceImport.xml"
xbf.getBean("resource1", ResourceTestBean.class);
// comes from "resource.xml"
@@ -1183,10 +1180,9 @@ class XmlBeanFactoryTests {
}
@Test
void fileSystemResourceWithImport() throws URISyntaxException {
String file = getClass().getResource(RESOURCE_CONTEXT.getPath()).toURI().getPath();
void fileSystemResourceWithImport() throws Exception {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new FileSystemResource(file));
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new FileSystemResource(RESOURCE_CONTEXT.getFile()));
// comes from "resourceImport.xml"
xbf.getBean("resource1", ResourceTestBean.class);
// comes from "resource.xml"