PR target/27599
[official-gcc.git] / libjava / classpath / test / java.io / FileTest.java
blob838b99e11c99540134525894e94be5f858955bcf
1 /*************************************************************************
2 /* File.java -- Tests File class
3 /*
4 /* Copyright (c) 1998 Free Software Foundation, Inc.
5 /* Written by Aaron M. Renn (arenn@urbanophile.com)
6 /*
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 /*************************************************************************/
21 import java.io.*;
23 public class FileTest
26 static PrintWriter pw;
28 public static void
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());
39 public static void
40 deleteTempDirs() throws IOException
42 File f = new File("tempfiletest/tmp/tmp");
43 if (!f.delete())
44 throw new IOException("Could not delete " + f.getPath());
46 f = new File("tempfiletest/tmp");
47 if (!f.delete())
48 throw new IOException("Could not delete " + f.getPath());
50 f = new File("tempfiletest/");
51 if (!f.delete())
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");
63 try
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("../.."));
75 pw.flush();
77 File f = new File("gimme");
78 if (f.isAbsolute())
79 throw new IOException("isAbsolute() failed");
81 f = new File("/etc/services");
82 if (!f.isFile())
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/");
90 if (!f.isDirectory())
91 throw new IOException("isDirectory() failed");
93 pw.close();
94 System.out.println("PASSED: Conditionally Passed Path Operations Test");
96 catch(IOException e)
98 System.out.println("FAILED: Path Operations Test: " + e);
99 pw.close();
102 System.out.println("Test 2: File/Directory Manipulation Test");
105 File f = new File("filetest");
106 if (!f.exists())
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/");
118 filelist = f.list();
119 for (int i = 0; i < filelist.length; i++)
120 System.out.println(filelist[i]);
122 f = new File("tempfiletest/tmp/tmp");
123 if (!f.mkdirs())
124 throw new IOException("Failed to create directories: " + f.getPath());
126 deleteTempDirs();
128 f = new File("tempfiletest/tmp/tmp/");
129 if (!f.mkdirs())
130 throw new IOException("Failed to create directories: " + f.getPath());
132 deleteTempDirs();
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());
140 if (!f.delete())
141 throw new IOException("Failed to delete file: " + f.getPath());
143 System.out.println("PASSED: File/Directory Manipulation Test");
145 catch(IOException e)
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");
169 File f1, f2;
171 f1 = new File("/etc/");
172 f2 = new File("/etc/");
173 if (!f1.equals(f2))
174 throw new IOException(f1 + " " + f2);
176 f2 = new File("/etc");
177 if (f1.equals(f2))
178 throw new IOException(f1 + " " + f2);
180 f1 = new File("a");
181 f2 = new File("b");
182 if (f1.compareTo(f2) >= 0)
183 throw new IOException(f1 + " " + f2);
185 f1 = new File("z");
186 f2 = new File("y");
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");