test/tools/javac/diags/RunExamples.java

changeset 1669
d3648557391b
parent 1409
33abf479f202
child 2525
2eb010b6cb22
equal deleted inserted replaced
1668:991f11e13598 1669:d3648557391b
1 /* 1 /*
2 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
31 /* 31 /*
32 * See CR 7127924 for info on why othervm is used. 32 * See CR 7127924 for info on why othervm is used.
33 */ 33 */
34 34
35 import java.io.*; 35 import java.io.*;
36 import java.text.SimpleDateFormat; 36 import java.nio.file.*;
37 import java.nio.file.attribute.BasicFileAttributes;
37 import java.util.*; 38 import java.util.*;
38 import java.util.regex.Matcher; 39 import java.util.regex.Matcher;
39 import java.util.regex.Pattern; 40 import java.util.regex.Pattern;
40 41
41 /** 42 /**
54 * -title string specify a title, only applies to HTML output 55 * -title string specify a title, only applies to HTML output
55 */ 56 */
56 public class RunExamples { 57 public class RunExamples {
57 public static void main(String... args) throws Exception { 58 public static void main(String... args) throws Exception {
58 jtreg = (System.getProperty("test.src") != null); 59 jtreg = (System.getProperty("test.src") != null);
59 File tmpDir; 60 Path tmpDir;
61 boolean deleteOnExit;
60 if (jtreg) { 62 if (jtreg) {
61 // use standard jtreg scratch directory: the current directory 63 // use standard jtreg scratch directory: the current directory
62 tmpDir = new File(System.getProperty("user.dir")); 64 tmpDir = Paths.get(System.getProperty("user.dir"));
65 deleteOnExit = false;
63 } else { 66 } else {
64 tmpDir = new File(System.getProperty("java.io.tmpdir"), 67 tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")),
65 RunExamples.class.getName() 68 RunExamples.class.getName());
66 + (new SimpleDateFormat("yyMMddHHmmss")).format(new Date())); 69 deleteOnExit = true;
67 } 70 }
68 Example.setTempDir(tmpDir); 71 Example.setTempDir(tmpDir.toFile());
69 72
70 RunExamples r = new RunExamples(); 73 RunExamples r = new RunExamples();
71 74
72 try { 75 try {
73 if (r.run(args)) 76 if (r.run(args))
74 return; 77 return;
75 } finally { 78 } finally {
76 /* VERY IMPORTANT NOTE. In jtreg mode, tmpDir is set to the 79 if (deleteOnExit) {
77 * jtreg scratch directory, which is the current directory. 80 clean(tmpDir);
78 * In case someone is faking jtreg mode, make sure to only
79 * clean tmpDir when it is reasonable to do so.
80 */
81 if (tmpDir.isDirectory() &&
82 tmpDir.getName().startsWith(RunExamples.class.getName())) {
83 if (clean(tmpDir))
84 tmpDir.delete();
85 } 81 }
86 } 82 }
87 83
88 if (jtreg) 84 if (jtreg)
89 throw new Exception(r.errors + " errors occurred"); 85 throw new Exception(r.errors + " errors occurred");
201 int errors; 197 int errors;
202 198
203 /** 199 /**
204 * Clean the contents of a directory. 200 * Clean the contents of a directory.
205 */ 201 */
206 static boolean clean(File dir) { 202 static void clean(Path dir) throws IOException {
207 boolean ok = true; 203 Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
208 for (File f: dir.listFiles()) { 204 @Override
209 if (f.isDirectory()) 205 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
210 ok &= clean(f); 206 Files.delete(file);
211 ok &= f.delete(); 207 return super.visitFile(file, attrs);
212 } 208 }
213 return ok; 209
210 @Override
211 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
212 if (exc == null) Files.delete(dir);
213 return super.postVisitDirectory(dir, exc);
214 }
215 });
214 } 216 }
215 217
216 static abstract class Runner { 218 static abstract class Runner {
217 Runner(boolean showFiles, boolean raw, boolean verbose) { 219 Runner(boolean showFiles, boolean raw, boolean verbose) {
218 this.showFiles = showFiles; 220 this.showFiles = showFiles;

mercurial