001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io;
018:
019: import java.io.BufferedReader;
020: import java.io.ByteArrayInputStream;
021: import java.io.File;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.InputStreamReader;
025: import java.io.OutputStream;
026:
027: import junit.framework.Test;
028: import junit.framework.TestSuite;
029: import junit.textui.TestRunner;
030:
031: import org.apache.commons.io.testtools.FileBasedTestCase;
032:
033: /**
034: * This is used to test FileSystemUtils.
035: *
036: * @version $Id: FileSystemUtilsTestCase.java 453889 2006-10-07 11:56:25Z scolebourne $
037: */
038: public class FileSystemUtilsTestCase extends FileBasedTestCase {
039:
040: public static void main(String[] args) {
041: TestRunner.run(suite());
042:
043: // try {
044: // System.out.println(FileSystemUtils.freeSpace("C:\\"));
045: // } catch (IOException ex) {
046: // ex.printStackTrace();
047: // }
048: }
049:
050: public static Test suite() {
051: return new TestSuite(FileSystemUtilsTestCase.class);
052: }
053:
054: public FileSystemUtilsTestCase(String name) throws IOException {
055: super (name);
056: }
057:
058: protected void setUp() throws Exception {
059: }
060:
061: protected void tearDown() throws Exception {
062: }
063:
064: //-----------------------------------------------------------------------
065: public void testGetFreeSpace_String() throws Exception {
066: // test coverage, as we can't check value
067: if (File.separatorChar == '/') {
068: // have to figure out unix block size
069: String[] cmd = null;
070: String osName = System.getProperty("os.name");
071: if (osName.indexOf("hp-ux") >= 0
072: || osName.indexOf("aix") >= 0) {
073: cmd = new String[] { "df", "-P", "/" };
074: } else {
075: cmd = new String[] { "df", "/" };
076: }
077: Process proc = Runtime.getRuntime().exec(cmd);
078: boolean kilobyteBlock = true;
079: BufferedReader r = null;
080: try {
081: r = new BufferedReader(new InputStreamReader(proc
082: .getInputStream()));
083: String line = r.readLine();
084: if (line.toLowerCase().indexOf("512") >= 0) {
085: kilobyteBlock = false;
086: }
087: } finally {
088: IOUtils.closeQuietly(r);
089: }
090:
091: // now perform the test
092: long free = FileSystemUtils.freeSpace("/");
093: long kb = FileSystemUtils.freeSpaceKb("/");
094: if (kilobyteBlock) {
095: assertEquals((double) free, (double) kb, 256d);
096: } else {
097: assertEquals((double) free / 2d, (double) kb, 256d);
098: }
099: } else {
100: long bytes = FileSystemUtils.freeSpace("");
101: long kb = FileSystemUtils.freeSpaceKb("");
102: assertEquals((double) bytes / 1024, (double) kb, 256d);
103: }
104: }
105:
106: //-----------------------------------------------------------------------
107: public void testGetFreeSpaceOS_String_NullPath() throws Exception {
108: FileSystemUtils fsu = new FileSystemUtils();
109: try {
110: fsu.freeSpaceOS(null, 1, false);
111: fail();
112: } catch (IllegalArgumentException ex) {
113: }
114: try {
115: fsu.freeSpaceOS(null, 1, true);
116: fail();
117: } catch (IllegalArgumentException ex) {
118: }
119: }
120:
121: public void testGetFreeSpaceOS_String_InitError() throws Exception {
122: FileSystemUtils fsu = new FileSystemUtils();
123: try {
124: fsu.freeSpaceOS("", -1, false);
125: fail();
126: } catch (IllegalStateException ex) {
127: }
128: try {
129: fsu.freeSpaceOS("", -1, true);
130: fail();
131: } catch (IllegalStateException ex) {
132: }
133: }
134:
135: public void testGetFreeSpaceOS_String_Other() throws Exception {
136: FileSystemUtils fsu = new FileSystemUtils();
137: try {
138: fsu.freeSpaceOS("", 0, false);
139: fail();
140: } catch (IllegalStateException ex) {
141: }
142: try {
143: fsu.freeSpaceOS("", 0, true);
144: fail();
145: } catch (IllegalStateException ex) {
146: }
147: }
148:
149: public void testGetFreeSpaceOS_String_Windows() throws Exception {
150: FileSystemUtils fsu = new FileSystemUtils() {
151: protected long freeSpaceWindows(String path)
152: throws IOException {
153: return 12345L;
154: }
155: };
156: assertEquals(12345L, fsu.freeSpaceOS("", 1, false));
157: assertEquals(12345L / 1024, fsu.freeSpaceOS("", 1, true));
158: }
159:
160: public void testGetFreeSpaceOS_String_Unix() throws Exception {
161: FileSystemUtils fsu = new FileSystemUtils() {
162: protected long freeSpaceUnix(String path, boolean kb,
163: boolean posix) throws IOException {
164: return (kb ? 12345L : 54321);
165: }
166: };
167: assertEquals(54321L, fsu.freeSpaceOS("", 2, false));
168: assertEquals(12345L, fsu.freeSpaceOS("", 2, true));
169: }
170:
171: //-----------------------------------------------------------------------
172: public void testGetFreeSpaceWindows_String_ParseCommaFormatBytes()
173: throws Exception {
174: // this is the format of response when calling dir /c
175: // we have now switched to dir /-c, so we should never get this
176: String lines = " Volume in drive C is HDD\n"
177: + " Volume Serial Number is XXXX-YYYY\n"
178: + "\n"
179: + " Directory of C:\\Documents and Settings\\Xxxx\n"
180: + "\n"
181: + "19/08/2005 22:43 <DIR> .\n"
182: + "19/08/2005 22:43 <DIR> ..\n"
183: + "11/08/2005 01:07 81 build.properties\n"
184: + "17/08/2005 21:44 <DIR> Desktop\n"
185: + " 7 File(s) 180,260 bytes\n"
186: + " 10 Dir(s) 41,411,551,232 bytes free";
187: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
188: assertEquals(41411551232L, fsu.freeSpaceWindows(""));
189: }
190:
191: //-----------------------------------------------------------------------
192: public void testGetFreeSpaceWindows_String_EmptyPath()
193: throws Exception {
194: String lines = " Volume in drive C is HDD\n"
195: + " Volume Serial Number is XXXX-YYYY\n"
196: + "\n"
197: + " Directory of C:\\Documents and Settings\\Xxxx\n"
198: + "\n"
199: + "19/08/2005 22:43 <DIR> .\n"
200: + "19/08/2005 22:43 <DIR> ..\n"
201: + "11/08/2005 01:07 81 build.properties\n"
202: + "17/08/2005 21:44 <DIR> Desktop\n"
203: + " 7 File(s) 180260 bytes\n"
204: + " 10 Dir(s) 41411551232 bytes free";
205: FileSystemUtils fsu = new MockFileSystemUtils(0, lines,
206: "dir /-c ");
207: assertEquals(41411551232L, fsu.freeSpaceWindows(""));
208: }
209:
210: public void testGetFreeSpaceWindows_String_NormalResponse()
211: throws Exception {
212: String lines = " Volume in drive C is HDD\n"
213: + " Volume Serial Number is XXXX-YYYY\n"
214: + "\n"
215: + " Directory of C:\\Documents and Settings\\Xxxx\n"
216: + "\n"
217: + "19/08/2005 22:43 <DIR> .\n"
218: + "19/08/2005 22:43 <DIR> ..\n"
219: + "11/08/2005 01:07 81 build.properties\n"
220: + "17/08/2005 21:44 <DIR> Desktop\n"
221: + " 7 File(s) 180260 bytes\n"
222: + " 10 Dir(s) 41411551232 bytes free";
223: FileSystemUtils fsu = new MockFileSystemUtils(0, lines,
224: "dir /-c C:");
225: assertEquals(41411551232L, fsu.freeSpaceWindows("C:"));
226: }
227:
228: public void testGetFreeSpaceWindows_String_StripDrive()
229: throws Exception {
230: String lines = " Volume in drive C is HDD\n"
231: + " Volume Serial Number is XXXX-YYYY\n"
232: + "\n"
233: + " Directory of C:\\Documents and Settings\\Xxxx\n"
234: + "\n"
235: + "19/08/2005 22:43 <DIR> .\n"
236: + "19/08/2005 22:43 <DIR> ..\n"
237: + "11/08/2005 01:07 81 build.properties\n"
238: + "17/08/2005 21:44 <DIR> Desktop\n"
239: + " 7 File(s) 180260 bytes\n"
240: + " 10 Dir(s) 41411551232 bytes free";
241: FileSystemUtils fsu = new MockFileSystemUtils(0, lines,
242: "dir /-c C:");
243: assertEquals(41411551232L, fsu.freeSpaceWindows("C:\\somedir"));
244: }
245:
246: public void testGetFreeSpaceWindows_String_EmptyResponse()
247: throws Exception {
248: String lines = "";
249: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
250: try {
251: fsu.freeSpaceWindows("C:");
252: fail();
253: } catch (IOException ex) {
254: }
255: }
256:
257: public void testGetFreeSpaceWindows_String_EmptyMultiLineResponse()
258: throws Exception {
259: String lines = "\n\n";
260: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
261: try {
262: fsu.freeSpaceWindows("C:");
263: fail();
264: } catch (IOException ex) {
265: }
266: }
267:
268: public void testGetFreeSpaceWindows_String_InvalidTextResponse()
269: throws Exception {
270: String lines = "BlueScreenOfDeath";
271: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
272: try {
273: fsu.freeSpaceWindows("C:");
274: fail();
275: } catch (IOException ex) {
276: }
277: }
278:
279: public void testGetFreeSpaceWindows_String_NoSuchDirectoryResponse()
280: throws Exception {
281: String lines = " Volume in drive C is HDD\n"
282: + " Volume Serial Number is XXXX-YYYY\n" + "\n"
283: + " Directory of C:\\Documents and Settings\\empty"
284: + "\n";
285: FileSystemUtils fsu = new MockFileSystemUtils(1, lines);
286: try {
287: fsu.freeSpaceWindows("C:");
288: fail();
289: } catch (IOException ex) {
290: }
291: }
292:
293: //-----------------------------------------------------------------------
294: public void testGetFreeSpaceUnix_String_EmptyPath()
295: throws Exception {
296: String lines = "Filesystem 1K-blocks Used Available Use% Mounted on\n"
297: + "xxx:/home/users/s 14428928 12956424 1472504 90% /home/users/s";
298: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
299: try {
300: fsu.freeSpaceUnix("", false, false);
301: fail();
302: } catch (IllegalArgumentException ex) {
303: }
304: try {
305: fsu.freeSpaceUnix("", true, false);
306: fail();
307: } catch (IllegalArgumentException ex) {
308: }
309: try {
310: fsu.freeSpaceUnix("", true, true);
311: fail();
312: } catch (IllegalArgumentException ex) {
313: }
314: try {
315: fsu.freeSpaceUnix("", false, true);
316: fail();
317: } catch (IllegalArgumentException ex) {
318: }
319:
320: }
321:
322: public void testGetFreeSpaceUnix_String_NormalResponseLinux()
323: throws Exception {
324: // from Sourceforge 'GNU bash, version 2.05b.0(1)-release (i386-redhat-linux-gnu)'
325: String lines = "Filesystem 1K-blocks Used Available Use% Mounted on\n"
326: + "/dev/xxx 497944 308528 189416 62% /";
327: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
328: assertEquals(189416L, fsu.freeSpaceUnix("/", false, false));
329: }
330:
331: public void testGetFreeSpaceUnix_String_NormalResponseFreeBSD()
332: throws Exception {
333: // from Apache 'FreeBSD 6.1-RELEASE (SMP-turbo)'
334: String lines = "Filesystem 1K-blocks Used Avail Capacity Mounted on\n"
335: + "/dev/xxxxxx 128990 102902 15770 87% /";
336: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
337: assertEquals(15770L, fsu.freeSpaceUnix("/", false, false));
338: }
339:
340: //-----------------------------------------------------------------------
341: public void testGetFreeSpaceUnix_String_NormalResponseKbLinux()
342: throws Exception {
343: // from Sourceforge 'GNU bash, version 2.05b.0(1)-release (i386-redhat-linux-gnu)'
344: // df, df -k and df -kP are all identical
345: String lines = "Filesystem 1K-blocks Used Available Use% Mounted on\n"
346: + "/dev/xxx 497944 308528 189416 62% /";
347: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
348: assertEquals(189416L, fsu.freeSpaceUnix("/", true, false));
349: }
350:
351: public void testGetFreeSpaceUnix_String_NormalResponseKbFreeBSD()
352: throws Exception {
353: // from Apache 'FreeBSD 6.1-RELEASE (SMP-turbo)'
354: // df and df -k are identical, but df -kP uses 512 blocks (not relevant as not used)
355: String lines = "Filesystem 1K-blocks Used Avail Capacity Mounted on\n"
356: + "/dev/xxxxxx 128990 102902 15770 87% /";
357: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
358: assertEquals(15770L, fsu.freeSpaceUnix("/", true, false));
359: }
360:
361: public void testGetFreeSpaceUnix_String_NormalResponseKbSolaris()
362: throws Exception {
363: // from IO-91 - ' SunOS et 5.10 Generic_118822-25 sun4u sparc SUNW,Ultra-4'
364: // non-kb response does not contain free space - see IO-91
365: String lines = "Filesystem kbytes used avail capacity Mounted on\n"
366: + "/dev/dsk/x0x0x0x0 1350955 815754 481163 63%";
367: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
368: assertEquals(481163L, fsu.freeSpaceUnix("/dev/dsk/x0x0x0x0",
369: true, false));
370: }
371:
372: public void testGetFreeSpaceUnix_String_LongResponse()
373: throws Exception {
374: String lines = "Filesystem 1K-blocks Used Available Use% Mounted on\n"
375: + "xxx-yyyyyyy-zzz:/home/users/s\n"
376: + " 14428928 12956424 1472504 90% /home/users/s";
377: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
378: assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s",
379: false, false));
380: }
381:
382: public void testGetFreeSpaceUnix_String_LongResponseKb()
383: throws Exception {
384: String lines = "Filesystem 1K-blocks Used Available Use% Mounted on\n"
385: + "xxx-yyyyyyy-zzz:/home/users/s\n"
386: + " 14428928 12956424 1472504 90% /home/users/s";
387: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
388: assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", true,
389: false));
390: }
391:
392: public void testGetFreeSpaceUnix_String_EmptyResponse()
393: throws Exception {
394: String lines = "";
395: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
396: try {
397: fsu.freeSpaceUnix("/home/users/s", false, false);
398: fail();
399: } catch (IOException ex) {
400: }
401: try {
402: fsu.freeSpaceUnix("/home/users/s", true, false);
403: fail();
404: } catch (IOException ex) {
405: }
406: try {
407: fsu.freeSpaceUnix("/home/users/s", false, true);
408: fail();
409: } catch (IOException ex) {
410: }
411: try {
412: fsu.freeSpaceUnix("/home/users/s", true, true);
413: fail();
414: } catch (IOException ex) {
415: }
416: }
417:
418: public void testGetFreeSpaceUnix_String_InvalidResponse1()
419: throws Exception {
420: String lines = "Filesystem 1K-blocks Used Available Use% Mounted on\n"
421: + " 14428928 12956424 100";
422: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
423: try {
424: fsu.freeSpaceUnix("/home/users/s", false, false);
425: fail();
426: } catch (IOException ex) {
427: }
428: try {
429: fsu.freeSpaceUnix("/home/users/s", true, false);
430: fail();
431: } catch (IOException ex) {
432: }
433: try {
434: fsu.freeSpaceUnix("/home/users/s", false, true);
435: fail();
436: } catch (IOException ex) {
437: }
438: try {
439: fsu.freeSpaceUnix("/home/users/s", true, true);
440: fail();
441: } catch (IOException ex) {
442: }
443: }
444:
445: public void testGetFreeSpaceUnix_String_InvalidResponse2()
446: throws Exception {
447: String lines = "Filesystem 1K-blocks Used Available Use% Mounted on\n"
448: + "xxx:/home/users/s 14428928 12956424 nnnnnnn 90% /home/users/s";
449: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
450: try {
451: fsu.freeSpaceUnix("/home/users/s", false, false);
452: fail();
453: } catch (IOException ex) {
454: }
455: try {
456: fsu.freeSpaceUnix("/home/users/s", true, false);
457: fail();
458: } catch (IOException ex) {
459: }
460: try {
461: fsu.freeSpaceUnix("/home/users/s", false, true);
462: fail();
463: } catch (IOException ex) {
464: }
465: try {
466: fsu.freeSpaceUnix("/home/users/s", true, true);
467: fail();
468: } catch (IOException ex) {
469: }
470: }
471:
472: public void testGetFreeSpaceUnix_String_InvalidResponse3()
473: throws Exception {
474: String lines = "Filesystem 1K-blocks Used Available Use% Mounted on\n"
475: + "xxx:/home/users/s 14428928 12956424 -1 90% /home/users/s";
476: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
477: try {
478: fsu.freeSpaceUnix("/home/users/s", false, false);
479: fail();
480: } catch (IOException ex) {
481: }
482: try {
483: fsu.freeSpaceUnix("/home/users/s", true, false);
484: fail();
485: } catch (IOException ex) {
486: }
487: try {
488: fsu.freeSpaceUnix("/home/users/s", false, true);
489: fail();
490: } catch (IOException ex) {
491: }
492: try {
493: fsu.freeSpaceUnix("/home/users/s", true, true);
494: fail();
495: } catch (IOException ex) {
496: }
497: }
498:
499: public void testGetFreeSpaceUnix_String_InvalidResponse4()
500: throws Exception {
501: String lines = "Filesystem 1K-blocks Used Available Use% Mounted on\n"
502: + "xxx-yyyyyyy-zzz:/home/users/s";
503: FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
504: try {
505: fsu.freeSpaceUnix("/home/users/s", false, false);
506: fail();
507: } catch (IOException ex) {
508: }
509: try {
510: fsu.freeSpaceUnix("/home/users/s", true, false);
511: fail();
512: } catch (IOException ex) {
513: }
514: try {
515: fsu.freeSpaceUnix("/home/users/s", false, true);
516: fail();
517: } catch (IOException ex) {
518: }
519: try {
520: fsu.freeSpaceUnix("/home/users/s", true, true);
521: fail();
522: } catch (IOException ex) {
523: }
524: }
525:
526: //-----------------------------------------------------------------------
527: static class MockFileSystemUtils extends FileSystemUtils {
528: private final int exitCode;
529: private final byte[] bytes;
530: private final String cmd;
531:
532: public MockFileSystemUtils(int exitCode, String lines) {
533: this (exitCode, lines, null);
534: }
535:
536: public MockFileSystemUtils(int exitCode, String lines,
537: String cmd) {
538: this .exitCode = exitCode;
539: this .bytes = lines.getBytes();
540: this .cmd = cmd;
541: }
542:
543: Process openProcess(String[] params) {
544: if (cmd != null) {
545: assertEquals(cmd, params[params.length - 1]);
546: }
547: return new Process() {
548: public InputStream getErrorStream() {
549: return null;
550: }
551:
552: public InputStream getInputStream() {
553: return new ByteArrayInputStream(bytes);
554: }
555:
556: public OutputStream getOutputStream() {
557: return null;
558: }
559:
560: public int waitFor() throws InterruptedException {
561: return exitCode;
562: }
563:
564: public int exitValue() {
565: return exitCode;
566: }
567:
568: public void destroy() {
569: }
570: };
571: }
572: }
573:
574: }
|