1 /*
2  * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /* @test
25    @bug 4131169 4109131
26    @summary Basic test for getAbsolutePath method
27  */
28 
29 import java.io.*;
30 
31 
32 public class GetAbsolutePath {
33 
34     private static boolean ignoreCase = false;
35 
ck(String path, String ans)36     private static void ck(String path, String ans) throws Exception {
37         File f = new File(path);
38         String p = f.getAbsolutePath();
39         if ((ignoreCase && p.equalsIgnoreCase(ans)) || p.equals(ans))
40             System.err.println(path + " ==> " + p);
41         else
42             throw new Exception(path + ": expected " + ans + ", got " + p);
43     }
44 
testWin32()45     private static void testWin32() throws Exception {
46         String wd = System.getProperty("user.dir");
47         char d;
48         if ((wd.length() > 2) && (wd.charAt(1) == ':')
49             && (wd.charAt(2) == '\\'))
50             d = wd.charAt(0);
51         else
52             throw new Exception("Current directory has no drive");
53         ck("/foo/bar", d + ":\\foo\\bar");
54         ck("\\foo\\bar", d + ":\\foo\\bar");
55         ck("c:\\foo\\bar", "c:\\foo\\bar");
56         ck("c:/foo/bar", "c:\\foo\\bar");
57         ck("\\\\foo\\bar", "\\\\foo\\bar");
58 
59         /* Tricky directory-relative case */
60         d = Character.toLowerCase(d);
61         char z = 0;
62         if (d != 'c') z = 'c';
63         else if (d != 'd') z = 'd';
64         if (z != 0) {
65             File f = new File(z + ":.");
66             if (f.exists()) {
67                 String zwd = f.getCanonicalPath();
68                 ck(z + ":foo", zwd + "\\foo");
69             }
70         }
71 
72         /* Empty path */
73         ck("", wd);
74     }
75 
testUnix()76     private static void testUnix() throws Exception {
77         String wd = System.getProperty("user.dir");
78         ck("foo", wd + "/foo");
79         ck("foo/bar", wd + "/foo/bar");
80         ck("/foo", "/foo");
81         ck("/foo/bar", "/foo/bar");
82 
83         /* Empty path */
84         ck("", wd);
85     }
86 
main(String[] args)87     public static void main(String[] args) throws Exception {
88         if (File.separatorChar == '\\') {
89             ignoreCase = true;
90             testWin32();
91         }
92         if (File.separatorChar == '/') testUnix();
93     }
94 
95 }
96