Monday, February 02, 2009

List Files in a directory


I remembered a good question which I was asked in one of my interviews some time back...

Write a program to list the files in a directory. The listed files should be greater than 1000 KB and it should be sorted.

This should take care of recursion also. Also write test cases for the same.

Here is what I came up with...

Java Program to List Files in a Directory.

/*
This program prints all the files in a directory(including sub directories recursively)
- more than 1000 KB - 1 KB = 1000 Bytes, 1000 KB = 1024000 bytes
- in descending order
*/

import java.io.*;
import java.util.*;

public class listFiles{
// Vector to hold all the list of files.
static Vector v1 = new Vector();
static boolean validdir = true;
public static void main(String[] args) {
// Get the directory name from the command line
if (args.length > 0 ) {
File dirName = new File(args[0].trim());
listFiles.listFilesinDir(dirName);
} else {
System.out.println("Enter a directory name.\n Usage: listFiles dirName");
validdir = false;
}
if (validdir) {
// Sort the Vector v1
Comparator comparator = Collections.reverseOrder();
// System.out.println("The List of files before sorting: " + v1);

Collections.sort(v1,comparator);
//System.out.println("The List of files after sorting in descending order : " + v1);
System.out.println("The Total Number of Files are: " + v1.size());

for (Enumeration e = v1.elements(); e.hasMoreElements();) {
System.out.println(e.nextElement());
}
}
} // end main()
public static void listFilesinDir(File fin) {
// Throw error if the directory does not exist.
if (!fin.exists()) {
validdir = false;
System.out.println(fin.getName() + " does not exist!!!");
return;
}
if (fin.isFile()) {
// 1 KB = 1000 Bytes, 1000 KB = 1024000 bytes
if (fin.length() > 1024000) {
// To print all files remove the comment in the below line.
// System.out.println(fin.getName());
// Add all the files into the vector.
listFiles.v1.add(fin.getName());
}
}
else if (fin.isDirectory()) {
String[] files = fin.list();
if (files == null) return;
for (int i=0; i


Here is the list of TestCases

A B C D E
1 Nu Test Case For Description Expected Results Remarks
2 1 Valid Dir Name Give a Valid Dir Name Should give the list of files Assume that size and ascending is taken care
3 2 Valid Dir Name - Order Check for Descending order The listed files should be in descending order Assume that size is taken care
4 3 Valid Dir Name - Size Check for the Size of Files The listed files should be greater than 1000 KB only Assume that ascending is taken care
5 4 Size - Boundary values Have a file of Exactly 1000 KB This file should be listed Assume that ascending is taken care
6 5 Size - Boundary values Have a file of Exactly 1001 KB This file should be listed Assume that ascending is taken care
7 6 Size - Boundary values Have a file of Exactly 999 KB This file should NOT be listed
8 7 Size - Low size Have a empty file/0 size This file should NOT be listed
9 8 Size - Low size Have a file of 1 KB size This file should NOT be listed
10 9 Size - Huge Size Have a file of 20000 KB This file should be listed Assume that ascending is taken care
11 10 Order Have files with names - a.txt, aa.txt, aaa.txt Observe that these should be sorted in descending order Assume that size is taken care
12 11 Order Have files with names - a.txt, b.txt, c.txt Observe that these should be sorted in descending order Assume that size is taken care
13 12 Dir Name -Space Let the directory name have space - ex: "Program Files" Should give the list of files Assume that size and ascending is taken care
14 13 Dir Name - Case Sensitive Have directories with case sensitive - ex: myDir1, mYdIR2 etc Should give the list of files Assume that size and ascending is taken care
15 14 Sub Directories Have many sub directories The sub directory names should NOT be listed
16 15 Sub Directories Have many sub directories The files inside the sub directories should be listed Assume that size and ascending is taken care
17 16 Sub Directories Have a sub directory with the "same name" as parent The files inside the sub directories should be listed Assume that size and ascending is taken care
18 17 Hidden Files Have some hidden files in the dir These should be listed Assume that size and ascending is taken care
19 18 Hidden Directory Specify a hidden dir as input The files inside a hidden dir should be listed Assume that size and ascending is taken care
20 19 Hidden sub directories Have some hidden sub directories inside the parent dir The files inside a hidden dir should be listed Assume that size and ascending is taken care
21 20 Read-Only Files Have some read only files in the dir These should be listed
22 21 Read-only Directory Specify a read-only dir as input The files inside a read-only dir should be listed Assume that size and ascending is taken care
23 22 Read-only Sub directories Have some read-only sub directories inside the parent dir The files inside a read-only dir should be listed Assume that size and ascending is taken care
24 23 Empty Dir Give a valid dir name - but empty one No Files should be listed
25 24 Dir Name - Special Chars Have some special chars in the name of the dir Should give the list of files Assume that size and ascending is taken care
26 25 Sub Directories - Breadth Have many sub directories in the parent dir… Have a breadth of say 10-20 dirs.
and files inside those directories
Should give the list of files Assume that size and ascending is taken care
27 26 Sub Directories - Depth Have a sub directoy inside a sub directory inside a sub dir…. Have a depth of say 10-20 dirs.
and files inside those directories
Should give the list of files Assume that size and ascending is taken care
28 27 Shared Dir in Windows Specify a shared directory Should give the list of files Assume that size and ascending is taken care
29 28 Specify the URI as dir name A dir on the intranet - ex: \\10.192.23.212\myDir1 Should give the list of files if accessible Assume that size and ascending is taken care
30 29 Dir Name - "." Specify "." as the dir name Should give the list of files in the current dir Assume that size and ascending is taken care
31 30 Dir Name - ".." Specify ".." as the dir name Should give the list of files in the parent dir Assume that size and ascending is taken care
32 31 Dir Name - Absolute Path - Forward Slash Specify "C:/Testing/MyDir1" as the dir name Should give the list of files Assume that size and ascending is taken care
33 32 Dir Name - Absolute Path - Back Slash Specify "C:\Testing\MyDir1" as the dir name Should give the list of files Assume that size and ascending is taken care
34 33 Dir Name - Relative Path - Forward slash Specify "MyDir1/MyDir2/MyDir3" as the dir name Should give the list of files Assume that size and ascending is taken care
35 34 Dir Name - Relative Path - Backward slash Specify "MyDir1\MyDir2\MyDir3" as the dir name Should give the list of files Assume that size and ascending is taken care
36 35 Dir Name - Relative Path Specify "..\..\MyDir2\MyDir3" as the dir name Should give the list of files Assume that size and ascending is taken care
37 36 Dir Name - integer The dir name is "12345" Should give the list of files Assume that size and ascending is taken care
38 37 Dir Name - very long name Ex: The dir name is "Thisismydirectoryandthenameisverybig" Should give the list of files Assume that size and ascending is taken care
39 38 Negative Test Specify some file name as input Throw error message
40 39 Negative Test Specify some non existing dir name Throw error message
41 40 Negative Test Specify null Throw error message
42 41 Formats All possible format files should be listed - ex: ".zip" ".doc" ".pdf" etc Should give the list of files Assume that size and ascending is taken care
43 42 Negative Test Specify a zip file as input Throw error message
44 43 Negative Test Specify the dir name + some blanks Should trim
45




46
Unix/Linux


47 44 Perform all the above tests for Linux/Unix; Also perform the below special cases for Unix


48




49 45 Dir Name - Mounted dir Specify a mounted dir as the input dir name Should give the list of files Assume that size and ascending is taken care
50 46 Dir Name - Case Sensitive Have directories with case sensitive - ex: myDir1, mYdIR2 etc Should give the list of files Assume that size and ascending is taken care
51 47 Read-Only Files Have some read only files in the dir These should be listed
52 48 Read-only Directory Specify a read-only dir as input The files inside a read-only dir should be listed Assume that size and ascending is taken care
53 49 Read-only Sub directories Have some read-only sub directories inside the parent dir The files inside a read-only dir should be listed Assume that size and ascending is taken care
54 50 Dir Name - Special Chars Have some special chars in the name of the dir Should give the list of files Assume that size and ascending is taken care
55 51 Dir Name - "." Specify "." as the dir name Should give the list of files in the current dir Assume that size and ascending is taken care
56 52 Dir Name - ".." Specify ".." as the dir name Should give the list of files in the parent dir Assume that size and ascending is taken care
57 53 Dir Name - Absolute Path - Forward Slash Specify "/opt/testing/MyDir1" as the dir name Should give the list of files Assume that size and ascending is taken care
58 54 Dir Name - Absolute Path - Back Slash Specify "opt\testing\MyDir1" as the dir name Should give the list of files Assume that size and ascending is taken care
59 55 Dir Name - Relative Path - Forward slash Specify "MyDir1/MyDir2/MyDir3" as the dir name Should give the list of files Assume that size and ascending is taken care
60 56 Dir Name - Relative Path - Backward slash Specify "MyDir1\MyDir2\MyDir3" as the dir name Should give the list of files Assume that size and ascending is taken care
61 57 Dir Name - Relative Path Specify "..\..\MyDir2\MyDir3" as the dir name Should give the list of files Assume that size and ascending is taken care
62




63
Performance Testing


64 58 50,000 Files( all > 1000 KB) Have a dir with 50,000 Files Should give the list of files - check for breakdown Assume that size and ascending is taken care
65 59 50,000 Files > 1000 KB + 50,000 Files < 1000 KB Have a dir with 50,000 Files > 1000 KB and 50,000 Files < 1000 KB Should give the list of files - check for breakdown Assume that size and ascending is taken care
66




67 60 10,000 Sub directories Have 10,000 sub directories with files Should give the list of files - check for breakdown Assume that size and ascending is taken care








No comments: