Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
bclozel committed Sep 26, 2022
1 parent b71e686 commit 9465110
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 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.
Expand Down

This file was deleted.

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.

0 comments on commit 9465110

Please sign in to comment.