Skip to content

Commit d00e4f1

Browse files
committed
ClassPathResource uses specific constructors in createRelative
Issue: SPR-16146
1 parent b921d56 commit d00e4f1

File tree

2 files changed

+48
-32
lines changed

2 files changed

+48
-32
lines changed

spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ public ClassPathResource(String path, @Nullable Class<?> clazz) {
105105
* @param path relative or absolute path within the classpath
106106
* @param classLoader the class loader to load the resource with, if any
107107
* @param clazz the class to load resources with, if any
108+
* @deprecated as of 4.3.13, in favor of selective use of
109+
* {@link #ClassPathResource(String, ClassLoader)} vs {@link #ClassPathResource(String, Class)}
108110
*/
111+
@Deprecated
109112
protected ClassPathResource(String path, @Nullable ClassLoader classLoader, @Nullable Class<?> clazz) {
110113
this.path = StringUtils.cleanPath(path);
111114
this.classLoader = classLoader;
@@ -202,7 +205,8 @@ public URL getURL() throws IOException {
202205
@Override
203206
public Resource createRelative(String relativePath) {
204207
String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
205-
return new ClassPathResource(pathToUse, this.classLoader, this.clazz);
208+
return (this.clazz != null ? new ClassPathResource(pathToUse, this.clazz) :
209+
new ClassPathResource(pathToUse, this.classLoader));
206210
}
207211

208212
/**

spring-core/src/test/java/org/springframework/core/io/ClassPathResourceTests.java

+43-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,40 +47,19 @@ public class ClassPathResourceTests {
4747
private static final Pattern DESCRIPTION_PATTERN = Pattern.compile("^class path resource \\[(.+?)\\]$");
4848

4949

50-
private void assertDescriptionContainsExpectedPath(ClassPathResource resource, String expectedPath) {
51-
Matcher matcher = DESCRIPTION_PATTERN.matcher(resource.getDescription());
52-
assertTrue(matcher.matches());
53-
assertEquals(1, matcher.groupCount());
54-
String match = matcher.group(1);
55-
56-
assertEquals(expectedPath, match);
57-
}
58-
59-
private void assertExceptionContainsFullyQualifiedPath(ClassPathResource resource) {
60-
try {
61-
resource.getInputStream();
62-
fail("FileNotFoundException expected for resource: " + resource);
63-
}
64-
catch (IOException ex) {
65-
assertThat(ex, instanceOf(FileNotFoundException.class));
66-
assertThat(ex.getMessage(), containsString(FQ_RESOURCE_PATH));
67-
}
68-
}
69-
7050
@Test
7151
public void stringConstructorRaisesExceptionWithFullyQualifiedPath() {
7252
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(FQ_RESOURCE_PATH));
7353
}
7454

7555
@Test
7656
public void classLiteralConstructorRaisesExceptionWithFullyQualifiedPath() {
77-
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(NONEXISTENT_RESOURCE_NAME, this.getClass()));
57+
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(NONEXISTENT_RESOURCE_NAME, getClass()));
7858
}
7959

8060
@Test
8161
public void classLoaderConstructorRaisesExceptionWithFullyQualifiedPath() {
82-
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(FQ_RESOURCE_PATH,
83-
this.getClass().getClassLoader()));
62+
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(FQ_RESOURCE_PATH, getClass().getClassLoader()));
8463
}
8564

8665
@Test
@@ -91,31 +70,64 @@ public void getDescriptionWithStringConstructor() {
9170
@Test
9271
public void getDescriptionWithStringConstructorAndLeadingSlash() {
9372
assertDescriptionContainsExpectedPath(new ClassPathResource(FQ_RESOURCE_PATH_WITH_LEADING_SLASH),
94-
FQ_RESOURCE_PATH);
73+
FQ_RESOURCE_PATH);
9574
}
9675

9776
@Test
9877
public void getDescriptionWithClassLiteralConstructor() {
99-
assertDescriptionContainsExpectedPath(new ClassPathResource(NONEXISTENT_RESOURCE_NAME, this.getClass()),
100-
FQ_RESOURCE_PATH);
78+
assertDescriptionContainsExpectedPath(new ClassPathResource(NONEXISTENT_RESOURCE_NAME, getClass()),
79+
FQ_RESOURCE_PATH);
10180
}
10281

10382
@Test
10483
public void getDescriptionWithClassLiteralConstructorAndLeadingSlash() {
10584
assertDescriptionContainsExpectedPath(
106-
new ClassPathResource(FQ_RESOURCE_PATH_WITH_LEADING_SLASH, this.getClass()), FQ_RESOURCE_PATH);
85+
new ClassPathResource(FQ_RESOURCE_PATH_WITH_LEADING_SLASH, getClass()), FQ_RESOURCE_PATH);
10786
}
10887

10988
@Test
11089
public void getDescriptionWithClassLoaderConstructor() {
11190
assertDescriptionContainsExpectedPath(
112-
new ClassPathResource(FQ_RESOURCE_PATH, this.getClass().getClassLoader()), FQ_RESOURCE_PATH);
91+
new ClassPathResource(FQ_RESOURCE_PATH, getClass().getClassLoader()), FQ_RESOURCE_PATH);
11392
}
11493

11594
@Test
11695
public void getDescriptionWithClassLoaderConstructorAndLeadingSlash() {
117-
assertDescriptionContainsExpectedPath(new ClassPathResource(FQ_RESOURCE_PATH_WITH_LEADING_SLASH,
118-
this.getClass().getClassLoader()), FQ_RESOURCE_PATH);
96+
assertDescriptionContainsExpectedPath(
97+
new ClassPathResource(FQ_RESOURCE_PATH_WITH_LEADING_SLASH, getClass().getClassLoader()), FQ_RESOURCE_PATH);
98+
}
99+
100+
@Test
101+
public void dropLeadingSlashForClassLoaderAccess() {
102+
assertEquals("test.html", new ClassPathResource("/test.html").getPath());
103+
assertEquals("test.html", ((ClassPathResource) new ClassPathResource("").createRelative("/test.html")).getPath());
104+
}
105+
106+
@Test
107+
public void preserveLeadingSlashForClassRelativeAccess() {
108+
assertEquals("/test.html", new ClassPathResource("/test.html", getClass()).getPath());
109+
assertEquals("/test.html", ((ClassPathResource) new ClassPathResource("", getClass()).createRelative("/test.html")).getPath());
110+
}
111+
112+
113+
private void assertDescriptionContainsExpectedPath(ClassPathResource resource, String expectedPath) {
114+
Matcher matcher = DESCRIPTION_PATTERN.matcher(resource.getDescription());
115+
assertTrue(matcher.matches());
116+
assertEquals(1, matcher.groupCount());
117+
String match = matcher.group(1);
118+
119+
assertEquals(expectedPath, match);
120+
}
121+
122+
private void assertExceptionContainsFullyQualifiedPath(ClassPathResource resource) {
123+
try {
124+
resource.getInputStream();
125+
fail("FileNotFoundException expected for resource: " + resource);
126+
}
127+
catch (IOException ex) {
128+
assertThat(ex, instanceOf(FileNotFoundException.class));
129+
assertThat(ex.getMessage(), containsString(FQ_RESOURCE_PATH));
130+
}
119131
}
120132

121133
}

0 commit comments

Comments
 (0)