1 /*************************************************************************
2 /* File.java -- Tests File class
4 /* Copyright (c) 1998 Free Software Foundation, Inc.
5 /* Written by Aaron M. Renn (arenn@urbanophile.com)
7 /* This program is free software; you can redistribute it and/or modify
8 /* it under the terms of the GNU General Public License as published
9 /* by the Free Software Foundation, version 2. (see COPYING)
11 /* This program is distributed in the hope that it will be useful, but
12 /* WITHOUT ANY WARRANTY; without even the implied warranty of
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 /* GNU General Public License for more details.
16 /* You should have received a copy of the GNU General Public License
17 /* along with this program; if not, write to the Free Software Foundation
18 /* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 /*************************************************************************/
26 static PrintWriter pw
;
29 dumpFile(File f
) throws IOException
31 pw
.println("Name: " + f
.getName());
32 pw
.println("Parent: " + f
.getParent());
33 pw
.println("Path: " + f
.getPath());
34 pw
.println("Absolute: " + f
.getAbsolutePath());
35 pw
.println("Canonical: " + f
.getCanonicalPath());
36 pw
.println("String: " + f
.toString());
40 deleteTempDirs() throws IOException
42 File f
= new File("tempfiletest/tmp/tmp");
44 throw new IOException("Could not delete " + f
.getPath());
46 f
= new File("tempfiletest/tmp");
48 throw new IOException("Could not delete " + f
.getPath());
50 f
= new File("tempfiletest/");
52 throw new IOException("Could not delete " + f
.getPath());
55 public static void main(String
[] argv
)
57 System
.out
.println("Started test of File");
59 // This test writes a bunch of things to a file. That file should
60 // be "diff-ed" against one generated when this test is run against
61 // the JDK java.io package.
62 System
.out
.println("Test 1: Path Operations Test");
65 pw
= new PrintWriter(new OutputStreamWriter(new
66 FileOutputStream("./file-test.out")));
68 dumpFile(new File("/"));
69 dumpFile(new File("~arenn/foo"));
70 dumpFile(new File("foo"));
71 dumpFile(new File("../../../jcl/"));
72 dumpFile(new File("/tmp/bar.txt"));
73 dumpFile(new File("/usr"));
74 dumpFile(new File("../.."));
77 File f
= new File("gimme");
79 throw new IOException("isAbsolute() failed");
81 f
= new File("/etc/services");
83 throw new IOException("isFile() failed");
85 pw
.println("length: " + f
.length());
86 pw
.println("lastModified: " + f
.lastModified());
87 pw
.println("hashCode: " + f
.hashCode());
89 f
= new File("/etc/");
91 throw new IOException("isDirectory() failed");
94 System
.out
.println("PASSED: Conditionally Passed Path Operations Test");
98 System
.out
.println("FAILED: Path Operations Test: " + e
);
102 System
.out
.println("Test 2: File/Directory Manipulation Test");
105 File f
= new File("filetest");
107 throw new IOException("The filetest directory doesn't exist");
109 String
[] filelist
= f
.list();
110 if ((filelist
== null) || (filelist
.length
!= 3))
111 throw new IOException("Failed to read directory list");
113 for (int i
= 0; i
< filelist
.length
; i
++)
114 System
.out
.println(filelist
[i
]);
116 System
.out
.println("Listing /etc/");
117 f
= new File("/etc/");
119 for (int i
= 0; i
< filelist
.length
; i
++)
120 System
.out
.println(filelist
[i
]);
122 f
= new File("tempfiletest/tmp/tmp");
124 throw new IOException("Failed to create directories: " + f
.getPath());
128 f
= new File("tempfiletest/tmp/tmp/");
130 throw new IOException("Failed to create directories: " + f
.getPath());
134 //f = File.createTempFile("tempfile#old", new File("."));
135 f
= new File("000000");
137 if (!f
.renameTo(new File("tempfiletemp")))
138 throw new IOException("Failed to rename file: " + f
.getPath());
141 throw new IOException("Failed to delete file: " + f
.getPath());
143 System
.out
.println("PASSED: File/Directory Manipulation Test");
147 System
.out
.println("FAILED: File/Directory Manipulation Test: " + e
);
150 System
.out
.println("Test 3: Read/Write Permissions Test");
153 if ((new File("/")).canWrite() == true)
154 throw new IOException("Permission to write / unexpectedly");
156 if ((new File("/etc/services")).canRead() == false)
157 throw new IOException("No permission to read /etc/services");
159 System
.out
.println("PASSED: Read/Write Permissions Test");
161 catch (IOException e
)
163 System
.out
.println("FAILED: Read/Write Permissions Test: " + e
);
166 System
.out
.println("Test 4: Name Comparison Tests");
171 f1
= new File("/etc/");
172 f2
= new File("/etc/");
174 throw new IOException(f1
+ " " + f2
);
176 f2
= new File("/etc");
178 throw new IOException(f1
+ " " + f2
);
182 if (f1.compareTo(f2) >= 0)
183 throw new IOException(f1 + " " + f2);
187 if (f1.compareTo(f2) <= 0)
188 throw new IOException(f1 + " " + f2);
190 f1 = new File("../../jcl/");
191 f2 = new File(".././.././jcl/.");
192 if (f1.compareTo(f2) != 0)
193 throw new IOException(f1 + " " + f2);
195 System
.out
.println("PASSED: Name Comparison Tests");
197 catch (IOException e
)
199 System
.out
.println("FAILED: Name Comparison Tests: " + e
);
202 System
.out
.println("Finished test of File");