1 import java.io.*; 2 import java.util.*; 3 4 /** 5 * Little program that checks to see if every file ending in .diff in 6 * the current directory is empty. For each file, displays whether or not 7 * it is empty. If any file is non-empty, exits with an error exit status, 8 * else exits with a 0 exit status. 9 * 10 * Usage: java VerifyDiffs [--show_all] 11 * 12 * If --show_all option is used, all tests that pass will also be displayed. 13 * If --show_all is not used, and all tests pass, then there will be no 14 * output. 15 */ 16 public class VerifyDiffs { 17 private static boolean show_all = false; 18 19 private static void parseArgs(String[] args) { 20 for (String s : args) { 21 if (s.equals("--show_all")) { 22 VerifyDiffs.show_all = true; 23 } 24 } 25 } 26 27 public static void main(String[] args) { 28 parseArgs(args); 29 30 int passCount = 0; 31 int failCount = 0; 32 boolean pass = true; 33 try { 34 35 File dir = new File("."); 36 37 List<File> allDiffs = new ArrayList<File>(); 38 gatherDiffs(allDiffs, dir); 39 Collections.sort(allDiffs); 40 for (File f : allDiffs) { 41 String fileName = f.toString(); 42 if (fileName.startsWith("./")) { 43 fileName = fileName.substring(2); 44 } 45 FileReader fr = new FileReader(f); 46 if (fr.read() != -1) { // if not empty, output error message 47 System.out.println(fileName + " ...FAILED"); 48 pass = false; 49 failCount++; 50 } else { 51 if (VerifyDiffs.show_all) { 52 System.out.println(fileName + " ...OK"); 53 } 54 passCount++; 55 } 56 fr.close(); 57 } 58 } catch (Exception e) { 59 System.out.println("verify diffs failed due to exception: " 60 + e.getMessage()); 61 pass = false; 62 } 63 64 System.out.println("Passed: " + passCount + " Failed: " + failCount); 65 if (pass) { 66 if (VerifyDiffs.show_all) { 67 System.out.println("All tests succeeded."); 68 } 69 } else { 70 System.out.println("Tests failed."); 71 System.exit(1); 72 } 73 } 74 75 /** 76 * Recursively adds all files in directory dir ending in .diff to 77 * the list diffs. 78 * 79 * @param diffs the array to place all diff files in 80 * @param dir the directory to start gathering diffs 81 */ 82 private static void gatherDiffs(List<File> diffs, File dir) { 83 for (File f : dir.listFiles()) { 84 if (f.toString().endsWith(".diff")) { 85 diffs.add(f); 86 } 87 if (f.isDirectory()) { 88 gatherDiffs(diffs, f); 89 } 90 } 91 } 92 } 93