提交 cef4478b 编写于 作者: S Sam Brannen

Treat InvalidPathException like an IOException in MockServletContext

Prior to this commit, if MockServletContext was configured with a
FileSystemResourceLoader, invocations of the following methods on a
Microsoft Windows operating system resulted in an InvalidPathException
if the supplied path contained a colon (such as "C:\\temp"). This is
inconsistent with the behavior on non-Windows operating systems. In
addition, for comparable errors resulting in an IOException, those
methods (except getRealPath()) return null instead of throwing the
exception.

- getResourcePaths()
- getResource()
- getResourceAsStream()
- getRealPath()

This commit makes handling of InvalidPathException and IOException
consistent for these methods: both exceptions now result in null be
returned by these methods.

Closes gh-23717
上级 8d88e291
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -21,6 +21,7 @@ import java.io.IOException; ...@@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.file.InvalidPathException;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.EventListener; import java.util.EventListener;
...@@ -294,8 +295,10 @@ public class MockServletContext implements ServletContext { ...@@ -294,8 +295,10 @@ public class MockServletContext implements ServletContext {
@Nullable @Nullable
public Set<String> getResourcePaths(String path) { public Set<String> getResourcePaths(String path) {
String actualPath = (path.endsWith("/") ? path : path + "/"); String actualPath = (path.endsWith("/") ? path : path + "/");
Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath)); String resourceLocation = getResourceLocation(actualPath);
Resource resource = null;
try { try {
resource = this.resourceLoader.getResource(resourceLocation);
File file = resource.getFile(); File file = resource.getFile();
String[] fileList = file.list(); String[] fileList = file.list();
if (ObjectUtils.isEmpty(fileList)) { if (ObjectUtils.isEmpty(fileList)) {
...@@ -311,9 +314,10 @@ public class MockServletContext implements ServletContext { ...@@ -311,9 +314,10 @@ public class MockServletContext implements ServletContext {
} }
return resourcePaths; return resourcePaths;
} }
catch (IOException ex) { catch (InvalidPathException | IOException ex ) {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
logger.warn("Could not get resource paths for " + resource, ex); logger.warn("Could not get resource paths for " +
(resource != null ? resource : resourceLocation), ex);
} }
return null; return null;
} }
...@@ -322,19 +326,22 @@ public class MockServletContext implements ServletContext { ...@@ -322,19 +326,22 @@ public class MockServletContext implements ServletContext {
@Override @Override
@Nullable @Nullable
public URL getResource(String path) throws MalformedURLException { public URL getResource(String path) throws MalformedURLException {
Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); String resourceLocation = getResourceLocation(path);
if (!resource.exists()) { Resource resource = null;
return null;
}
try { try {
resource = this.resourceLoader.getResource(resourceLocation);
if (!resource.exists()) {
return null;
}
return resource.getURL(); return resource.getURL();
} }
catch (MalformedURLException ex) { catch (MalformedURLException ex) {
throw ex; throw ex;
} }
catch (IOException ex) { catch (InvalidPathException | IOException ex) {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
logger.warn("Could not get URL for " + resource, ex); logger.warn("Could not get URL for resource " +
(resource != null ? resource : resourceLocation), ex);
} }
return null; return null;
} }
...@@ -343,16 +350,19 @@ public class MockServletContext implements ServletContext { ...@@ -343,16 +350,19 @@ public class MockServletContext implements ServletContext {
@Override @Override
@Nullable @Nullable
public InputStream getResourceAsStream(String path) { public InputStream getResourceAsStream(String path) {
Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); String resourceLocation = getResourceLocation(path);
if (!resource.exists()) { Resource resource = null;
return null;
}
try { try {
resource = this.resourceLoader.getResource(resourceLocation);
if (!resource.exists()) {
return null;
}
return resource.getInputStream(); return resource.getInputStream();
} }
catch (IOException ex) { catch (InvalidPathException | IOException ex) {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
logger.warn("Could not open InputStream for " + resource, ex); logger.warn("Could not open InputStream for resource " +
(resource != null ? resource : resourceLocation), ex);
} }
return null; return null;
} }
...@@ -459,13 +469,16 @@ public class MockServletContext implements ServletContext { ...@@ -459,13 +469,16 @@ public class MockServletContext implements ServletContext {
@Override @Override
@Nullable @Nullable
public String getRealPath(String path) { public String getRealPath(String path) {
Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); String resourceLocation = getResourceLocation(path);
Resource resource = null;
try { try {
resource = this.resourceLoader.getResource(resourceLocation);
return resource.getFile().getAbsolutePath(); return resource.getFile().getAbsolutePath();
} }
catch (IOException ex) { catch (InvalidPathException | IOException ex) {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
logger.warn("Could not determine real path of resource " + resource, ex); logger.warn("Could not determine real path of resource " +
(resource != null ? resource : resourceLocation), ex);
} }
return null; return null;
} }
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package org.springframework.mock.web; package org.springframework.mock.web;
import java.io.InputStream;
import java.net.URL;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
...@@ -23,171 +25,225 @@ import javax.servlet.FilterRegistration; ...@@ -23,171 +25,225 @@ import javax.servlet.FilterRegistration;
import javax.servlet.RequestDispatcher; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletRegistration; import javax.servlet.ServletRegistration;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.OS;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Unit tests for {@link MockServletContext}.
*
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams * @author Chris Beams
* @author Sam Brannen * @author Sam Brannen
* @since 19.02.2006 * @since 19.02.2006
*/ */
@DisplayName("MockServletContext unit tests")
class MockServletContextTests { class MockServletContextTests {
private final MockServletContext sc = new MockServletContext("org/springframework/mock"); @Nested
@DisplayName("with DefaultResourceLoader")
class MockServletContextWithDefaultResourceLoaderTests {
@Test
void listFiles() { private final MockServletContext servletContext = new MockServletContext("org/springframework/mock");
Set<String> paths = sc.getResourcePaths("/web");
assertThat(paths).isNotNull(); @Test
assertThat(paths.contains("/web/MockServletContextTests.class")).isTrue(); void getResourcePaths() {
} Set<String> paths = servletContext.getResourcePaths("/web");
assertThat(paths).isNotNull();
@Test assertThat(paths.contains("/web/MockServletContextTests.class")).isTrue();
void listSubdirectories() { }
Set<String> paths = sc.getResourcePaths("/");
assertThat(paths).isNotNull(); @Test
assertThat(paths.contains("/web/")).isTrue(); void getResourcePathsWithSubdirectories() {
} Set<String> paths = servletContext.getResourcePaths("/");
assertThat(paths).isNotNull();
@Test assertThat(paths.contains("/web/")).isTrue();
void listNonDirectory() { }
Set<String> paths = sc.getResourcePaths("/web/MockServletContextTests.class");
assertThat(paths).isNull(); @Test
} void getResourcePathsWithNonDirectory() {
Set<String> paths = servletContext.getResourcePaths("/web/MockServletContextTests.class");
@Test assertThat(paths).isNull();
void listInvalidPath() { }
Set<String> paths = sc.getResourcePaths("/web/invalid");
assertThat(paths).isNull(); @Test
} void getResourcePathsWithInvalidPath() {
Set<String> paths = servletContext.getResourcePaths("/web/invalid");
@Test assertThat(paths).isNull();
void registerContextAndGetContext() { }
MockServletContext sc2 = new MockServletContext();
sc.setContextPath("/"); @Test
sc.registerContext("/second", sc2); void registerContextAndGetContext() {
assertThat(sc.getContext("/")).isSameAs(sc); MockServletContext sc2 = new MockServletContext();
assertThat(sc.getContext("/second")).isSameAs(sc2); servletContext.setContextPath("/");
} servletContext.registerContext("/second", sc2);
assertThat(servletContext.getContext("/")).isSameAs(servletContext);
@Test assertThat(servletContext.getContext("/second")).isSameAs(sc2);
void getMimeType() { }
assertThat(sc.getMimeType("test.html")).isEqualTo("text/html");
assertThat(sc.getMimeType("test.gif")).isEqualTo("image/gif"); @Test
assertThat(sc.getMimeType("test.foobar")).isNull(); void getMimeType() {
} assertThat(servletContext.getMimeType("test.html")).isEqualTo("text/html");
assertThat(servletContext.getMimeType("test.gif")).isEqualTo("image/gif");
/** assertThat(servletContext.getMimeType("test.foobar")).isNull();
* Introduced to dispel claims in a thread on Stack Overflow: }
* <a href="https://stackoverflow.com/questions/22986109/testing-spring-managed-servlet">Testing Spring managed servlet</a>
*/ /**
@Test * Introduced to dispel claims in a thread on Stack Overflow:
void getMimeTypeWithCustomConfiguredType() { * <a href="https://stackoverflow.com/questions/22986109/testing-spring-managed-servlet">Testing Spring managed servlet</a>
sc.addMimeType("enigma", new MediaType("text", "enigma")); */
assertThat(sc.getMimeType("filename.enigma")).isEqualTo("text/enigma"); @Test
} void getMimeTypeWithCustomConfiguredType() {
servletContext.addMimeType("enigma", new MediaType("text", "enigma"));
@Test assertThat(servletContext.getMimeType("filename.enigma")).isEqualTo("text/enigma");
void servletVersion() { }
assertThat(sc.getMajorVersion()).isEqualTo(3);
assertThat(sc.getMinorVersion()).isEqualTo(1); @Test
assertThat(sc.getEffectiveMajorVersion()).isEqualTo(3); void servletVersion() {
assertThat(sc.getEffectiveMinorVersion()).isEqualTo(1); assertThat(servletContext.getMajorVersion()).isEqualTo(3);
assertThat(servletContext.getMinorVersion()).isEqualTo(1);
sc.setMajorVersion(4); assertThat(servletContext.getEffectiveMajorVersion()).isEqualTo(3);
sc.setMinorVersion(0); assertThat(servletContext.getEffectiveMinorVersion()).isEqualTo(1);
sc.setEffectiveMajorVersion(4);
sc.setEffectiveMinorVersion(0); servletContext.setMajorVersion(4);
assertThat(sc.getMajorVersion()).isEqualTo(4); servletContext.setMinorVersion(0);
assertThat(sc.getMinorVersion()).isEqualTo(0); servletContext.setEffectiveMajorVersion(4);
assertThat(sc.getEffectiveMajorVersion()).isEqualTo(4); servletContext.setEffectiveMinorVersion(0);
assertThat(sc.getEffectiveMinorVersion()).isEqualTo(0); assertThat(servletContext.getMajorVersion()).isEqualTo(4);
} assertThat(servletContext.getMinorVersion()).isEqualTo(0);
assertThat(servletContext.getEffectiveMajorVersion()).isEqualTo(4);
@Test assertThat(servletContext.getEffectiveMinorVersion()).isEqualTo(0);
void registerAndUnregisterNamedDispatcher() throws Exception { }
final String name = "test-servlet";
final String url = "/test"; @Test
assertThat(sc.getNamedDispatcher(name)).isNull(); void registerAndUnregisterNamedDispatcher() throws Exception {
final String name = "test-servlet";
sc.registerNamedDispatcher(name, new MockRequestDispatcher(url)); final String url = "/test";
RequestDispatcher namedDispatcher = sc.getNamedDispatcher(name); assertThat(servletContext.getNamedDispatcher(name)).isNull();
assertThat(namedDispatcher).isNotNull();
MockHttpServletResponse response = new MockHttpServletResponse(); servletContext.registerNamedDispatcher(name, new MockRequestDispatcher(url));
namedDispatcher.forward(new MockHttpServletRequest(sc), response); RequestDispatcher namedDispatcher = servletContext.getNamedDispatcher(name);
assertThat(response.getForwardedUrl()).isEqualTo(url); assertThat(namedDispatcher).isNotNull();
MockHttpServletResponse response = new MockHttpServletResponse();
sc.unregisterNamedDispatcher(name); namedDispatcher.forward(new MockHttpServletRequest(servletContext), response);
assertThat(sc.getNamedDispatcher(name)).isNull(); assertThat(response.getForwardedUrl()).isEqualTo(url);
}
servletContext.unregisterNamedDispatcher(name);
@Test assertThat(servletContext.getNamedDispatcher(name)).isNull();
void getNamedDispatcherForDefaultServlet() throws Exception { }
final String name = "default";
RequestDispatcher namedDispatcher = sc.getNamedDispatcher(name); @Test
assertThat(namedDispatcher).isNotNull(); void getNamedDispatcherForDefaultServlet() throws Exception {
final String name = "default";
MockHttpServletResponse response = new MockHttpServletResponse(); RequestDispatcher namedDispatcher = servletContext.getNamedDispatcher(name);
namedDispatcher.forward(new MockHttpServletRequest(sc), response); assertThat(namedDispatcher).isNotNull();
assertThat(response.getForwardedUrl()).isEqualTo(name);
} MockHttpServletResponse response = new MockHttpServletResponse();
namedDispatcher.forward(new MockHttpServletRequest(servletContext), response);
@Test assertThat(response.getForwardedUrl()).isEqualTo(name);
void setDefaultServletName() throws Exception { }
final String originalDefault = "default";
final String newDefault = "test"; @Test
assertThat(sc.getNamedDispatcher(originalDefault)).isNotNull(); void setDefaultServletName() throws Exception {
final String originalDefault = "default";
sc.setDefaultServletName(newDefault); final String newDefault = "test";
assertThat(sc.getDefaultServletName()).isEqualTo(newDefault); assertThat(servletContext.getNamedDispatcher(originalDefault)).isNotNull();
assertThat(sc.getNamedDispatcher(originalDefault)).isNull();
servletContext.setDefaultServletName(newDefault);
RequestDispatcher namedDispatcher = sc.getNamedDispatcher(newDefault); assertThat(servletContext.getDefaultServletName()).isEqualTo(newDefault);
assertThat(namedDispatcher).isNotNull(); assertThat(servletContext.getNamedDispatcher(originalDefault)).isNull();
MockHttpServletResponse response = new MockHttpServletResponse();
namedDispatcher.forward(new MockHttpServletRequest(sc), response); RequestDispatcher namedDispatcher = servletContext.getNamedDispatcher(newDefault);
assertThat(response.getForwardedUrl()).isEqualTo(newDefault); assertThat(namedDispatcher).isNotNull();
} MockHttpServletResponse response = new MockHttpServletResponse();
namedDispatcher.forward(new MockHttpServletRequest(servletContext), response);
/** assertThat(response.getForwardedUrl()).isEqualTo(newDefault);
* @since 4.1.2 }
*/
@Test /**
void getServletRegistration() { * @since 4.1.2
assertThat(sc.getServletRegistration("servlet")).isNull(); */
} @Test
void getServletRegistration() {
assertThat(servletContext.getServletRegistration("servlet")).isNull();
}
/**
* @since 4.1.2
*/
@Test
void getServletRegistrations() {
Map<String, ? extends ServletRegistration> servletRegistrations = servletContext.getServletRegistrations();
assertThat(servletRegistrations).isNotNull();
assertThat(servletRegistrations.size()).isEqualTo(0);
}
/**
* @since 4.1.2
*/
@Test
void getFilterRegistration() {
assertThat(servletContext.getFilterRegistration("filter")).isNull();
}
/**
* @since 4.1.2
*/
@Test
void getFilterRegistrations() {
Map<String, ? extends FilterRegistration> filterRegistrations = servletContext.getFilterRegistrations();
assertThat(filterRegistrations).isNotNull();
assertThat(filterRegistrations.size()).isEqualTo(0);
}
/**
* @since 4.1.2
*/
@Test
void getServletRegistrations() {
Map<String, ? extends ServletRegistration> servletRegistrations = sc.getServletRegistrations();
assertThat(servletRegistrations).isNotNull();
assertThat(servletRegistrations.size()).isEqualTo(0);
} }
/** /**
* @since 4.1.2 * @since 5.1.11
*/ */
@Test @Nested
void getFilterRegistration() { @DisplayName("with FileSystemResourceLoader")
assertThat(sc.getFilterRegistration("filter")).isNull(); class MockServletContextWithFileSystemResourceLoaderTests {
}
private final MockServletContext servletContext =
new MockServletContext( "org/springframework/mock", new FileSystemResourceLoader());
@Test
void getResourcePathsWithRelativePathToWindowsCDrive() {
Set<String> paths = servletContext.getResourcePaths("C:\\temp");
assertThat(paths).isNull();
}
@Test
void getResourceWithRelativePathToWindowsCDrive() throws Exception {
URL resource = servletContext.getResource("C:\\temp");
assertThat(resource).isNull();
}
@Test
void getResourceAsStreamWithRelativePathToWindowsCDrive() {
InputStream inputStream = servletContext.getResourceAsStream("C:\\temp");
assertThat(inputStream).isNull();
}
@Test
void getRealPathWithRelativePathToWindowsCDrive() {
String realPath = servletContext.getRealPath("C:\\temp");
if (OS.WINDOWS.isCurrentOs()) {
assertThat(realPath).isNull();
}
else {
assertThat(realPath).isNotNull();
}
}
/**
* @since 4.1.2
*/
@Test
void getFilterRegistrations() {
Map<String, ? extends FilterRegistration> filterRegistrations = sc.getFilterRegistrations();
assertThat(filterRegistrations).isNotNull();
assertThat(filterRegistrations.size()).isEqualTo(0);
} }
} }
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -21,6 +21,7 @@ import java.io.IOException; ...@@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.file.InvalidPathException;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.EventListener; import java.util.EventListener;
...@@ -294,8 +295,10 @@ public class MockServletContext implements ServletContext { ...@@ -294,8 +295,10 @@ public class MockServletContext implements ServletContext {
@Nullable @Nullable
public Set<String> getResourcePaths(String path) { public Set<String> getResourcePaths(String path) {
String actualPath = (path.endsWith("/") ? path : path + "/"); String actualPath = (path.endsWith("/") ? path : path + "/");
Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath)); String resourceLocation = getResourceLocation(actualPath);
Resource resource = null;
try { try {
resource = this.resourceLoader.getResource(resourceLocation);
File file = resource.getFile(); File file = resource.getFile();
String[] fileList = file.list(); String[] fileList = file.list();
if (ObjectUtils.isEmpty(fileList)) { if (ObjectUtils.isEmpty(fileList)) {
...@@ -311,9 +314,10 @@ public class MockServletContext implements ServletContext { ...@@ -311,9 +314,10 @@ public class MockServletContext implements ServletContext {
} }
return resourcePaths; return resourcePaths;
} }
catch (IOException ex) { catch (InvalidPathException | IOException ex ) {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
logger.warn("Could not get resource paths for " + resource, ex); logger.warn("Could not get resource paths for " +
(resource != null ? resource : resourceLocation), ex);
} }
return null; return null;
} }
...@@ -322,19 +326,22 @@ public class MockServletContext implements ServletContext { ...@@ -322,19 +326,22 @@ public class MockServletContext implements ServletContext {
@Override @Override
@Nullable @Nullable
public URL getResource(String path) throws MalformedURLException { public URL getResource(String path) throws MalformedURLException {
Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); String resourceLocation = getResourceLocation(path);
if (!resource.exists()) { Resource resource = null;
return null;
}
try { try {
resource = this.resourceLoader.getResource(resourceLocation);
if (!resource.exists()) {
return null;
}
return resource.getURL(); return resource.getURL();
} }
catch (MalformedURLException ex) { catch (MalformedURLException ex) {
throw ex; throw ex;
} }
catch (IOException ex) { catch (InvalidPathException | IOException ex) {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
logger.warn("Could not get URL for " + resource, ex); logger.warn("Could not get URL for resource " +
(resource != null ? resource : resourceLocation), ex);
} }
return null; return null;
} }
...@@ -343,16 +350,19 @@ public class MockServletContext implements ServletContext { ...@@ -343,16 +350,19 @@ public class MockServletContext implements ServletContext {
@Override @Override
@Nullable @Nullable
public InputStream getResourceAsStream(String path) { public InputStream getResourceAsStream(String path) {
Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); String resourceLocation = getResourceLocation(path);
if (!resource.exists()) { Resource resource = null;
return null;
}
try { try {
resource = this.resourceLoader.getResource(resourceLocation);
if (!resource.exists()) {
return null;
}
return resource.getInputStream(); return resource.getInputStream();
} }
catch (IOException ex) { catch (InvalidPathException | IOException ex) {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
logger.warn("Could not open InputStream for " + resource, ex); logger.warn("Could not open InputStream for resource " +
(resource != null ? resource : resourceLocation), ex);
} }
return null; return null;
} }
...@@ -459,13 +469,16 @@ public class MockServletContext implements ServletContext { ...@@ -459,13 +469,16 @@ public class MockServletContext implements ServletContext {
@Override @Override
@Nullable @Nullable
public String getRealPath(String path) { public String getRealPath(String path) {
Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); String resourceLocation = getResourceLocation(path);
Resource resource = null;
try { try {
resource = this.resourceLoader.getResource(resourceLocation);
return resource.getFile().getAbsolutePath(); return resource.getFile().getAbsolutePath();
} }
catch (IOException ex) { catch (InvalidPathException | IOException ex) {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
logger.warn("Could not determine real path of resource " + resource, ex); logger.warn("Could not determine real path of resource " +
(resource != null ? resource : resourceLocation), ex);
} }
return null; return null;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册