test/jdk/java/io/File/Cons.java
changeset 47216 71c04702a3d5
parent 5506 202f599c92aa
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 1998, 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 4168988
       
    26    @summary Basic File constructor tests
       
    27  */
       
    28 
       
    29 
       
    30 import java.io.*;
       
    31 import java.util.ArrayList;
       
    32 
       
    33 
       
    34 public class Cons {
       
    35 
       
    36     private static boolean debug = false;
       
    37     private static boolean old = false;
       
    38     private static boolean win32 = (File.separatorChar == '\\');
       
    39 
       
    40 
       
    41     private static String cvt(String s) {
       
    42         if (s == null) return s;
       
    43         if (win32) return s.replace('/', '\\');
       
    44         return s;
       
    45     }
       
    46 
       
    47     private static String[] slashPerms(String s) {
       
    48         if (!win32) return new String[] { s };
       
    49         if (s == null) return new String[] { s };
       
    50         int i = s.indexOf('/');
       
    51         if (i < 0) return new String[] { s };
       
    52         ArrayList a = new ArrayList();
       
    53         String p1 = s.substring(0, i);
       
    54         String[] p2 = slashPerms(s.substring(i + 1));
       
    55         for (int j = 0; j < p2.length; j++)
       
    56             a.add(p1 + '/' + p2[j]);
       
    57         for (int j = 0; j < p2.length; j++)
       
    58             a.add(p1 + '\\' + p2[j]);
       
    59         return (String[])(a.toArray(new String[a.size()]));
       
    60     }
       
    61 
       
    62 
       
    63     static class F extends File {
       
    64         String exp;
       
    65 
       
    66         public F(String path) {
       
    67             super(path);
       
    68             this.exp = cons(path);
       
    69         }
       
    70 
       
    71         public F(String parent, String child) {
       
    72             super(parent, child);
       
    73             this.exp = cons(parent, child);
       
    74         }
       
    75 
       
    76         public F(F parent, String child) {
       
    77             super(parent, child);
       
    78             if (parent == null) this.exp = cons((String)null, child);
       
    79             else this.exp = cons(parent, child);
       
    80         }
       
    81 
       
    82     }
       
    83 
       
    84 
       
    85     private static String nos(String s) {
       
    86         if (s == null) return "null";
       
    87         else return "\"" + s + "\"";
       
    88     }
       
    89 
       
    90     private static void ok(String ans, String exp) {
       
    91         System.err.println(nos(ans) + " <== " + exp);
       
    92     }
       
    93 
       
    94     private static void err(String ans, String exp, String got) throws Exception {
       
    95         System.err.println(nos(ans) + " <-- " + exp + " ==> " + nos(got));
       
    96         if (!debug) {
       
    97             throw new Exception("Mismatch: " + exp + " ==> " + nos(got)
       
    98                                 + ", should be " + nos(ans));
       
    99         }
       
   100     }
       
   101 
       
   102     private static void ck(String ans, String exp, String got)
       
   103         throws Exception
       
   104     {
       
   105         if ((got == ans) || ((got != null) && got.equals(ans))) ok(ans, exp);
       
   106         else err(ans, exp, got);
       
   107     }
       
   108 
       
   109     private static String cons(String arg) {
       
   110         return "new File(" + nos(arg) + ")";
       
   111     }
       
   112 
       
   113     private static String cons(String arg1, String arg2) {
       
   114         return "new File(" + nos(arg1) + ", " + nos(arg2) + ")";
       
   115     }
       
   116 
       
   117     private static String cons(F arg1, String arg2) {
       
   118         return "new File(" + arg1.exp + ", " + nos(arg2) + ")";
       
   119     }
       
   120 
       
   121     private static String op(String exp, String opname) {
       
   122         return exp + "." + opname + "()";
       
   123     }
       
   124 
       
   125     private static void ckpnp(F f,
       
   126                               String parent, String name, String path)
       
   127         throws Exception
       
   128     {
       
   129         ck(cvt(path), op(f.exp, "getPath"), f.getPath());
       
   130         ck(cvt(parent), op(f.exp, "getParent"), f.getParent());
       
   131         ck(cvt(name), op(f.exp, "getName"), f.getName());
       
   132     }
       
   133 
       
   134     private static void ck1(String arg,
       
   135                             String parent, String name, String path)
       
   136         throws Exception
       
   137     {
       
   138         String[] parg = slashPerms(arg);
       
   139         for (int i = 0; i < parg.length; i++)
       
   140             ckpnp(new F(parg[i]), parent, name, path);
       
   141     }
       
   142 
       
   143     private static void ck2(String arg1, String arg2,
       
   144                             String parent, String name, String path)
       
   145         throws Exception
       
   146     {
       
   147         String[] parg1 = slashPerms(arg1);
       
   148         String[] parg2 = slashPerms(arg2);
       
   149         for (int i = 0; i < parg1.length; i++)
       
   150             for (int j = 0; j < parg2.length; j++)
       
   151                 ckpnp(new F(parg1[i], parg2[j]), parent, name, path);
       
   152     }
       
   153 
       
   154     private static void ck2f(String arg1, String arg2,
       
   155                              String parent, String name, String path)
       
   156         throws Exception
       
   157     {
       
   158         String[] parg1 = slashPerms(arg1);
       
   159         String[] parg2 = slashPerms(arg2);
       
   160         for (int i = 0; i < parg1.length; i++)
       
   161             for (int j = 0; j < parg2.length; j++) {
       
   162                 F p = (parg1[i] == null) ? null : new F(parg1[i]);
       
   163                 ckpnp(new F(p, parg2[j]), parent, name, path);
       
   164             }
       
   165     }
       
   166 
       
   167 
       
   168     static void testBoth() throws Exception {
       
   169 
       
   170         /* Null/empty constructor cases */
       
   171         ck1("", null, "", "");
       
   172         ck2(null, "", null, "", "");
       
   173         ck2("", "", null, "", "/"); /* ugh */
       
   174         ck2f("", "", null, "", "/");
       
   175         if (!old) {
       
   176             /* throws NullPointerException */
       
   177             ck2f(null, "", null, "", "");
       
   178         }
       
   179 
       
   180         /* Separators-in-pathnames cases */
       
   181         ck1("/", null, "", "/");
       
   182         ck2f("/", "", null, "", "/");
       
   183 
       
   184         /* One-arg constructor cases */
       
   185         ck1("foo", null, "foo", "foo");
       
   186         ck1("/foo", "/", "foo", "/foo");
       
   187         ck1("/foo/bar", "/foo", "bar", "/foo/bar");
       
   188         ck1("foo/bar", "foo", "bar", "foo/bar");
       
   189         if (!old) {
       
   190             ck1("foo/", null, "foo", "foo");
       
   191             ck1("/foo/", "/", "foo", "/foo");
       
   192             ck1("/foo//", "/", "foo", "/foo");
       
   193             ck1("/foo///", "/", "foo", "/foo");
       
   194             ck1("/foo//bar", "/foo", "bar", "/foo/bar");
       
   195             ck1("/foo/bar//", "/foo", "bar", "/foo/bar");
       
   196             ck1("foo//bar", "foo", "bar", "foo/bar");
       
   197             ck1("foo/bar/", "foo", "bar", "foo/bar");
       
   198             ck1("foo/bar//", "foo", "bar", "foo/bar");
       
   199         }
       
   200 
       
   201         /* Two-arg constructor cases, string parent */
       
   202         ck2("foo", "bar", "foo", "bar", "foo/bar");
       
   203         ck2("foo/", "bar", "foo", "bar", "foo/bar");
       
   204         ck2("/foo", "bar", "/foo", "bar", "/foo/bar");
       
   205         ck2("/foo/", "bar", "/foo", "bar", "/foo/bar");
       
   206         if (!old) {
       
   207             ck2("foo//", "bar", "foo", "bar", "foo/bar");
       
   208             ck2("foo", "bar/", "foo", "bar", "foo/bar");
       
   209             ck2("foo", "bar//", "foo", "bar", "foo/bar");
       
   210             ck2("/foo//", "bar", "/foo", "bar", "/foo/bar");
       
   211             ck2("/foo", "bar/", "/foo", "bar", "/foo/bar");
       
   212             ck2("/foo", "bar//", "/foo", "bar", "/foo/bar");
       
   213             ck2("foo", "/bar", "foo", "bar", "foo/bar");
       
   214             ck2("foo", "//bar", "foo", "bar", "foo/bar");
       
   215             ck2("/", "bar", "/", "bar", "/bar");
       
   216             ck2("/", "/bar", "/", "bar", "/bar");
       
   217         }
       
   218 
       
   219         /* Two-arg constructor cases, File parent */
       
   220         ck2f("foo", "bar", "foo", "bar", "foo/bar");
       
   221         ck2f("foo/", "bar", "foo", "bar", "foo/bar");
       
   222         ck2f("/foo", "bar", "/foo", "bar", "/foo/bar");
       
   223         ck2f("/foo/", "bar", "/foo", "bar", "/foo/bar");
       
   224         if (!old) {
       
   225             ck2f("foo//", "bar", "foo", "bar", "foo/bar");
       
   226             ck2f("foo", "bar/", "foo", "bar", "foo/bar");
       
   227             ck2f("foo", "bar//", "foo", "bar", "foo/bar");
       
   228             ck2f("/foo//", "bar", "/foo", "bar", "/foo/bar");
       
   229             ck2f("/foo", "bar/", "/foo", "bar", "/foo/bar");
       
   230             ck2f("/foo", "bar//", "/foo", "bar", "/foo/bar");
       
   231             ck2f("foo", "/bar", "foo", "bar", "foo/bar");
       
   232             ck2f("foo", "//bar", "foo", "bar", "foo/bar");
       
   233         }
       
   234     }
       
   235 
       
   236 
       
   237     static void testUnix() throws Exception {
       
   238 
       
   239         /* Separators-in-pathnames cases */
       
   240         if (!old) {
       
   241             ck1("//", null, "", "/");
       
   242         }
       
   243 
       
   244         /* One-arg constructor cases */
       
   245         if (!old) {
       
   246             ck1("//foo", "/", "foo", "/foo");
       
   247             ck1("///foo", "/", "foo", "/foo");
       
   248             ck1("//foo/bar", "/foo", "bar", "/foo/bar");
       
   249         }
       
   250 
       
   251         /* Two-arg constructors cases, string parent */
       
   252         if (!old) {
       
   253             ck2("//foo", "bar", "/foo", "bar", "/foo/bar");
       
   254         }
       
   255 
       
   256         /* Two-arg constructor cases, File parent */
       
   257         if (!old) {
       
   258             ck2f("//foo", "bar", "/foo", "bar", "/foo/bar");
       
   259         }
       
   260 
       
   261         File f = new File("/foo");
       
   262         if (! f.isAbsolute()) throw new Exception(f + " should be absolute");
       
   263 
       
   264         f = new File("foo");
       
   265         if (f.isAbsolute()) throw new Exception(f + " should not be absolute");
       
   266     }
       
   267 
       
   268 
       
   269     static void testWin32() throws Exception {
       
   270 
       
   271         if (!old) {
       
   272             /* Separators-in-pathnames cases */
       
   273             ck1("//", null, "", "//");
       
   274 
       
   275             /* One-arg constructor cases */
       
   276             ck1("//foo", "//", "foo", "//foo");
       
   277             ck1("///foo", "//", "foo", "//foo");
       
   278             ck1("//foo/bar", "//foo", "bar", "//foo/bar");
       
   279 
       
   280             ck1("z:", null, "", "z:");
       
   281             ck1("z:/", null, "", "z:/");
       
   282             ck1("z://", null, "", "z:/");
       
   283             ck1("z:/foo", "z:/", "foo", "z:/foo");
       
   284             ck1("z:/foo/", "z:/", "foo", "z:/foo");
       
   285             ck1("/z:/foo", "z:/", "foo", "z:/foo");
       
   286             ck1("//z:/foo", "z:/", "foo", "z:/foo");
       
   287             ck1("z:/foo/bar", "z:/foo", "bar", "z:/foo/bar");
       
   288             ck1("z:foo", "z:", "foo", "z:foo");
       
   289 
       
   290             /* Two-arg constructors cases, string parent */
       
   291             ck2("z:", "/", null, "", "z:/");    /* ## ? */
       
   292             ck2("z:", "/foo", "z:/", "foo", "z:/foo");
       
   293             ck2("z:/", "foo", "z:/", "foo", "z:/foo");
       
   294             ck2("z://", "foo", "z:/", "foo", "z:/foo");
       
   295             ck2("z:/", "/foo", "z:/", "foo", "z:/foo");
       
   296             ck2("z:/", "//foo", "z:/", "foo", "z:/foo");
       
   297             ck2("z:/", "foo/", "z:/", "foo", "z:/foo");
       
   298             ck2("//foo", "bar", "//foo", "bar", "//foo/bar");
       
   299 
       
   300             /* Two-arg constructor cases, File parent */
       
   301             ck2f("//foo", "bar", "//foo", "bar", "//foo/bar");
       
   302 
       
   303         }
       
   304     }
       
   305 
       
   306 
       
   307     public static void main(String[] args) throws Exception {
       
   308         old = new File("foo/").getPath().equals("foo/");
       
   309         if (old) System.err.println("** Testing old java.io.File class");
       
   310         testBoth();
       
   311         if (win32) testWin32();
       
   312         else testUnix();
       
   313     }
       
   314 
       
   315 }