View Javadoc

1   /*
2    * Copyright 1999-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
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  /**
25   * This subclass of BaseValidator largely removes the need of 
26   * a validator to know whether a particular test is supposed
27   * to succeed or fail.  The validate(Response) method 
28   * becomes final, and the logic is moved into two 
29   * abstract methods (see Javadocs): assertTrue(Response)
30   * and generateBareExceptionMessage(). 
31   * 
32   * @author MorganDelagrange
33   * @version
34   * $Id: BaseConditionalValidator.java 155424 2005-02-26 13:09:29Z dirkv $
35   */
36  public abstract class BaseConditionalValidator extends BaseValidator {
37      // ------------------------------------------------------ Instance Variables
38  
39      /** log4j category for output */
40      protected final Category _log = Category.getInstance(
41          BaseConditionalValidator.class);
42      /** condition to test against */
43      protected boolean _condition = true;
44  
45      // ----------------------------------------------------------- Constructors
46  
47      /**
48       * A test without a label
49       * 
50       * @param condition Whether the test should evaluate to true or false
51       */
52      public BaseConditionalValidator(boolean condition) {
53          this(null, condition);
54      }
55  
56      /**
57       * A test with a label
58       * 
59       * @param label     test label
60       * @param condition whether the test should evaluate to true or false
61       */
62      public BaseConditionalValidator(String label, boolean condition) {
63          super(label);
64          _condition = condition;
65      }
66  
67      // ---------------------------------------------------------------- Methods
68  
69      /**
70       * Set whether or not this test is supposed to succeed
71       * 
72       * @param condition true if the test should succeed
73       */
74      public void setCondition(boolean condition) {
75          _condition = condition;
76      }
77  
78      /**
79       * Returns whether or not this test is supposed to succeed
80       * 
81       * @return true if the test is supposed to succeed
82       */
83      public boolean getCondition() {
84          return _condition;
85      }
86  
87      /**
88       * Final method.  Implement the assertion logic for
89       * the test in assertTrue(Response), and the exception generation
90       * in generateBareExceptionMessage()
91       * 
92       * @param response Response from the HTTP server
93       * @throws ValidationException when a validation fails
94       */
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     /**
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 }