1 11 package org.jboss.portal.junit.result; 12 13 import junit.framework.AssertionFailedError; 14 import org.apache.log4j.Logger; 15 import org.jboss.portal.junit.Result; 16 import org.jboss.portal.common.junit.ExtendedAssert; 17 18 22 public class AssertResult implements Result 23 { 24 25 private static Logger log = Logger.getLogger(AssertResult.class); 26 27 private int count; 28 private Throwable throwable; 29 30 public AssertResult() 31 { 32 this.count = 0; 33 this.throwable = null; 34 } 35 36 43 public void execute(Test test) throws IllegalArgumentException , IllegalStateException 44 { 45 if (test == null) 46 { 47 throw new IllegalArgumentException ("Test must not be null"); 48 } 49 if (throwable != null) 50 { 51 throw new IllegalStateException ("Already recorded a throwable, should not be called"); 52 } 53 try 54 { 55 test.run(); 56 } 57 catch (AssertionFailedError afe) 58 { 59 log.error("Failure during test", afe); 60 throwable = afe; 61 } 62 catch (Exception e) 63 { 64 log.error("Error during test", e); 65 throwable = e; 66 } 67 finally 68 { 69 count++; 70 } 71 } 72 73 79 public void executeIfNotFailed(Test test) throws IllegalArgumentException 80 { 81 if (!isFailed()) 82 { 83 execute(test); 84 } 85 } 86 87 90 public int getCount() 91 { 92 return count; 93 } 94 95 98 public Throwable getThrowable() 99 { 100 return throwable; 101 } 102 103 106 public boolean isFailed() 107 { 108 return throwable != null; 109 } 110 111 118 public void check(int expectedCount) throws Throwable 119 { 120 if (throwable != null) 121 { 122 throw throwable; 123 } 124 if (count < expectedCount) 125 { 126 throw new AssertionFailedError("Too few test have been executed, " + 127 "expectedCount=" + expectedCount + " and effective count=" + count); 128 } 129 if (count > expectedCount) 130 { 131 throw new AssertionFailedError("Too many test have been executed, " + 132 "expectedCount=" + expectedCount + " and effective count=" + count); 133 } 134 } 135 136 public static abstract class Test extends ExtendedAssert 137 { 138 public abstract void run() throws Exception ; 139 } 140 } 141 | Popular Tags |