Skip to content

Commit e608b36

Browse files
committed
Improve GenericApplicationContextTests.getResource*() tests
This commit updates the tests so that they test something meaningful on MS Windows as well as on Linux/Mac. See gh-28703, gh-28746
1 parent 59180e7 commit e608b36

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
package org.springframework.context.support;
1818

19+
import java.nio.file.InvalidPathException;
20+
1921
import org.junit.jupiter.api.AfterEach;
2022
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.condition.OS;
2124

2225
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
2326
import org.springframework.beans.factory.config.BeanDefinition;
@@ -238,25 +241,36 @@ private void assertGetResourceSemantics(ResourceLoader resourceLoader, Class<? e
238241
}
239242

240243
String relativePathLocation = "foo";
241-
String pingLocation = "ping:foo";
242244
String fileLocation = "file:foo";
245+
String pingLocation = "ping:foo";
243246

244247
Resource resource = context.getResource(relativePathLocation);
245248
assertThat(resource).isInstanceOf(defaultResourceType);
246249
resource = context.getResource(fileLocation);
247250
assertThat(resource).isInstanceOf(FileUrlResource.class);
248251

249-
context.addProtocolResolver(new PingPongProtocolResolver());
252+
if (OS.WINDOWS.isCurrentOs()) {
253+
// On Windows we expect an error similar to the following.
254+
// java.nio.file.InvalidPathException: Illegal char <:> at index 4: ping:foo
255+
assertThatExceptionOfType(InvalidPathException.class)
256+
.isThrownBy(() -> context.getResource(pingLocation))
257+
.withMessageContaining(pingLocation);
258+
}
259+
else {
260+
resource = context.getResource(pingLocation);
261+
assertThat(resource).isInstanceOf(defaultResourceType);
262+
}
250263

251-
resource = context.getResource(pingLocation);
252-
assertThat(resource).asInstanceOf(type(ByteArrayResource.class))
253-
.extracting(bar -> new String(bar.getByteArray(), UTF_8))
254-
.isEqualTo("pong:foo");
264+
context.addProtocolResolver(new PingPongProtocolResolver());
255265

256266
resource = context.getResource(relativePathLocation);
257267
assertThat(resource).isInstanceOf(defaultResourceType);
258268
resource = context.getResource(fileLocation);
259269
assertThat(resource).isInstanceOf(FileUrlResource.class);
270+
resource = context.getResource(pingLocation);
271+
assertThat(resource).asInstanceOf(type(ByteArrayResource.class))
272+
.extracting(bar -> new String(bar.getByteArray(), UTF_8))
273+
.isEqualTo("pong:foo");
260274
}
261275

262276

0 commit comments

Comments
 (0)