|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.web.context.support; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import org.springframework.core.io.Resource; |
| 24 | +import org.springframework.web.testfixture.servlet.MockServletContext; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.mockito.ArgumentMatchers.eq; |
| 28 | +import static org.mockito.BDDMockito.given; |
| 29 | +import static org.mockito.Mockito.mock; |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests for {@link ServletContextResource}. |
| 33 | + * |
| 34 | + * @author Chris Beams |
| 35 | + * @author Brian Clozel |
| 36 | + */ |
| 37 | +public class ServletContextResourceTests { |
| 38 | + |
| 39 | + private static final String TEST_RESOURCE_PATH = "org/springframework/web/context/support/resource.txt"; |
| 40 | + |
| 41 | + private final MockServletContext servletContext = new MockServletContext(); |
| 42 | + |
| 43 | + @Test |
| 44 | + void resourceShouldHaveExpectedProperties() throws IOException { |
| 45 | + Resource resource = new ServletContextResource(this.servletContext, TEST_RESOURCE_PATH); |
| 46 | + |
| 47 | + assertThat(resource.getFile()).isNotNull(); |
| 48 | + assertThat(resource.exists()).isTrue(); |
| 49 | + assertThat(resource.isFile()).isTrue(); |
| 50 | + assertThat(resource.getFilename()).isEqualTo("resource.txt"); |
| 51 | + assertThat(resource.getURL().getFile().endsWith("resource.txt")).isTrue(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void relativeResourcesShouldHaveExpectedProperties() throws IOException { |
| 56 | + Resource resource = new ServletContextResource(this.servletContext, TEST_RESOURCE_PATH); |
| 57 | + Resource relative1 = resource.createRelative("relative.txt"); |
| 58 | + assertThat(relative1.getFilename()).isEqualTo("relative.txt"); |
| 59 | + assertThat(relative1.getURL().getFile().endsWith("relative.txt")).isTrue(); |
| 60 | + assertThat(relative1.exists()).isTrue(); |
| 61 | + |
| 62 | + Resource relative2 = resource.createRelative("folder/other.txt"); |
| 63 | + assertThat(relative2.getFilename()).isEqualTo("other.txt"); |
| 64 | + assertThat(relative2.getURL().getFile().endsWith("other.txt")).isTrue(); |
| 65 | + assertThat(relative2.exists()).isTrue(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void resourceWithDotPathShouldBeEqual() { |
| 70 | + Resource resource = new ServletContextResource(this.servletContext, TEST_RESOURCE_PATH); |
| 71 | + assertThat(new ServletContextResource(servletContext, "org/springframework/web/context/../context/support/./resource.txt")).isEqualTo(resource); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void resourceWithRelativePathShouldBeEqual() throws IOException { |
| 76 | + Resource resource = new ServletContextResource(this.servletContext, "dir/"); |
| 77 | + Resource relative = resource.createRelative("subdir"); |
| 78 | + assertThat(relative).isEqualTo(new ServletContextResource(this.servletContext, "dir/subdir")); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void missingResourceShouldHaveExpectedProperties() { |
| 83 | + MockServletContext context = mock(MockServletContext.class); |
| 84 | + given(context.getRealPath(eq("/org/springframework/web/context/support/missing.txt"))) |
| 85 | + .willReturn(this.servletContext.getRealPath("org/springframework/web/context/support/") + "missing.txt"); |
| 86 | + Resource missing = new ServletContextResource(context, "org/springframework/web/context/support/missing.txt"); |
| 87 | + |
| 88 | + assertThat(missing.exists()).isFalse(); |
| 89 | + assertThat(missing.isFile()).isFalse(); |
| 90 | + } |
| 91 | +} |
0 commit comments