1 package hudson.tasks.junit; 2 3 import hudson.model.AbstractBuild; 4 import org.dom4j.Element; 5 6 import java.util.Comparator ; 7 8 13 public final class CaseResult extends TestObject implements Comparable <CaseResult> { 14 private final String className; 15 private final String testName; 16 private final String errorStackTrace; 17 private transient SuiteResult parent; 18 19 private transient ClassResult classResult; 20 21 26 private int failedSince; 27 28 CaseResult(SuiteResult parent, Element testCase) { 29 String cn = testCase.attributeValue("classname"); 30 if(cn==null) 31 cn = parent.getName(); 33 className = cn.replace('/','_'); testName = testCase.attributeValue("name").replace('/','_'); 35 errorStackTrace = getError(testCase); 36 } 37 38 private String getError(Element testCase) { 39 String msg = testCase.elementText("error"); 40 if(msg!=null) 41 return msg; 42 return testCase.elementText("failure"); 43 } 44 45 public String getDisplayName() { 46 return testName; 47 } 48 49 55 public String getName() { 56 return testName; 57 } 58 59 62 public String getSafeName() { 63 StringBuffer buf = new StringBuffer (testName); 64 for( int i=0; i<buf.length(); i++ ) { 65 char ch = buf.charAt(i); 66 if(!Character.isJavaIdentifierPart(ch)) 67 buf.setCharAt(i,'_'); 68 } 69 return buf.toString(); 70 } 71 72 75 public String getClassName() { 76 return className; 77 } 78 79 82 public String getSimpleName() { 83 int idx = className.lastIndexOf('.'); 84 return className.substring(idx+1); 85 } 86 87 90 public String getPackageName() { 91 int idx = className.lastIndexOf('.'); 92 if(idx<0) return "(root)"; 93 else return className.substring(0,idx); 94 } 95 96 public String getFullName() { 97 return className+'.'+getName(); 98 } 99 100 104 public int getFailedSince() { 105 return failedSince; 106 } 107 108 112 public int getAge() { 113 if(isPassed()) 114 return 0; 115 else 116 return getOwner().getNumber()-failedSince+1; 117 } 118 119 @Override 120 public CaseResult getPreviousResult() { 121 SuiteResult pr = parent.getPreviousResult(); 122 if(pr==null) return null; 123 return pr.getCase(getName()); 124 } 125 126 129 public String getErrorStackTrace() { 130 return errorStackTrace; 131 } 132 133 public boolean isPassed() { 134 return errorStackTrace==null; 135 } 136 137 public SuiteResult getParent() { 138 return parent; 139 } 140 141 public AbstractBuild<?,?> getOwner() { 142 return parent.getParent().getOwner(); 143 } 144 145 148 public String getRelativePathFrom(TestObject it) { 149 if(it==this) 150 return "."; 151 152 StringBuffer buf = new StringBuffer (); 154 buf.append(getSafeName()); 155 if(it!=classResult) { 156 buf.insert(0,'/'); 157 buf.insert(0,classResult.getName()); 158 159 PackageResult pkg = classResult.getParent(); 160 if(it!=pkg) { 161 buf.insert(0,'/'); 162 buf.insert(0,pkg.getName()); 163 } 164 } 165 166 return buf.toString(); 167 } 168 169 public void freeze(SuiteResult parent) { 170 this.parent = parent; 171 if(!isPassed() && failedSince==0) { 173 CaseResult prev = getPreviousResult(); 174 if(prev!=null && !prev.isPassed()) 175 this.failedSince = prev.failedSince; 176 else 177 this.failedSince = getOwner().getNumber(); 178 } 179 } 180 181 public int compareTo(CaseResult that) { 182 return this.getFullName().compareTo(that.getFullName()); 183 } 184 185 public Status getStatus() { 186 CaseResult pr = getPreviousResult(); 187 if(pr==null) { 188 return isPassed() ? Status.PASSED : Status.FAILED; 189 } 190 191 if(pr.isPassed()) { 192 return isPassed() ? Status.PASSED : Status.REGRESSION; 193 } else { 194 return isPassed() ? Status.FIXED : Status.FAILED; 195 } 196 } 197 198 void setClass(ClassResult classResult) { 199 this.classResult = classResult; 200 } 201 202 205 public enum Status { 206 209 PASSED("result-passed","Passed",true), 210 213 FAILED("result-failed","Failed",false), 214 217 FIXED("result-fixed","Fixed",true), 218 221 REGRESSION("result-regression","Regression",false); 222 223 private final String cssClass; 224 private final String message; 225 public final boolean isOK; 226 227 Status(String cssClass, String message, boolean OK) { 228 this.cssClass = cssClass; 229 this.message = message; 230 isOK = OK; 231 } 232 233 public String getCssClass() { 234 return cssClass; 235 } 236 237 public String getMessage() { 238 return message; 239 } 240 241 public boolean isRegression() { 242 return this==REGRESSION; 243 } 244 } 245 246 249 static final Comparator <CaseResult> BY_AGE = new Comparator <CaseResult>() { 250 public int compare(CaseResult lhs, CaseResult rhs) { 251 return lhs.getAge()-rhs.getAge(); 252 } 253 }; 254 } 255 | Popular Tags |