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 4107063 4131237 26 @summary Basic test for new File-returning methods 27 */ 28 29import java.io.*; 30 31 32public class FileMethods { 33 34 private static void ck(String op, File got, File ans) throws Exception { 35 if (!got.equals(ans)) 36 throw new Exception(op + " incorrect"); 37 } 38 39 private static void ck(String op, File f, String[] ls, File[] lf) 40 throws Exception 41 { 42 System.err.println("--- " + op); 43 int n = lf.length; 44 if (ls.length != n) 45 throw new Exception("listFiles returned incorrect count"); 46 for (int i = 0; i < n; i++) { 47 if (ls[i].equals(lf[i].getName()) 48 && lf[i].getParentFile().equals(f)) { 49 System.err.println(ls[i] + " ==> " + lf[i]); 50 } else { 51 throw new Exception("list mismatch: " + ls[i] + ", " + lf[i]); 52 } 53 } 54 } 55 56 public static void main(String[] args) throws Exception { 57 58 File f; 59 f = new File("foo/bar"); 60 ck("getParentFile", f.getParentFile(), new File(f.getParent())); 61 62 f = new File("."); 63 ck("getAbsoluteFile", 64 f.getAbsoluteFile(), new File(f.getAbsolutePath())); 65 66 ck("getCanonicalFile", 67 f.getCanonicalFile(), new File(f.getCanonicalPath())); 68 69 f = f.getCanonicalFile(); 70 ck("listFiles", f, f.list(), f.listFiles()); 71 72 FilenameFilter ff = new FilenameFilter() { 73 public boolean accept(File dir, String name) { 74 return name.endsWith(".class"); 75 }}; 76 ck("listFiles/filtered", f, f.list(ff), f.listFiles(ff)); 77 78 FileFilter ff2 = new FileFilter() { 79 public boolean accept(File f) { 80 return f.getPath().endsWith(".class"); 81 }}; 82 ck("listFiles/filtered2", f, f.list(ff), f.listFiles(ff2)); 83 84 } 85 86} 87