1 11 12 package org.eclipse.jdt.internal.junit.model; 13 14 import org.eclipse.core.runtime.Assert; 15 16 import org.eclipse.jdt.junit.model.ITestElement; 17 import org.eclipse.jdt.junit.model.ITestElementContainer; 18 import org.eclipse.jdt.junit.model.ITestRunSession; 19 20 21 public abstract class TestElement implements ITestElement { 22 public final static class Status { 23 public static final Status RUNNING_ERROR= new Status("RUNNING_ERROR", 5); public static final Status RUNNING_FAILURE= new Status("RUNNING_FAILURE", 6); public static final Status RUNNING= new Status("RUNNING", 3); 27 public static final Status ERROR= new Status("ERROR", ITestRunListener2.STATUS_ERROR); public static final Status FAILURE= new Status("FAILURE", ITestRunListener2.STATUS_FAILURE); public static final Status OK= new Status("OK", ITestRunListener2.STATUS_OK); public static final Status NOT_RUN= new Status("NOT_RUN", 4); 32 private static final Status[] OLD_CODE= { OK, ERROR, FAILURE}; 33 34 private final String fName; 35 private final int fOldCode; 36 37 private Status(String name, int oldCode) { 38 fName= name; 39 fOldCode= oldCode; 40 } 41 42 public int getOldCode() { 43 return fOldCode; 44 } 45 46 public String toString() { 47 return fName; 48 } 49 50 51 52 public boolean isOK() { 53 return this == OK || this == RUNNING || this == NOT_RUN; 54 } 55 56 public boolean isFailure() { 57 return this == FAILURE || this == RUNNING_FAILURE; 58 } 59 60 public boolean isError() { 61 return this == ERROR || this == RUNNING_ERROR; 62 } 63 64 public boolean isErrorOrFailure() { 65 return isError() || isFailure(); 66 } 67 68 69 70 public boolean isNotRun() { 71 return this == NOT_RUN; 72 } 73 74 public boolean isRunning() { 75 return this == RUNNING || this == RUNNING_FAILURE || this == RUNNING_ERROR; 76 } 77 78 public boolean isDone() { 79 return this == OK || this == FAILURE || this == ERROR; 80 } 81 82 public static Status combineStatus(Status one, Status two) { 83 Status progress= combineProgress(one, two); 84 Status error= combineError(one, two); 85 return combineProgressAndErrorStatus(progress, error); 86 } 87 88 private static Status combineProgress(Status one, Status two) { 89 if (one.isNotRun() && two.isNotRun()) 90 return NOT_RUN; 91 else if (one.isDone() && two.isDone()) 92 return OK; 93 else if (!one.isRunning() && !two.isRunning()) 94 return OK; else 96 return RUNNING; 97 } 98 99 private static Status combineError(Status one, Status two) { 100 if (one.isError() || two.isError()) 101 return ERROR; 102 else if (one.isFailure() || two.isFailure()) 103 return FAILURE; 104 else 105 return OK; 106 } 107 108 private static Status combineProgressAndErrorStatus(Status progress, Status error) { 109 if (progress.isDone()) { 110 if (error.isError()) 111 return ERROR; 112 if (error.isFailure()) 113 return FAILURE; 114 return OK; 115 } 116 117 if (progress.isNotRun()) { 118 return NOT_RUN; 120 } 121 122 if (error.isError()) 124 return RUNNING_ERROR; 125 if (error.isFailure()) 126 return RUNNING_FAILURE; 127 return RUNNING; 129 } 130 131 135 public static Status convert(int oldStatus) { 136 return OLD_CODE[oldStatus]; 137 } 138 139 public Result convertToResult() { 140 if (isNotRun()) 141 return Result.UNDEFINED; 142 if (isError()) 143 return Result.ERROR; 144 if (isFailure()) 145 return Result.FAILURE; 146 if (isRunning()) { 147 return Result.UNDEFINED; 148 } 149 return Result.OK; 150 } 151 152 public ProgressState convertToProgressState() { 153 if (isRunning()) { 154 return ProgressState.RUNNING; 155 } 156 if (isDone()) { 157 return ProgressState.COMPLETED; 158 } 159 return ProgressState.NOT_STARTED; 160 } 161 162 } 163 164 private final TestSuiteElement fParent; 165 private final String fId; 166 private String fTestName; 167 168 private Status fStatus; 169 private String fTrace; 170 private String fExpected; 171 private String fActual; 172 173 178 public TestElement(TestSuiteElement parent, String id, String testName) { 179 Assert.isNotNull(id); 180 Assert.isNotNull(testName); 181 fParent= parent; 182 fId= id; 183 fTestName= testName; 184 fStatus= Status.NOT_RUN; 185 if (parent != null) 186 parent.addChild(this); 187 } 188 189 192 public ProgressState getProgressState() { 193 return getStatus().convertToProgressState(); 194 } 195 196 199 public Result getTestResult(boolean includeChildren) { 200 return getStatus().convertToResult(); 201 } 202 203 206 public ITestRunSession getTestRunSession() { 207 return getRoot().getTestRunSession(); 208 } 209 210 213 public ITestElementContainer getParentContainer() { 214 if (fParent instanceof TestRoot) { 215 return getTestRunSession(); 216 } 217 return fParent; 218 } 219 220 223 public FailureTrace getFailureTrace() { 224 Result testResult= getTestResult(false); 225 if (testResult == Result.ERROR || testResult == Result.FAILURE) { 226 return new FailureTrace(fTrace, fExpected, fActual); 227 } 228 return null; 229 } 230 231 234 public TestSuiteElement getParent() { 235 return fParent; 236 } 237 238 public String getId() { 239 return fId; 240 } 241 242 public String getTestName() { 243 return fTestName; 244 } 245 246 public void setName(String name) { 247 fTestName= name; 248 } 249 250 public void setStatus(Status status) { 251 254 fStatus= status; 255 TestSuiteElement parent= getParent(); 256 if (parent != null) 257 parent.childChangedStatus(this, status); 258 } 259 260 public void setStatus(Status status, String trace, String expected, String actual) { 261 fTrace= trace; 264 fExpected= expected; 265 fActual= actual; 266 setStatus(status); 267 } 268 269 public Status getStatus() { 270 return fStatus; 271 } 272 273 public String getTrace() { 274 return fTrace; 275 } 276 277 public String getExpected() { 278 return fExpected; 279 } 280 281 public String getActual() { 282 return fActual; 283 } 284 285 public boolean isComparisonFailure() { 286 return fExpected != null && fActual != null; 287 } 288 289 293 public String getClassName() { 294 return extractClassName(getTestName()); 295 } 296 297 private String extractClassName(String testNameString) { 298 int index= testNameString.indexOf('('); 299 if (index < 0) 300 return testNameString; 301 testNameString= testNameString.substring(index + 1); 302 testNameString= testNameString.replace('$', '.'); return testNameString.substring(0, testNameString.indexOf(')')); 304 } 305 306 public TestRoot getRoot() { 307 return getParent().getRoot(); 308 } 309 310 public String toString() { 311 return getProgressState() + " - " + getTestResult(true); } 313 } 314 | Popular Tags |