Skip to content

Commit

Permalink
Decouple urlResourceWithCharset test from existence of tmp directory
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Oct 12, 2021
1 parent a2c52a9 commit 1490d27
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.CacheControl;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.context.support.GenericWebApplicationContext;
Expand Down Expand Up @@ -64,7 +63,7 @@ public class ResourceHandlerRegistryTests {


@BeforeEach
public void setUp() {
public void setup() {
GenericWebApplicationContext appContext = new GenericWebApplicationContext();
appContext.refresh();

Expand All @@ -76,8 +75,14 @@ public void setUp() {
this.response = new MockHttpServletResponse();
}

private ResourceHttpRequestHandler getHandler(String pathPattern) {
SimpleUrlHandlerMapping hm = (SimpleUrlHandlerMapping) this.registry.getHandlerMapping();
return (ResourceHttpRequestHandler) hm.getUrlMap().get(pathPattern);
}


@Test
public void noResourceHandlers() throws Exception {
public void noResourceHandlers() {
this.registry = new ResourceHandlerRegistry(new GenericWebApplicationContext(), new MockServletContext());
assertThat((Object) this.registry.getHandlerMapping()).isNull();
}
Expand Down Expand Up @@ -126,7 +131,7 @@ public void hasMappingForPattern() {
}

@Test
public void resourceChain() throws Exception {
public void resourceChain() {
ResourceResolver mockResolver = Mockito.mock(ResourceResolver.class);
ResourceTransformer mockTransformer = Mockito.mock(ResourceTransformer.class);
this.registration.resourceChain(true).addResolver(mockResolver).addTransformer(mockTransformer);
Expand All @@ -148,7 +153,7 @@ public void resourceChain() throws Exception {
}

@Test
public void resourceChainWithoutCaching() throws Exception {
public void resourceChainWithoutCaching() {
this.registration.resourceChain(false);

ResourceHttpRequestHandler handler = getHandler("/resources/**");
Expand All @@ -163,7 +168,7 @@ public void resourceChainWithoutCaching() throws Exception {

@Test
@SuppressWarnings("deprecation")
public void resourceChainWithVersionResolver() throws Exception {
public void resourceChainWithVersionResolver() {
VersionResourceResolver versionResolver = new VersionResourceResolver()
.addFixedVersionStrategy("fixed", "/**/*.js")
.addContentVersionStrategy("/**");
Expand All @@ -188,7 +193,7 @@ public void resourceChainWithVersionResolver() throws Exception {

@Test
@SuppressWarnings("deprecation")
public void resourceChainWithOverrides() throws Exception {
public void resourceChainWithOverrides() {
CachingResourceResolver cachingResolver = Mockito.mock(CachingResourceResolver.class);
VersionResourceResolver versionResolver = Mockito.mock(VersionResourceResolver.class);
WebJarsResourceResolver webjarsResolver = Mockito.mock(WebJarsResourceResolver.class);
Expand Down Expand Up @@ -224,13 +229,11 @@ public void resourceChainWithOverrides() throws Exception {
}

@Test
public void urlResourceWithCharset() throws Exception {
public void urlResourceWithCharset() {
this.registration.addResourceLocations("[charset=ISO-8859-1]file:///tmp");
this.registration.resourceChain(true);

ResourceHttpRequestHandler handler = getHandler("/resources/**");
UrlResource resource = (UrlResource) handler.getLocations().get(1);
assertThat(resource.getURL().toString()).isEqualTo("file:/tmp");
assertThat(handler.getUrlPathHelper()).isNotNull();

List<ResourceResolver> resolvers = handler.getResourceResolvers();
Expand All @@ -241,15 +244,10 @@ public void urlResourceWithCharset() throws Exception {
}

@Test
void lastModifiedDisabled() {
public void lastModifiedDisabled() {
this.registration.setUseLastModified(false);
ResourceHttpRequestHandler handler = getHandler("/resources/**");
assertThat(handler.isUseLastModified()).isFalse();
}

private ResourceHttpRequestHandler getHandler(String pathPattern) {
SimpleUrlHandlerMapping hm = (SimpleUrlHandlerMapping) this.registry.getHandlerMapping();
return (ResourceHttpRequestHandler) hm.getUrlMap().get(pathPattern);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ public void getMediaTypeWithFavorPathExtensionOff() throws Exception {

@Test // SPR-14368
public void getResourceWithMediaTypeResolvedThroughServletContext() throws Exception {

MockServletContext servletContext = new MockServletContext() {
@Override
public String getMimeType(String filePath) {
Expand All @@ -312,7 +311,7 @@ public String getMimeType(String filePath) {
assertThat(this.response.getContentAsString()).isEqualTo("h1 { color:red; }");
}

@Test // gh-27538
@Test // gh-27538
public void filterNonExistingLocations() throws Exception {
List<Resource> inputLocations = Arrays.asList(
new ClassPathResource("test/", getClass()),
Expand All @@ -332,7 +331,6 @@ public void filterNonExistingLocations() throws Exception {

@Test
public void testInvalidPath() throws Exception {

// Use mock ResourceResolver: i.e. we're only testing upfront validations...

Resource resource = mock(Resource.class);
Expand Down Expand Up @@ -678,7 +676,7 @@ public void partialContentMultipleByteRanges() throws Exception {
assertThat(ranges[11]).isEqualTo("t.");
}

@Test // gh-25976
@Test // gh-25976
public void partialContentByteRangeWithEncodedResource(GzipSupport.GzippedFiles gzippedFiles) throws Exception {
String path = "js/foo.js";
gzippedFiles.create(path);
Expand Down Expand Up @@ -707,7 +705,7 @@ public void partialContentByteRangeWithEncodedResource(GzipSupport.GzippedFiles
assertThat(this.response.getHeaderValues("Vary")).containsExactly("Accept-Encoding");
}

@Test // gh-25976
@Test // gh-25976
public void partialContentWithHttpHead() throws Exception {
this.request.setMethod("HEAD");
this.request.addHeader("Range", "bytes=0-1");
Expand Down

0 comments on commit 1490d27

Please sign in to comment.