Path: blob/master/test/jdk/java/io/File/WinDeviceName.java
41149 views
/*1* Copyright (c) 2006, 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 6176051 485845725@summary Check whether reserved names are handled correctly on Windows26*/2728import java.io.File;29import java.io.IOException;3031public class WinDeviceName {32private static String devnames[] = {33"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4",34"COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2",35"LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",36"CLOCK$"37};38public static void main(String[] args) {39String osName = System.getProperty("os.name");40if (!osName.startsWith("Windows")) {41return;42}4344for (int i = 0; i < devnames.length; i++) {45String names[] = { devnames[i], devnames[i] + ".TXT",46devnames[i].toLowerCase(),47devnames[i].toLowerCase() + ".txt" };4849for (String name : names) {50File f = new File(name);51if (f.isFile()) {52if ("CLOCK$".equals(devnames[i]) &&53(osName.startsWith("Windows 9") ||54osName.startsWith("Windows Me"))) {55//"CLOCK$" is a reserved device name for NT56continue;57}58throw new RuntimeException("isFile() returns true for " +59"Device name " + devnames[i]);60}6162if (!"CLOCK$".equals(devnames[i])) {63try {64System.out.println((new File(name)).getCanonicalPath());65} catch(IOException ie) {66throw new RuntimeException("Fail to get canonical " +67"path for " + name);68}69}70}71}72}73}747576