Path: blob/master/test/jdk/java/io/File/GetAbsolutePath.java
41149 views
/*1* Copyright (c) 1998, 2001, 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 4131169 410913125@summary Basic test for getAbsolutePath method26*/2728import java.io.*;293031public class GetAbsolutePath {3233private static boolean ignoreCase = false;3435private static void ck(String path, String ans) throws Exception {36File f = new File(path);37String p = f.getAbsolutePath();38if ((ignoreCase && p.equalsIgnoreCase(ans)) || p.equals(ans))39System.err.println(path + " ==> " + p);40else41throw new Exception(path + ": expected " + ans + ", got " + p);42}4344private static void testWin32() throws Exception {45String wd = System.getProperty("user.dir");46char d;47if ((wd.length() > 2) && (wd.charAt(1) == ':')48&& (wd.charAt(2) == '\\'))49d = wd.charAt(0);50else51throw new Exception("Current directory has no drive");52ck("/foo/bar", d + ":\\foo\\bar");53ck("\\foo\\bar", d + ":\\foo\\bar");54ck("c:\\foo\\bar", "c:\\foo\\bar");55ck("c:/foo/bar", "c:\\foo\\bar");56ck("\\\\foo\\bar", "\\\\foo\\bar");5758/* Tricky directory-relative case */59d = Character.toLowerCase(d);60char z = 0;61if (d != 'c') z = 'c';62else if (d != 'd') z = 'd';63if (z != 0) {64File f = new File(z + ":.");65if (f.exists()) {66String zwd = f.getCanonicalPath();67ck(z + ":foo", zwd + "\\foo");68}69}7071/* Empty path */72ck("", wd);73}7475private static void testUnix() throws Exception {76String wd = System.getProperty("user.dir");77ck("foo", wd + "/foo");78ck("foo/bar", wd + "/foo/bar");79ck("/foo", "/foo");80ck("/foo/bar", "/foo/bar");8182/* Empty path */83ck("", wd);84}8586public static void main(String[] args) throws Exception {87if (File.separatorChar == '\\') {88ignoreCase = true;89testWin32();90}91if (File.separatorChar == '/') testUnix();92}9394}959697