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