6140N/A/*
6140N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
6140N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6140N/A *
6140N/A * This code is free software; you can redistribute it and/or modify it
6140N/A * under the terms of the GNU General Public License version 2 only, as
6140N/A * published by the Free Software Foundation.
6140N/A *
6140N/A * This code is distributed in the hope that it will be useful, but WITHOUT
6140N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6140N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6140N/A * version 2 for more details (a copy is included in the LICENSE file that
6140N/A * accompanied this code).
6140N/A *
6140N/A * You should have received a copy of the GNU General Public License version
6140N/A * 2 along with this work; if not, write to the Free Software Foundation,
6140N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6140N/A *
6140N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6140N/A * or visit www.oracle.com if you need additional information or have any
6140N/A * questions.
6140N/A */
6140N/A
6140N/A/* @test
6140N/A * @bug 8011128
6140N/A * @summary Test file and directory name limits. This test is primarily
6140N/A * intended to test Files.createDirectory on resolved paths at or around
6140N/A * the short path limit of 248 on Windows.
6140N/A */
6140N/A
6140N/Aimport java.io.IOException;
6140N/Aimport java.nio.file.Files;
6140N/Aimport java.nio.file.Path;
6140N/Aimport java.nio.file.Paths;
6140N/A
6140N/Apublic class NameLimits {
6140N/A
6140N/A static final int MAX_PATH = 255;
6140N/A static final int MIN_PATH = 8; // arbitrarily chosen
6140N/A
6140N/A static Path generatePath(int len) {
6140N/A if (len < MIN_PATH)
6140N/A throw new RuntimeException("Attempting to generate path less than MIN_PATH");
6140N/A StringBuilder sb = new StringBuilder(len);
6140N/A sb.append("name");
6140N/A while (sb.length() < len) {
6140N/A sb.append('X');
6140N/A }
6140N/A return Paths.get(sb.toString());
6140N/A }
6140N/A
6140N/A static boolean tryCreateFile(int len) throws IOException {
6140N/A Path name = generatePath(len);
6140N/A try {
6140N/A Files.createFile(name);
6140N/A } catch (IOException ioe) {
6140N/A System.err.format("Unable to create file of length %d (full path %d), %s%n",
6140N/A name.toString().length(), name.toAbsolutePath().toString().length(), ioe);
6140N/A return false;
6140N/A }
6140N/A Files.delete(name);
6140N/A return true;
6140N/A }
6140N/A
6140N/A static boolean tryCreateDirectory(int len) throws IOException {
6140N/A Path name = generatePath(len);
6140N/A try {
6140N/A Files.createDirectory(name);
6140N/A } catch (IOException ioe) {
6140N/A System.err.format("Unable to create directory of length %d (full path %d), %s%n",
6140N/A name.toString().length(), name.toAbsolutePath().toString().length(), ioe);
6140N/A return false;
6140N/A }
6140N/A Files.delete(name);
6140N/A return true;
6140N/A }
6140N/A
6140N/A public static void main(String[] args) throws Exception {
6140N/A int len;
6140N/A
6140N/A // find the maximum file name if MAX_PATH or less
6140N/A len = MAX_PATH;
6140N/A while (!tryCreateFile(len)) {
6140N/A len--;
6140N/A }
6140N/A System.out.format("Testing createFile on paths %d .. %d%n", MIN_PATH, len);
6140N/A while (len >= MIN_PATH) {
6140N/A if (!tryCreateFile(len--))
6140N/A throw new RuntimeException("Test failed");
6140N/A }
6140N/A
6140N/A // find the maximum directory name if MAX_PATH or less
6140N/A len = MAX_PATH;
6140N/A while (!tryCreateDirectory(len)) {
6140N/A len--;
6140N/A }
6140N/A System.out.format("Testing createDirectory on paths %d .. %d%n", MIN_PATH, len);
6140N/A while (len >= MIN_PATH) {
6140N/A if (!tryCreateDirectory(len--))
6140N/A throw new RuntimeException("Test failed");
6140N/A }
6140N/A }
6140N/A}