test/jdk/java/io/pathNames/General.java
author tschatzl
Wed, 24 Jul 2019 11:49:39 +0200
changeset 57508 28ab01c06755
parent 47216 71c04702a3d5
permissions -rw-r--r--
8228388: Add information about dirty/skipped card for Merge HCC in G1 log Summary: Collect and print informatio about the number of processed cards during the Merge HCC phase to improve log output. Reviewed-by: kbarrett, sangheki
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43108
9849ccac1f10 8156595: java/io/pathNames/GeneralWin32.java fail intermittently on windows-x64
amlu
parents: 21824
diff changeset
     2
 * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
   @summary Common definitions for general exhaustive pathname tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
   @author  Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
19611
0c8d4fbae4f2 4792059: test/java/io/pathNames/GeneralSolaris.java fails on symbolic links
dxu
parents: 17924
diff changeset
    31
import java.nio.file.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class General {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static boolean win32 = (File.separatorChar == '\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static int gensymCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
21824
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    42
    protected static final String userDir = System.getProperty("user.dir");
43108
9849ccac1f10 8156595: java/io/pathNames/GeneralWin32.java fail intermittently on windows-x64
amlu
parents: 21824
diff changeset
    43
    protected static final String workSubDir = "tmp";
21824
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    44
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    45
    protected static String baseDir = null;
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    46
    protected static String relative = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /* Generate a filename unique to this run */
21824
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    49
    private static String gensym() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        return "x." + ++gensymCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
21824
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    53
    /**
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    54
     * Create files and folders in the test working directory.
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    55
     * The purpose is to make sure the test will not go out of
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    56
     * its user dir when walking the file tree.
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    57
     *
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    58
     * @param  depth    The number of directory levels to be created under
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    59
     *                  the user directory. It should be the maximum value
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    60
     *                  of the depths passed to checkNames method (including
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    61
     *                  direct or indirect calling) in a whole test.
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    62
     */
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    63
    protected static void initTestData(int depth) throws IOException {
43108
9849ccac1f10 8156595: java/io/pathNames/GeneralWin32.java fail intermittently on windows-x64
amlu
parents: 21824
diff changeset
    64
        File parent = new File(userDir + File.separator + workSubDir);
9849ccac1f10 8156595: java/io/pathNames/GeneralWin32.java fail intermittently on windows-x64
amlu
parents: 21824
diff changeset
    65
        if (!parent.mkdir()) {
9849ccac1f10 8156595: java/io/pathNames/GeneralWin32.java fail intermittently on windows-x64
amlu
parents: 21824
diff changeset
    66
            throw new IOException("Fail to create directory: " + parent);
9849ccac1f10 8156595: java/io/pathNames/GeneralWin32.java fail intermittently on windows-x64
amlu
parents: 21824
diff changeset
    67
        }
21824
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    68
        for (int i = 0; i < depth; i++) {
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    69
            File tmp = new File(parent, gensym());
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    70
            tmp.createNewFile();
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    71
            tmp = new File(parent, gensym());
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    72
            if (tmp.mkdir())
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    73
                parent = tmp;
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    74
            else
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    75
                throw new IOException("Fail to create directory, " + tmp);
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    76
        }
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    77
        baseDir = parent.getAbsolutePath();
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    78
        relative = baseDir.substring(userDir.length() + 1);
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
    79
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Find a file in the given subdirectory, or descend into further
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * subdirectories, if any, if no file is found here.  Return null if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * file can be found anywhere beneath the given subdirectory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param  dir     Directory at which we started
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param  subdir  Subdirectory that we're exploring
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param  dl      Listing of subdirectory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static String findSomeFile(String dir, String subdir, String[] dl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        for (int i = 0; i < dl.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            File f = new File(subdir, dl[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            File df = new File(dir, f.getPath());
19611
0c8d4fbae4f2 4792059: test/java/io/pathNames/GeneralSolaris.java fails on symbolic links
dxu
parents: 17924
diff changeset
    93
            if (Files.isRegularFile(df.toPath(), LinkOption.NOFOLLOW_LINKS)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                return f.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        for (int i = 0; i < dl.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            File f = (subdir.length() == 0) ? new File(dl[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                            : new File(subdir, dl[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            File df = new File(dir, f.getPath());
19611
0c8d4fbae4f2 4792059: test/java/io/pathNames/GeneralSolaris.java fails on symbolic links
dxu
parents: 17924
diff changeset
   101
            if (Files.isDirectory(df.toPath(), LinkOption.NOFOLLOW_LINKS)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                String[] dl2 = df.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                if (dl2 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    String ff = findSomeFile(dir, f.getPath(), dl2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    if (ff != null) return ff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Construct a string that names a file in the given directory.  If create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * is true, then create a file if none is found, and throw an exception if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * that is not possible; otherwise, return null if no file can be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static String findSomeFile(String dir, boolean create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        File d = new File(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        String[] dl = d.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (dl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new RuntimeException("Can't list " + dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        for (int i = 0; i < dl.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            File f = new File(dir, dl[i]);
19611
0c8d4fbae4f2 4792059: test/java/io/pathNames/GeneralSolaris.java fails on symbolic links
dxu
parents: 17924
diff changeset
   126
            if (Files.isRegularFile(f.toPath(), LinkOption.NOFOLLOW_LINKS)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                return dl[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        String f = findSomeFile(dir, "", dl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (f != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            File nf = new File(d, gensym());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            OutputStream os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                os = new FileOutputStream(nf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                throw new RuntimeException("Can't create a file in " + dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return nf.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Construct a string that names a subdirectory of the given directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * If create is true, then create a subdirectory if none is found, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * throw an exception if that is not possible; otherwise, return null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * no subdirectory can be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static String findSomeDir(String dir, boolean create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        File d = new File(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        String[] dl = d.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (dl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new RuntimeException("Can't list " + dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        for (int i = 0; i < dl.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            File f = new File(d, dl[i]);
19611
0c8d4fbae4f2 4792059: test/java/io/pathNames/GeneralSolaris.java fails on symbolic links
dxu
parents: 17924
diff changeset
   163
            if (Files.isDirectory(f.toPath(), LinkOption.NOFOLLOW_LINKS)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                String[] dl2 = f.list();
17726
4aff99bf471b 8009258: TEST_BUG:java/io/pathNames/GeneralWin32.java fails intermittently
ewang
parents: 5506
diff changeset
   165
                if (dl2 == null || dl2.length >= 250) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    /* Heuristic to avoid scanning huge directories */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                return dl[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            File sd = new File(d, gensym());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (sd.mkdir()) return sd.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /** Construct a string that does not name a file in the given directory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private static String findNon(String dir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        File d = new File(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        String[] x = new String[] { "foo", "bar", "baz" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        for (int i = 0; i < x.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            File f = new File(d, x[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (!f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                return x[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        for (int i = 0; i < 1024; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            String n = "xx" + Integer.toString(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            File f = new File(d, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (!f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        throw new RuntimeException("Can't find a non-existent file in " + dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /** Ensure that the named file does not exist */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public static void ensureNon(String fn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if ((new File(fn)).exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new RuntimeException("Test path " + fn + " exists");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /** Tell whether the given character is a "slash" on this platform */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private static boolean isSlash(char x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (x == File.separatorChar) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (win32 && (x == '/')) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Trim trailing slashes from the given string, but leave singleton slashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * alone (they denote root directories)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private static String trimTrailingSlashes(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (n == 0) return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        while ((n > 0) && isSlash(s.charAt(n))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if ((n >= 1) && s.charAt(n - 1) == ':') break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return s.substring(0, n + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /** Concatenate two paths, trimming slashes as needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private static String pathConcat(String a, String b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (a.length() == 0) return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (b.length() == 0) return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (isSlash(a.charAt(a.length() - 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            || isSlash(b.charAt(0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            || (win32 && (a.charAt(a.length() - 1) == ':'))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return a + b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return a + File.separatorChar + b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /** Hash table of input pathnames, used to detect duplicates */
21824
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
   249
    private static Hashtable<String, String> checked = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Check the given pathname.  Its canonical pathname should be the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * answer.  If the path names a file that exists and is readable, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * FileInputStream and RandomAccessFile should both be able to open it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public static void check(String answer, String path) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        String ans = trimTrailingSlashes(answer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (path.length() == 0) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (checked.get(path) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            System.err.println("DUP " + path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        checked.put(path, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        String cpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            File f = new File(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            cpath = f.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (f.exists() && f.isFile() && f.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                InputStream in = new FileInputStream(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                RandomAccessFile raf = new RandomAccessFile(path, "r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                raf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            System.err.println(ans + " <-- " + path + " ==> " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (debug) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            else throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (cpath.equals(ans)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            System.err.println(ans + " <== " + path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            System.err.println(ans + " <-- " + path + " ==> " + cpath + " MISMATCH");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (!debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                throw new RuntimeException("Mismatch: " + path + " ==> " + cpath +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                           ", should be " + ans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * The following three mutually-recursive methods generate and check a tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * of filenames of arbitrary depth.  Each method has (at least) these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * arguments:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *     int depth         Remaining tree depth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *     boolean create    Controls whether test files and directories
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *                       will be created as needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *     String ans        Expected answer for the check method (above)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *     String ask        Input pathname to be passed to the check method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /** Check a single slash case, plus its children */
21824
123a828f30dc 8028631: Improve the test coverage to the pathname handling on unix-like platforms
dxu
parents: 19611
diff changeset
   307
    private static void checkSlash(int depth, boolean create,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                  String ans, String ask, String slash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        check(ans, ask + slash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        checkNames(depth, create,
17924
c4e51dc609b1 8015628: Test Failure in closed/java/io/pathNames/GeneralSolaris.java
dxu
parents: 17726
diff changeset
   313
                   ans.endsWith(File.separator) ? ans : ans + File.separator,
c4e51dc609b1 8015628: Test Failure in closed/java/io/pathNames/GeneralSolaris.java
dxu
parents: 17726
diff changeset
   314
                   ask + slash);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /** Check slash cases for the given ask string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public static void checkSlashes(int depth, boolean create,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                    String ans, String ask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        check(ans, ask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (depth == 0) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        checkSlash(depth, create, ans, ask, "/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        checkSlash(depth, create, ans, ask, "//");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        checkSlash(depth, create, ans, ask, "///");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (win32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            checkSlash(depth, create, ans, ask, "\\");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            checkSlash(depth, create, ans, ask, "\\\\");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            checkSlash(depth, create, ans, ask, "\\/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            checkSlash(depth, create, ans, ask, "/\\");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            checkSlash(depth, create, ans, ask, "\\\\\\");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /** Check name cases for the given ask string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public static void checkNames(int depth, boolean create,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                  String ans, String ask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        int d = depth - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        File f = new File(ans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        String n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        /* Normal name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (f.exists()) {
19611
0c8d4fbae4f2 4792059: test/java/io/pathNames/GeneralSolaris.java fails on symbolic links
dxu
parents: 17924
diff changeset
   350
            if (Files.isDirectory(f.toPath(), LinkOption.NOFOLLOW_LINKS) && f.list() != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                if ((n = findSomeFile(ans, create)) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    checkSlashes(d, create, ans + n, ask + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                if ((n = findSomeDir(ans, create)) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    checkSlashes(d, create, ans + n, ask + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            n = findNon(ans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            checkSlashes(d, create, ans + n, ask + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            n = "foo" + depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            checkSlashes(d, create, ans + n, ask + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        /* "." */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        checkSlashes(d, create, trimTrailingSlashes(ans), ask + ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        /* ".." */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if ((n = f.getParent()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            String n2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (win32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                && ((n2 = f.getParentFile().getParent()) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                && n2.equals("\\\\")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                /* Win32 resolves \\foo\bar\.. to \\foo\bar */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                checkSlashes(d, create, ans, ask + "..");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                checkSlashes(d, create, n, ask + "..");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            if (win32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                checkSlashes(d, create, ans, ask + "..");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                // Fix for 4237875. We must ensure that we are sufficiently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                // deep in the path hierarchy to test parents this high up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                File thisPath = new File(ask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                File nextPath = new File(ask + "..");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                if (!thisPath.getCanonicalPath().equals(nextPath.getCanonicalPath()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    checkSlashes(d, create, ans + "..", ask + "..");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
}