J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-1998 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | @bug 4027914 |
| 26 | @summary Check getParent's handling of root directories |
| 27 | */ |
| 28 | |
| 29 | import java.io.File; |
| 30 | |
| 31 | |
| 32 | public class GetParent { |
| 33 | |
| 34 | static void check(String path, String[] parents) throws Exception { |
| 35 | File f = new File(path); |
| 36 | String p; |
| 37 | System.err.print(path + ":"); |
| 38 | for (int i = 0; i < parents.length; i++) { |
| 39 | p = f.getParent(); |
| 40 | System.err.print(" (" + p + ")"); |
| 41 | if (p == null) |
| 42 | throw new Exception("No parent for " + f); |
| 43 | if (! p.equals(parents[i])) |
| 44 | throw new Exception("Wrong parent for " + f |
| 45 | + ": Expected " + parents[i] |
| 46 | + ", got " + p); |
| 47 | f = new File(p); |
| 48 | } |
| 49 | if (f.getParent() != null) |
| 50 | throw new Exception("Too many parents for " + path); |
| 51 | System.err.println(); |
| 52 | } |
| 53 | |
| 54 | static void testUnix() throws Exception { |
| 55 | check("foo", new String[] { }); |
| 56 | check("./foo", new String[] { "." }); |
| 57 | check("foo/bar/baz", new String[] { "foo/bar", "foo" }); |
| 58 | check("../../foo", new String[] { "../..", ".." }); |
| 59 | check("foo//bar", new String[] { "foo" }); |
| 60 | check("/foo/bar/baz.gorp", |
| 61 | new String[] { "/foo/bar", "/foo", "/" }); |
| 62 | } |
| 63 | |
| 64 | static void testWin32() throws Exception { |
| 65 | System.err.println("Win32"); |
| 66 | check("foo", new String[] { }); |
| 67 | check(".\\foo", new String[] { "." }); |
| 68 | check("foo\\bar\\baz", new String[] { "foo\\bar", "foo" }); |
| 69 | check("..\\..\\foo", new String[] { "..\\..", ".." }); |
| 70 | check("c:\\foo\\bar\\baz.xxx", |
| 71 | new String[] { "c:\\foo\\bar", "c:\\foo", "c:\\" }); |
| 72 | } |
| 73 | |
| 74 | public static void main(String[] args) throws Exception { |
| 75 | if (File.separatorChar == '/') testUnix(); |
| 76 | if (File.separatorChar == '\\') testWin32(); |
| 77 | } |
| 78 | |
| 79 | } |