This repository has been archived by the owner on Feb 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFs_test.java
341 lines (283 loc) · 13.5 KB
/
Fs_test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//
// Copyright 2016, Yahoo Inc.
// Copyrights licensed under the New BSD License.
// See the accompanying LICENSE file for terms.
//
package github.com.jminusminus.core;
import github.com.jminusminus.simplebdd.Test;
public class Fs_test extends Test {
public static void main(String[] args) {
Fs_test test = new Fs_test();
test.run();
}
protected String tmp = "./fixtures/filesystem/tmp.txt";
public void test_new_FileSystem() {
this.should("return an instance of Fs");
Fs fs = new Fs();
this.assertEqual("github.com.jminusminus.core.Fs", fs.getClass().getName());
}
public void test_readdir() {
this.should("return an array of files");
String[] files = Fs.readdir("./fixtures/filesystem");
java.util.Arrays.sort(files);
this.assertEqual("execute.txt", files[0]);
}
public void test_readdir_bad_path() {
this.should("return null as the directory doesn't exist");
String[] files = Fs.readdir("/foo/bar");
this.assertEqual(true, files == null);
}
public void test_readdirr() {
this.should("return an array of files from all sub directories");
String[] files = Fs.readdirr("./fixtures");
java.util.Arrays.sort(files);
this.assertEqual(13, files.length);
}
public void test_chown_bad_user() {
this.should("return false as the chmod user is invalid");
Fs.writeFile("./fixtures/filesystem/xchown.txt", "chown".getBytes());
this.assertEqual(false, Fs.chown("./fixtures/filesystem/xchown.txt", "xxx"));
}
public void test_chown_bad_bad_file() {
this.should("return false as the file doesn't exist");
this.assertEqual(false, Fs.chown("/dev/null/foo", "root"));
}
public void test_chmod_bad() {
this.should("return false as the chmod is invalid");
this.assertEqual(false, Fs.chmod("./fixtures/filesystem/exists.txt", 888));
}
public void test_access_bad() {
this.should("return false as the mode is invalid");
this.assertEqual(false, Fs.access("./fixtures/filesystem/exists.txt", 888));
}
public void test_access_exists_true() {
this.should("return true as the file exists");
Fs.chmod("./fixtures/filesystem/exists.txt", Fs.F_OK);
this.assertEqual(true, Fs.access("./fixtures/filesystem/exists.txt"));
}
public void test_access_exists_false() {
this.should("return false as the file doesn't exists");
this.assertEqual(false, Fs.access("./fixtures/filesystem/no.txt"));
}
public void test_access_execute_true() {
this.should("return true as the file can execute");
Fs.chmod("./fixtures/filesystem/execute.txt", Fs.X_OK);
this.assertEqual(true, Fs.access("./fixtures/filesystem/execute.txt", Fs.X_OK));
}
public void test_access_execute_false() {
this.should("return false as the file cannot execute");
Fs.chmod("./fixtures/filesystem/read.txt", Fs.R_OK);
this.assertEqual(false, Fs.access("./fixtures/filesystem/read.txt", Fs.X_OK));
}
public void test_access_write_true() {
this.should("return true as the file can be written");
Fs.chmod("./fixtures/filesystem/write.txt", Fs.W_OK);
this.assertEqual(true, Fs.access("./fixtures/filesystem/write.txt", Fs.W_OK));
}
public void test_access_write_false() {
this.should("return false as the file cannot be written");
Fs.chmod("./fixtures/filesystem/execute.txt", Fs.X_OK);
this.assertEqual(false, Fs.access("./fixtures/filesystem/execute.txt", Fs.W_OK));
}
public void test_access_write_execute_true() {
this.should("return true as the file can be read and executed");
Fs.chmod("./fixtures/filesystem/write_execute.txt", Fs.W_OK | Fs.X_OK);
this.assertEqual(true, Fs.access("./fixtures/filesystem/write_execute.txt", Fs.W_OK | Fs.X_OK));
}
public void test_access_write_execute_false() {
this.should("return false as the file cannot be read and executed");
Fs.chmod("./fixtures/filesystem/write.txt", Fs.W_OK);
this.assertEqual(false, Fs.access("./fixtures/filesystem/write.txt", Fs.W_OK | Fs.X_OK));
}
public void test_access_read_true() {
this.should("return true as the file can be read");
Fs.chmod("./fixtures/filesystem/read.txt", Fs.R_OK);
this.assertEqual(true, Fs.access("./fixtures/filesystem/read.txt", Fs.R_OK));
}
public void test_access_read_false() {
this.should("return false as the file cannot be read");
this.assertEqual(false, Fs.access("./fixtures/filesystem/no.txt", Fs.R_OK));
}
public void test_access_read_execute_true() {
this.should("return true as the file can be read and executed");
Fs.chmod("./fixtures/filesystem/read_execute.txt", Fs.R_OK | Fs.X_OK);
this.assertEqual(true, Fs.access("./fixtures/filesystem/read_execute.txt", Fs.R_OK | Fs.X_OK));
}
public void test_access_read_execute_false() {
this.should("return false as the file cannot be read and executed");
Fs.chmod("./fixtures/filesystem/read_write.txt", Fs.R_OK | Fs.W_OK);
this.assertEqual(false, Fs.access("./fixtures/filesystem/read_write.txt", Fs.R_OK | Fs.X_OK));
}
public void test_access_read_write_true() {
this.should("return true as the file can be read and written");
Fs.chmod("./fixtures/filesystem/read_write.txt", Fs.R_OK | Fs.W_OK);
this.assertEqual(true, Fs.access("./fixtures/filesystem/read_write.txt", Fs.R_OK | Fs.W_OK));
}
public void test_access_read_write_false() {
this.should("return false as the file cannot be read and written");
Fs.chmod("./fixtures/filesystem/read_execute.txt", Fs.R_OK | Fs.X_OK);
this.assertEqual(false, Fs.access("./fixtures/filesystem/read_execute.txt", Fs.R_OK | Fs.W_OK));
}
public void test_access_read_write_execute_true() {
this.should("return true as the file can be read, written and executed");
Fs.chmod("./fixtures/filesystem/read_write_execute.txt", Fs.R_OK | Fs.W_OK | Fs.X_OK);
this.assertEqual(true, Fs.access("./fixtures/filesystem/read_write_execute.txt", Fs.R_OK | Fs.W_OK | Fs.X_OK));
}
public void test_access_read_write_execute_false() {
this.should("return false as the file cannot be read, written and executed");
Fs.chmod("./fixtures/filesystem/read_write.txt", Fs.R_OK | Fs.W_OK);
this.assertEqual(false, Fs.access("./fixtures/filesystem/read_write.txt", Fs.R_OK | Fs.W_OK | Fs.X_OK));
}
public void test_appendFile_new() {
this.should("create a new file and write the bytes to it");
this.assertEqual(true, Fs.appendFile(this.tmp, "test".getBytes()));
Fs.unlink(this.tmp);
}
public void test_appendFile_existing() {
this.should("append bytes to an existing file");
Fs.appendFile(this.tmp, "test".getBytes());
this.assertEqual(true, Fs.appendFile(this.tmp, "test".getBytes()));
Fs.unlink(this.tmp);
}
public void test_appendFile_string() {
this.should("create a new file and append the string to it");
this.assertEqual(true, Fs.appendFile(this.tmp, "test"));
Fs.unlink(this.tmp);
}
public void test_appendFile_string_encoding() {
this.should("create a new file and append the string as utf8");
this.assertEqual(true, Fs.appendFile(this.tmp, "test", "utf8"));
Fs.unlink(this.tmp);
}
public void test_appendFile_string_bad_encoding() {
this.should("create a new file and append the string as utf8");
this.assertEqual(false, Fs.appendFile(this.tmp, "test", "xxx"));
Fs.unlink(this.tmp);
}
public void test_appendFile_dev_null() {
this.should("return false as file cannot be created");
this.assertEqual(false, Fs.appendFile("/dev/null/foo", "test".getBytes()));
}
public void test_unlink() {
this.should("create a file and then unlink it");
Fs.appendFile(this.tmp, "test".getBytes());
this.assertEqual(true, Fs.unlink(this.tmp));
}
public void test_unlink_no_file() {
this.should("return true even if the file doesn't exist");
this.assertEqual(false, Fs.unlink(this.tmp));
}
public void test_stat_file() {
this.should("return true from file");
Stat s = Fs.stat("./fixtures/filesystem/read_write_execute.txt");
this.assertEqual(false, s.dir);
this.assertEqual(true, s.file);
}
public void test_stat_dir() {
this.should("return true from dir");
Stat s = Fs.stat("./fixtures/filesystem");
this.assertEqual(true, s.dir);
this.assertEqual(false, s.file);
}
public void test_readFile() {
this.should("return the content of the file");
byte[] b = Fs.readFile("./fixtures/filesystem/read.txt");
this.assertEqual("read", new String(b));
}
public void test_readFile_null() {
this.should("return the content of the file");
this.assertEqual(true, Fs.readFile("/dev/null/foo") == null);
}
public void test_writeFile() {
this.should("return true after the file is written");
Fs.chmod("./fixtures/filesystem/write.txt", Fs.R_OK | Fs.W_OK);
this.assertEqual(true, Fs.writeFile("./fixtures/filesystem/write.txt", "write foo".getBytes()));
byte[] b = Fs.readFile("./fixtures/filesystem/write.txt");
this.assertEqual("write foo", new String(b));
}
public void test_writeFile_string() {
this.should("create a new file and write the string to it");
this.assertEqual(true, Fs.writeFile(this.tmp, "test"));
Fs.unlink(this.tmp);
}
public void test_writeFile_string_encoding() {
this.should("create a new file and write the string as utf8");
this.assertEqual(true, Fs.writeFile(this.tmp, "test", "utf8"));
Fs.unlink(this.tmp);
}
public void test_writeFile_string_bad_encoding() {
this.should("create a new file and write the string as utf8");
this.assertEqual(false, Fs.writeFile(this.tmp, "test", "xxx"));
Fs.unlink(this.tmp);
}
public void test_writeFile_null() {
this.should("return false as the file content be written");
this.assertEqual(false, Fs.writeFile("/dev/null/foo", "write foo".getBytes()));
}
public void test_mkdir() {
this.should("create the directory");
boolean b = Fs.mkdir("./fixtures/filesystem/newDir");
this.assertEqual(true, b);
Fs.rmdir("./fixtures/filesystem/newDir");
}
public void test_mkdir_null() {
this.should("return false as the directory cannot be made");
this.assertEqual(false, Fs.mkdir("/dev/null/foo"));
}
public void test_mkdirs() {
this.should("create the directories");
boolean b = Fs.mkdirs("./fixtures/filesystem/newDir/newSubDir");
this.assertEqual(true, b);
Fs.rmdir("./fixtures/filesystem/newDir/newSubDir");
Fs.rmdir("./fixtures/filesystem/newDir");
}
public void test_mkdirs_null() {
this.should("return false as the directories cannot be made");
this.assertEqual(false, Fs.mkdirs("/dev/null/foo/bar"));
}
public void test_truncate() {
this.should("truncate the file to 10 bytes");
Fs.writeFile("./fixtures/filesystem/truncate.txt", "12345678901234567890".getBytes());
this.assertEqual((long)20, Fs.stat("./fixtures/filesystem/truncate.txt").size);
Fs.truncate("./fixtures/filesystem/truncate.txt", 10);
this.assertEqual((long)10, Fs.stat("./fixtures/filesystem/truncate.txt").size);
Fs.unlink("./fixtures/filesystem/truncate.txt");
}
public void test_truncate_null() {
this.should("return false as truncate cannot be called");
this.assertEqual(false, Fs.truncate("/dev/null/foo", 5));
}
public void test_stat_null() {
this.should("return false as stat cannot be called");
this.assertEqual(true, Fs.stat("/dev/null/foo") == null);
}
public void test_rename_same() {
this.should("return true as the file renamed it's self");
this.assertEqual(true, Fs.rename("./fixtures/filesystem/rename.txt", "./fixtures/filesystem/rename.txt"));
}
public void test_rename_different() {
this.should("return true as the file is renamed");
this.assertEqual(true, Fs.rename("./fixtures/filesystem/rename.txt", "./fixtures/filesystem/rename1.txt"));
this.assertEqual(true, Fs.rename("./fixtures/filesystem/rename1.txt", "./fixtures/filesystem/rename.txt"));
}
public void test_rename_null() {
this.should("return false as the file cannot be renamed");
this.assertEqual(false, Fs.rename("/dev/null/foo", "/dev/null/bar"));
}
public void test_mtime() {
this.should("set the new modified time on the file");
Fs.mtime("./fixtures/filesystem/modified.txt", (long)0);
this.assertEqual(true, Fs.mtime("./fixtures/filesystem/modified.txt", (long)123451000));
this.assertEqual((long)123451000, Fs.stat("./fixtures/filesystem/modified.txt").modify);
}
public void test_mtime_null() {
this.should("return false as the file doesn't exist");
this.assertEqual(false, Fs.mtime("/dev/null/foo", (long)123451000));
}
public void test_rmdirr() {
this.should("remove directory and sub folders");
Fs.mkdirs("./fixtures/tmp/foo/bar");
Fs.writeFile("./fixtures/tmp/foo/tmp.txt", "bar");
this.assertEqual(true, Fs.rmdirr("./fixtures/tmp"));
}
}