org.apache.commons.latka.validators
Class BaseConditionalValidator

java.lang.Object
  extended by org.apache.commons.latka.validators.BaseValidator
      extended by org.apache.commons.latka.validators.BaseConditionalValidator
All Implemented Interfaces:
Validator
Direct Known Subclasses:
CookieValidator, RegexpValidator, ResponseHeaderValidator, XPathValidator

public abstract class BaseConditionalValidator
extends BaseValidator

This subclass of BaseValidator largely removes the need of a validator to know whether a particular test is supposed to succeed or fail. The validate(Response) method becomes final, and the logic is moved into two abstract methods (see Javadocs): assertTrue(Response) and generateBareExceptionMessage().

Version:
$Id: BaseConditionalValidator.java 155424 2005-02-26 13:09:29Z dirkv $
Author:
MorganDelagrange

Field Summary
protected  boolean _condition
          condition to test against
protected  org.apache.log4j.Category _log
          log4j category for output
 
Fields inherited from class org.apache.commons.latka.validators.BaseValidator
_label
 
Constructor Summary
BaseConditionalValidator(boolean condition)
          A test without a label
BaseConditionalValidator(String label, boolean condition)
          A test with a label
 
Method Summary
abstract  boolean assertTrue(Response response)
          Return true or false, depending on whether or not the test conditions were met.
abstract  String generateBareExceptionMessage()
          The BASE exception message for a subclass of BaseConditionalValidator.
 boolean getCondition()
          Returns whether or not this test is supposed to succeed
 void setCondition(boolean condition)
          Set whether or not this test is supposed to succeed
protected  void throwValidationException()
          Automatically generated exception messages, based on the value of getCondition() and generateBareExceptionMessage().
 void validate(Response response)
          Final method.
 
Methods inherited from class org.apache.commons.latka.validators.BaseValidator
fail, getLabel, setLabel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_log

protected final org.apache.log4j.Category _log
log4j category for output


_condition

protected boolean _condition
condition to test against

Constructor Detail

BaseConditionalValidator

public BaseConditionalValidator(boolean condition)
A test without a label

Parameters:
condition - Whether the test should evaluate to true or false

BaseConditionalValidator

public BaseConditionalValidator(String label,
                                boolean condition)
A test with a label

Parameters:
label - test label
condition - whether the test should evaluate to true or false
Method Detail

setCondition

public void setCondition(boolean condition)
Set whether or not this test is supposed to succeed

Parameters:
condition - true if the test should succeed

getCondition

public boolean getCondition()
Returns whether or not this test is supposed to succeed

Returns:
true if the test is supposed to succeed

validate

public final void validate(Response response)
                    throws ValidationException
Final method. Implement the assertion logic for the test in assertTrue(Response), and the exception generation in generateBareExceptionMessage()

Specified by:
validate in interface Validator
Specified by:
validate in class BaseValidator
Parameters:
response - Response from the HTTP server
Throws:
ValidationException - when a validation fails

assertTrue

public abstract boolean assertTrue(Response response)
                            throws ValidationException
Return true or false, depending on whether or not the test conditions were met. This is regardless of the value of getCondition(), which is handled elsewhere. Note: this method should _only_ throw ValidationExceptions under exceptional circumstances (e.g. invalid regular expressions, IOExceptions, etc). A successful test should only return true or false.

Parameters:
response - HTTP response
Returns:
true if the test conditions were met, false otherwise
Throws:
ValidationException - when a validation fails

generateBareExceptionMessage

public abstract String generateBareExceptionMessage()
The BASE exception message for a subclass of BaseConditionalValidator. Expect that the String "EXPECTED" or "DID NOT EXPECT" will be prepended to this message, depending on the value of getCondition(). For example, if you are validator attempts to match regular expression x, this methods should generate something like " TO MATCH REGULAR EXPRESSION x.".

Returns:
bare exception message, to which will be prepended "EXPECTED" or "DID NOT EXPECT"

throwValidationException

protected void throwValidationException()
                                 throws ValidationException
Automatically generated exception messages, based on the value of getCondition() and generateBareExceptionMessage().

Throws:
ValidationException - generated Exception


Copyright © 2001-2010 The Apache Software Foundation. All Rights Reserved.