changeset 1326:cffb77974716

7901730: jmh-core-ct should cleanup CLASS_OUTPUT before using a new compile session
author shade
date Wed, 13 Jul 2016 19:31:07 +0300
parents 49e2fb0c9f3b
children c050a47b2b37
files jmh-core-ct/src/test/java/org/openjdk/jmh/ct/CompileTest.java
diffstat 1 files changed, 20 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core-ct/src/test/java/org/openjdk/jmh/ct/CompileTest.java	Wed Jul 13 01:00:45 2016 +0300
+++ b/jmh-core-ct/src/test/java/org/openjdk/jmh/ct/CompileTest.java	Wed Jul 13 19:31:07 2016 +0300
@@ -32,24 +32,13 @@
 import org.openjdk.jmh.util.FileUtils;
 import org.openjdk.jmh.util.Utils;
 
-import javax.tools.Diagnostic;
-import javax.tools.DiagnosticCollector;
-import javax.tools.JavaCompiler;
-import javax.tools.JavaFileObject;
-import javax.tools.SimpleJavaFileObject;
-import javax.tools.StandardJavaFileManager;
-import javax.tools.StandardLocation;
-import javax.tools.ToolProvider;
+import javax.tools.*;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URI;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 public class CompileTest {
 
@@ -134,12 +123,7 @@
 
         JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
         StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);
-
-        try {
-            fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(new File(System.getProperty("java.io.tmpdir"))));
-        } catch (IOException e) {
-            Assert.fail(e.getMessage());
-        }
+        setupClassOutput(fm);
 
         Collection<JavaSourceFromString> sources = new ArrayList<JavaSourceFromString>();
         for (Map.Entry<String, String> e : destination.getClasses().entrySet()) {
@@ -166,12 +150,7 @@
 
         JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
         StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);
-
-        try {
-            fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(new File(System.getProperty("java.io.tmpdir"))));
-        } catch (IOException e) {
-            Assert.fail(e.getMessage());
-        }
+        setupClassOutput(fm);
 
         String name = "/" + klass.getCanonicalName().replaceAll("\\.", "/") + ".java";
         String shortName = klass.getName();
@@ -202,6 +181,22 @@
         }
     }
 
+    private static void setupClassOutput(StandardJavaFileManager fm) {
+        try {
+            File tmp = File.createTempFile("jmh-core-ct", "temp");
+            if (!tmp.delete()) {
+                throw new IOException("Cannot delete temp file: " + tmp);
+            }
+            if (!tmp.mkdirs()) {
+                throw new IOException("Cannot create temp dir: " + tmp);
+            }
+            tmp.deleteOnExit();
+            fm.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(tmp));
+        } catch (IOException e) {
+            Assert.fail(e.getMessage());
+        }
+    }
+
     public static class JavaSourceFromString extends SimpleJavaFileObject {
         final String code;