1 // Tags: JDK1.2 |
|
2 |
|
3 // Copyright (C) 2002 Free Software Foundation, Inc. |
|
4 // Written by Mark Wielaard (mark@klomp.org) |
|
5 |
|
6 // This file is part of Mauve. |
|
7 |
|
8 // Mauve is free software; you can redistribute it and/or modify |
|
9 // it under the terms of the GNU General Public License as published by |
|
10 // the Free Software Foundation; either version 2, or (at your option) |
|
11 // any later version. |
|
12 |
|
13 // Mauve is distributed in the hope that it will be useful, |
|
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 // GNU General Public License for more details. |
|
17 |
|
18 // You should have received a copy of the GNU General Public License |
|
19 // along with Mauve; see the file COPYING. If not, write to |
|
20 // the Free Software Foundation, 59 Temple Place - Suite 330, |
|
21 // Boston, MA 02111-1307, USA. */ |
|
22 |
|
23 package gnu.testlet.java.io.File; |
|
24 |
|
25 import java.io.File; |
|
26 import java.io.FileOutputStream; |
|
27 import java.io.IOException; |
|
28 import java.io.OutputStream; |
|
29 |
|
30 import gnu.testlet.Testlet; |
|
31 import gnu.testlet.TestHarness; |
|
32 |
|
33 public class canWrite implements Testlet |
|
34 { |
|
35 File tmpdir; |
|
36 File tmpfile; |
|
37 |
|
38 public void test (TestHarness harness) |
|
39 { |
|
40 try |
|
41 { |
|
42 // Setup |
|
43 String tmp = harness.getTempDirectory(); |
|
44 tmpdir = new File(tmp + File.separator + "mauve-testdir"); |
|
45 harness.check(tmpdir.mkdir() || tmpdir.exists(), "temp directory"); |
|
46 tmpfile = new File(tmpdir, "testfile"); |
|
47 harness.check(tmpfile.delete() || !tmpfile.exists(), "no temp file"); |
|
48 |
|
49 harness.check(tmpdir.canRead(), "dir.canWrite()"); |
|
50 harness.check(tmpdir.canWrite(), "dir.canWrite()"); |
|
51 harness.check(tmpdir.setReadOnly(), "dir.setReadOnly()"); |
|
52 harness.check(tmpdir.canRead(), "dir.canWrite() after setReadOnly()"); |
|
53 harness.check(!tmpdir.canWrite(), "dir.canWrite() after SetReadOnly()"); |
|
54 |
|
55 harness.check(!tmpfile.canRead(), "non-existing-file.canRead()"); |
|
56 harness.check(!tmpfile.canWrite(), "non-existing-file.canWrite()"); |
|
57 harness.check(!tmpfile.setReadOnly(), |
|
58 "non-existing-file.setReadOnly()"); |
|
59 |
|
60 boolean create; |
|
61 try |
|
62 { |
|
63 create = tmpfile.createNewFile(); |
|
64 } |
|
65 catch (IOException ioe) |
|
66 { |
|
67 create = false; |
|
68 } |
|
69 harness.check(!create, "creating file in read only dir"); |
|
70 |
|
71 // Remove and re-setup |
|
72 tmpfile.delete(); |
|
73 tmpdir.delete(); |
|
74 tmpdir.mkdir(); |
|
75 harness.check(tmpdir.canRead(), "dir.canRead() after recreation"); |
|
76 harness.check(tmpdir.canWrite(), "dir.canWrite() after recreation"); |
|
77 |
|
78 try |
|
79 { |
|
80 create = tmpfile.createNewFile(); |
|
81 } |
|
82 catch (IOException ioe) |
|
83 { |
|
84 create = false; |
|
85 harness.debug(ioe); |
|
86 } |
|
87 harness.check(create, "creating file in new dir"); |
|
88 harness.check(tmpfile.canRead(), "file.canRead() after recreation"); |
|
89 harness.check(tmpfile.canWrite(), "file.canWrite() after recreation"); |
|
90 |
|
91 boolean write; |
|
92 OutputStream os = null; |
|
93 try |
|
94 { |
|
95 os = new FileOutputStream(tmpfile); |
|
96 os.write(0); |
|
97 write = true; |
|
98 } |
|
99 catch(IOException ioe) |
|
100 { |
|
101 write = false; |
|
102 harness.debug(ioe); |
|
103 } |
|
104 finally |
|
105 { |
|
106 try |
|
107 { |
|
108 if (os != null) |
|
109 os.close(); |
|
110 os = null; |
|
111 } |
|
112 catch(IOException _) |
|
113 { |
|
114 } |
|
115 } |
|
116 harness.check(write, "Actually write to new file"); |
|
117 |
|
118 harness.check(tmpfile.setReadOnly(), "file.setReadOnly()"); |
|
119 |
|
120 try |
|
121 { |
|
122 os = new FileOutputStream(tmpfile); |
|
123 os.write(0); |
|
124 write = true; |
|
125 } |
|
126 catch(IOException ioe) |
|
127 { |
|
128 write = false; |
|
129 } |
|
130 finally |
|
131 { |
|
132 try |
|
133 { |
|
134 if (os != null) |
|
135 os.close(); |
|
136 os = null; |
|
137 } |
|
138 catch(IOException _) |
|
139 { |
|
140 } |
|
141 } |
|
142 harness.check(!write, "Write to file after setReadOnly()"); |
|
143 } |
|
144 finally |
|
145 { |
|
146 // Cleanup |
|
147 if (tmpdir != null && tmpdir.exists()) |
|
148 { |
|
149 if (tmpfile != null && tmpfile.exists()) |
|
150 tmpfile.delete(); |
|
151 tmpdir.delete(); |
|
152 } |
|
153 } |
|
154 } |
|
155 } |
|
156 |
|