J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2001 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 4427862 |
| 26 | * @summary Ensure that trailing slashes are ignored when opening files |
| 27 | */ |
| 28 | |
| 29 | import java.io.*; |
| 30 | |
| 31 | |
| 32 | public class TrailingSlash { |
| 33 | |
| 34 | static PrintStream log = System.err; |
| 35 | static int failures = 0; |
| 36 | |
| 37 | static void check(String what, String fns, |
| 38 | boolean expected, boolean threw) |
| 39 | { |
| 40 | if (expected == threw) { |
| 41 | log.println(" FAIL: new " + what + "(\"" + fns + "\") " |
| 42 | + (expected ? "failed" : "succeeded")); |
| 43 | failures++; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | static void go(String fns, boolean fis, boolean raf, boolean fos) |
| 48 | throws IOException |
| 49 | { |
| 50 | boolean threw; |
| 51 | |
| 52 | threw = false; |
| 53 | try { |
| 54 | new FileInputStream(fns).close(); |
| 55 | log.println(" FileInputStream okay"); |
| 56 | } catch (IOException x) { |
| 57 | log.println(" FileInputStream: " + x); |
| 58 | threw = true; |
| 59 | } |
| 60 | check("FileInputStream", fns, fis, threw); |
| 61 | |
| 62 | threw = false; |
| 63 | try { |
| 64 | new RandomAccessFile(fns, "r").close(); |
| 65 | log.println(" RandomAccessFile okay"); |
| 66 | } catch (IOException x) { |
| 67 | log.println(" RandomAccessFile: " + x); |
| 68 | threw = true; |
| 69 | } |
| 70 | check("RandomAccessFile", fns, raf, threw); |
| 71 | |
| 72 | threw = false; |
| 73 | try { |
| 74 | new FileOutputStream(fns).close(); |
| 75 | log.println(" FileOutputStream okay"); |
| 76 | } catch (IOException x) { |
| 77 | log.println(" FileOutputStream: " + x); |
| 78 | threw = true; |
| 79 | } |
| 80 | check("FileOutputStream", fns, fos, threw); |
| 81 | |
| 82 | } |
| 83 | |
| 84 | static void go(String fn, String fns) throws Exception { |
| 85 | |
| 86 | log.println("Test case: " + fns); |
| 87 | |
| 88 | File f = new File(fn); |
| 89 | |
| 90 | f.delete(); |
| 91 | if (f.exists()) |
| 92 | throw new Exception("Can't delete " + f); |
| 93 | |
| 94 | log.println(" " + fn + " does not exist"); |
| 95 | go(fns, false, false, true); |
| 96 | |
| 97 | f.delete(); |
| 98 | f.mkdir(); |
| 99 | log.println(" " + fn + " is a directory"); |
| 100 | go(fns, false, false, false); |
| 101 | |
| 102 | f.delete(); |
| 103 | f.createNewFile(); |
| 104 | log.println(" " + fn + " is a file"); |
| 105 | go(fns, true, true, true); |
| 106 | |
| 107 | } |
| 108 | |
| 109 | public static void main(String[] args) throws Exception { |
| 110 | if (File.separatorChar != '/') { |
| 111 | // This test is only valid on Unix systems |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | go("xyzzy", "xyzzy"); |
| 116 | go("xyzzy", "xyzzy/"); |
| 117 | go("xyzzy", "xyzzy//"); |
| 118 | |
| 119 | if (failures > 0) |
| 120 | throw new Exception(failures + " failures"); |
| 121 | |
| 122 | } |
| 123 | |
| 124 | } |