Path: blob/master/test/jdk/java/nio/file/Files/NameLimits.java
41153 views
/*1* Copyright (c) 2013, 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/* @test24* @bug 801112825* @summary Test file and directory name limits. This test is primarily26* intended to test Files.createDirectory on resolved paths at or around27* the short path limit of 248 on Windows.28*/2930import java.io.IOException;31import java.nio.file.Files;32import java.nio.file.Path;33import java.nio.file.Paths;3435public class NameLimits {3637static final int MAX_PATH = 255;38static final int MIN_PATH = 8; // arbitrarily chosen3940static Path generatePath(int len) {41if (len < MIN_PATH)42throw new RuntimeException("Attempting to generate path less than MIN_PATH");43StringBuilder sb = new StringBuilder(len);44sb.append("name");45while (sb.length() < len) {46sb.append('X');47}48return Paths.get(sb.toString());49}5051static boolean tryCreateFile(int len) throws IOException {52Path name = generatePath(len);53try {54Files.createFile(name);55} catch (IOException ioe) {56System.err.format("Unable to create file of length %d (full path %d), %s%n",57name.toString().length(), name.toAbsolutePath().toString().length(), ioe);58return false;59}60Files.delete(name);61return true;62}6364static boolean tryCreateDirectory(int len) throws IOException {65Path name = generatePath(len);66try {67Files.createDirectory(name);68} catch (IOException ioe) {69System.err.format("Unable to create directory of length %d (full path %d), %s%n",70name.toString().length(), name.toAbsolutePath().toString().length(), ioe);71return false;72}73Files.delete(name);74return true;75}7677public static void main(String[] args) throws Exception {78int len;7980// find the maximum file name if MAX_PATH or less81len = MAX_PATH;82while (!tryCreateFile(len)) {83len--;84}85System.out.format("Testing createFile on paths %d .. %d%n", MIN_PATH, len);86while (len >= MIN_PATH) {87if (!tryCreateFile(len--))88throw new RuntimeException("Test failed");89}9091// find the maximum directory name if MAX_PATH or less92len = MAX_PATH;93while (!tryCreateDirectory(len)) {94len--;95}96System.out.format("Testing createDirectory on paths %d .. %d%n", MIN_PATH, len);97while (len >= MIN_PATH) {98if (!tryCreateDirectory(len--))99throw new RuntimeException("Test failed");100}101}102}103104105