Path: blob/master/test/jdk/java/io/File/Cons.java
41149 views
/*1* Copyright (c) 1998, 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 416898825@summary Basic File constructor tests26*/272829import java.io.*;30import java.util.ArrayList;313233public class Cons {3435private static boolean debug = false;36private static boolean old = false;37private static boolean win32 = (File.separatorChar == '\\');383940private static String cvt(String s) {41if (s == null) return s;42if (win32) return s.replace('/', '\\');43return s;44}4546private static String[] slashPerms(String s) {47if (!win32) return new String[] { s };48if (s == null) return new String[] { s };49int i = s.indexOf('/');50if (i < 0) return new String[] { s };51ArrayList a = new ArrayList();52String p1 = s.substring(0, i);53String[] p2 = slashPerms(s.substring(i + 1));54for (int j = 0; j < p2.length; j++)55a.add(p1 + '/' + p2[j]);56for (int j = 0; j < p2.length; j++)57a.add(p1 + '\\' + p2[j]);58return (String[])(a.toArray(new String[a.size()]));59}606162static class F extends File {63String exp;6465public F(String path) {66super(path);67this.exp = cons(path);68}6970public F(String parent, String child) {71super(parent, child);72this.exp = cons(parent, child);73}7475public F(F parent, String child) {76super(parent, child);77if (parent == null) this.exp = cons((String)null, child);78else this.exp = cons(parent, child);79}8081}828384private static String nos(String s) {85if (s == null) return "null";86else return "\"" + s + "\"";87}8889private static void ok(String ans, String exp) {90System.err.println(nos(ans) + " <== " + exp);91}9293private static void err(String ans, String exp, String got) throws Exception {94System.err.println(nos(ans) + " <-- " + exp + " ==> " + nos(got));95if (!debug) {96throw new Exception("Mismatch: " + exp + " ==> " + nos(got)97+ ", should be " + nos(ans));98}99}100101private static void ck(String ans, String exp, String got)102throws Exception103{104if ((got == ans) || ((got != null) && got.equals(ans))) ok(ans, exp);105else err(ans, exp, got);106}107108private static String cons(String arg) {109return "new File(" + nos(arg) + ")";110}111112private static String cons(String arg1, String arg2) {113return "new File(" + nos(arg1) + ", " + nos(arg2) + ")";114}115116private static String cons(F arg1, String arg2) {117return "new File(" + arg1.exp + ", " + nos(arg2) + ")";118}119120private static String op(String exp, String opname) {121return exp + "." + opname + "()";122}123124private static void ckpnp(F f,125String parent, String name, String path)126throws Exception127{128ck(cvt(path), op(f.exp, "getPath"), f.getPath());129ck(cvt(parent), op(f.exp, "getParent"), f.getParent());130ck(cvt(name), op(f.exp, "getName"), f.getName());131}132133private static void ck1(String arg,134String parent, String name, String path)135throws Exception136{137String[] parg = slashPerms(arg);138for (int i = 0; i < parg.length; i++)139ckpnp(new F(parg[i]), parent, name, path);140}141142private static void ck2(String arg1, String arg2,143String parent, String name, String path)144throws Exception145{146String[] parg1 = slashPerms(arg1);147String[] parg2 = slashPerms(arg2);148for (int i = 0; i < parg1.length; i++)149for (int j = 0; j < parg2.length; j++)150ckpnp(new F(parg1[i], parg2[j]), parent, name, path);151}152153private static void ck2f(String arg1, String arg2,154String parent, String name, String path)155throws Exception156{157String[] parg1 = slashPerms(arg1);158String[] parg2 = slashPerms(arg2);159for (int i = 0; i < parg1.length; i++)160for (int j = 0; j < parg2.length; j++) {161F p = (parg1[i] == null) ? null : new F(parg1[i]);162ckpnp(new F(p, parg2[j]), parent, name, path);163}164}165166167static void testBoth() throws Exception {168169/* Null/empty constructor cases */170ck1("", null, "", "");171ck2(null, "", null, "", "");172ck2("", "", null, "", "/"); /* ugh */173ck2f("", "", null, "", "/");174if (!old) {175/* throws NullPointerException */176ck2f(null, "", null, "", "");177}178179/* Separators-in-pathnames cases */180ck1("/", null, "", "/");181ck2f("/", "", null, "", "/");182183/* One-arg constructor cases */184ck1("foo", null, "foo", "foo");185ck1("/foo", "/", "foo", "/foo");186ck1("/foo/bar", "/foo", "bar", "/foo/bar");187ck1("foo/bar", "foo", "bar", "foo/bar");188if (!old) {189ck1("foo/", null, "foo", "foo");190ck1("/foo/", "/", "foo", "/foo");191ck1("/foo//", "/", "foo", "/foo");192ck1("/foo///", "/", "foo", "/foo");193ck1("/foo//bar", "/foo", "bar", "/foo/bar");194ck1("/foo/bar//", "/foo", "bar", "/foo/bar");195ck1("foo//bar", "foo", "bar", "foo/bar");196ck1("foo/bar/", "foo", "bar", "foo/bar");197ck1("foo/bar//", "foo", "bar", "foo/bar");198}199200/* Two-arg constructor cases, string parent */201ck2("foo", "bar", "foo", "bar", "foo/bar");202ck2("foo/", "bar", "foo", "bar", "foo/bar");203ck2("/foo", "bar", "/foo", "bar", "/foo/bar");204ck2("/foo/", "bar", "/foo", "bar", "/foo/bar");205if (!old) {206ck2("foo//", "bar", "foo", "bar", "foo/bar");207ck2("foo", "bar/", "foo", "bar", "foo/bar");208ck2("foo", "bar//", "foo", "bar", "foo/bar");209ck2("/foo//", "bar", "/foo", "bar", "/foo/bar");210ck2("/foo", "bar/", "/foo", "bar", "/foo/bar");211ck2("/foo", "bar//", "/foo", "bar", "/foo/bar");212ck2("foo", "/bar", "foo", "bar", "foo/bar");213ck2("foo", "//bar", "foo", "bar", "foo/bar");214ck2("/", "bar", "/", "bar", "/bar");215ck2("/", "/bar", "/", "bar", "/bar");216}217218/* Two-arg constructor cases, File parent */219ck2f("foo", "bar", "foo", "bar", "foo/bar");220ck2f("foo/", "bar", "foo", "bar", "foo/bar");221ck2f("/foo", "bar", "/foo", "bar", "/foo/bar");222ck2f("/foo/", "bar", "/foo", "bar", "/foo/bar");223if (!old) {224ck2f("foo//", "bar", "foo", "bar", "foo/bar");225ck2f("foo", "bar/", "foo", "bar", "foo/bar");226ck2f("foo", "bar//", "foo", "bar", "foo/bar");227ck2f("/foo//", "bar", "/foo", "bar", "/foo/bar");228ck2f("/foo", "bar/", "/foo", "bar", "/foo/bar");229ck2f("/foo", "bar//", "/foo", "bar", "/foo/bar");230ck2f("foo", "/bar", "foo", "bar", "foo/bar");231ck2f("foo", "//bar", "foo", "bar", "foo/bar");232}233}234235236static void testUnix() throws Exception {237238/* Separators-in-pathnames cases */239if (!old) {240ck1("//", null, "", "/");241}242243/* One-arg constructor cases */244if (!old) {245ck1("//foo", "/", "foo", "/foo");246ck1("///foo", "/", "foo", "/foo");247ck1("//foo/bar", "/foo", "bar", "/foo/bar");248}249250/* Two-arg constructors cases, string parent */251if (!old) {252ck2("//foo", "bar", "/foo", "bar", "/foo/bar");253}254255/* Two-arg constructor cases, File parent */256if (!old) {257ck2f("//foo", "bar", "/foo", "bar", "/foo/bar");258}259260File f = new File("/foo");261if (! f.isAbsolute()) throw new Exception(f + " should be absolute");262263f = new File("foo");264if (f.isAbsolute()) throw new Exception(f + " should not be absolute");265}266267268static void testWin32() throws Exception {269270if (!old) {271/* Separators-in-pathnames cases */272ck1("//", null, "", "//");273274/* One-arg constructor cases */275ck1("//foo", "//", "foo", "//foo");276ck1("///foo", "//", "foo", "//foo");277ck1("//foo/bar", "//foo", "bar", "//foo/bar");278279ck1("z:", null, "", "z:");280ck1("z:/", null, "", "z:/");281ck1("z://", null, "", "z:/");282ck1("z:/foo", "z:/", "foo", "z:/foo");283ck1("z:/foo/", "z:/", "foo", "z:/foo");284ck1("/z:/foo", "z:/", "foo", "z:/foo");285ck1("//z:/foo", "z:/", "foo", "z:/foo");286ck1("z:/foo/bar", "z:/foo", "bar", "z:/foo/bar");287ck1("z:foo", "z:", "foo", "z:foo");288289/* Two-arg constructors cases, string parent */290ck2("z:", "/", null, "", "z:/"); /* ## ? */291ck2("z:", "/foo", "z:/", "foo", "z:/foo");292ck2("z:/", "foo", "z:/", "foo", "z:/foo");293ck2("z://", "foo", "z:/", "foo", "z:/foo");294ck2("z:/", "/foo", "z:/", "foo", "z:/foo");295ck2("z:/", "//foo", "z:/", "foo", "z:/foo");296ck2("z:/", "foo/", "z:/", "foo", "z:/foo");297ck2("//foo", "bar", "//foo", "bar", "//foo/bar");298299/* Two-arg constructor cases, File parent */300ck2f("//foo", "bar", "//foo", "bar", "//foo/bar");301302}303}304305306public static void main(String[] args) throws Exception {307old = new File("foo/").getPath().equals("foo/");308if (old) System.err.println("** Testing old java.io.File class");309testBoth();310if (win32) testWin32();311else testUnix();312}313314}315316317