From 92d006e0539c59ac1db6af148e04610924a22045 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Mon, 30 Mar 2026 15:34:02 +0200 Subject: [PATCH 1/2] use java nio to create temp files in tests --- .../javadsl/server/FileUploadExamplesTest.java | 3 ++- .../FileUploadDirectivesExamplesTest.java | 6 +++--- .../scaladsl/server/FileUploadExamplesSpec.scala | 2 +- .../FileUploadDirectivesExamplesSpec.scala | 11 ++++++----- .../marshallers/xml/ScalaXmlSupportSpec.scala | 2 +- .../FileAndResourceDirectivesSpec.scala | 15 ++++++++------- .../directives/FileUploadDirectivesSpec.scala | 5 +++-- 7 files changed, 24 insertions(+), 20 deletions(-) diff --git a/docs/src/test/java/docs/http/javadsl/server/FileUploadExamplesTest.java b/docs/src/test/java/docs/http/javadsl/server/FileUploadExamplesTest.java index 69e9f064c..fae4984be 100644 --- a/docs/src/test/java/docs/http/javadsl/server/FileUploadExamplesTest.java +++ b/docs/src/test/java/docs/http/javadsl/server/FileUploadExamplesTest.java @@ -30,6 +30,7 @@ import java.io.File; import java.io.Serializable; +import java.nio.file.Files; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -83,7 +84,7 @@ Route uploadVideo() { // stream into a file as the chunks of it arrives and return a // CompletionStage // file to where it got stored - final File file = File.createTempFile("upload", "tmp"); + final File file = Files.createTempFile("upload", "tmp"); return bodyPart .getEntity() .getDataBytes() diff --git a/docs/src/test/java/docs/http/javadsl/server/directives/FileUploadDirectivesExamplesTest.java b/docs/src/test/java/docs/http/javadsl/server/directives/FileUploadDirectivesExamplesTest.java index 8a52500b6..cee478a85 100644 --- a/docs/src/test/java/docs/http/javadsl/server/directives/FileUploadDirectivesExamplesTest.java +++ b/docs/src/test/java/docs/http/javadsl/server/directives/FileUploadDirectivesExamplesTest.java @@ -75,7 +75,7 @@ public void testStoreUploadedFile() { final Function temporaryDestination = (info) -> { try { - return File.createTempFile(info.getFileName(), ".tmp"); + return Files.createTempFile(info.getFileName(), ".tmp"); } catch (Exception e) { return null; } @@ -117,7 +117,7 @@ public void testStoreUploadedFiles() { final Function temporaryDestination = info -> { try { - return File.createTempFile(info.getFileName(), ".tmp"); + return Files.createTempFile(info.getFileName(), ".tmp"); } catch (Exception e) { return null; } @@ -295,7 +295,7 @@ public void testFileProcessing() { File tempFile = null; try { - tempFile = File.createTempFile(prefix, suffix); + tempFile = Files.createTempFile(prefix, suffix); tempFile.deleteOnExit(); Files.write( tempFile.toPath(), diff --git a/docs/src/test/scala/docs/http/scaladsl/server/FileUploadExamplesSpec.scala b/docs/src/test/scala/docs/http/scaladsl/server/FileUploadExamplesSpec.scala index 5906ebbf4..3f003de60 100644 --- a/docs/src/test/scala/docs/http/scaladsl/server/FileUploadExamplesSpec.scala +++ b/docs/src/test/scala/docs/http/scaladsl/server/FileUploadExamplesSpec.scala @@ -47,7 +47,7 @@ class FileUploadExamplesSpec extends RoutingSpec with CompileOnlySpec { case b: BodyPart if b.name == "file" => // stream into a file as the chunks of it arrives and return a future // file to where it got stored - val file = File.createTempFile("upload", "tmp") + val file = Files.createTempFile("upload", "tmp") b.entity.dataBytes.runWith(FileIO.toPath(file.toPath)).map(_ => b.name -> file) diff --git a/docs/src/test/scala/docs/http/scaladsl/server/directives/FileUploadDirectivesExamplesSpec.scala b/docs/src/test/scala/docs/http/scaladsl/server/directives/FileUploadDirectivesExamplesSpec.scala index 4b4e27c30..646451a15 100644 --- a/docs/src/test/scala/docs/http/scaladsl/server/directives/FileUploadDirectivesExamplesSpec.scala +++ b/docs/src/test/scala/docs/http/scaladsl/server/directives/FileUploadDirectivesExamplesSpec.scala @@ -15,16 +15,17 @@ package docs.http.scaladsl.server.directives import org.apache.pekko import pekko.http.scaladsl.model._ +import pekko.http.scaladsl.server.RoutingSpec import pekko.http.scaladsl.server.directives.FileInfo import pekko.http.scaladsl.testkit.RouteTestTimeout import pekko.stream.scaladsl.Framing import pekko.testkit.TestDuration import pekko.util.ByteString -import java.io.File - -import pekko.http.scaladsl.server.RoutingSpec import docs.CompileOnlySpec +import java.io.File +import java.nio.file.Files + import scala.concurrent.Future import scala.concurrent.duration._ @@ -41,7 +42,7 @@ class FileUploadDirectivesExamplesSpec extends RoutingSpec with CompileOnlySpec // #storeUploadedFile def tempDestination(fileInfo: FileInfo): File = - File.createTempFile(fileInfo.fileName, ".tmp") + Files.createTempFile(fileInfo.fileName, ".tmp") val route = storeUploadedFile("csv", tempDestination) { @@ -70,7 +71,7 @@ class FileUploadDirectivesExamplesSpec extends RoutingSpec with CompileOnlySpec // #storeUploadedFiles def tempDestination(fileInfo: FileInfo): File = - File.createTempFile(fileInfo.fileName, ".tmp") + Files.createTempFile(fileInfo.fileName, ".tmp") val route = storeUploadedFiles("csv", tempDestination) { files => diff --git a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/marshallers/xml/ScalaXmlSupportSpec.scala b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/marshallers/xml/ScalaXmlSupportSpec.scala index 0a195f759..0720b8346 100644 --- a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/marshallers/xml/ScalaXmlSupportSpec.scala +++ b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/marshallers/xml/ScalaXmlSupportSpec.scala @@ -114,7 +114,7 @@ class ScalaXmlSupportSpec extends AnyFreeSpec with Matchers with ScalatestRouteT } def withTempFile[T](content: String)(f: File => T): T = { - val file = File.createTempFile("xxe", ".txt") + val file = Files.createTempFile("xxe", ".txt") try { Files.write(file.toPath, content.getBytes("UTF-8")) f(file) diff --git a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileAndResourceDirectivesSpec.scala b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileAndResourceDirectivesSpec.scala index e183a62ba..6e3c0f122 100644 --- a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileAndResourceDirectivesSpec.scala +++ b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileAndResourceDirectivesSpec.scala @@ -16,6 +16,7 @@ package directives import java.io.File import java.nio.charset.StandardCharsets +import java.nio.file.Files import scala.concurrent.duration._ import scala.util.Properties @@ -60,7 +61,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins Get() ~> getFromFile(Properties.javaHome) ~> check { handled shouldEqual false } } "return the file content with the MediaType matching the file extension" in { - val file = File.createTempFile("pekko Http Test", ".PDF") + val file = Files.createTempFile("pekko Http Test", ".PDF") try { writeAllText("This is PDF", file) Get() ~> getFromFile(file.getPath) ~> check { @@ -72,7 +73,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins } finally file.delete } "return the file content with MediaType 'application/octet-stream' on unknown file extensions" in { - val file = File.createTempFile("pekkoHttpTest", null) + val file = Files.createTempFile("pekkoHttpTest", null) try { writeAllText("Some content", file) Get() ~> getFromFile(file) ~> check { @@ -83,7 +84,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins } "return a single range from a file" in { - val file = File.createTempFile("pekkoHttpTest", null) + val file = Files.createTempFile("pekkoHttpTest", null) try { writeAllText("ABCDEFGHIJKLMNOPQRSTUVWXYZ", file) Get() ~> addHeader(Range(ByteRange(0, 10))) ~> getFromFile(file) ~> check { @@ -95,7 +96,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins } "return multiple ranges from a file at once" in { - val file = File.createTempFile("pekkoHttpTest", null) + val file = Files.createTempFile("pekkoHttpTest", null) try { writeAllText("ABCDEFGHIJKLMNOPQRSTUVWXYZ", file) val rangeHeader = Range(ByteRange(1, 10), ByteRange.suffix(10)) @@ -112,7 +113,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins } "properly handle zero-byte files" in { - val file = File.createTempFile("pekkoHttpTest", null) + val file = Files.createTempFile("pekkoHttpTest", null) try { Get() ~> getFromFile(file) ~> check { mediaType shouldEqual NoMediaType @@ -122,7 +123,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins } "support precompressed files with registered MediaType" in { - val file = File.createTempFile("pekkoHttpTest", ".svgz") + val file = Files.createTempFile("pekkoHttpTest", ".svgz") try { writeAllText("123", file) Get() ~> getFromFile(file) ~> check { @@ -134,7 +135,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins } "support files with registered MediaType and .gz suffix" in { - val file = File.createTempFile("pekkoHttpTest", ".js.gz") + val file = Files.createTempFile("pekkoHttpTest", ".js.gz") try { writeAllText("456", file) Get() ~> getFromFile(file) ~> check { diff --git a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileUploadDirectivesSpec.scala b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileUploadDirectivesSpec.scala index 172dd1ed1..fff9fac87 100644 --- a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileUploadDirectivesSpec.scala +++ b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileUploadDirectivesSpec.scala @@ -14,6 +14,7 @@ package org.apache.pekko.http.scaladsl.server.directives import java.io.File +import java.nio.file.Files import scala.concurrent.Future import scala.concurrent.duration._ @@ -45,7 +46,7 @@ class FileUploadDirectivesSpec extends RoutingSpec with Eventually { @volatile var file: Option[File] = None def tempDest(fileInfo: FileInfo): File = { - val dest = File.createTempFile("http-FileUploadDirectivesSpec", ".tmp") + val dest = Files.createTempFile("http-FileUploadDirectivesSpec", ".tmp") file = Some(dest) dest } @@ -102,7 +103,7 @@ class FileUploadDirectivesSpec extends RoutingSpec with Eventually { @volatile var files: Seq[File] = Nil def tempDest(fileInfo: FileInfo): File = { - val dest = File.createTempFile("http-FileUploadDirectivesSpec", ".tmp") + val dest = Files.createTempFile("http-FileUploadDirectivesSpec", ".tmp") files = files :+ dest dest } From 5e0483a3c3264b7f729edbf57bce44d53d236a6e Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Mon, 30 Mar 2026 16:05:45 +0200 Subject: [PATCH 2/2] compile issues --- .../javadsl/server/FileUploadExamplesTest.java | 2 +- .../FileUploadDirectivesExamplesTest.java | 6 +++--- .../scaladsl/server/FileUploadExamplesSpec.scala | 3 ++- .../FileUploadDirectivesExamplesSpec.scala | 4 ++-- .../marshallers/xml/ScalaXmlSupportSpec.scala | 7 ++++--- .../directives/FileAndResourceDirectivesSpec.scala | 14 +++++++------- .../directives/FileUploadDirectivesSpec.scala | 4 ++-- 7 files changed, 21 insertions(+), 19 deletions(-) diff --git a/docs/src/test/java/docs/http/javadsl/server/FileUploadExamplesTest.java b/docs/src/test/java/docs/http/javadsl/server/FileUploadExamplesTest.java index fae4984be..75743fb5d 100644 --- a/docs/src/test/java/docs/http/javadsl/server/FileUploadExamplesTest.java +++ b/docs/src/test/java/docs/http/javadsl/server/FileUploadExamplesTest.java @@ -84,7 +84,7 @@ Route uploadVideo() { // stream into a file as the chunks of it arrives and return a // CompletionStage // file to where it got stored - final File file = Files.createTempFile("upload", "tmp"); + final File file = Files.createTempFile("upload", "tmp").toFile(); return bodyPart .getEntity() .getDataBytes() diff --git a/docs/src/test/java/docs/http/javadsl/server/directives/FileUploadDirectivesExamplesTest.java b/docs/src/test/java/docs/http/javadsl/server/directives/FileUploadDirectivesExamplesTest.java index cee478a85..332d52252 100644 --- a/docs/src/test/java/docs/http/javadsl/server/directives/FileUploadDirectivesExamplesTest.java +++ b/docs/src/test/java/docs/http/javadsl/server/directives/FileUploadDirectivesExamplesTest.java @@ -75,7 +75,7 @@ public void testStoreUploadedFile() { final Function temporaryDestination = (info) -> { try { - return Files.createTempFile(info.getFileName(), ".tmp"); + return Files.createTempFile(info.getFileName(), ".tmp").toFile(); } catch (Exception e) { return null; } @@ -117,7 +117,7 @@ public void testStoreUploadedFiles() { final Function temporaryDestination = info -> { try { - return Files.createTempFile(info.getFileName(), ".tmp"); + return Files.createTempFile(info.getFileName(), ".tmp").toFile(); } catch (Exception e) { return null; } @@ -295,7 +295,7 @@ public void testFileProcessing() { File tempFile = null; try { - tempFile = Files.createTempFile(prefix, suffix); + tempFile = Files.createTempFile(prefix, suffix).toFile(); tempFile.deleteOnExit(); Files.write( tempFile.toPath(), diff --git a/docs/src/test/scala/docs/http/scaladsl/server/FileUploadExamplesSpec.scala b/docs/src/test/scala/docs/http/scaladsl/server/FileUploadExamplesSpec.scala index 3f003de60..c56a27380 100644 --- a/docs/src/test/scala/docs/http/scaladsl/server/FileUploadExamplesSpec.scala +++ b/docs/src/test/scala/docs/http/scaladsl/server/FileUploadExamplesSpec.scala @@ -14,6 +14,7 @@ package docs.http.scaladsl.server import java.io.File +import java.nio.file.Files import org.apache.pekko import pekko.Done @@ -47,7 +48,7 @@ class FileUploadExamplesSpec extends RoutingSpec with CompileOnlySpec { case b: BodyPart if b.name == "file" => // stream into a file as the chunks of it arrives and return a future // file to where it got stored - val file = Files.createTempFile("upload", "tmp") + val file = Files.createTempFile("upload", "tmp").toFile b.entity.dataBytes.runWith(FileIO.toPath(file.toPath)).map(_ => b.name -> file) diff --git a/docs/src/test/scala/docs/http/scaladsl/server/directives/FileUploadDirectivesExamplesSpec.scala b/docs/src/test/scala/docs/http/scaladsl/server/directives/FileUploadDirectivesExamplesSpec.scala index 646451a15..4cf7ab7c6 100644 --- a/docs/src/test/scala/docs/http/scaladsl/server/directives/FileUploadDirectivesExamplesSpec.scala +++ b/docs/src/test/scala/docs/http/scaladsl/server/directives/FileUploadDirectivesExamplesSpec.scala @@ -42,7 +42,7 @@ class FileUploadDirectivesExamplesSpec extends RoutingSpec with CompileOnlySpec // #storeUploadedFile def tempDestination(fileInfo: FileInfo): File = - Files.createTempFile(fileInfo.fileName, ".tmp") + Files.createTempFile(fileInfo.fileName, ".tmp").toFile val route = storeUploadedFile("csv", tempDestination) { @@ -71,7 +71,7 @@ class FileUploadDirectivesExamplesSpec extends RoutingSpec with CompileOnlySpec // #storeUploadedFiles def tempDestination(fileInfo: FileInfo): File = - Files.createTempFile(fileInfo.fileName, ".tmp") + Files.createTempFile(fileInfo.fileName, ".tmp").toFile val route = storeUploadedFiles("csv", tempDestination) { files => diff --git a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/marshallers/xml/ScalaXmlSupportSpec.scala b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/marshallers/xml/ScalaXmlSupportSpec.scala index 0720b8346..e462ad725 100644 --- a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/marshallers/xml/ScalaXmlSupportSpec.scala +++ b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/marshallers/xml/ScalaXmlSupportSpec.scala @@ -115,11 +115,12 @@ class ScalaXmlSupportSpec extends AnyFreeSpec with Matchers with ScalatestRouteT def withTempFile[T](content: String)(f: File => T): T = { val file = Files.createTempFile("xxe", ".txt") + val fileRef = file.toFile try { - Files.write(file.toPath, content.getBytes("UTF-8")) - f(file) + Files.write(file, content.getBytes("UTF-8")) + f(fileRef) } finally { - file.delete() + fileRef.delete() } } } diff --git a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileAndResourceDirectivesSpec.scala b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileAndResourceDirectivesSpec.scala index 6e3c0f122..5d7afb139 100644 --- a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileAndResourceDirectivesSpec.scala +++ b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileAndResourceDirectivesSpec.scala @@ -61,7 +61,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins Get() ~> getFromFile(Properties.javaHome) ~> check { handled shouldEqual false } } "return the file content with the MediaType matching the file extension" in { - val file = Files.createTempFile("pekko Http Test", ".PDF") + val file = Files.createTempFile("pekko Http Test", ".PDF").toFile try { writeAllText("This is PDF", file) Get() ~> getFromFile(file.getPath) ~> check { @@ -73,7 +73,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins } finally file.delete } "return the file content with MediaType 'application/octet-stream' on unknown file extensions" in { - val file = Files.createTempFile("pekkoHttpTest", null) + val file = Files.createTempFile("pekkoHttpTest", null).toFile try { writeAllText("Some content", file) Get() ~> getFromFile(file) ~> check { @@ -84,7 +84,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins } "return a single range from a file" in { - val file = Files.createTempFile("pekkoHttpTest", null) + val file = Files.createTempFile("pekkoHttpTest", null).toFile try { writeAllText("ABCDEFGHIJKLMNOPQRSTUVWXYZ", file) Get() ~> addHeader(Range(ByteRange(0, 10))) ~> getFromFile(file) ~> check { @@ -96,7 +96,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins } "return multiple ranges from a file at once" in { - val file = Files.createTempFile("pekkoHttpTest", null) + val file = Files.createTempFile("pekkoHttpTest", null).toFile try { writeAllText("ABCDEFGHIJKLMNOPQRSTUVWXYZ", file) val rangeHeader = Range(ByteRange(1, 10), ByteRange.suffix(10)) @@ -113,7 +113,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins } "properly handle zero-byte files" in { - val file = Files.createTempFile("pekkoHttpTest", null) + val file = Files.createTempFile("pekkoHttpTest", null).toFile try { Get() ~> getFromFile(file) ~> check { mediaType shouldEqual NoMediaType @@ -123,7 +123,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins } "support precompressed files with registered MediaType" in { - val file = Files.createTempFile("pekkoHttpTest", ".svgz") + val file = Files.createTempFile("pekkoHttpTest", ".svgz").toFile try { writeAllText("123", file) Get() ~> getFromFile(file) ~> check { @@ -135,7 +135,7 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins } "support files with registered MediaType and .gz suffix" in { - val file = Files.createTempFile("pekkoHttpTest", ".js.gz") + val file = Files.createTempFile("pekkoHttpTest", ".js.gz").toFile try { writeAllText("456", file) Get() ~> getFromFile(file) ~> check { diff --git a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileUploadDirectivesSpec.scala b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileUploadDirectivesSpec.scala index fff9fac87..5947f3374 100644 --- a/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileUploadDirectivesSpec.scala +++ b/http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileUploadDirectivesSpec.scala @@ -46,7 +46,7 @@ class FileUploadDirectivesSpec extends RoutingSpec with Eventually { @volatile var file: Option[File] = None def tempDest(fileInfo: FileInfo): File = { - val dest = Files.createTempFile("http-FileUploadDirectivesSpec", ".tmp") + val dest = Files.createTempFile("http-FileUploadDirectivesSpec", ".tmp").toFile file = Some(dest) dest } @@ -103,7 +103,7 @@ class FileUploadDirectivesSpec extends RoutingSpec with Eventually { @volatile var files: Seq[File] = Nil def tempDest(fileInfo: FileInfo): File = { - val dest = Files.createTempFile("http-FileUploadDirectivesSpec", ".tmp") + val dest = Files.createTempFile("http-FileUploadDirectivesSpec", ".tmp").toFile files = files :+ dest dest }