diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/resource/CachingResourceResolverTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/CachingResourceResolverTests.java index 80c0b953b59..84d5c5a12cf 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/resource/CachingResourceResolverTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/CachingResourceResolverTests.java @@ -23,6 +23,7 @@ import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito; import org.springframework.cache.Cache; @@ -30,6 +31,7 @@ import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.mock.web.test.server.MockServerWebExchange; +import org.springframework.web.reactive.resource.GzipSupport.GzippedFiles; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.get; @@ -39,6 +41,7 @@ import static org.springframework.mock.http.server.reactive.test.MockServerHttpR * * @author Rossen Stoyanchev */ +@ExtendWith(GzipSupport.class) public class CachingResourceResolverTests { private static final Duration TIMEOUT = Duration.ofSeconds(5); @@ -116,10 +119,10 @@ public class CachingResourceResolverTests { } @Test - public void resolveResourceAcceptEncodingInCacheKey() throws IOException { + public void resolveResourceAcceptEncodingInCacheKey(GzippedFiles gzippedFiles) throws IOException { String file = "bar.css"; - EncodedResourceResolverTests.createGzippedFile(file); + gzippedFiles.create(file); // 1. Resolve plain resource diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/resource/CssLinkResourceTransformerTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/CssLinkResourceTransformerTests.java index ca008df9831..d3584775452 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/resource/CssLinkResourceTransformerTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/CssLinkResourceTransformerTests.java @@ -23,6 +23,7 @@ import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito; import reactor.test.StepVerifier; @@ -31,6 +32,7 @@ import org.springframework.core.io.Resource; import org.springframework.mock.web.test.server.MockServerWebExchange; import org.springframework.util.StringUtils; import org.springframework.web.reactive.resource.EncodedResourceResolver.EncodedResource; +import org.springframework.web.reactive.resource.GzipSupport.GzippedFiles; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.get; @@ -41,6 +43,7 @@ import static org.springframework.mock.http.server.reactive.test.MockServerHttpR * @author Rossen Stoyanchev * @author Sam Brannen */ +@ExtendWith(GzipSupport.class) public class CssLinkResourceTransformerTests { private ResourceTransformerChain transformerChain; @@ -147,9 +150,8 @@ public class CssLinkResourceTransformerTests { } @Test - public void transformSkippedForGzippedResource() throws Exception { - - EncodedResourceResolverTests.createGzippedFile("main.css"); + public void transformSkippedForGzippedResource(GzippedFiles gzippedFiles) throws Exception { + gzippedFiles.create("main.css"); MockServerWebExchange exchange = MockServerWebExchange.from(get("/static/main.css")); Resource resource = getResource("main.css"); diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/resource/EncodedResourceResolverTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/EncodedResourceResolverTests.java index 648b6f06145..24d680e6e9b 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/resource/EncodedResourceResolverTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/EncodedResourceResolverTests.java @@ -16,31 +16,23 @@ package org.springframework.web.reactive.resource; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import java.time.Duration; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.zip.GZIPOutputStream; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.cache.Cache; import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.web.test.server.MockServerWebExchange; -import org.springframework.util.FileCopyUtils; +import org.springframework.web.reactive.resource.GzipSupport.GzippedFiles; import static org.assertj.core.api.Assertions.assertThat; @@ -49,6 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Rossen Stoyanchev */ +@ExtendWith(GzipSupport.class) public class EncodedResourceResolverTests { private static final Duration TIMEOUT = Duration.ofSeconds(5); @@ -59,26 +52,6 @@ public class EncodedResourceResolverTests { private List locations; - @BeforeAll - public static void createGzippedResources() throws IOException { - createGzippedFile("/js/foo.js"); - createGzippedFile("foo.css"); - } - - static void createGzippedFile(String filePath) throws IOException { - Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class); - Resource resource = new FileSystemResource(location.createRelative(filePath).getFile()); - - Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz"); - Files.deleteIfExists(gzFilePath); - - File gzFile = Files.createFile(gzFilePath).toFile(); - GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile)); - FileCopyUtils.copy(resource.getInputStream(), out); - gzFile.deleteOnExit(); - } - - @BeforeEach public void setup() { Cache cache = new ConcurrentMapCache("resourceCache"); @@ -100,12 +73,13 @@ public class EncodedResourceResolverTests { @Test - public void resolveGzipped() { + public void resolveGzipped(GzippedFiles gzippedFiles) { MockServerWebExchange exchange = MockServerWebExchange.from( MockServerHttpRequest.get("").header("Accept-Encoding", "gzip")); String file = "js/foo.js"; + gzippedFiles.create(file); Resource actual = this.resolver.resolveResource(exchange, file, this.locations).block(TIMEOUT); assertThat(actual.getDescription()).isEqualTo(getResource(file + ".gz").getDescription()); @@ -119,11 +93,11 @@ public class EncodedResourceResolverTests { } @Test - public void resolveGzippedWithVersion() { + public void resolveGzippedWithVersion(GzippedFiles gzippedFiles) { MockServerWebExchange exchange = MockServerWebExchange.from( MockServerHttpRequest.get("").header("Accept-Encoding", "gzip")); - + gzippedFiles.create("foo.css"); String file = "foo-e36d2e05253c6c7085a91522ce43a0b4.css"; Resource actual = this.resolver.resolveResource(exchange, file, this.locations).block(TIMEOUT); @@ -134,7 +108,7 @@ public class EncodedResourceResolverTests { } @Test - public void resolveFromCacheWithEncodingVariants() { + public void resolveFromCacheWithEncodingVariants(GzippedFiles gzippedFiles) { // 1. Resolve, and cache .gz variant @@ -142,6 +116,7 @@ public class EncodedResourceResolverTests { MockServerHttpRequest.get("").header("Accept-Encoding", "gzip")); String file = "js/foo.js"; + gzippedFiles.create(file); Resource resolved = this.resolver.resolveResource(exchange, file, this.locations).block(TIMEOUT); assertThat(resolved.getDescription()).isEqualTo(getResource(file + ".gz").getDescription()); diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/resource/GzipSupport.java b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/GzipSupport.java new file mode 100644 index 00000000000..238c0a17e79 --- /dev/null +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/GzipSupport.java @@ -0,0 +1,94 @@ +/* + * 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.reactive.resource; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashSet; +import java.util.Set; +import java.util.zip.GZIPOutputStream; + +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ExtensionContext.Namespace; +import org.junit.jupiter.api.extension.ExtensionContext.Store; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; +import org.springframework.util.FileCopyUtils; + +class GzipSupport implements AfterEachCallback, ParameterResolver { + + @Override + public void afterEach(ExtensionContext context) throws Exception { + GzippedFiles gzippedFiles = getStore(context).get(GzippedFiles.class, GzippedFiles.class); + if (gzippedFiles != null) { + for (File gzippedFile: gzippedFiles.created) { + gzippedFile.delete(); + } + } + } + + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + return parameterContext.getParameter().getType().equals(GzippedFiles.class); + } + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + return getStore(extensionContext).getOrComputeIfAbsent(GzippedFiles.class); + } + + private Store getStore(ExtensionContext extensionContext) { + return extensionContext.getStore(Namespace.create(getClass())); + } + + static class GzippedFiles { + + private final Set created = new HashSet<>(); + + void create(String filePath) { + try { + Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class); + Resource resource = new FileSystemResource(location.createRelative(filePath).getFile()); + + Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz"); + Files.deleteIfExists(gzFilePath); + + File gzFile = Files.createFile(gzFilePath).toFile(); + GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile)); + FileCopyUtils.copy(resource.getInputStream(), out); + created.add(gzFile); + } + catch (IOException ex) { + throw new RuntimeException(ex); + } + } + + } + +} diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/CachingResourceResolverTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/CachingResourceResolverTests.java index ba0d71087f6..702256459ad 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/CachingResourceResolverTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/CachingResourceResolverTests.java @@ -22,6 +22,7 @@ import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito; import org.springframework.cache.Cache; @@ -29,6 +30,7 @@ import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.mock.web.test.MockHttpServletRequest; +import org.springframework.web.servlet.resource.GzipSupport.GzippedFiles; import static org.assertj.core.api.Assertions.assertThat; @@ -38,6 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Rossen Stoyanchev */ +@ExtendWith(GzipSupport.class) public class CachingResourceResolverTests { private Cache cache; @@ -108,10 +111,10 @@ public class CachingResourceResolverTests { } @Test - public void resolveResourceAcceptEncodingInCacheKey() throws IOException { + public void resolveResourceAcceptEncodingInCacheKey(GzippedFiles gzippedFiles) throws IOException { String file = "bar.css"; - EncodedResourceResolverTests.createGzippedFile(file); + gzippedFiles.create(file); // 1. Resolve plain resource diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/CssLinkResourceTransformerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/CssLinkResourceTransformerTests.java index d49ddd229ee..b318f50288b 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/CssLinkResourceTransformerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/CssLinkResourceTransformerTests.java @@ -23,6 +23,7 @@ import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito; import org.springframework.core.io.ClassPathResource; @@ -30,6 +31,7 @@ import org.springframework.core.io.Resource; import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.util.StringUtils; import org.springframework.web.servlet.resource.EncodedResourceResolver.EncodedResource; +import org.springframework.web.servlet.resource.GzipSupport.GzippedFiles; import static org.assertj.core.api.Assertions.assertThat; @@ -41,6 +43,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Sam Brannen * @since 4.1 */ +@ExtendWith(GzipSupport.class) public class CssLinkResourceTransformerTests { private ResourceTransformerChain transformerChain; @@ -137,9 +140,8 @@ public class CssLinkResourceTransformerTests { } @Test - public void transformSkippedForGzippedResource() throws Exception { - - EncodedResourceResolverTests.createGzippedFile("main.css"); + public void transformSkippedForGzippedResource(GzippedFiles gzippedFiles) throws Exception { + gzippedFiles.create("main.css"); this.request = new MockHttpServletRequest("GET", "/static/main.css"); Resource original = new ClassPathResource("test/main.css", getClass()); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/EncodedResourceResolverTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/EncodedResourceResolverTests.java index 8e258e13aa6..fdc914ae858 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/EncodedResourceResolverTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/EncodedResourceResolverTests.java @@ -16,29 +16,21 @@ package org.springframework.web.servlet.resource; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.zip.GZIPOutputStream; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.cache.Cache; import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; import org.springframework.mock.web.test.MockHttpServletRequest; -import org.springframework.util.FileCopyUtils; +import org.springframework.web.servlet.resource.GzipSupport.GzippedFiles; import static org.assertj.core.api.Assertions.assertThat; @@ -48,6 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Jeremy Grelle * @author Rossen Stoyanchev */ +@ExtendWith(GzipSupport.class) public class EncodedResourceResolverTests { private ResourceResolverChain resolver; @@ -56,27 +49,6 @@ public class EncodedResourceResolverTests { private Cache cache; - - @BeforeAll - public static void createGzippedResources() throws IOException { - createGzippedFile("/js/foo.js"); - createGzippedFile("foo.css"); - } - - static void createGzippedFile(String filePath) throws IOException { - Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class); - Resource resource = new FileSystemResource(location.createRelative(filePath).getFile()); - - Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz"); - Files.deleteIfExists(gzFilePath); - - File gzFile = Files.createFile(gzFilePath).toFile(); - GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile)); - FileCopyUtils.copy(resource.getInputStream(), out); - gzFile.deleteOnExit(); - } - - @BeforeEach public void setup() { this.cache = new ConcurrentMapCache("resourceCache"); @@ -98,8 +70,9 @@ public class EncodedResourceResolverTests { @Test - public void resolveGzipped() { + public void resolveGzipped(GzippedFiles gzippedFiles) { String file = "js/foo.js"; + gzippedFiles.create(file); MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("Accept-Encoding", "gzip"); Resource actual = this.resolver.resolveResource(request, file, this.locations); @@ -115,7 +88,8 @@ public class EncodedResourceResolverTests { } @Test - public void resolveGzippedWithVersion() { + public void resolveGzippedWithVersion(GzippedFiles gzippedFiles) { + gzippedFiles.create("foo.css"); String file = "foo-e36d2e05253c6c7085a91522ce43a0b4.css"; MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("Accept-Encoding", "gzip"); @@ -128,9 +102,10 @@ public class EncodedResourceResolverTests { } @Test - public void resolveFromCacheWithEncodingVariants() { + public void resolveFromCacheWithEncodingVariants(GzippedFiles gzippedFiles) { // 1. Resolve, and cache .gz variant String file = "js/foo.js"; + gzippedFiles.create(file); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/js/foo.js"); request.addHeader("Accept-Encoding", "gzip"); Resource resolved = this.resolver.resolveResource(request, file, this.locations); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/GzipSupport.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/GzipSupport.java new file mode 100644 index 00000000000..598cd38cb4d --- /dev/null +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/GzipSupport.java @@ -0,0 +1,94 @@ +/* + * 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.servlet.resource; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashSet; +import java.util.Set; +import java.util.zip.GZIPOutputStream; + +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ExtensionContext.Namespace; +import org.junit.jupiter.api.extension.ExtensionContext.Store; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; +import org.springframework.util.FileCopyUtils; + +class GzipSupport implements AfterEachCallback, ParameterResolver { + + @Override + public void afterEach(ExtensionContext context) throws Exception { + GzippedFiles gzippedFiles = getStore(context).get(GzippedFiles.class, GzippedFiles.class); + if (gzippedFiles != null) { + for (File gzippedFile: gzippedFiles.created) { + gzippedFile.delete(); + } + } + } + + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + return parameterContext.getParameter().getType().equals(GzippedFiles.class); + } + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) + throws ParameterResolutionException { + return getStore(extensionContext).getOrComputeIfAbsent(GzippedFiles.class); + } + + private Store getStore(ExtensionContext extensionContext) { + return extensionContext.getStore(Namespace.create(getClass())); + } + + static class GzippedFiles { + + private final Set created = new HashSet<>(); + + void create(String filePath) { + try { + Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class); + Resource resource = new FileSystemResource(location.createRelative(filePath).getFile()); + + Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz"); + Files.deleteIfExists(gzFilePath); + + File gzFile = Files.createFile(gzFilePath).toFile(); + GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile)); + FileCopyUtils.copy(resource.getInputStream(), out); + created.add(gzFile); + } + catch (IOException ex) { + throw new RuntimeException(ex); + } + } + + } + +} diff --git a/src/checkstyle/checkstyle-suppressions.xml b/src/checkstyle/checkstyle-suppressions.xml index cfc6066dc31..bd0ffdcdb24 100644 --- a/src/checkstyle/checkstyle-suppressions.xml +++ b/src/checkstyle/checkstyle-suppressions.xml @@ -73,7 +73,7 @@ - + @@ -89,11 +89,15 @@ + + + +