This is an automated email from the ASF dual-hosted git repository.

fanningpj pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko-http.git


The following commit(s) were added to refs/heads/main by this push:
     new 592d74bb1 use java nio to create temp files in tests (#1000)
592d74bb1 is described below

commit 592d74bb1460c247cd6a56311ba3d5310c90d16a
Author: PJ Fanning <[email protected]>
AuthorDate: Fri Apr 3 06:51:32 2026 +0200

    use java nio to create temp files in tests (#1000)
    
    * use java nio to create temp files in tests
    
    * compile issues
---
 .../docs/http/javadsl/server/FileUploadExamplesTest.java  |  3 ++-
 .../directives/FileUploadDirectivesExamplesTest.java      |  6 +++---
 .../http/scaladsl/server/FileUploadExamplesSpec.scala     |  3 ++-
 .../directives/FileUploadDirectivesExamplesSpec.scala     | 11 ++++++-----
 .../scaladsl/marshallers/xml/ScalaXmlSupportSpec.scala    |  9 +++++----
 .../server/directives/FileAndResourceDirectivesSpec.scala | 15 ++++++++-------
 .../server/directives/FileUploadDirectivesSpec.scala      |  5 +++--
 7 files changed, 29 insertions(+), 23 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..75743fb5d 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 org.junit.Test;
 
 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 @@ public class FileUploadExamplesTest extends JUnitRouteTest {
                                   // 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").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 8a52500b6..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 class FileUploadDirectivesExamplesTest extends 
JUnitRouteTest {
     final Function<FileInfo, File> temporaryDestination =
         (info) -> {
           try {
-            return File.createTempFile(info.getFileName(), ".tmp");
+            return Files.createTempFile(info.getFileName(), ".tmp").toFile();
           } catch (Exception e) {
             return null;
           }
@@ -117,7 +117,7 @@ public class FileUploadDirectivesExamplesTest extends 
JUnitRouteTest {
     final Function<FileInfo, File> temporaryDestination =
         info -> {
           try {
-            return File.createTempFile(info.getFileName(), ".tmp");
+            return Files.createTempFile(info.getFileName(), ".tmp").toFile();
           } catch (Exception e) {
             return null;
           }
@@ -295,7 +295,7 @@ public class FileUploadDirectivesExamplesTest extends 
JUnitRouteTest {
 
     File tempFile = null;
     try {
-      tempFile = File.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 5906ebbf4..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 = File.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 4b4e27c30..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
@@ -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").toFile
 
     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").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 0a195f759..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
@@ -114,12 +114,13 @@ 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")
+    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 e183a62ba..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
@@ -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").toFile
       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).toFile
       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).toFile
       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).toFile
       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).toFile
       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").toFile
       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").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 172dd1ed1..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
@@ -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").toFile
             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").toFile
             files = files :+ dest
             dest
           }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to