-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See gh-26707
- Loading branch information
Showing
6 changed files
with
92 additions
and
75 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
spring-web/src/main/java/org/springframework/web/context/support/ServletContextResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 0 additions & 74 deletions
74
spring-web/src/test/java/org/springframework/web/context/support/ResourceTests.java
This file was deleted.
Oops, something went wrong.
91 changes: 91 additions & 0 deletions
91
...eb/src/test/java/org/springframework/web/context/support/ServletContextResourceTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
Empty file.
Empty file.
Empty file.