test/tools/javac/diags/CheckExamples.java

changeset 1669
d3648557391b
parent 1409
33abf479f202
child 2525
2eb010b6cb22
     1.1 --- a/test/tools/javac/diags/CheckExamples.java	Thu Mar 28 10:49:39 2013 -0700
     1.2 +++ b/test/tools/javac/diags/CheckExamples.java	Thu Mar 28 10:58:45 2013 -0700
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -34,6 +34,8 @@
    1.11   */
    1.12  
    1.13  import java.io.*;
    1.14 +import java.nio.file.*;
    1.15 +import java.nio.file.attribute.BasicFileAttributes;
    1.16  import java.util.*;
    1.17  
    1.18  /**
    1.19 @@ -53,7 +55,27 @@
    1.20       * Standard entry point.
    1.21       */
    1.22      public static void main(String... args) throws Exception {
    1.23 -        new CheckExamples().run();
    1.24 +        boolean jtreg = (System.getProperty("test.src") != null);
    1.25 +        Path tmpDir;
    1.26 +        boolean deleteOnExit;
    1.27 +        if (jtreg) {
    1.28 +            // use standard jtreg scratch directory: the current directory
    1.29 +            tmpDir = Paths.get(System.getProperty("user.dir"));
    1.30 +            deleteOnExit = false;
    1.31 +        } else {
    1.32 +            tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")),
    1.33 +                    CheckExamples.class.getName());
    1.34 +            deleteOnExit = true;
    1.35 +        }
    1.36 +        Example.setTempDir(tmpDir.toFile());
    1.37 +
    1.38 +        try {
    1.39 +            new CheckExamples().run();
    1.40 +        } finally {
    1.41 +            if (deleteOnExit) {
    1.42 +                clean(tmpDir);
    1.43 +            }
    1.44 +        }
    1.45      }
    1.46  
    1.47      /**
    1.48 @@ -190,6 +212,25 @@
    1.49  
    1.50      int errors;
    1.51  
    1.52 +    /**
    1.53 +     * Clean the contents of a directory.
    1.54 +     */
    1.55 +    static void clean(Path dir) throws IOException {
    1.56 +        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
    1.57 +            @Override
    1.58 +            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    1.59 +                Files.delete(file);
    1.60 +                return super.visitFile(file, attrs);
    1.61 +            }
    1.62 +
    1.63 +            @Override
    1.64 +            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
    1.65 +                if (exc == null) Files.delete(dir);
    1.66 +                return super.postVisitDirectory(dir, exc);
    1.67 +            }
    1.68 +        });
    1.69 +    }
    1.70 +
    1.71      static class Counts {
    1.72          static String[] prefixes = {
    1.73              "compiler.err.",

mercurial