1 import java.io.BufferedReader; 2 import java.io.FileReader; 3 import java.io.IOException; 4 5 /** 6 * TestWrapper is a class that examines JUnit test results and outputs reports 7 * only if one or more tests failed. 8 */ 9 public class TestWrapper { 10 11 /** 12 * Prints each file listed in args to standard out, only if it 13 * contained a failed JUnit test. 14 * @param args the file names of the test results to examine 15 */ 16 public static void main(String[] args) { 17 for (String filename : args) { 18 try { 19 if (containsJUnitFailure(filename)) { 20 System.out.println(); 21 System.out.println("Failed tests in: " + filename); 22 print(filename); 23 } 24 } catch (Exception e) { 25 System.out.println("Problem reading file " + filename); 26 e.printStackTrace(System.out); 27 } 28 } 29 } 30 31 /** 32 * Examines the given file and displays it if there are failed tests. 33 * 34 * @param filename the name of the file to examine. 35 */ 36 private static boolean containsJUnitFailure(String filename) throws IOException { 37 BufferedReader in = new BufferedReader(new FileReader(filename)); 38 String line = in.readLine(); 39 while (line != null) { 40 if (line.contains("FAILED")) { 41 in.close(); 42 return true; 43 } 44 line = in.readLine(); 45 } 46 in.close(); 47 return false; 48 } 49 50 /** 51 * Prints the specified file. 52 * @param filename the name of the file to print 53 * @throws Exception if an error occurs 54 */ 55 private static void print(String filename) throws IOException { 56 BufferedReader in = new BufferedReader(new FileReader(filename)); 57 String line = in.readLine(); 58 while (line != null) { 59 System.out.println(line); 60 line = in.readLine(); 61 } 62 in.close(); 63 } 64 } 65