From 59180e76a7b35f060c056c2f44edc59d6e63a6ca Mon Sep 17 00:00:00 2001 From: jason <819635822@qq.com> Date: Sat, 2 Jul 2022 22:20:39 +0800 Subject: [PATCH 1/2] Fix GenericApplicationContextTests on Microsoft Windows The tests introduced in commit 9868c28c73 pass on Mac OS and Linux but fail on Microsoft Windows. This commit updates the tests so that they pass on MS Windows as well. See gh-28703 Closes gh-28746 --- .../context/support/GenericApplicationContextTests.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java b/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java index 6f0abe360c..6a3f3e5773 100644 --- a/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java +++ b/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java @@ -237,10 +237,11 @@ class GenericApplicationContextTests { context.setResourceLoader(resourceLoader); } + String relativePathLocation = "foo"; String pingLocation = "ping:foo"; String fileLocation = "file:foo"; - Resource resource = context.getResource(pingLocation); + Resource resource = context.getResource(relativePathLocation); assertThat(resource).isInstanceOf(defaultResourceType); resource = context.getResource(fileLocation); assertThat(resource).isInstanceOf(FileUrlResource.class); @@ -251,6 +252,9 @@ class GenericApplicationContextTests { assertThat(resource).asInstanceOf(type(ByteArrayResource.class)) .extracting(bar -> new String(bar.getByteArray(), UTF_8)) .isEqualTo("pong:foo"); + + resource = context.getResource(relativePathLocation); + assertThat(resource).isInstanceOf(defaultResourceType); resource = context.getResource(fileLocation); assertThat(resource).isInstanceOf(FileUrlResource.class); } From e608b367135fe232d359bd878085e609d2465a17 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 3 Jul 2022 14:34:53 +0200 Subject: [PATCH 2/2] 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 --- .../GenericApplicationContextTests.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java b/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java index 6a3f3e5773..fcf467a906 100644 --- a/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java +++ b/spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java @@ -16,8 +16,11 @@ package org.springframework.context.support; +import java.nio.file.InvalidPathException; + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.OS; import org.springframework.beans.factory.NoUniqueBeanDefinitionException; import org.springframework.beans.factory.config.BeanDefinition; @@ -238,25 +241,36 @@ class GenericApplicationContextTests { } String relativePathLocation = "foo"; - String pingLocation = "ping:foo"; String fileLocation = "file:foo"; + String pingLocation = "ping:foo"; Resource resource = context.getResource(relativePathLocation); assertThat(resource).isInstanceOf(defaultResourceType); resource = context.getResource(fileLocation); assertThat(resource).isInstanceOf(FileUrlResource.class); - context.addProtocolResolver(new PingPongProtocolResolver()); + if (OS.WINDOWS.isCurrentOs()) { + // On Windows we expect an error similar to the following. + // java.nio.file.InvalidPathException: Illegal char <:> at index 4: ping:foo + assertThatExceptionOfType(InvalidPathException.class) + .isThrownBy(() -> context.getResource(pingLocation)) + .withMessageContaining(pingLocation); + } + else { + resource = context.getResource(pingLocation); + assertThat(resource).isInstanceOf(defaultResourceType); + } - resource = context.getResource(pingLocation); - assertThat(resource).asInstanceOf(type(ByteArrayResource.class)) - .extracting(bar -> new String(bar.getByteArray(), UTF_8)) - .isEqualTo("pong:foo"); + context.addProtocolResolver(new PingPongProtocolResolver()); resource = context.getResource(relativePathLocation); assertThat(resource).isInstanceOf(defaultResourceType); resource = context.getResource(fileLocation); assertThat(resource).isInstanceOf(FileUrlResource.class); + resource = context.getResource(pingLocation); + assertThat(resource).asInstanceOf(type(ByteArrayResource.class)) + .extracting(bar -> new String(bar.getByteArray(), UTF_8)) + .isEqualTo("pong:foo"); }