8183320: Better cleanup in java/nio/file/Files/probeContentType/ParallelProbes.java
authorbpb
Wed, 19 Jul 2017 08:46:38 -0700
changeset 45923 00a75e0998d1
parent 45917 4bbea012e567
child 45924 8bbd04c0791e
8183320: Better cleanup in java/nio/file/Files/probeContentType/ParallelProbes.java 8183321: Better cleanup for jdk/test/java/io/File/createTempFile/NameTooLong.java 8183343: Better cleanup for jdk/test/java/nio/file/spi/SetDefaultProvider.java 8183344: Better cleanup for jdk/test/java/io/File/createTempFile/SpecialTempFile.java Summary: Change locations where temporary files are created to ensure cleanup Reviewed-by: chegar, alanb
jdk/test/java/io/File/createTempFile/NameTooLong.java
jdk/test/java/io/File/createTempFile/SpecialTempFile.java
jdk/test/java/nio/file/Files/probeContentType/ParallelProbes.java
jdk/test/java/nio/file/spi/SetDefaultProvider.java
--- a/jdk/test/java/io/File/createTempFile/NameTooLong.java	Thu Aug 24 16:29:53 2017 +0200
+++ b/jdk/test/java/io/File/createTempFile/NameTooLong.java	Wed Jul 19 08:46:38 2017 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -43,7 +43,8 @@
         for (String[] ps : prefixSuffix) {
             File f;
             try {
-                f = File.createTempFile(ps[0], ps[1]);
+                f = File.createTempFile(ps[0], ps[1],
+                        new File(System.getProperty("test.dir", ".")));
                 String s = f.toPath().getFileName().toString();
                 if (!s.startsWith(ps[0].substring(0, 3))) {
                     System.err.printf("%s did not start with %s%n", s,
--- a/jdk/test/java/io/File/createTempFile/SpecialTempFile.java	Thu Aug 24 16:29:53 2017 +0200
+++ b/jdk/test/java/io/File/createTempFile/SpecialTempFile.java	Wed Jul 19 08:46:38 2017 -0700
@@ -30,6 +30,9 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 
 public class SpecialTempFile {
 
@@ -45,21 +48,21 @@
         final String exceptionMsg = "Unable to create temporary file";
         String[] dirs = { null, "." };
 
+        Path testPath = Paths.get(System.getProperty("test.dir", "."));
         for (int i = 0; i < prefix.length; i++) {
             boolean exceptionThrown = false;
             File f = null;
 
             for (String dir: dirs) {
+                Path tempDir = Files.createTempDirectory(testPath, dir);
                 System.out.println("In test " + name +
                                    ", creating temp file with prefix, " +
                                    prefix[i] + ", suffix, " + suffix[i] +
-                                   ", in dir, " + dir);
+                                   ", in dir, " + tempDir);
 
                 try {
-                    if (dir == null || dir.isEmpty())
-                        f = File.createTempFile(prefix[i], suffix[i]);
-                    else
-                        f = File.createTempFile(prefix[i], suffix[i], new File(dir));
+                    f = File.createTempFile(prefix[i], suffix[i],
+                        tempDir.toFile());
                 } catch (IOException e) {
                     if (exceptionExpected) {
                         if (e.getMessage().startsWith(exceptionMsg))
@@ -81,10 +84,6 @@
     public static void main(String[] args) throws Exception {
         // Common test
         final String name = "SpecialTempFile";
-        File f = new File(System.getProperty("java.io.tmpdir"), name);
-        if (!f.exists()) {
-            f.createNewFile();
-        }
         String[] nulPre = { name + "\u0000" };
         String[] nulSuf = { ".test" };
         test("NulName", nulPre, nulSuf, true);
--- a/jdk/test/java/nio/file/Files/probeContentType/ParallelProbes.java	Thu Aug 24 16:29:53 2017 +0200
+++ b/jdk/test/java/nio/file/Files/probeContentType/ParallelProbes.java	Wed Jul 19 08:46:38 2017 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -31,6 +31,7 @@
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 
 public class ParallelProbes {
@@ -47,7 +48,8 @@
     }
 
     private Path createTmpFile() throws IOException {
-        final Path p = Files.createTempFile("prefix", ".json");
+        Path dir = Paths.get(System.getProperty("test.dir", "."));
+        final Path p = Files.createTempFile(dir, "prefix", ".json");
         Files.write(p, "{\"test\"}".getBytes());
         System.out.println("Write test file <" + p + ">");
         return p;
--- a/jdk/test/java/nio/file/spi/SetDefaultProvider.java	Thu Aug 24 16:29:53 2017 +0200
+++ b/jdk/test/java/nio/file/spi/SetDefaultProvider.java	Wed Jul 19 08:46:38 2017 -0700
@@ -32,6 +32,7 @@
  */
 
 import java.io.File;
+import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -54,6 +55,11 @@
             new RuntimeException("jar tool not found")
         );
 
+    private static Path createTempDirectory(String prefix) throws IOException {
+        Path testDir = Paths.get(System.getProperty("test.dir", "."));
+        return Files.createTempDirectory(testDir, prefix);
+    }
+
     /**
      * Test override of default FileSystemProvider with the main application
      * on the class path.
@@ -91,7 +97,7 @@
      * is a module that is patched by an exploded patch.
      */
     public void testExplodedModuleWithExplodedPatch() throws Exception {
-        Path patchdir = Files.createTempDirectory("patch");
+        Path patchdir = createTempDirectory("patch");
         String modulePath = System.getProperty("jdk.module.path");
         int exitValue = exec(SET_DEFAULT_FSP,
                              "--patch-module", "m=" + patchdir,
@@ -105,7 +111,7 @@
      * is a module that is patched by an exploded patch.
      */
     public void testExplodedModuleWithJarPatch() throws Exception {
-        Path patchdir = Files.createTempDirectory("patch");
+        Path patchdir = createTempDirectory("patch");
         Files.createDirectory(patchdir.resolve("m.properties"));
         Path patch = createJarFile(patchdir);
         String modulePath = System.getProperty("jdk.module.path");
@@ -142,7 +148,7 @@
      * Creates a JAR file containing the entries in the given file tree.
      */
     private Path createJarFile(Path dir) throws Exception {
-        Path jar = Files.createTempDirectory("tmp").resolve("m.jar");
+        Path jar = createTempDirectory("tmp").resolve("m.jar");
         String[] args = { "--create", "--file=" + jar, "-C", dir.toString(), "." };
         int ret = JAR_TOOL.run(System.out, System.out, args);
         assertTrue(ret == 0);