1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io;
18
19 import java.io.BufferedReader;
20 import java.io.ByteArrayInputStream;
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.OutputStream;
26 import java.util.Locale;
27
28 import org.junit.Assert;
29
30 import org.apache.commons.io.testtools.FileBasedTestCase;
31
32
33
34
35
36
37 public class FileSystemUtilsTestCase extends FileBasedTestCase {
38
39 public FileSystemUtilsTestCase(final String name) {
40 super(name);
41 }
42
43 @Override
44 protected void setUp() throws Exception {
45 }
46
47 @Override
48 protected void tearDown() throws Exception {
49 }
50
51
52 public void testGetFreeSpace_String() throws Exception {
53
54 if (File.separatorChar == '/') {
55
56 String[] cmd = null;
57 String osName = System.getProperty("os.name");
58 osName = osName.toLowerCase(Locale.ENGLISH);
59
60 if (osName.indexOf("hp-ux") >= 0 || osName.indexOf("aix") >= 0) {
61 cmd = new String[] {"df", "-P", "/"};
62 } else if (osName.indexOf("sunos") >= 0 || osName.indexOf("sun os") >= 0
63 || osName.indexOf("solaris") >= 0) {
64 cmd = new String[] {"/usr/xpg4/bin/df", "-P", "/"};
65 } else {
66 cmd = new String[] {"df", "/"};
67 }
68 final Process proc = Runtime.getRuntime().exec(cmd);
69 boolean kilobyteBlock = true;
70 BufferedReader r = null;
71 try {
72 r = new BufferedReader(new InputStreamReader(proc.getInputStream()));
73 final String line = r.readLine();
74 Assert.assertNotNull("Unexpected null line", line);
75 if (line.indexOf("512") >= 0) {
76 kilobyteBlock = false;
77 }
78 } finally {
79 IOUtils.closeQuietly(r);
80 }
81
82
83 @SuppressWarnings("deprecation")
84 final
85 long free = FileSystemUtils.freeSpace("/");
86 final long kb = FileSystemUtils.freeSpaceKb("/");
87 if (kilobyteBlock) {
88 assertEquals(free, kb, 256d);
89 } else {
90 assertEquals(free / 2d, kb, 256d);
91 }
92 } else {
93 @SuppressWarnings("deprecation")
94 final
95 long bytes = FileSystemUtils.freeSpace("");
96 final long kb = FileSystemUtils.freeSpaceKb("");
97 assertEquals((double) bytes / 1024, kb, 256d);
98 }
99 }
100
101
102 public void testGetFreeSpaceOS_String_NullPath() throws Exception {
103 final FileSystemUtils fsu = new FileSystemUtils();
104 try {
105 fsu.freeSpaceOS(null, 1, false, -1);
106 fail();
107 } catch (final IllegalArgumentException ex) {}
108 try {
109 fsu.freeSpaceOS(null, 1, true, -1);
110 fail();
111 } catch (final IllegalArgumentException ex) {}
112 }
113
114 public void testGetFreeSpaceOS_String_InitError() throws Exception {
115 final FileSystemUtils fsu = new FileSystemUtils();
116 try {
117 fsu.freeSpaceOS("", -1, false, -1);
118 fail();
119 } catch (final IllegalStateException ex) {}
120 try {
121 fsu.freeSpaceOS("", -1, true, -1);
122 fail();
123 } catch (final IllegalStateException ex) {}
124 }
125
126 public void testGetFreeSpaceOS_String_Other() throws Exception {
127 final FileSystemUtils fsu = new FileSystemUtils();
128 try {
129 fsu.freeSpaceOS("", 0, false, -1);
130 fail();
131 } catch (final IllegalStateException ex) {}
132 try {
133 fsu.freeSpaceOS("", 0, true, -1);
134 fail();
135 } catch (final IllegalStateException ex) {}
136 }
137
138 public void testGetFreeSpaceOS_String_Windows() throws Exception {
139 final FileSystemUtils fsu = new FileSystemUtils() {
140 @Override
141 protected long freeSpaceWindows(final String path, final long timeout) throws IOException {
142 return 12345L;
143 }
144 };
145 assertEquals(12345L, fsu.freeSpaceOS("", 1, false, -1));
146 assertEquals(12345L / 1024, fsu.freeSpaceOS("", 1, true, -1));
147 }
148
149 public void testGetFreeSpaceOS_String_Unix() throws Exception {
150 final FileSystemUtils fsu = new FileSystemUtils() {
151 @Override
152 protected long freeSpaceUnix(final String path, final boolean kb, final boolean posix, final long timeout) throws IOException {
153 return kb ? 12345L : 54321;
154 }
155 };
156 assertEquals(54321L, fsu.freeSpaceOS("", 2, false, -1));
157 assertEquals(12345L, fsu.freeSpaceOS("", 2, true, -1));
158 }
159
160
161 public void testGetFreeSpaceWindows_String_ParseCommaFormatBytes() throws Exception {
162
163
164 final String lines =
165 " Volume in drive C is HDD\n" +
166 " Volume Serial Number is XXXX-YYYY\n" +
167 "\n" +
168 " Directory of C:\\Documents and Settings\\Xxxx\n" +
169 "\n" +
170 "19/08/2005 22:43 <DIR> .\n" +
171 "19/08/2005 22:43 <DIR> ..\n" +
172 "11/08/2005 01:07 81 build.properties\n" +
173 "17/08/2005 21:44 <DIR> Desktop\n" +
174 " 7 File(s) 180,260 bytes\n" +
175 " 10 Dir(s) 41,411,551,232 bytes free";
176 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
177 assertEquals(41411551232L, fsu.freeSpaceWindows("", -1));
178 }
179
180
181 public void testGetFreeSpaceWindows_String_EmptyPath() throws Exception {
182 final String lines =
183 " Volume in drive C is HDD\n" +
184 " Volume Serial Number is XXXX-YYYY\n" +
185 "\n" +
186 " Directory of C:\\Documents and Settings\\Xxxx\n" +
187 "\n" +
188 "19/08/2005 22:43 <DIR> .\n" +
189 "19/08/2005 22:43 <DIR> ..\n" +
190 "11/08/2005 01:07 81 build.properties\n" +
191 "17/08/2005 21:44 <DIR> Desktop\n" +
192 " 7 File(s) 180260 bytes\n" +
193 " 10 Dir(s) 41411551232 bytes free";
194 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines, "dir /a /-c ");
195 assertEquals(41411551232L, fsu.freeSpaceWindows("", -1));
196 }
197
198 public void testGetFreeSpaceWindows_String_NormalResponse() throws Exception {
199 final String lines =
200 " Volume in drive C is HDD\n" +
201 " Volume Serial Number is XXXX-YYYY\n" +
202 "\n" +
203 " Directory of C:\\Documents and Settings\\Xxxx\n" +
204 "\n" +
205 "19/08/2005 22:43 <DIR> .\n" +
206 "19/08/2005 22:43 <DIR> ..\n" +
207 "11/08/2005 01:07 81 build.properties\n" +
208 "17/08/2005 21:44 <DIR> Desktop\n" +
209 " 7 File(s) 180260 bytes\n" +
210 " 10 Dir(s) 41411551232 bytes free";
211 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines, "dir /a /-c \"C:\"");
212 assertEquals(41411551232L, fsu.freeSpaceWindows("C:", -1));
213 }
214
215 public void testGetFreeSpaceWindows_String_StripDrive() throws Exception {
216 final String lines =
217 " Volume in drive C is HDD\n" +
218 " Volume Serial Number is XXXX-YYYY\n" +
219 "\n" +
220 " Directory of C:\\Documents and Settings\\Xxxx\n" +
221 "\n" +
222 "19/08/2005 22:43 <DIR> .\n" +
223 "19/08/2005 22:43 <DIR> ..\n" +
224 "11/08/2005 01:07 81 build.properties\n" +
225 "17/08/2005 21:44 <DIR> Desktop\n" +
226 " 7 File(s) 180260 bytes\n" +
227 " 10 Dir(s) 41411551232 bytes free";
228 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines, "dir /a /-c \"C:\\somedir\"");
229 assertEquals(41411551232L, fsu.freeSpaceWindows("C:\\somedir", -1));
230 }
231
232 public void testGetFreeSpaceWindows_String_quoted() throws Exception {
233 final String lines =
234 " Volume in drive C is HDD\n" +
235 " Volume Serial Number is XXXX-YYYY\n" +
236 "\n" +
237 " Directory of C:\\Documents and Settings\\Xxxx\n" +
238 "\n" +
239 "19/08/2005 22:43 <DIR> .\n" +
240 "19/08/2005 22:43 <DIR> ..\n" +
241 "11/08/2005 01:07 81 build.properties\n" +
242 "17/08/2005 21:44 <DIR> Desktop\n" +
243 " 7 File(s) 180260 bytes\n" +
244 " 10 Dir(s) 41411551232 bytes free";
245 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines, "dir /a /-c \"C:\\somedir\"");
246 assertEquals(41411551232L, fsu.freeSpaceWindows("\"C:\\somedir\"", -1));
247 }
248
249 public void testGetFreeSpaceWindows_String_EmptyResponse() throws Exception {
250 final String lines = "";
251 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
252 try {
253 fsu.freeSpaceWindows("C:", -1);
254 fail();
255 } catch (final IOException ex) {}
256 }
257
258 public void testGetFreeSpaceWindows_String_EmptyMultiLineResponse() throws Exception {
259 final String lines = "\n\n";
260 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
261 try {
262 fsu.freeSpaceWindows("C:", -1);
263 fail();
264 } catch (final IOException ex) {}
265 }
266
267 public void testGetFreeSpaceWindows_String_InvalidTextResponse() throws Exception {
268 final String lines = "BlueScreenOfDeath";
269 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
270 try {
271 fsu.freeSpaceWindows("C:", -1);
272 fail();
273 } catch (final IOException ex) {}
274 }
275
276 public void testGetFreeSpaceWindows_String_NoSuchDirectoryResponse() throws Exception {
277 final String lines =
278 " Volume in drive C is HDD\n" +
279 " Volume Serial Number is XXXX-YYYY\n" +
280 "\n" +
281 " Directory of C:\\Documents and Settings\\empty" +
282 "\n";
283 final FileSystemUtils fsu = new MockFileSystemUtils(1, lines);
284 try {
285 fsu.freeSpaceWindows("C:", -1);
286 fail();
287 } catch (final IOException ex) {}
288 }
289
290
291 public void testGetFreeSpaceUnix_String_EmptyPath() throws Exception {
292 final String lines =
293 "Filesystem 1K-blocks Used Available Use% Mounted on\n" +
294 "xxx:/home/users/s 14428928 12956424 1472504 90% /home/users/s";
295 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
296 try {
297 fsu.freeSpaceUnix("", false, false, -1);
298 fail();
299 } catch (final IllegalArgumentException ex) {}
300 try {
301 fsu.freeSpaceUnix("", true, false, -1);
302 fail();
303 } catch (final IllegalArgumentException ex) {}
304 try {
305 fsu.freeSpaceUnix("", true, true, -1);
306 fail();
307 } catch (final IllegalArgumentException ex) {}
308 try {
309 fsu.freeSpaceUnix("", false, true, -1);
310 fail();
311 } catch (final IllegalArgumentException ex) {}
312
313 }
314
315 public void testGetFreeSpaceUnix_String_NormalResponseLinux() throws Exception {
316
317 final String lines =
318 "Filesystem 1K-blocks Used Available Use% Mounted on\n" +
319 "/dev/xxx 497944 308528 189416 62% /";
320 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
321 assertEquals(189416L, fsu.freeSpaceUnix("/", false, false, -1));
322 }
323
324 public void testGetFreeSpaceUnix_String_NormalResponseFreeBSD() throws Exception {
325
326 final String lines =
327 "Filesystem 1K-blocks Used Avail Capacity Mounted on\n" +
328 "/dev/xxxxxx 128990 102902 15770 87% /";
329 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
330 assertEquals(15770L, fsu.freeSpaceUnix("/", false, false, -1));
331 }
332
333
334 public void testGetFreeSpaceUnix_String_NormalResponseKbLinux() throws Exception {
335
336
337 final String lines =
338 "Filesystem 1K-blocks Used Available Use% Mounted on\n" +
339 "/dev/xxx 497944 308528 189416 62% /";
340 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
341 assertEquals(189416L, fsu.freeSpaceUnix("/", true, false, -1));
342 }
343
344 public void testGetFreeSpaceUnix_String_NormalResponseKbFreeBSD() throws Exception {
345
346
347 final String lines =
348 "Filesystem 1K-blocks Used Avail Capacity Mounted on\n" +
349 "/dev/xxxxxx 128990 102902 15770 87% /";
350 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
351 assertEquals(15770L, fsu.freeSpaceUnix("/", true, false, -1));
352 }
353
354 public void testGetFreeSpaceUnix_String_NormalResponseKbSolaris() throws Exception {
355
356
357 final String lines =
358 "Filesystem kbytes used avail capacity Mounted on\n" +
359 "/dev/dsk/x0x0x0x0 1350955 815754 481163 63%";
360 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
361 assertEquals(481163L, fsu.freeSpaceUnix("/dev/dsk/x0x0x0x0", true, false, -1));
362 }
363
364 public void testGetFreeSpaceUnix_String_LongResponse() throws Exception {
365 final String lines =
366 "Filesystem 1K-blocks Used Available Use% Mounted on\n" +
367 "xxx-yyyyyyy-zzz:/home/users/s\n" +
368 " 14428928 12956424 1472504 90% /home/users/s";
369 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
370 assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", false, false, -1));
371 }
372
373 public void testGetFreeSpaceUnix_String_LongResponseKb() throws Exception {
374 final String lines =
375 "Filesystem 1K-blocks Used Available Use% Mounted on\n" +
376 "xxx-yyyyyyy-zzz:/home/users/s\n" +
377 " 14428928 12956424 1472504 90% /home/users/s";
378 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
379 assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", true, false, -1));
380 }
381
382 public void testGetFreeSpaceUnix_String_EmptyResponse() throws Exception {
383 final String lines = "";
384 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
385 try {
386 fsu.freeSpaceUnix("/home/users/s", false, false, -1);
387 fail();
388 } catch (final IOException ex) {}
389 try {
390 fsu.freeSpaceUnix("/home/users/s", true, false, -1);
391 fail();
392 } catch (final IOException ex) {}
393 try {
394 fsu.freeSpaceUnix("/home/users/s", false, true, -1);
395 fail();
396 } catch (final IOException ex) {}
397 try {
398 fsu.freeSpaceUnix("/home/users/s", true, true, -1);
399 fail();
400 } catch (final IOException ex) {}
401 }
402
403 public void testGetFreeSpaceUnix_String_InvalidResponse1() throws Exception {
404 final String lines =
405 "Filesystem 1K-blocks Used Available Use% Mounted on\n" +
406 " 14428928 12956424 100";
407 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
408 try {
409 fsu.freeSpaceUnix("/home/users/s", false, false, -1);
410 fail();
411 } catch (final IOException ex) {}
412 try {
413 fsu.freeSpaceUnix("/home/users/s", true, false, -1);
414 fail();
415 } catch (final IOException ex) {}
416 try {
417 fsu.freeSpaceUnix("/home/users/s", false, true, -1);
418 fail();
419 } catch (final IOException ex) {}
420 try {
421 fsu.freeSpaceUnix("/home/users/s", true, true, -1);
422 fail();
423 } catch (final IOException ex) {}
424 }
425
426 public void testGetFreeSpaceUnix_String_InvalidResponse2() throws Exception {
427 final String lines =
428 "Filesystem 1K-blocks Used Available Use% Mounted on\n" +
429 "xxx:/home/users/s 14428928 12956424 nnnnnnn 90% /home/users/s";
430 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
431 try {
432 fsu.freeSpaceUnix("/home/users/s", false, false, -1);
433 fail();
434 } catch (final IOException ex) {}
435 try {
436 fsu.freeSpaceUnix("/home/users/s", true, false, -1);
437 fail();
438 } catch (final IOException ex) {}
439 try {
440 fsu.freeSpaceUnix("/home/users/s", false, true, -1);
441 fail();
442 } catch (final IOException ex) {}
443 try {
444 fsu.freeSpaceUnix("/home/users/s", true, true, -1);
445 fail();
446 } catch (final IOException ex) {}
447 }
448
449 public void testGetFreeSpaceUnix_String_InvalidResponse3() throws Exception {
450 final String lines =
451 "Filesystem 1K-blocks Used Available Use% Mounted on\n" +
452 "xxx:/home/users/s 14428928 12956424 -1 90% /home/users/s";
453 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
454 try {
455 fsu.freeSpaceUnix("/home/users/s", false, false, -1);
456 fail();
457 } catch (final IOException ex) {}
458 try {
459 fsu.freeSpaceUnix("/home/users/s", true, false, -1);
460 fail();
461 } catch (final IOException ex) {}
462 try {
463 fsu.freeSpaceUnix("/home/users/s", false, true, -1);
464 fail();
465 } catch (final IOException ex) {}
466 try {
467 fsu.freeSpaceUnix("/home/users/s", true, true, -1);
468 fail();
469 } catch (final IOException ex) {}
470 }
471
472 public void testGetFreeSpaceUnix_String_InvalidResponse4() throws Exception {
473 final String lines =
474 "Filesystem 1K-blocks Used Available Use% Mounted on\n" +
475 "xxx-yyyyyyy-zzz:/home/users/s";
476 final FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
477 try {
478 fsu.freeSpaceUnix("/home/users/s", false, false, -1);
479 fail();
480 } catch (final IOException ex) {}
481 try {
482 fsu.freeSpaceUnix("/home/users/s", true, false, -1);
483 fail();
484 } catch (final IOException ex) {}
485 try {
486 fsu.freeSpaceUnix("/home/users/s", false, true, -1);
487 fail();
488 } catch (final IOException ex) {}
489 try {
490 fsu.freeSpaceUnix("/home/users/s", true, true, -1);
491 fail();
492 } catch (final IOException ex) {}
493 }
494
495
496 static class MockFileSystemUtils extends FileSystemUtils {
497 private final int exitCode;
498 private final byte[] bytes;
499 private final String cmd;
500 public MockFileSystemUtils(final int exitCode, final String lines) {
501 this(exitCode, lines, null);
502 }
503 public MockFileSystemUtils(final int exitCode, final String lines, final String cmd) {
504 this.exitCode = exitCode;
505 this.bytes = lines.getBytes();
506 this.cmd = cmd;
507 }
508 @Override
509 Process openProcess(final String[] params) {
510 if (cmd != null) {
511 assertEquals(cmd, params[params.length - 1]);
512 }
513 return new Process() {
514 @Override
515 public InputStream getErrorStream() {
516 return null;
517 }
518 @Override
519 public InputStream getInputStream() {
520 return new ByteArrayInputStream(bytes);
521 }
522 @Override
523 public OutputStream getOutputStream() {
524 return null;
525 }
526 @Override
527 public int waitFor() throws InterruptedException {
528 return exitCode;
529 }
530 @Override
531 public int exitValue() {
532 return exitCode;
533 }
534 @Override
535 public void destroy() {
536 }
537 };
538 }
539 }
540
541 }