Browse Source

Implement AutoCloseable in GzippedFiles

pull/35221/head
Sam Brannen 5 months ago
parent
commit
fbc5ff80f4
  1. 29
      spring-webflux/src/test/java/org/springframework/web/reactive/resource/GzipSupport.java
  2. 29
      spring-webmvc/src/test/java/org/springframework/web/servlet/resource/GzipSupport.java
  3. 3
      spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

29
spring-webflux/src/test/java/org/springframework/web/reactive/resource/GzipSupport.java

@ -19,6 +19,7 @@ package org.springframework.web.reactive.resource; @@ -19,6 +19,7 @@ package org.springframework.web.reactive.resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -26,7 +27,6 @@ import java.util.HashSet; @@ -26,7 +27,6 @@ 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;
@ -40,21 +40,13 @@ import org.springframework.util.FileCopyUtils; @@ -40,21 +40,13 @@ import org.springframework.util.FileCopyUtils;
/**
* @author Andy Wilkinson
* @author Sam Brannen
* @since 5.2.2
*/
class GzipSupport implements AfterEachCallback, ParameterResolver {
class GzipSupport implements ParameterResolver {
private static final Namespace namespace = Namespace.create(GzipSupport.class);
@Override
public void afterEach(ExtensionContext context) {
GzippedFiles gzippedFiles = getStore(context).remove(GzippedFiles.class, GzippedFiles.class);
if (gzippedFiles != null) {
for (File gzippedFile: gzippedFiles.created) {
gzippedFile.delete();
}
}
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
@ -70,13 +62,14 @@ class GzipSupport implements AfterEachCallback, ParameterResolver { @@ -70,13 +62,14 @@ class GzipSupport implements AfterEachCallback, ParameterResolver {
return extensionContext.getStore(namespace);
}
static class GzippedFiles {
static class GzippedFiles implements AutoCloseable {
private final Set<File> created = new HashSet<>();
void create(String filePath) {
try {
Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class);
Resource location = new ClassPathResource("test/", getClass());
Resource resource = new FileSystemResource(location.createRelative(filePath).getFile());
Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz");
@ -85,13 +78,19 @@ class GzipSupport implements AfterEachCallback, ParameterResolver { @@ -85,13 +78,19 @@ class GzipSupport implements AfterEachCallback, ParameterResolver {
File gzFile = Files.createFile(gzFilePath).toFile();
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile));
FileCopyUtils.copy(resource.getInputStream(), out);
created.add(gzFile);
this.created.add(gzFile);
}
catch (IOException ex) {
throw new RuntimeException(ex);
throw new UncheckedIOException(ex);
}
}
@Override
public void close() {
for (File file: this.created) {
file.delete();
}
}
}
}

29
spring-webmvc/src/test/java/org/springframework/web/servlet/resource/GzipSupport.java

@ -19,6 +19,7 @@ package org.springframework.web.servlet.resource; @@ -19,6 +19,7 @@ package org.springframework.web.servlet.resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -26,7 +27,6 @@ import java.util.HashSet; @@ -26,7 +27,6 @@ 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;
@ -40,21 +40,13 @@ import org.springframework.util.FileCopyUtils; @@ -40,21 +40,13 @@ import org.springframework.util.FileCopyUtils;
/**
* @author Andy Wilkinson
* @author Sam Brannen
* @since 5.2.2
*/
class GzipSupport implements AfterEachCallback, ParameterResolver {
class GzipSupport implements ParameterResolver {
private static final Namespace namespace = Namespace.create(GzipSupport.class);
@Override
public void afterEach(ExtensionContext context) {
GzippedFiles gzippedFiles = getStore(context).remove(GzippedFiles.class, GzippedFiles.class);
if (gzippedFiles != null) {
for (File gzippedFile: gzippedFiles.created) {
gzippedFile.delete();
}
}
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
@ -70,13 +62,14 @@ class GzipSupport implements AfterEachCallback, ParameterResolver { @@ -70,13 +62,14 @@ class GzipSupport implements AfterEachCallback, ParameterResolver {
return extensionContext.getStore(namespace);
}
static class GzippedFiles {
static class GzippedFiles implements AutoCloseable {
private final Set<File> created = new HashSet<>();
void create(String filePath) {
try {
Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class);
Resource location = new ClassPathResource("test/", getClass());
Resource resource = new FileSystemResource(location.createRelative(filePath).getFile());
Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz");
@ -85,13 +78,19 @@ class GzipSupport implements AfterEachCallback, ParameterResolver { @@ -85,13 +78,19 @@ class GzipSupport implements AfterEachCallback, ParameterResolver {
File gzFile = Files.createFile(gzFilePath).toFile();
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile));
FileCopyUtils.copy(resource.getInputStream(), out);
created.add(gzFile);
this.created.add(gzFile);
}
catch (IOException ex) {
throw new RuntimeException(ex);
throw new UncheckedIOException(ex);
}
}
@Override
public void close() {
for (File file: this.created) {
file.delete();
}
}
}
}

3
spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

@ -37,6 +37,7 @@ import org.springframework.util.StringUtils; @@ -37,6 +37,7 @@ import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.resource.GzipSupport.GzippedFiles;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import org.springframework.web.testfixture.servlet.MockServletContext;
@ -357,7 +358,7 @@ class ResourceHttpRequestHandlerTests { @@ -357,7 +358,7 @@ class ResourceHttpRequestHandlerTests {
}
@Test // gh-25976
void partialContentByteRangeWithEncodedResource(GzipSupport.GzippedFiles gzippedFiles) throws Exception {
void partialContentByteRangeWithEncodedResource(GzippedFiles gzippedFiles) throws Exception {
String path = "js/foo.js";
gzippedFiles.create(path);

Loading…
Cancel
Save