0N/A/*
2362N/A * Copyright (c) 1997, 1998, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A @bug 4027914
0N/A @summary Check getParent's handling of root directories
0N/A */
0N/A
0N/Aimport java.io.File;
0N/A
0N/A
0N/Apublic class GetParent {
0N/A
0N/A static void check(String path, String[] parents) throws Exception {
0N/A File f = new File(path);
0N/A String p;
0N/A System.err.print(path + ":");
0N/A for (int i = 0; i < parents.length; i++) {
0N/A p = f.getParent();
0N/A System.err.print(" (" + p + ")");
0N/A if (p == null)
0N/A throw new Exception("No parent for " + f);
0N/A if (! p.equals(parents[i]))
0N/A throw new Exception("Wrong parent for " + f
0N/A + ": Expected " + parents[i]
0N/A + ", got " + p);
0N/A f = new File(p);
0N/A }
0N/A if (f.getParent() != null)
0N/A throw new Exception("Too many parents for " + path);
0N/A System.err.println();
0N/A }
0N/A
0N/A static void testUnix() throws Exception {
0N/A check("foo", new String[] { });
0N/A check("./foo", new String[] { "." });
0N/A check("foo/bar/baz", new String[] { "foo/bar", "foo" });
0N/A check("../../foo", new String[] { "../..", ".." });
0N/A check("foo//bar", new String[] { "foo" });
0N/A check("/foo/bar/baz.gorp",
0N/A new String[] { "/foo/bar", "/foo", "/" });
0N/A }
0N/A
0N/A static void testWin32() throws Exception {
0N/A System.err.println("Win32");
0N/A check("foo", new String[] { });
0N/A check(".\\foo", new String[] { "." });
0N/A check("foo\\bar\\baz", new String[] { "foo\\bar", "foo" });
0N/A check("..\\..\\foo", new String[] { "..\\..", ".." });
0N/A check("c:\\foo\\bar\\baz.xxx",
0N/A new String[] { "c:\\foo\\bar", "c:\\foo", "c:\\" });
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A if (File.separatorChar == '/') testUnix();
0N/A if (File.separatorChar == '\\') testWin32();
0N/A }
0N/A
0N/A}