Skip to content

Commit e653ee6

Browse files
committed
Polish ClassPathResourceTests
1 parent 138f9c6 commit e653ee6

File tree

1 file changed

+83
-77
lines changed

1 file changed

+83
-77
lines changed

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

Lines changed: 83 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717
package org.springframework.core.io;
1818

1919
import java.io.FileNotFoundException;
20-
import java.util.regex.Matcher;
21-
import java.util.regex.Pattern;
2220

21+
import org.junit.jupiter.api.Nested;
2322
import org.junit.jupiter.api.Test;
2423

2524
import static org.assertj.core.api.Assertions.assertThat;
2625
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2726

2827
/**
29-
* Unit tests that serve as regression tests for the bugs described in SPR-6888
30-
* and SPR-9413.
28+
* Unit tests for {@link ClassPathResource}.
29+
*
30+
* <p>These also originally served as regression tests for the bugs described in
31+
* SPR-6888 and SPR-9413.
3132
*
3233
* @author Chris Beams
3334
* @author Sam Brannen
@@ -36,105 +37,110 @@ class ClassPathResourceTests {
3637

3738
private static final String PACKAGE_PATH = "org/springframework/core/io";
3839
private static final String NONEXISTENT_RESOURCE_NAME = "nonexistent.xml";
39-
private static final String FQ_RESOURCE_PATH = PACKAGE_PATH + '/' + NONEXISTENT_RESOURCE_NAME;
40+
private static final String ABSOLUTE_PATH_TO_NONEXISTENT_RESOURCE = PACKAGE_PATH + '/' + NONEXISTENT_RESOURCE_NAME;
41+
private static final String ABSOLUTE_PATH_TO_NONEXISTENT_RESOURCE_WITH_LEADING_SLASH = '/' + ABSOLUTE_PATH_TO_NONEXISTENT_RESOURCE;
4042

41-
/**
42-
* Absolute path version of {@link #FQ_RESOURCE_PATH}.
43-
*/
44-
private static final String FQ_RESOURCE_PATH_WITH_LEADING_SLASH = '/' + FQ_RESOURCE_PATH;
4543

46-
private static final Pattern DESCRIPTION_PATTERN = Pattern.compile("^class path resource \\[(.+?)]$");
44+
@Nested
45+
class GetInputStream {
4746

47+
@Test
48+
void withStringConstructorRaisesExceptionForNonexistentResource() {
49+
assertExceptionContainsAbsolutePath(new ClassPathResource(ABSOLUTE_PATH_TO_NONEXISTENT_RESOURCE));
50+
}
4851

49-
@Test
50-
void stringConstructorRaisesExceptionWithFullyQualifiedPath() {
51-
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(FQ_RESOURCE_PATH));
52-
}
52+
@Test
53+
void withClassLoaderConstructorRaisesExceptionForNonexistentResource() {
54+
assertExceptionContainsAbsolutePath(new ClassPathResource(ABSOLUTE_PATH_TO_NONEXISTENT_RESOURCE, getClass().getClassLoader()));
55+
}
5356

54-
@Test
55-
void classLiteralConstructorRaisesExceptionWithFullyQualifiedPath() {
56-
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(NONEXISTENT_RESOURCE_NAME, getClass()));
57-
}
57+
@Test
58+
void withClassLiteralConstructorRaisesExceptionForNonexistentRelativeResource() {
59+
assertExceptionContainsAbsolutePath(new ClassPathResource(NONEXISTENT_RESOURCE_NAME, getClass()));
60+
}
5861

59-
@Test
60-
void classLoaderConstructorRaisesExceptionWithFullyQualifiedPath() {
61-
assertExceptionContainsFullyQualifiedPath(new ClassPathResource(FQ_RESOURCE_PATH, getClass().getClassLoader()));
62-
}
62+
@Test
63+
void withClassLiteralConstructorRaisesExceptionForNonexistentAbsoluteResource() {
64+
assertExceptionContainsAbsolutePath(new ClassPathResource(ABSOLUTE_PATH_TO_NONEXISTENT_RESOURCE, getClass()));
65+
}
6366

64-
@Test
65-
void getDescriptionWithStringConstructor() {
66-
assertDescriptionContainsExpectedPath(new ClassPathResource(FQ_RESOURCE_PATH), FQ_RESOURCE_PATH);
67-
}
67+
private static void assertExceptionContainsAbsolutePath(ClassPathResource resource) {
68+
assertThatExceptionOfType(FileNotFoundException.class)
69+
.isThrownBy(resource::getInputStream)
70+
.withMessageContaining(ABSOLUTE_PATH_TO_NONEXISTENT_RESOURCE);
71+
}
6872

69-
@Test
70-
void getDescriptionWithStringConstructorAndLeadingSlash() {
71-
assertDescriptionContainsExpectedPath(new ClassPathResource(FQ_RESOURCE_PATH_WITH_LEADING_SLASH),
72-
FQ_RESOURCE_PATH);
7373
}
7474

75-
@Test
76-
void getDescriptionWithClassLiteralConstructor() {
77-
assertDescriptionContainsExpectedPath(new ClassPathResource(NONEXISTENT_RESOURCE_NAME, getClass()),
78-
FQ_RESOURCE_PATH);
79-
}
75+
@Nested
76+
class GetDescription {
8077

81-
@Test
82-
void getDescriptionWithClassLiteralConstructorAndLeadingSlash() {
83-
assertDescriptionContainsExpectedPath(
84-
new ClassPathResource(FQ_RESOURCE_PATH_WITH_LEADING_SLASH, getClass()), FQ_RESOURCE_PATH);
85-
}
78+
@Test
79+
void withStringConstructor() {
80+
assertDescription(new ClassPathResource(ABSOLUTE_PATH_TO_NONEXISTENT_RESOURCE));
81+
}
8682

87-
@Test
88-
void getDescriptionWithClassLoaderConstructor() {
89-
assertDescriptionContainsExpectedPath(
90-
new ClassPathResource(FQ_RESOURCE_PATH, getClass().getClassLoader()), FQ_RESOURCE_PATH);
91-
}
83+
@Test
84+
void withStringConstructorAndLeadingSlash() {
85+
assertDescription(new ClassPathResource(ABSOLUTE_PATH_TO_NONEXISTENT_RESOURCE_WITH_LEADING_SLASH));
86+
}
9287

93-
@Test
94-
void getDescriptionWithClassLoaderConstructorAndLeadingSlash() {
95-
assertDescriptionContainsExpectedPath(
96-
new ClassPathResource(FQ_RESOURCE_PATH_WITH_LEADING_SLASH, getClass().getClassLoader()), FQ_RESOURCE_PATH);
97-
}
88+
@Test
89+
void withClassLiteralConstructor() {
90+
assertDescription(new ClassPathResource(NONEXISTENT_RESOURCE_NAME, getClass()));
91+
}
92+
93+
@Test
94+
void withClassLiteralConstructorAndLeadingSlash() {
95+
assertDescription(new ClassPathResource(ABSOLUTE_PATH_TO_NONEXISTENT_RESOURCE_WITH_LEADING_SLASH, getClass()));
96+
}
97+
98+
@Test
99+
void withClassLoaderConstructor() {
100+
assertDescription(new ClassPathResource(ABSOLUTE_PATH_TO_NONEXISTENT_RESOURCE, getClass().getClassLoader()));
101+
}
102+
103+
@Test
104+
void withClassLoaderConstructorAndLeadingSlash() {
105+
assertDescription(new ClassPathResource(ABSOLUTE_PATH_TO_NONEXISTENT_RESOURCE_WITH_LEADING_SLASH, getClass().getClassLoader()));
106+
}
107+
108+
private static void assertDescription(ClassPathResource resource) {
109+
assertThat(resource.getDescription()).isEqualTo("class path resource [%s]", ABSOLUTE_PATH_TO_NONEXISTENT_RESOURCE);
110+
}
98111

99-
@Test
100-
void dropLeadingSlashForClassLoaderAccess() {
101-
assertThat(new ClassPathResource("/test.html").getPath()).isEqualTo("test.html");
102-
assertThat(((ClassPathResource) new ClassPathResource("").createRelative("/test.html")).getPath()).isEqualTo("test.html");
103112
}
104113

105-
@Test
106-
void convertToAbsolutePathForClassRelativeAccess() {
107-
assertThat(new ClassPathResource("/test.html", getClass()).getPath()).isEqualTo("test.html");
108-
assertThat(new ClassPathResource("", getClass()).getPath()).isEqualTo(PACKAGE_PATH + "/");
109-
assertThat(((ClassPathResource) new ClassPathResource("", getClass()).createRelative("/test.html")).getPath()).isEqualTo("test.html");
110-
assertThat(((ClassPathResource) new ClassPathResource("", getClass()).createRelative("test.html")).getPath()).isEqualTo(PACKAGE_PATH + "/test.html");
114+
@Nested
115+
class GetPath {
116+
117+
@Test
118+
void dropsLeadingSlashForClassLoaderAccess() {
119+
assertThat(new ClassPathResource("/test.html").getPath()).isEqualTo("test.html");
120+
assertThat(((ClassPathResource) new ClassPathResource("").createRelative("/test.html")).getPath()).isEqualTo("test.html");
121+
}
122+
123+
@Test
124+
void convertsToAbsolutePathForClassRelativeAccess() {
125+
assertThat(new ClassPathResource("/test.html", getClass()).getPath()).isEqualTo("test.html");
126+
assertThat(new ClassPathResource("", getClass()).getPath()).isEqualTo(PACKAGE_PATH + "/");
127+
assertThat(((ClassPathResource) new ClassPathResource("", getClass()).createRelative("/test.html")).getPath()).isEqualTo("test.html");
128+
assertThat(((ClassPathResource) new ClassPathResource("", getClass()).createRelative("test.html")).getPath()).isEqualTo(PACKAGE_PATH + "/test.html");
129+
}
130+
111131
}
112132

113133
@Test
114-
void directoryNotReadable() {
134+
void directoryNotReadable() throws Exception {
115135
Resource fileDir = new ClassPathResource("org/springframework/core");
136+
assertThat(fileDir.getURL()).asString().startsWith("file:");
116137
assertThat(fileDir.exists()).isTrue();
117138
assertThat(fileDir.isReadable()).isFalse();
118139

119140
Resource jarDir = new ClassPathResource("reactor/core");
141+
assertThat(jarDir.getURL()).asString().startsWith("jar:");
120142
assertThat(jarDir.exists()).isTrue();
121143
assertThat(jarDir.isReadable()).isFalse();
122144
}
123145

124-
125-
private void assertDescriptionContainsExpectedPath(ClassPathResource resource, String expectedPath) {
126-
Matcher matcher = DESCRIPTION_PATTERN.matcher(resource.getDescription());
127-
assertThat(matcher.matches()).isTrue();
128-
assertThat(matcher.groupCount()).isEqualTo(1);
129-
String match = matcher.group(1);
130-
131-
assertThat(match).isEqualTo(expectedPath);
132-
}
133-
134-
private void assertExceptionContainsFullyQualifiedPath(ClassPathResource resource) {
135-
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(
136-
resource::getInputStream)
137-
.withMessageContaining(FQ_RESOURCE_PATH);
138-
}
139-
140146
}

0 commit comments

Comments
 (0)