1
1
/*
2
- * Copyright 2012-2024 the original author or authors.
2
+ * Copyright 2012-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
import java .io .IOException ;
21
21
import java .net .URL ;
22
22
import java .nio .charset .StandardCharsets ;
23
+ import java .nio .file .Path ;
23
24
import java .util .Base64 ;
24
25
import java .util .Enumeration ;
25
26
import java .util .function .UnaryOperator ;
45
46
* Tests for {@link ApplicationResourceLoader}.
46
47
*
47
48
* @author Phillip Webb
49
+ * @author Moritz Halbritter
48
50
*/
49
51
class ApplicationResourceLoaderTests {
50
52
@@ -61,6 +63,121 @@ void getIncludesProtocolResolvers() throws IOException {
61
63
assertThat (contentAsString (resource )).isEqualTo ("test" );
62
64
}
63
65
66
+ @ Test
67
+ void shouldLoadAbsolutePath () throws IOException {
68
+ Resource resource = ApplicationResourceLoader .get ().getResource ("/root/file.txt" );
69
+ assertThat (resource .isFile ()).isTrue ();
70
+ assertThat (resource .getFile ()).hasParent ("/root" ).hasName ("file.txt" );
71
+ }
72
+
73
+ @ Test
74
+ void shouldLoadAbsolutePathWithWorkingDirectory () throws IOException {
75
+ ClassLoader classLoader = getClass ().getClassLoader ();
76
+ Resource resource = ApplicationResourceLoader
77
+ .get (classLoader , SpringFactoriesLoader .forDefaultResourceLocation (classLoader ),
78
+ Path .of ("/working-directory" ))
79
+ .getResource ("/root/file.txt" );
80
+ assertThat (resource .isFile ()).isTrue ();
81
+ assertThat (resource .getFile ()).hasParent ("/root" ).hasName ("file.txt" );
82
+ }
83
+
84
+ @ Test
85
+ void shouldLoadRelativeFilename () throws IOException {
86
+ Resource resource = ApplicationResourceLoader .get ().getResource ("file.txt" );
87
+ assertThat (resource .isFile ()).isTrue ();
88
+ assertThat (resource .getFile ()).hasNoParent ().hasName ("file.txt" );
89
+ }
90
+
91
+ @ Test
92
+ void shouldLoadRelativeFilenameWithWorkingDirectory () throws IOException {
93
+ ClassLoader classLoader = getClass ().getClassLoader ();
94
+ Resource resource = ApplicationResourceLoader
95
+ .get (classLoader , SpringFactoriesLoader .forDefaultResourceLocation (classLoader ),
96
+ Path .of ("/working-directory" ))
97
+ .getResource ("file.txt" );
98
+ assertThat (resource .isFile ()).isTrue ();
99
+ assertThat (resource .getFile ()).hasParent ("/working-directory" ).hasName ("file.txt" );
100
+ }
101
+
102
+ @ Test
103
+ void shouldLoadRelativePathWithWorkingDirectory () throws IOException {
104
+ ClassLoader classLoader = getClass ().getClassLoader ();
105
+ Resource resource = ApplicationResourceLoader
106
+ .get (classLoader , SpringFactoriesLoader .forDefaultResourceLocation (classLoader ),
107
+ Path .of ("/working-directory" ))
108
+ .getResource ("a/file.txt" );
109
+ assertThat (resource .isFile ()).isTrue ();
110
+ assertThat (resource .getFile ()).hasParent ("/working-directory/a" ).hasName ("file.txt" );
111
+ }
112
+
113
+ @ Test
114
+ void shouldLoadClasspathLocations () {
115
+ Resource resource = ApplicationResourceLoader .get ().getResource ("classpath:a-file" );
116
+ assertThat (resource .exists ()).isTrue ();
117
+ }
118
+
119
+ @ Test
120
+ void shouldLoadNonExistentClasspathLocations () {
121
+ Resource resource = ApplicationResourceLoader .get ().getResource ("classpath:doesnt-exist" );
122
+ assertThat (resource .exists ()).isFalse ();
123
+ }
124
+
125
+ @ Test
126
+ void shouldLoadClasspathLocationsWithWorkingDirectory () {
127
+ ClassLoader classLoader = getClass ().getClassLoader ();
128
+ Resource resource = ApplicationResourceLoader
129
+ .get (classLoader , SpringFactoriesLoader .forDefaultResourceLocation (classLoader ),
130
+ Path .of ("/working-directory" ))
131
+ .getResource ("classpath:a-file" );
132
+ assertThat (resource .exists ()).isTrue ();
133
+ }
134
+
135
+ @ Test
136
+ void shouldLoadNonExistentClasspathLocationsWithWorkingDirectory () {
137
+ ClassLoader classLoader = getClass ().getClassLoader ();
138
+ Resource resource = ApplicationResourceLoader
139
+ .get (classLoader , SpringFactoriesLoader .forDefaultResourceLocation (classLoader ),
140
+ Path .of ("/working-directory" ))
141
+ .getResource ("classpath:doesnt-exist" );
142
+ assertThat (resource .exists ()).isFalse ();
143
+ }
144
+
145
+ @ Test
146
+ void shouldLoadRelativeFileUris () throws IOException {
147
+ Resource resource = ApplicationResourceLoader .get ().getResource ("file:file.txt" );
148
+ assertThat (resource .isFile ()).isTrue ();
149
+ assertThat (resource .getFile ()).hasNoParent ().hasName ("file.txt" );
150
+ }
151
+
152
+ @ Test
153
+ void shouldLoadAbsoluteFileUris () throws IOException {
154
+ Resource resource = ApplicationResourceLoader .get ().getResource ("file:/file.txt" );
155
+ assertThat (resource .isFile ()).isTrue ();
156
+ assertThat (resource .getFile ()).hasParent ("/" ).hasName ("file.txt" );
157
+ }
158
+
159
+ @ Test
160
+ void shouldLoadRelativeFileUrisWithWorkingDirectory () throws IOException {
161
+ ClassLoader classLoader = getClass ().getClassLoader ();
162
+ Resource resource = ApplicationResourceLoader
163
+ .get (classLoader , SpringFactoriesLoader .forDefaultResourceLocation (classLoader ),
164
+ Path .of ("/working-directory" ))
165
+ .getResource ("file:file.txt" );
166
+ assertThat (resource .isFile ()).isTrue ();
167
+ assertThat (resource .getFile ()).hasParent ("/working-directory" ).hasName ("file.txt" );
168
+ }
169
+
170
+ @ Test
171
+ void shouldLoadAbsoluteFileUrisWithWorkingDirectory () throws IOException {
172
+ ClassLoader classLoader = getClass ().getClassLoader ();
173
+ Resource resource = ApplicationResourceLoader
174
+ .get (classLoader , SpringFactoriesLoader .forDefaultResourceLocation (classLoader ),
175
+ Path .of ("/working-directory" ))
176
+ .getResource ("file:/file.txt" );
177
+ assertThat (resource .isFile ()).isTrue ();
178
+ assertThat (resource .getFile ()).hasParent ("/" ).hasName ("file.txt" );
179
+ }
180
+
64
181
@ Test
65
182
void getWithClassPathIncludesProtocolResolvers () throws IOException {
66
183
ClassLoader classLoader = new TestClassLoader (this ::useTestProtocolResolversFactories );
0 commit comments