17
17
package org .springframework .core .io ;
18
18
19
19
import java .io .FileNotFoundException ;
20
- import java .util .regex .Matcher ;
21
- import java .util .regex .Pattern ;
22
20
21
+ import org .junit .jupiter .api .Nested ;
23
22
import org .junit .jupiter .api .Test ;
24
23
25
24
import static org .assertj .core .api .Assertions .assertThat ;
26
25
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
27
26
28
27
/**
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.
31
32
*
32
33
* @author Chris Beams
33
34
* @author Sam Brannen
@@ -36,105 +37,110 @@ class ClassPathResourceTests {
36
37
37
38
private static final String PACKAGE_PATH = "org/springframework/core/io" ;
38
39
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 ;
40
42
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 ;
45
43
46
- private static final Pattern DESCRIPTION_PATTERN = Pattern .compile ("^class path resource \\ [(.+?)]$" );
44
+ @ Nested
45
+ class GetInputStream {
47
46
47
+ @ Test
48
+ void withStringConstructorRaisesExceptionForNonexistentResource () {
49
+ assertExceptionContainsAbsolutePath (new ClassPathResource (ABSOLUTE_PATH_TO_NONEXISTENT_RESOURCE ));
50
+ }
48
51
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
+ }
53
56
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
+ }
58
61
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
+ }
63
66
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
+ }
68
72
69
- @ Test
70
- void getDescriptionWithStringConstructorAndLeadingSlash () {
71
- assertDescriptionContainsExpectedPath (new ClassPathResource (FQ_RESOURCE_PATH_WITH_LEADING_SLASH ),
72
- FQ_RESOURCE_PATH );
73
73
}
74
74
75
- @ Test
76
- void getDescriptionWithClassLiteralConstructor () {
77
- assertDescriptionContainsExpectedPath (new ClassPathResource (NONEXISTENT_RESOURCE_NAME , getClass ()),
78
- FQ_RESOURCE_PATH );
79
- }
75
+ @ Nested
76
+ class GetDescription {
80
77
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
+ }
86
82
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
+ }
92
87
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
+ }
98
111
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" );
103
112
}
104
113
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
+
111
131
}
112
132
113
133
@ Test
114
- void directoryNotReadable () {
134
+ void directoryNotReadable () throws Exception {
115
135
Resource fileDir = new ClassPathResource ("org/springframework/core" );
136
+ assertThat (fileDir .getURL ()).asString ().startsWith ("file:" );
116
137
assertThat (fileDir .exists ()).isTrue ();
117
138
assertThat (fileDir .isReadable ()).isFalse ();
118
139
119
140
Resource jarDir = new ClassPathResource ("reactor/core" );
141
+ assertThat (jarDir .getURL ()).asString ().startsWith ("jar:" );
120
142
assertThat (jarDir .exists ()).isTrue ();
121
143
assertThat (jarDir .isReadable ()).isFalse ();
122
144
}
123
145
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
-
140
146
}
0 commit comments