1 18 package org.apache.batik.test; 19 20 import java.io.StringWriter ; 21 import java.io.PrintWriter ; 22 23 84 public abstract class AbstractTest implements Test { 85 88 protected String id = ""; 89 90 94 protected TestSuite parent; 95 96 99 protected String name; 100 101 104 private DefaultTestReport report 105 = new DefaultTestReport(this) { 106 { 107 setErrorCode(ERROR_INTERNAL_TEST_FAILURE); 108 setPassed(false); 109 } 110 }; 111 112 115 public String getName(){ 116 if(name == null){ 117 if (id != null && !"".equals(id)){ 118 return id; 119 } else { 120 return getClass().getName(); 121 } 122 } 123 124 return name; 125 } 126 127 130 public void setName(String name){ 131 this.name = name; 132 } 133 134 137 public String getId(){ 138 return id; 139 } 140 141 144 public String getQualifiedId(){ 145 if(parent == null){ 146 return getId(); 147 } 148 return getParent().getQualifiedId() + "." + getId(); 149 } 150 151 154 public void setId(String id){ 155 if(id == null){ 156 throw new IllegalArgumentException (); 157 } 158 159 this.id = id; 160 } 161 162 public TestSuite getParent(){ 163 return parent; 164 } 165 166 public void setParent(TestSuite parent){ 167 this.parent = parent; 168 } 169 170 179 public TestReport run(){ 180 try{ 181 return runImpl(); 182 } catch(TestErrorConditionException e){ 183 return e.getTestReport(this); 184 } catch(Exception e){ 185 try { 186 187 StringWriter trace = new StringWriter (); 188 e.printStackTrace(new PrintWriter (trace)); 189 190 TestReport.Entry[] entries = new TestReport.Entry[]{ 191 new TestReport.Entry 192 (Messages.formatMessage 193 (TestReport.ENTRY_KEY_INTERNAL_TEST_FAILURE_EXCEPTION_CLASS, null), 194 e.getClass().getName()), 195 new TestReport.Entry 196 (Messages.formatMessage 197 (TestReport.ENTRY_KEY_INTERNAL_TEST_FAILURE_EXCEPTION_MESSAGE, null), 198 e.getMessage()), 199 new TestReport.Entry 200 (Messages.formatMessage 201 (TestReport.ENTRY_KEY_INTERNAL_TEST_FAILURE_EXCEPTION_STACK_TRACE, null), 202 trace.toString()) 203 }; 204 205 report.setDescription(entries); 206 207 }catch(Exception ex){ 208 ex.printStackTrace(); 209 }finally { 210 e.printStackTrace(); 216 System.out.println("SERIOUS ERROR"); 217 return report; 218 } 219 220 } 221 } 222 223 230 public TestReport runImpl() throws Exception { 231 boolean passed = runImplBasic(); 232 233 DefaultTestReport report = new DefaultTestReport(this); 237 if(!passed){ 238 report.setErrorCode(TestReport.ERROR_TEST_FAILED); 239 } 240 report.setPassed(passed); 241 return report; 242 } 243 244 248 public boolean runImplBasic() throws Exception { 249 return true; 250 } 251 252 255 public TestReport reportSuccess() { 256 DefaultTestReport report = new DefaultTestReport(this); 257 report.setPassed(true); 258 return report; 259 } 260 261 264 public TestReport reportError(String errorCode){ 265 DefaultTestReport report = new DefaultTestReport(this); 266 report.setErrorCode(errorCode); 267 report.setPassed(false); 268 return report; 269 } 270 271 274 public void error(String errorCode) throws TestErrorConditionException { 275 throw new TestErrorConditionException(errorCode); 276 } 277 278 281 public void assertNull(Object ref) throws AssertNullException { 282 if(ref != null){ 283 throw new AssertNullException(); 284 } 285 } 286 287 290 public void assertTrue(boolean b) throws AssertTrueException { 291 if (!b){ 292 throw new AssertTrueException(); 293 } 294 } 295 296 301 public void assertEquals(Object ref, Object cmp) throws AssertEqualsException { 302 if(ref == null && cmp != null){ 303 throw new AssertEqualsException(ref, cmp); 304 } 305 306 if(ref != null && !ref.equals(cmp)){ 307 throw new AssertEqualsException(ref, cmp); 308 } 309 } 310 311 public void assertEquals(int ref, int cmp) throws AssertEqualsException { 312 assertEquals(new Integer (ref), new Integer (cmp)); 313 } 314 315 335 public TestReport reportException(String errorCode, 336 Exception e){ 337 DefaultTestReport report 338 = new DefaultTestReport(this); 339 340 StringWriter trace = new StringWriter (); 341 e.printStackTrace(new PrintWriter (trace)); 342 report.setErrorCode(errorCode); 343 344 345 TestReport.Entry[] entries = new TestReport.Entry[]{ 346 new TestReport.Entry 347 (Messages.formatMessage 348 (TestReport.ENTRY_KEY_REPORTED_TEST_FAILURE_EXCEPTION_CLASS, null), 349 e.getClass().getName()), 350 new TestReport.Entry 351 (Messages.formatMessage 352 (TestReport.ENTRY_KEY_REPORTED_TEST_FAILURE_EXCEPTION_MESSAGE, null), 353 e.getMessage()), 354 new TestReport.Entry 355 (Messages.formatMessage 356 (TestReport.ENTRY_KEY_REPORTED_TEST_FAILURE_EXCEPTION_STACK_TRACE, null), 357 trace.toString()) 358 }; 359 report.setDescription(entries); 360 report.setPassed(false); 361 return report; 362 } 363 364 365 } 366 | Popular Tags |