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