Merge branch '5.3.x'
This commit is contained in:
commit
1707a22c18
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -139,10 +139,15 @@ public class ServletContextResource extends AbstractFileResolvingResource implem
|
|||
return true;
|
||||
}
|
||||
else {
|
||||
return (this.servletContext.getRealPath(this.path) != null);
|
||||
String realPath = this.servletContext.getRealPath(this.path);
|
||||
if (realPath == null) {
|
||||
return false;
|
||||
}
|
||||
File file = new File(realPath);
|
||||
return (file.exists() && file.isFile());
|
||||
}
|
||||
}
|
||||
catch (MalformedURLException ex) {
|
||||
catch (IOException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,64 +0,0 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.context.support;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.web.testfixture.servlet.MockServletContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Chris Beams
|
||||
* @see org.springframework.core.io.ResourceTests
|
||||
*/
|
||||
public class ResourceTests {
|
||||
|
||||
@Test
|
||||
public void testServletContextResource() throws IOException {
|
||||
MockServletContext sc = new MockServletContext();
|
||||
Resource resource = new ServletContextResource(sc, "org/springframework/core/io/Resource.class");
|
||||
doTestResource(resource);
|
||||
assertThat(new ServletContextResource(sc, "org/springframework/core/../core/io/./Resource.class")).isEqualTo(resource);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServletContextResourceWithRelativePath() throws IOException {
|
||||
MockServletContext sc = new MockServletContext();
|
||||
Resource resource = new ServletContextResource(sc, "dir/");
|
||||
Resource relative = resource.createRelative("subdir");
|
||||
assertThat(relative).isEqualTo(new ServletContextResource(sc, "dir/subdir"));
|
||||
}
|
||||
|
||||
private void doTestResource(Resource resource) throws IOException {
|
||||
assertThat(resource.getFilename()).isEqualTo("Resource.class");
|
||||
assertThat(resource.getURL().getFile().endsWith("Resource.class")).isTrue();
|
||||
|
||||
Resource relative1 = resource.createRelative("ClassPathResource.class");
|
||||
assertThat(relative1.getFilename()).isEqualTo("ClassPathResource.class");
|
||||
assertThat(relative1.getURL().getFile().endsWith("ClassPathResource.class")).isTrue();
|
||||
assertThat(relative1.exists()).isTrue();
|
||||
|
||||
Resource relative2 = resource.createRelative("support/ResourcePatternResolver.class");
|
||||
assertThat(relative2.getFilename()).isEqualTo("ResourcePatternResolver.class");
|
||||
assertThat(relative2.getURL().getFile().endsWith("ResourcePatternResolver.class")).isTrue();
|
||||
assertThat(relative2.exists()).isTrue();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.context.support;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.web.testfixture.servlet.MockServletContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link ServletContextResource}.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
public class ServletContextResourceTests {
|
||||
|
||||
private static final String TEST_RESOURCE_PATH = "org/springframework/web/context/support/resource.txt";
|
||||
|
||||
private final MockServletContext servletContext = new MockServletContext();
|
||||
|
||||
@Test
|
||||
void resourceShouldHaveExpectedProperties() throws IOException {
|
||||
Resource resource = new ServletContextResource(this.servletContext, TEST_RESOURCE_PATH);
|
||||
|
||||
assertThat(resource.getFile()).isNotNull();
|
||||
assertThat(resource.exists()).isTrue();
|
||||
assertThat(resource.isFile()).isTrue();
|
||||
assertThat(resource.getFilename()).isEqualTo("resource.txt");
|
||||
assertThat(resource.getURL().getFile().endsWith("resource.txt")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void relativeResourcesShouldHaveExpectedProperties() throws IOException {
|
||||
Resource resource = new ServletContextResource(this.servletContext, TEST_RESOURCE_PATH);
|
||||
Resource relative1 = resource.createRelative("relative.txt");
|
||||
assertThat(relative1.getFilename()).isEqualTo("relative.txt");
|
||||
assertThat(relative1.getURL().getFile().endsWith("relative.txt")).isTrue();
|
||||
assertThat(relative1.exists()).isTrue();
|
||||
|
||||
Resource relative2 = resource.createRelative("folder/other.txt");
|
||||
assertThat(relative2.getFilename()).isEqualTo("other.txt");
|
||||
assertThat(relative2.getURL().getFile().endsWith("other.txt")).isTrue();
|
||||
assertThat(relative2.exists()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceWithDotPathShouldBeEqual() {
|
||||
Resource resource = new ServletContextResource(this.servletContext, TEST_RESOURCE_PATH);
|
||||
assertThat(new ServletContextResource(servletContext, "org/springframework/web/context/../context/support/./resource.txt")).isEqualTo(resource);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceWithRelativePathShouldBeEqual() throws IOException {
|
||||
Resource resource = new ServletContextResource(this.servletContext, "dir/");
|
||||
Resource relative = resource.createRelative("subdir");
|
||||
assertThat(relative).isEqualTo(new ServletContextResource(this.servletContext, "dir/subdir"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingResourceShouldHaveExpectedProperties() {
|
||||
MockServletContext context = mock(MockServletContext.class);
|
||||
given(context.getRealPath(eq("/org/springframework/web/context/support/missing.txt")))
|
||||
.willReturn(this.servletContext.getRealPath("org/springframework/web/context/support/") + "missing.txt");
|
||||
Resource missing = new ServletContextResource(context, "org/springframework/web/context/support/missing.txt");
|
||||
|
||||
assertThat(missing.exists()).isFalse();
|
||||
assertThat(missing.isFile()).isFalse();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue