Path: blob/master/test/jdk/java/io/pathNames/unix/TrailingSlash.java
41152 views
/*1* Copyright (c) 2001, 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 442786225* @summary Ensure that trailing slashes are ignored when opening files26*/2728import java.io.*;293031public class TrailingSlash {3233static PrintStream log = System.err;34static int failures = 0;3536static void check(String what, String fns,37boolean expected, boolean threw)38{39if (expected == threw) {40log.println(" FAIL: new " + what + "(\"" + fns + "\") "41+ (expected ? "failed" : "succeeded"));42failures++;43}44}4546static void go(String fns, boolean fis, boolean raf, boolean fos)47throws IOException48{49boolean threw;5051threw = false;52try {53new FileInputStream(fns).close();54log.println(" FileInputStream okay");55} catch (IOException x) {56log.println(" FileInputStream: " + x);57threw = true;58}59check("FileInputStream", fns, fis, threw);6061threw = false;62try {63new RandomAccessFile(fns, "r").close();64log.println(" RandomAccessFile okay");65} catch (IOException x) {66log.println(" RandomAccessFile: " + x);67threw = true;68}69check("RandomAccessFile", fns, raf, threw);7071threw = false;72try {73new FileOutputStream(fns).close();74log.println(" FileOutputStream okay");75} catch (IOException x) {76log.println(" FileOutputStream: " + x);77threw = true;78}79check("FileOutputStream", fns, fos, threw);8081}8283static void go(String fn, String fns) throws Exception {8485log.println("Test case: " + fns);8687File f = new File(fn);8889f.delete();90if (f.exists())91throw new Exception("Can't delete " + f);9293log.println(" " + fn + " does not exist");94go(fns, false, false, true);9596f.delete();97f.mkdir();98log.println(" " + fn + " is a directory");99go(fns, false, false, false);100101f.delete();102f.createNewFile();103log.println(" " + fn + " is a file");104go(fns, true, true, true);105106}107108public static void main(String[] args) throws Exception {109if (File.separatorChar != '/') {110// This test is only valid on Unix systems111return;112}113114go("xyzzy", "xyzzy");115go("xyzzy", "xyzzy/");116go("xyzzy", "xyzzy//");117118if (failures > 0)119throw new Exception(failures + " failures");120121}122123}124125126