Path: blob/master/test/jdk/java/io/File/FileMethods.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 4107063 413123725@summary Basic test for new File-returning methods26*/2728import java.io.*;293031public class FileMethods {3233private static void ck(String op, File got, File ans) throws Exception {34if (!got.equals(ans))35throw new Exception(op + " incorrect");36}3738private static void ck(String op, File f, String[] ls, File[] lf)39throws Exception40{41System.err.println("--- " + op);42int n = lf.length;43if (ls.length != n)44throw new Exception("listFiles returned incorrect count");45for (int i = 0; i < n; i++) {46if (ls[i].equals(lf[i].getName())47&& lf[i].getParentFile().equals(f)) {48System.err.println(ls[i] + " ==> " + lf[i]);49} else {50throw new Exception("list mismatch: " + ls[i] + ", " + lf[i]);51}52}53}5455public static void main(String[] args) throws Exception {5657File f;58f = new File("foo/bar");59ck("getParentFile", f.getParentFile(), new File(f.getParent()));6061f = new File(".");62ck("getAbsoluteFile",63f.getAbsoluteFile(), new File(f.getAbsolutePath()));6465ck("getCanonicalFile",66f.getCanonicalFile(), new File(f.getCanonicalPath()));6768f = f.getCanonicalFile();69ck("listFiles", f, f.list(), f.listFiles());7071FilenameFilter ff = new FilenameFilter() {72public boolean accept(File dir, String name) {73return name.endsWith(".class");74}};75ck("listFiles/filtered", f, f.list(ff), f.listFiles(ff));7677FileFilter ff2 = new FileFilter() {78public boolean accept(File f) {79return f.getPath().endsWith(".class");80}};81ck("listFiles/filtered2", f, f.list(ff), f.listFiles(ff2));8283}8485}868788