001    /*
002     * Copyright 1999-2001,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.apache.commons.latka.validators;   
018    
019    import org.apache.commons.latka.ValidationException;
020    import org.apache.commons.latka.http.Response;
021    
022    import org.apache.log4j.Category;
023    
024    /**
025     * This subclass of BaseValidator largely removes the need of 
026     * a validator to know whether a particular test is supposed
027     * to succeed or fail.  The validate(Response) method 
028     * becomes final, and the logic is moved into two 
029     * abstract methods (see Javadocs): assertTrue(Response)
030     * and generateBareExceptionMessage(). 
031     * 
032     * @author MorganDelagrange
033     * @version
034     * $Id: BaseConditionalValidator.java 155424 2005-02-26 13:09:29Z dirkv $
035     */
036    public abstract class BaseConditionalValidator extends BaseValidator {
037        // ------------------------------------------------------ Instance Variables
038    
039        /** log4j category for output */
040        protected final Category _log = Category.getInstance(
041            BaseConditionalValidator.class);
042        /** condition to test against */
043        protected boolean _condition = true;
044    
045        // ----------------------------------------------------------- Constructors
046    
047        /**
048         * A test without a label
049         * 
050         * @param condition Whether the test should evaluate to true or false
051         */
052        public BaseConditionalValidator(boolean condition) {
053            this(null, condition);
054        }
055    
056        /**
057         * A test with a label
058         * 
059         * @param label     test label
060         * @param condition whether the test should evaluate to true or false
061         */
062        public BaseConditionalValidator(String label, boolean condition) {
063            super(label);
064            _condition = condition;
065        }
066    
067        // ---------------------------------------------------------------- Methods
068    
069        /**
070         * Set whether or not this test is supposed to succeed
071         * 
072         * @param condition true if the test should succeed
073         */
074        public void setCondition(boolean condition) {
075            _condition = condition;
076        }
077    
078        /**
079         * Returns whether or not this test is supposed to succeed
080         * 
081         * @return true if the test is supposed to succeed
082         */
083        public boolean getCondition() {
084            return _condition;
085        }
086    
087        /**
088         * Final method.  Implement the assertion logic for
089         * the test in assertTrue(Response), and the exception generation
090         * in generateBareExceptionMessage()
091         * 
092         * @param response Response from the HTTP server
093         * @throws ValidationException when a validation fails
094         */
095        public final void validate(Response response) throws ValidationException {
096            boolean assertion = assertTrue(response);
097    
098            if ((assertion == true) && (_condition == false)) {
099                throwValidationException();
100            } else if ((assertion == false) && (_condition == true)) {
101                throwValidationException();
102            }
103        }
104    
105        /**
106         * Return true or false, depending on whether or not
107         * the test conditions were met.  This
108         * is <i>regardless</i> of the value of getCondition(),
109         * which is handled elsewhere.
110         * 
111         * Note: this method
112         * should _only_ throw ValidationExceptions under 
113         * exceptional circumstances (e.g. invalid regular 
114         * expressions, IOExceptions, etc).  A successful test
115         * should only return true or false.
116         * 
117         * @param response HTTP response
118         * @return true if the test conditions were met, false otherwise
119         * @throws ValidationException when a validation fails
120         */
121        public abstract boolean assertTrue(Response response) throws
122            ValidationException;
123    
124        /**
125         * The BASE exception message for a subclass of
126         * BaseConditionalValidator.  Expect that the String
127         * "EXPECTED" or "DID NOT EXPECT" will be prepended to
128         * this message, depending on the value of getCondition().
129         * For example, if you are validator attempts to match
130         * regular expression <i>x</i>, this methods should
131         * generate something like " TO MATCH REGULAR EXPRESSION
132         * <i>x</i>.".
133         * 
134         * @return bare exception message, to which will be prepended
135         *         "EXPECTED" or "DID NOT EXPECT"
136         */
137        public abstract String generateBareExceptionMessage();
138    
139        /**
140         * Automatically generated exception messages, based
141         * on the value of getCondition() and
142         * generateBareExceptionMessage().
143         * 
144         * @exception ValidationException
145         *                   generated Exception
146         */
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    }