1 16 17 package org.apache.commons.latka.validators; 18 19 import org.apache.commons.latka.ValidationException; 20 import org.apache.commons.latka.http.Response; 21 22 import org.apache.log4j.Category; 23 24 36 public abstract class BaseConditionalValidator extends BaseValidator { 37 39 40 protected final Category _log = Category.getInstance( 41 BaseConditionalValidator.class); 42 43 protected boolean _condition = true; 44 45 47 52 public BaseConditionalValidator(boolean condition) { 53 this(null, condition); 54 } 55 56 62 public BaseConditionalValidator(String label, boolean condition) { 63 super(label); 64 _condition = condition; 65 } 66 67 69 74 public void setCondition(boolean condition) { 75 _condition = condition; 76 } 77 78 83 public boolean getCondition() { 84 return _condition; 85 } 86 87 95 public final void validate(Response response) throws ValidationException { 96 boolean assertion = assertTrue(response); 97 98 if ((assertion == true) && (_condition == false)) { 99 throwValidationException(); 100 } else if ((assertion == false) && (_condition == true)) { 101 throwValidationException(); 102 } 103 } 104 105 121 public abstract boolean assertTrue(Response response) throws 122 ValidationException; 123 124 137 public abstract String generateBareExceptionMessage(); 138 139 147 protected void throwValidationException() throws ValidationException { 148 149 if (_condition == true) { 150 fail("EXPECTED" + generateBareExceptionMessage()); 151 } else { 152 fail("DID NOT EXPECT" + generateBareExceptionMessage()); 153 } 154 155 } 156 157 } 158 | Popular Tags |