diff options
author | Martin Polden <mpolden@mpolden.no> | 2022-04-05 17:41:45 +0200 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2022-04-05 17:45:36 +0200 |
commit | e8e8672fb395c1e2af40c5e1bddde61fa07c27f1 (patch) | |
tree | 5a940b181ed182b213199c6f442b67fa47d237ee | |
parent | c69083fd9eab7b4a995524b6896ee2e40b355bd7 (diff) |
Use TemporaryFolder in test
3 files changed, 25 insertions, 20 deletions
diff --git a/application-preprocessor/src/test/java/com/yahoo/application/preprocessor/ApplicationPreprocessorTest.java b/application-preprocessor/src/test/java/com/yahoo/application/preprocessor/ApplicationPreprocessorTest.java index 1fc4d08b405..5039b05b393 100644 --- a/application-preprocessor/src/test/java/com/yahoo/application/preprocessor/ApplicationPreprocessorTest.java +++ b/application-preprocessor/src/test/java/com/yahoo/application/preprocessor/ApplicationPreprocessorTest.java @@ -2,17 +2,15 @@ package com.yahoo.application.preprocessor; import org.junit.Rule; -import org.junit.rules.TemporaryFolder; - -import java.io.File; -import java.io.IOException; -import java.util.Optional; - import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.xml.sax.SAXException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; +import java.io.File; +import java.io.IOException; +import java.util.Optional; public class ApplicationPreprocessorTest { diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/application/CompressedApplicationInputStreamTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/application/CompressedApplicationInputStreamTest.java index 1e8005c8af6..c7662ac9ee4 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/application/CompressedApplicationInputStreamTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/application/CompressedApplicationInputStreamTest.java @@ -7,12 +7,16 @@ import com.yahoo.yolean.Exceptions; import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; import java.util.List; import java.util.zip.GZIPOutputStream; @@ -26,6 +30,9 @@ import static org.junit.Assert.assertTrue; */ public class CompressedApplicationInputStreamTest { + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + private static void writeFileToTar(ArchiveOutputStream taos, File file) throws IOException { taos.putArchiveEntry(taos.createArchiveEntry(file, file.getName())); ByteStreams.copy(new FileInputStream(file), taos); @@ -41,14 +48,14 @@ public class CompressedApplicationInputStreamTest { return outFile; } - public static File createTarFile() throws IOException { - File outFile = File.createTempFile("testapp", ".tar.gz"); + public static File createTarFile(Path dir) throws IOException { + File outFile = Files.createTempFile(dir, "testapp", ".tar.gz").toFile(); ArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(outFile))); return createArchiveFile(archiveOutputStream, outFile); } - private static File createZipFile() throws IOException { - File outFile = File.createTempFile("testapp", ".tar.gz"); + private File createZipFile(Path dir) throws IOException { + File outFile = Files.createTempFile(dir, "testapp", ".tar.gz").toFile(); ArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(new FileOutputStream(outFile)); return createArchiveFile(archiveOutputStream, outFile); } @@ -62,7 +69,7 @@ public class CompressedApplicationInputStreamTest { @Test public void require_that_valid_tar_application_can_be_unpacked() throws IOException { - File outFile = createTarFile(); + File outFile = createTarFile(temporaryFolder.getRoot().toPath()); try (CompressedApplicationInputStream unpacked = streamFromTarGz(outFile)) { File outApp = unpacked.decompress(); assertTestApp(outApp); @@ -71,7 +78,7 @@ public class CompressedApplicationInputStreamTest { @Test public void require_that_valid_tar_application_in_subdir_can_be_unpacked() throws IOException { - File outFile = File.createTempFile("testapp", ".tar.gz"); + File outFile = Files.createTempFile(temporaryFolder.getRoot().toPath(), "testapp", ".tar.gz").toFile(); ArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(outFile))); File app = new File("src/test/resources/deploy/validapp"); @@ -100,7 +107,7 @@ public class CompressedApplicationInputStreamTest { @Test public void require_that_valid_zip_application_can_be_unpacked() throws IOException { - File outFile = createZipFile(); + File outFile = createZipFile(temporaryFolder.getRoot().toPath()); try (CompressedApplicationInputStream unpacked = streamFromZip(outFile)) { File outApp = unpacked.decompress(); assertTestApp(outApp); @@ -161,8 +168,8 @@ public class CompressedApplicationInputStreamTest { streamFromTarGz(app).close(); } - private static File createTarGz(String appDir) throws IOException, InterruptedException { - File tmpTar = File.createTempFile("myapp", ".tar"); + private File createTarGz(String appDir) throws IOException, InterruptedException { + File tmpTar = Files.createTempFile(temporaryFolder.getRoot().toPath(), "myapp", ".tar").toFile(); Process p = new ProcessBuilder("tar", "-C", appDir, "-cvf", tmpTar.getAbsolutePath(), ".").start(); p.waitFor(); p = new ProcessBuilder("gzip", tmpTar.getAbsolutePath()).start(); diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionCreateHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionCreateHandlerTest.java index 702dd2792da..6043dddc1db 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionCreateHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionCreateHandlerTest.java @@ -109,20 +109,20 @@ public class SessionCreateHandlerTest extends SessionHandlerTest { @Test public void require_that_post_request_must_have_correct_content_type() throws IOException { HashMap<String, String> headers = new HashMap<>(); // no Content-Type header - File outFile = CompressedApplicationInputStreamTest.createTarFile(); + File outFile = CompressedApplicationInputStreamTest.createTarFile(temporaryFolder.getRoot().toPath()); HttpResponse response = createHandler().handle(post(outFile, headers, null)); assertHttpStatusCodeErrorCodeAndMessage(response, BAD_REQUEST, HttpErrorResponse.ErrorCode.BAD_REQUEST, "Request contains no Content-Type header"); } private void assertIllegalFromParameter(String fromValue) throws IOException { - File outFile = CompressedApplicationInputStreamTest.createTarFile(); + File outFile = CompressedApplicationInputStreamTest.createTarFile(temporaryFolder.getRoot().toPath()); HttpRequest request = post(outFile, postHeaders, Collections.singletonMap("from", fromValue)); assertHttpStatusCodeErrorCodeAndMessage(createHandler().handle(request), BAD_REQUEST, HttpErrorResponse.ErrorCode.BAD_REQUEST, "Parameter 'from' has illegal value '" + fromValue + "'"); } @Test public void require_that_prepare_url_is_returned_on_success() throws IOException { - File outFile = CompressedApplicationInputStreamTest.createTarFile(); + File outFile = CompressedApplicationInputStreamTest.createTarFile(temporaryFolder.getRoot().toPath()); Map<String, String> parameters = Collections.singletonMap("name", "foo"); HttpResponse response = createHandler().handle(post(outFile, postHeaders, parameters)); assertNotNull(response); @@ -143,7 +143,7 @@ public class SessionCreateHandlerTest extends SessionHandlerTest { @Test public void require_internal_error_when_exception() throws IOException { - File outFile = CompressedApplicationInputStreamTest.createTarFile(); + File outFile = CompressedApplicationInputStreamTest.createTarFile(temporaryFolder.getRoot().toPath()); new FileWriter(outFile).write("rubbish"); HttpResponse response = createHandler().handle(post(outFile)); assertHttpStatusCodeErrorCodeAndMessage(response, INTERNAL_SERVER_ERROR, @@ -153,7 +153,7 @@ public class SessionCreateHandlerTest extends SessionHandlerTest { @Test public void require_that_handler_unpacks_application() throws IOException { - File outFile = CompressedApplicationInputStreamTest.createTarFile(); + File outFile = CompressedApplicationInputStreamTest.createTarFile(temporaryFolder.getRoot().toPath()); createHandler().handle(post(outFile)); ApplicationFile applicationFile = applicationRepository.getApplicationFileFromSession(tenant, 2, "services.xml", Session.Mode.READ); assertTrue(applicationFile.exists()); |