1 package jester; 2 3 import java.awt.Color ; 4 import java.io.PrintWriter ; 5 import java.util.*; 6 7 public class RealReport implements Report { 8 private Configuration myConfiguration; 9 private PrintWriter myOutput; 10 private int myTotalNumberOfChangesThatCausedTestsToFail = 0; 11 private int myTotalNumberOfChangesThatDidNotCauseTestsToFail = 0; 12 13 private List myFileChangesThatDidNotCauseTestsToFail = new Vector(); 14 private int myNumberOfFileChangesThatCausedTestsToFail = 0; 15 private IgnoreListDocument myOriginalContents; 16 private String mySourceFileName; 17 private XMLReportWriter myXMLReportWriter; 18 private ProgressReporter myProgressReporter; 19 20 public RealReport(Configuration configuration, PrintWriter aPrintWriter, XMLReportWriter anXMLReportWriter, ProgressReporter aProgressReporter) { 21 super(); 22 myConfiguration = configuration; 23 myOutput = aPrintWriter; 24 myXMLReportWriter = anXMLReportWriter; 25 myProgressReporter = aProgressReporter; 26 } 27 28 public void setNumberOfFilesThatWillBeTested(int numberOfFilesThatWillBeTested) { 29 myProgressReporter.setMaximum(numberOfFilesThatWillBeTested); 30 } 31 32 private void reportFileProgress() { 33 myProgressReporter.progress(); 34 } 35 private void redBar() { 36 myProgressReporter.setColor(Color.red); 37 } 38 private void greenBar() { 39 myProgressReporter.setColor(Color.green); 40 } 41 42 public void startFile(String sourceFileName, IgnoreListDocument originalContents) throws SourceChangeException { 43 if (mySourceFileName != null) { 44 throw new SourceChangeException("Cannot start a file until finished previous one (tried to start " + sourceFileName + " when already doing " + mySourceFileName + ")"); 45 } 46 myNumberOfFileChangesThatCausedTestsToFail = 0; 47 myFileChangesThatDidNotCauseTestsToFail = new Vector(); 48 mySourceFileName = sourceFileName; 49 myOriginalContents = originalContents; 50 } 51 52 public void finishFile(String sourceFileName) throws SourceChangeException { 53 if (!mySourceFileName.equals(sourceFileName)) { 54 throw new SourceChangeException("Cannot finish a different file to the one you started (finished " + sourceFileName + " but expected " + mySourceFileName + ")"); 55 } 56 reportFileProgress(); 57 printFileChanges(); 58 writeChangesToXMLFile(); 59 mySourceFileName = null; 60 myOriginalContents = null; 61 } 62 63 public void changeThatCausedTestsToFail(int indexOfChange, String valueChangedFrom, String valueChangedTo) throws SourceChangeException { 64 if (mySourceFileName == null) { 65 throw new SourceChangeException("Cannot report change of file haven't started (internal error1)"); 66 } 67 greenBar(); 68 ReportItem aReportItem = new ReportItem(mySourceFileName, myOriginalContents, indexOfChange, valueChangedFrom, valueChangedTo); 69 myProgressReporter.setText(aReportItem.toString()); 70 myNumberOfFileChangesThatCausedTestsToFail++; 71 myTotalNumberOfChangesThatCausedTestsToFail++; 72 } 73 74 public void changeThatDidNotCauseTestsToFail(int indexOfChange, String valueChangedFrom, String valueChangedTo) throws SourceChangeException { 75 if (mySourceFileName == null) { 76 throw new SourceChangeException("Cannot report change of file haven't started (internal error2)"); 77 } 78 redBar(); 79 myTotalNumberOfChangesThatDidNotCauseTestsToFail++; 80 ReportItem aReportItem = new ReportItem(mySourceFileName, myOriginalContents, indexOfChange, valueChangedFrom, valueChangedTo); 81 myProgressReporter.setText(aReportItem.toString()); 82 83 if (myConfiguration.shouldReportEagerly()) { 84 myOutput.println(aReportItem.toString()); 85 myOutput.flush(); 86 } 87 88 myFileChangesThatDidNotCauseTestsToFail.add(aReportItem); 89 } 90 91 public String toString() { 92 String summary = myTotalNumberOfChangesThatDidNotCauseTestsToFail + " mutations survived out of " + totalNumberOfChanges() + " changes. Score = " + totalScore(); 93 return summary; 94 } 95 96 private int fileNumberOfChanges() { 97 return myNumberOfFileChangesThatCausedTestsToFail + fileNumberOfChangesThatDidNotCauseTestsToFail(); 98 } 99 100 public int fileScore() { 101 return score(fileNumberOfChanges(), fileNumberOfChangesThatDidNotCauseTestsToFail()); 102 } 103 104 private void printFileChanges() { 105 String summary = "For File " + mySourceFileName + ": " + fileNumberOfChangesThatDidNotCauseTestsToFail() + " mutations survived out of " + fileNumberOfChanges() + " changes. Score = " + fileScore(); 106 107 Object [] sortedReportItems = sortedReportItems(); 108 StringBuffer result = new StringBuffer (); 109 result.append(summary + "\n"); 110 for (int i = 0; i < sortedReportItems.length; i++) { 111 result.append(sortedReportItems[i] + "\n"); 112 113 } 114 myOutput.println(result.toString()); 115 myOutput.flush(); 116 } 117 118 123 private Comparator reportItemComparitor() { 124 return new Comparator() { 125 public int compare(Object o1, Object o2) { 126 ReportItem ri1 = (ReportItem) o1; 127 ReportItem ri2 = (ReportItem) o2; 128 return ri1.compareToReportItem(ri2); 129 } 130 }; 131 } 132 133 private int score(int numberOfChanges, int numberOfChangesThatDidNotCauseTestsToFail) { 134 int score = INITIAL_SCORE; 135 if (numberOfChanges != 0) { 136 score = 100 - ((numberOfChangesThatDidNotCauseTestsToFail * 100) / numberOfChanges); 137 } 138 return score; 139 } 140 141 private int totalNumberOfChanges() { 142 return myTotalNumberOfChangesThatCausedTestsToFail + myTotalNumberOfChangesThatDidNotCauseTestsToFail; 143 } 144 145 public int totalScore() { 146 return score(totalNumberOfChanges(), myTotalNumberOfChangesThatDidNotCauseTestsToFail); 147 } 148 149 private int fileNumberOfChangesThatDidNotCauseTestsToFail() { 150 return myFileChangesThatDidNotCauseTestsToFail.size(); 151 } 152 153 private Object [] sortedReportItems() { 154 Object [] result = myFileChangesThatDidNotCauseTestsToFail.toArray(); 155 Arrays.sort(result, reportItemComparitor()); 156 return result; 157 } 158 159 private void writeChangesToXMLFile() throws SourceChangeException { 160 myXMLReportWriter.writeXMLReport(sortedReportItems(), mySourceFileName, fileNumberOfChangesThatDidNotCauseTestsToFail(), fileNumberOfChanges(), fileScore()); 161 } 162 } | Popular Tags |