jdk/test/java/util/logging/CheckLockLocationTest.java
author jlahoda
Wed, 12 Dec 2012 20:26:56 +0100
changeset 14803 88347e495d34
parent 14509 4358b75583be
child 14757 1da20d79c7c5
permissions -rw-r--r--
8004504: ListBuffer could reuse List.nil() as the sentinel element Summary: ListBuffer.last now points to the last elements with client data, or null if none. Reviewed-by: jjg, mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14509
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
     1
/*
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
     4
 *
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
     8
 *
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    13
 * accompanied this code).
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    14
 *
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    18
 *
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    21
 * questions.
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    22
 */
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    23
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    24
/*
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    25
 * @test
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    26
 * @bug     6244047
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    27
 * @author Jim Gish
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    28
 * @summary throw more precise IOException when pattern specifies invalid directory
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    29
 *
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    30
 * @run  main/othervm CheckLockLocationTest
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    31
 */
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    32
import java.io.File;
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    33
import java.io.FileNotFoundException;
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    34
import java.io.IOException;
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    35
import java.nio.file.AccessDeniedException;
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    36
import java.nio.file.FileSystemException;
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    37
import java.nio.file.NoSuchFileException;
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    38
import java.util.logging.FileHandler;
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    39
public class CheckLockLocationTest {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    40
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    41
    private static final String NON_WRITABLE_DIR = "non-writable-dir";
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    42
    private static final String NOT_A_DIR = "not-a-dir";
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    43
    private static final String WRITABLE_DIR = "writable-dir";
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    44
    private static final String NON_EXISTENT_DIR = "non-existent-dir";
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    45
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    46
    public static void main(String... args) throws IOException {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    47
        // we'll base all file creation attempts on the system temp directory,
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    48
        // %t and also try specifying non-existent directories and plain files
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    49
        // that should be directories, and non-writable directories,
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    50
        // to exercise all code paths of checking the lock location
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    51
        File writableDir = setup();
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    52
        // we now have three files/directories to work with:
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    53
        //    writableDir
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    54
        //    notAdir
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    55
        //    nonWritableDir
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    56
        //    nonExistentDir (which doesn't exist)
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    57
        runTests(writableDir);
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    58
    }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    59
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    60
    /**
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    61
     * @param writableDir in which log and lock file are created
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    62
     * @throws SecurityException
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    63
     * @throws RuntimeException
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    64
     * @throws IOException
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    65
     */
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    66
    private static void runTests(File writableDir) throws SecurityException,
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    67
            RuntimeException, IOException {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    68
        // Test 1: make sure we can create FileHandler in writable directory
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    69
        try {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    70
            new FileHandler("%t/" + WRITABLE_DIR + "/log.log");
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    71
        } catch (IOException ex) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    72
            throw new RuntimeException("Test failed: should have been able"
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    73
                    + " to create FileHandler for " + "%t/" + WRITABLE_DIR
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    74
                    + "/log.log in writable directory.", ex);
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    75
        } finally {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    76
            // the above test leaves files in the directory.  Get rid of the
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    77
            // files created and the directory
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    78
            delete(writableDir);
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    79
        }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    80
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    81
        // Test 2: creating FileHandler in non-writable directory should fail
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    82
        try {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    83
            new FileHandler("%t/" + NON_WRITABLE_DIR + "/log.log");
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    84
            throw new RuntimeException("Test failed: should not have been able"
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    85
                    + " to create FileHandler for " + "%t/" + NON_WRITABLE_DIR
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    86
                    + "/log.log in non-writable directory.");
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    87
        } catch (IOException ex) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    88
            // check for the right exception
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    89
            if (!(ex instanceof AccessDeniedException)) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    90
                throw new RuntimeException("Test failed: Expected exception was not an AccessDeniedException", ex);
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    91
            }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    92
        }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    93
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    94
        // Test 3: creating FileHandler in non-directory should fail
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    95
        try {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    96
            new FileHandler("%t/" + NOT_A_DIR + "/log.log");
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    97
            throw new RuntimeException("Test failed: should not have been able"
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    98
                    + " to create FileHandler for " + "%t/" + NOT_A_DIR
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
    99
                    + "/log.log in non-directory.");
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   100
        } catch (IOException ex) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   101
            // check for the right exception
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   102
            if (!(ex instanceof FileSystemException && ex.getMessage().contains("Not a directory"))) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   103
                throw new RuntimeException("Test failed: Expected exception was not a FileSystemException", ex);
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   104
            }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   105
        }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   106
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   107
        // Test 4: make sure we can't create a FileHandler in a non-existent dir
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   108
        try {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   109
            new FileHandler("%t/" + NON_EXISTENT_DIR + "/log.log");
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   110
            throw new RuntimeException("Test failed: should not have been able"
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   111
                    + " to create FileHandler for " + "%t/" + NON_EXISTENT_DIR
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   112
                    + "/log.log in a non-existent directory.");
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   113
        } catch (IOException ex) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   114
            // check for the right exception
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   115
            if (!(ex instanceof NoSuchFileException)) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   116
                throw new RuntimeException("Test failed: Expected exception was not a NoSuchFileException", ex);
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   117
            }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   118
        }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   119
    }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   120
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   121
    /**
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   122
     * Setup all the files and directories needed for the tests
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   123
     *
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   124
     * @return writable directory created that needs to be deleted when done
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   125
     * @throws RuntimeException
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   126
     */
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   127
    private static File setup() throws RuntimeException {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   128
        // First do some setup in the temporary directory (using same logic as
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   129
        // FileHandler for %t pattern)
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   130
        String tmpDir = System.getProperty("java.io.tmpdir"); // i.e. %t
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   131
        if (tmpDir == null) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   132
            tmpDir = System.getProperty("user.home");
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   133
        }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   134
        File tmpOrHomeDir = new File(tmpDir);
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   135
        // Create a writable directory here (%t/writable-dir)
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   136
        File writableDir = new File(tmpOrHomeDir, WRITABLE_DIR);
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   137
        if (!createFile(writableDir, true)) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   138
            throw new RuntimeException("Test setup failed: unable to create"
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   139
                    + " writable working directory "
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   140
                    + writableDir.getAbsolutePath() );
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   141
        }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   142
        // writableDirectory and its contents will be deleted after the test
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   143
        // that uses it
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   144
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   145
        // Create a plain file which we will attempt to use as a directory
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   146
        // (%t/not-a-dir)
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   147
        File notAdir = new File(tmpOrHomeDir, NOT_A_DIR);
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   148
        if (!createFile(notAdir, false)) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   149
            throw new RuntimeException("Test setup failed: unable to a plain"
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   150
                    + " working file " + notAdir.getAbsolutePath() );
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   151
        }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   152
        notAdir.deleteOnExit();
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   153
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   154
        // Create a non-writable directory (%t/non-writable-dir)
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   155
        File nonWritableDir = new File(tmpOrHomeDir, NON_WRITABLE_DIR);
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   156
        if (!createFile(nonWritableDir, true)) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   157
            throw new RuntimeException("Test setup failed: unable to create"
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   158
                    + " a non-"
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   159
                    + "writable working directory "
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   160
                    + nonWritableDir.getAbsolutePath() );
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   161
        }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   162
        nonWritableDir.deleteOnExit();
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   163
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   164
        // make it non-writable
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   165
        if (!nonWritableDir.setWritable(false)) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   166
            throw new RuntimeException("Test setup failed: unable to make"
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   167
                    + " working directory " + nonWritableDir.getAbsolutePath()
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   168
                    + " non-writable.");
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   169
        }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   170
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   171
        // make sure non-existent directory really doesn't exist
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   172
        File nonExistentDir = new File(tmpOrHomeDir, NON_EXISTENT_DIR);
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   173
        if (nonExistentDir.exists()) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   174
            nonExistentDir.delete();
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   175
        }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   176
        return writableDir;
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   177
    }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   178
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   179
    /**
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   180
     * @param newFile
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   181
     * @return true if file already exists or creation succeeded
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   182
     */
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   183
    private static boolean createFile(File newFile, boolean makeDirectory) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   184
        if (newFile.exists()) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   185
            return true;
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   186
        }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   187
        if (makeDirectory) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   188
            return newFile.mkdir();
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   189
        } else {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   190
            try {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   191
                return newFile.createNewFile();
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   192
            } catch (IOException ioex) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   193
                ioex.printStackTrace();
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   194
                return false;
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   195
            }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   196
        }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   197
    }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   198
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   199
    /*
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   200
     * Recursively delete all files starting at specified file
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   201
     */
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   202
    private static void delete(File f) throws IOException {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   203
        if (f != null && f.isDirectory()) {
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   204
            for (File c : f.listFiles())
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   205
                delete(c);
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   206
        }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   207
        if (!f.delete())
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   208
            throw new FileNotFoundException("Failed to delete file: " + f);
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   209
    }
4358b75583be 6244047: impossible to specify directories to logging FileHandler unless they exist
jgish
parents:
diff changeset
   210
}