001    /*
002     * Copyright 1999-2002,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.Validator;
020    import org.apache.commons.latka.ValidationException;
021    
022    import org.apache.commons.latka.http.Response;
023    
024    import org.apache.regexp.RE;
025    import org.apache.regexp.RESyntaxException;
026    
027    /**
028     * Perform regular expression matches on the body of
029     * the HTTP response.  Setting the "condition" attribute
030     * of the test indicates whether or not a match 
031     * is expected.  If the server returns a <i>null</i> 
032     * (e.g. in the case of an empty HEAD response), the 
033     * validator will always fail, regardless of the setting
034     * of the "condition" attribute.
035     * 
036     * @author Morgan Delagrange
037     * @author dIon Gillard
038     * @version $Id: RegexpValidator.java 155424 2005-02-26 13:09:29Z dirkv $
039     */
040    public class RegexpValidator extends BaseConditionalValidator implements Validator {
041    
042        // --------------------------------------------------------------- Attributes
043    
044        protected String _pattern = null;
045        protected boolean _ignoreCase = false;
046    
047        protected static final String BARE_EXCEPTION_MESSAGE = " TO MATCH PATTERN: ";
048    
049        // ------------------------------------------------------------- Constructors
050    
051        public RegexpValidator() {
052            this(null,null,true,false);
053        }
054    
055        public RegexpValidator(String label) {
056            this(label,null,true,false);
057        }
058    
059        public RegexpValidator(String label, String pattern, boolean cond, boolean ignoreCase) {
060            super(label, cond);
061            _pattern = pattern;
062            _ignoreCase = ignoreCase;
063        }
064    
065        // ------------------------------------------------------------------ Methods
066    
067        public void setPattern(String pattern) {
068            _pattern = pattern;
069        }
070    
071        public void setIgnoreCase(boolean ignoreCase) {
072            _ignoreCase = ignoreCase;
073        }
074    
075        public boolean assertTrue(Response response)
076        throws ValidationException {
077    
078            String responseBody = response.getResource();
079            if (responseBody == null) {
080                fail("HTTP RESPONSE BODY WAS NULL");
081            }
082    
083            RE r = null;
084            try {
085                r = new RE(_pattern);  // Compile expression
086            } catch (RESyntaxException e) {
087                fail(e.toString());
088            }
089    
090            if (_ignoreCase == true) {
091                r.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
092            }
093    
094            boolean matched =
095              r.match(response.getResource()); // Match against expression
096    
097            return matched;
098    
099        }
100    
101        public String generateBareExceptionMessage() {
102            return BARE_EXCEPTION_MESSAGE + _pattern;
103        }
104    
105    }