test/jdk/java/io/File/WinDirRelative.java
author dholmes
Wed, 06 Nov 2019 21:18:42 -0500
changeset 58956 9a0a5e70eeb2
parent 47216 71c04702a3d5
permissions -rw-r--r--
8233454: Test fails with assert(!is_init_completed(), "should only happen during init") after JDK-8229516 Reviewed-by: jiefu, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43105
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
     1
/*
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
     4
 *
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
     8
 *
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    13
 * accompanied this code).
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    14
 *
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    18
 *
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    21
 * questions.
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    22
 */
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    23
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    24
/*
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    25
 * @test
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    26
 * @bug 8153250
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    27
 * @summary Tests that files are correctly listed for a directory-relative path
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    28
 * @requires (os.family == "windows")
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    29
 */
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    30
import java.io.File;
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    31
import java.util.ArrayList;
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    32
import java.util.List;
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    33
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    34
public class WinDirRelative {
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    35
    private static final char COLON = ':';
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    36
    private static final String BASENAME = "TestFile_";
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    37
    private static final String EXTENSION = ".txt";
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    38
    private static final int NUM_FILES = 10;
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    39
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    40
    private static boolean isLetter(char c) {
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    41
        return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    42
    }
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    43
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    44
    public static void main(String[] args) throws Throwable {
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    45
        // Get the working directory which is also the default
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    46
        // directory for the current drive.
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    47
        String userDir = System.getProperty("user.dir");
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    48
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    49
        // Test only if a leading drive letter is found
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    50
        if (isLetter(userDir.charAt(0)) && userDir.charAt(1) == COLON) {
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    51
            // Create some empty files
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    52
            List<String> filenames = new ArrayList<String>(NUM_FILES);
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    53
            for (int i = 0; i < NUM_FILES; i++) {
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    54
                String filename = BASENAME + i + EXTENSION;
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    55
                filenames.add(filename);
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    56
                File f = new File(filename);
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    57
                f.createNewFile();
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    58
                f.deleteOnExit();
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    59
                System.out.printf("Created %s (%s)%n", filename,
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    60
                    f.getAbsolutePath());
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    61
            }
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    62
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    63
            // List files and verify that the ones with recognized names exist.
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    64
            String prefix = userDir.substring(0, 2);
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    65
            File p = new File(prefix);
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    66
            int failures = 0;
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    67
            int successes = 0;
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    68
            for (File f : p.listFiles()) {
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    69
                if (f.getName().toString().startsWith(BASENAME)) {
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    70
                    if (!f.exists()) {
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    71
                        System.err.printf("%s (%s) does not exist%n", f,
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    72
                            f.getAbsolutePath());
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    73
                        failures++;
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    74
                    } else {
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    75
                        successes++;
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    76
                    }
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    77
                }
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    78
            }
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    79
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    80
            // Fail if there was an existence test failure or if not
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    81
            // enough of the created files were found
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    82
            boolean testFailed = false;
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    83
            if (failures > 0) {
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    84
                System.err.println("Existence check failed");
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    85
                testFailed = true;
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    86
            }
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    87
            if (successes != NUM_FILES) {
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    88
                System.err.println("Count check failed");
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    89
                testFailed = true;
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    90
            }
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    91
            if (testFailed) {
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    92
                throw new RuntimeException("Test failed");
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    93
            }
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    94
        }
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    95
    }
b1d99aa9f29f 8153250: java.io.File does not handle Windows paths of the form "D:" (no path) correctly
bpb
parents:
diff changeset
    96
}